Example #1
0
    def run(self):

        self.platform = onl.platform.current.OnlPlatform()
        self.pc = self.platform.platform_config

        self.pm = ProcMountsParser()
        self.blkid = BlkidParser(log=self.log.getChild("blkid"))

        if 'grub' in self.pc:
            return self.runGrub()

        if 'flat_image_tree' in self.pc:
            return self.runUboot()

        self.log.error("invalid platform-config")
        return 1
    def umountAny(self, device=None, label=None):
        p = ProcMountsParser()
        if label is not None:
            b = BlkidParser(log=self.log)
            for e in b.parts:
                if label == e.label:
                    device = e.device
                    break

        for m in p.mounts:
            if device is not None and device in m.device:
                try:
                    self.check_call(('umount', m.device,),
                                    vmode=self.V1)
                except CalledProcessError, what:
                    self.log.warn("cannot umount %s: %s",
                                  m.device, str(what))
Example #3
0
    def assertUnmounted(self):
        """Make sure the install device does not have any active mounts."""
        pm = ProcMountsParser()
        for m in pm.mounts:
            if m.device.startswith(self.device):
                if not self.force:
                    self.log.error("mount %s on %s will be erased by install (try --force)",
                                   m.dir, m.device)
                    return 1
                else:
                    self.log.warn("unmounting %s from %s (--force)",
                                  m.dir, m.device)
                    try:
                        self.check_call(('umount', m.dir,))
                    except subprocess.CalledProcessError:
                        self.log.error("cannot unmount")
                        return 1

        return 0
Example #4
0
    def runUrl(self):
        pm = ProcMountsParser()
        for m in pm.mounts:
            if m.dir.startswith('/mnt/onl'):
                if not self.force:
                    self.log.error("directory %s is still mounted", m.dir)
                    return 1
                self.log.warn("unmounting %s (--force)", m.dir)
                self.check_call((
                    'umount',
                    m.dir,
                ))

        def reporthook(blocks, bsz, sz):
            if time.time() < self.nextUpdate: return
            self.nextUpdate = time.time() + 0.25
            if sz:
                pct = blocks * bsz * 100 / sz
                sys.stderr.write("downloaded %d%% ...\r" % pct)
            else:
                icon = "|/-\\"[blocks % 4]
                sys.stderr.write("downloading ... %s\r" % icon)

        p = tempfile.mktemp(prefix="installer-", suffix=".bin")
        try:
            self.log.info("downloading installer from %s --> %s", self.url, p)
            self.nextUpdate = 0
            if os.isatty(sys.stdout.fileno()):
                dst, headers = urllib.urlretrieve(self.url, p, reporthook)
            else:
                dst, headers = urllib.urlretrieve(self.url, p)
            sys.stdout.write("\n")

            self.log.debug("+ chmod +x %s", p)
            os.chmod(p, 0755)

            env = {}
            env.update(os.environ)

            if os.path.exists("/etc/onl/platform"):
                self.log.debug("enabling unzip features for ONL")
                env['SFX_UNZIP'] = '1'
                self.log.debug("+ export SFX_UNZIP=1")
                env['SFX_LOOP'] = '1'
                self.log.debug("+ export SFX_LOOP=1")
                env['SFX_PIPE'] = '1'
                self.log.debug("+ export SFX_PIPE=1")

            self.log.debug("enabling in-place fixups")
            env['SFX_INPLACE'] = '1'
            self.log.debug("+ export SFX_INPLACE=1")

            if self.debug:
                self.log.debug("enabling installer debug")
                env['installer_debug'] = 'y'
                self.log.debug("+ export installer_debug=y")
            if self.log.level < logging.INFO:
                self.log.debug("enabling installer verbose logging")
                env['installer_verbose'] = 'y'
                self.log.debug("+ export installer_verbose=y")

            self.log.info("invoking installer...")
            try:
                self.check_call((p, ), env=env)
            except subprocess.CalledProcessError as ex:
                self.log.error("installer failed")
                return ex.returncode
        finally:
            if os.path.exists(p):
                os.unlink(p)

        self.log.info("please reboot this system now.")
        return 0
Example #5
0
    def run(self):

        self.pm = ProcMountsParser()
        self.blkid = BlkidParser(log=self.log.getChild("blkid"))
        self.mtd = ProcMtdParser(log=self.log.getChild("mtd"))

        def _g(d):
            pat = os.path.join(d, "onie/initrd.img*")
            l = glob.glob(pat)
            if l: return l[0]
            return None

        # try to find a mounted, labeled partition
        try:
            dev = self.blkid['ONIE-BOOT'].device
        except IndexError:
            dev = None
        if dev is not None:
            self.log.debug("found ONIE boot device %s", dev)

            parts = [p for p in self.pm.mounts if p.device == dev]
            if parts:
                onieDir = parts[0]
                self.log.debug("found ONIE boot mounted at %s", onieDir)
                initrd = _g(onieDir)
                if initrd is None:
                    self.log.warn("cannot find ONIE initrd on %s", onieDir)
                else:
                    self.log.debug("found ONIE initrd at %s", initrd)
                    return _runInitrdShell(initrd)

            with MountContext(dev, log=self.log) as ctx:
                initrd = _g(ctx.dir)
                if initrd is None:
                    self.log.warn("cannot find ONIE initrd on %s", dev)
                else:
                    self.log.debug("found ONIE initrd at %s", initrd)
                    return self._runInitrdShell(initrd)

            self.log.warn("cannot find an ONIE initrd")
            return 1

        # try to find onie initrd on a mounted fs (GRUB);
        # for ONIE images this is usually /mnt/onie-boot
        for part in self.pm.mounts:
            if not part.device.startswith('/dev/'): continue
            initrd = _g(part.dir)
            if initrd is None:
                self.log.debug("cannot find ONIE initrd on %s (%s)",
                               part.device, part.dir)
            else:
                self.log.debug("found ONIE initrd at %s", initrd)
                return self._runInitrdShell(initrd)

        # grovel through MTD devices (u-boot)
        parts = [p for p in self.mtd.parts if p.label == "onie"]
        if parts:
            part = parts[0]
            self.log.debug("found ONIE MTD device %s", part.charDevice
                           or part.blockDevice)
            return self._runFitShell(part.blockDevice)
        elif self.mtd.mounts:
            self.log.error("cannot find ONIE MTD device")
            return 1

        self.log.error("cannot find ONIE initrd")
        return 1
Example #6
0
    def __enter__(self):

        self.pm = ProcMountsParser()
        self.blkid = BlkidParser(log=self.log.getChild("blkid"))
        self.mtd = ProcMtdParser(log=self.log.getChild("mtd"))

        def _g(d):
            pat = os.path.join(d, "onie/initrd.img*")
            l = glob.glob(pat)
            if l: return l[0]
            return None

        # try to find a mounted, labeled partition
        try:
            dev = self.blkid['ONIE-BOOT'].device
        except IndexError:
            dev = None
        if dev is not None:
            self.log.debug("found ONIE boot device %s", dev)

            parts = [p for p in self.pm.mounts if p.device == dev]
            if parts:
                self.log.debug("found ONIE boot mounted at %s", parts[0].dir)
                initrd = _g(parts[0].dir)
                if initrd is None:
                    raise ValueError("cannot find ONIE initrd on %s" %
                                     parts[0].dir)
                self.log.debug("found ONIE initrd at %s", initrd)
                with InitrdContext(initrd=initrd, log=self.log) as self.ictx:
                    self.initrd = initrd
                    self.initrdDir = self.ictx.dir
                    self.ictx.detach()
                    return self

            # else, try to mount the directory containing the initrd
            with MountContext(dev, log=self.log) as self.dctx:
                initrd = _g(self.dctx.dir)
                if initrd is None:
                    raise ValueError("cannot find ONIE initrd on %s" % dev)
                self.onieDir = self.dctx.dir
                self.dctx.detach()
                self.log.debug("found ONIE initrd at %s", initrd)
                with InitrdContext(initrd=initrd, log=self.log) as self.ictx:
                    self.initrd = initrd
                    self.initrdDir = self.ictx.dir
                    self.ictx.detach()
                    return self

            raise ValueError("cannot find an ONIE initrd")

        # try to find onie initrd on a mounted fs (GRUB);
        # for ONIE images this is usually /mnt/onie-boot
        for part in self.pm.mounts:
            if not part.device.startswith('/dev/'): continue
            initrd = _g(part.dir)
            if initrd is None:
                self.log.debug("cannot find ONIE initrd on %s (%s)",
                               part.device, part.dir)
            else:
                self.onieDir = part.dir
                self.log.debug("found ONIE initrd at %s", initrd)
                with InitrdContext(initrd=initrd, log=self.log) as self.ictx:
                    self.initrd = initrd
                    self.initrdDir = self.ictx.dir
                    self.ictx.detach()
                    return self

        # grovel through MTD devices (u-boot)
        parts = [p for p in self.mtd.parts if p.label == "onie"]
        if parts:
            part = parts[0]
            self.log.debug("found ONIE MTD device %s", part.charDevice
                           or part.blockDevice)
            with UbootInitrdContext(part.blockDevice,
                                    log=self.log) as self.fctx:
                with InitrdContext(initrd=self.fctx.initrd,
                                   log=self.log) as self.ictx:
                    self.initrd = self.fctx.initrd
                    self.fctx.detach()
                    self.initrdDir = self.ictx.dir
                    self.ictx.detach()
                    return self

        if self.mtd.mounts:
            raise ValueError("cannot find ONIE MTD device")

        raise ValueError("cannot find ONIE initrd")