Example #1
0
    def get_dev(self):
        device = self._allocate_nbd()
        if not device:
            return False
        _out, err = utils.trycmd('qemu-nbd', '-c', device, self.image,
                                 run_as_root=True)
        if err:
            self.error = _('qemu-nbd error: %s') % err
            self._free_nbd(device)
            return False

        # NOTE(vish): this forks into another process, so give it a chance
        #             to set up before continuing
        for _i in range(FLAGS.timeout_nbd):
            if os.path.exists("/sys/block/%s/pid" % os.path.basename(device)):
                self.device = device
                break
            time.sleep(1)
        else:
            self.error = _('nbd device %s did not show up') % device
            self._free_nbd(device)
            return False

        self.linked = True
        return True
Example #2
0
    def get_dev(self):
        device = self._allocate_nbd()
        if not device:
            return False
        _out, err = utils.trycmd('qemu-nbd',
                                 '-c',
                                 device,
                                 self.image,
                                 run_as_root=True)
        if err:
            self.error = _('qemu-nbd error: %s') % err
            self._free_nbd(device)
            return False

        # NOTE(vish): this forks into another process, so give it a chance
        #             to set up before continuing
        for _i in range(FLAGS.timeout_nbd):
            if os.path.exists("/sys/block/%s/pid" % os.path.basename(device)):
                self.device = device
                break
            time.sleep(1)
        else:
            self.error = _('nbd device %s did not show up') % device
            self._free_nbd(device)
            return False

        self.linked = True
        return True
Example #3
0
    def map_dev(self):
        """Map partitions of the device to the file system namespace."""
        assert (os.path.exists(self.device))

        if self.partition:
            map_path = '/dev/mapper/%sp%s' % (self.device.split('/')[-1],
                                              self.partition)
            assert (not os.path.exists(map_path))

            # Note kpartx can output warnings to stderr and succeed
            # Also it can output failures to stderr and "succeed"
            # So we just go on the existence of the mapped device
            _out, err = utils.trycmd('kpartx',
                                     '-a',
                                     self.device,
                                     run_as_root=True,
                                     discard_warnings=True)

            # Note kpartx does nothing when presented with a raw image,
            # so given we only use it when we expect a partitioned image, fail
            if not os.path.exists(map_path):
                if not err:
                    err = _('no partitions found')
                self.error = _('Failed to map partitions: %s') % err
            else:
                self.mapped_device = map_path
                self.mapped = True
        else:
            self.mapped_device = self.device
            self.mapped = True

        # This is an orthogonal operation
        # which only needs to be done once
        if self.disable_auto_fsck and self.mapped:
            self.disable_auto_fsck = False
            # attempt to set ext[234] so that it doesn't auto-fsck
            _out, err = utils.trycmd('tune2fs',
                                     '-c',
                                     0,
                                     '-i',
                                     0,
                                     self.mapped_device,
                                     run_as_root=True)
            if err:
                LOG.info(_('Failed to disable fs check: %s') % err)

        return self.mapped
Example #4
0
    def mnt_dev(self):
        args = ('guestmount', '--rw', '-a', self.image)
        if self.partition == -1:
            args += ('-i', )  # find the OS partition
        elif self.partition:
            args += ('-m', '/dev/sda%d' % self.partition)
        else:
            # We don't resort to -i for this case yet,
            # as some older versions of libguestfs
            # have problems identifying ttylinux images for example
            args += ('-m', '/dev/sda')
        args += (self.mount_dir, )
        # root access should not required for guestfs (if the user
        # has permissions to fusermount (by being part of the fuse
        # group for example)).  Also note the image and mount_dir
        # have appropriate creditials at this point for read/write
        # mounting by the engine user.  However currently there are
        # subsequent access issues by both the engine and root users
        # if the engine user mounts the image, as detailed here:
        # https://bugzilla.redhat.com/show_bug.cgi?id=765814
        _out, err = utils.trycmd(*args,
                                 discard_warnings=True,
                                 run_as_root=True)
        if err:
            self.error = _('Failed to mount filesystem: %s') % err
            # Be defensive and ensure this is unmounted,
            # as I'm not sure guestmount will never have
            # mounted when it returns EXIT_FAILURE.
            # This is required if discard_warnings=False above
            utils.trycmd('fusermount', '-u', self.mount_dir, run_as_root=True)
            return False

        # More defensiveness as there are edge cases where
        # guestmount can return success while not mounting
        try:
            if not os.listdir(self.mount_dir):
                # Assume we've just got the original empty temp dir
                err = _('unknown guestmount error')
                self.error = _('Failed to mount filesystem: %s') % err
                return False
        except OSError:
            # This is the usual path and means root has
            # probably mounted fine
            pass

        self.mounted = True
        return True
Example #5
0
    def mnt_dev(self):
        args = ('guestmount', '--rw', '-a', self.image)
        if self.partition == -1:
            args += ('-i',)  # find the OS partition
        elif self.partition:
            args += ('-m', '/dev/sda%d' % self.partition)
        else:
            # We don't resort to -i for this case yet,
            # as some older versions of libguestfs
            # have problems identifying ttylinux images for example
            args += ('-m', '/dev/sda')
        args += (self.mount_dir,)
        # root access should not required for guestfs (if the user
        # has permissions to fusermount (by being part of the fuse
        # group for example)).  Also note the image and mount_dir
        # have appropriate creditials at this point for read/write
        # mounting by the engine user.  However currently there are
        # subsequent access issues by both the engine and root users
        # if the engine user mounts the image, as detailed here:
        # https://bugzilla.redhat.com/show_bug.cgi?id=765814
        _out, err = utils.trycmd(*args, discard_warnings=True,
                                 run_as_root=True)
        if err:
            self.error = _('Failed to mount filesystem: %s') % err
            # Be defensive and ensure this is unmounted,
            # as I'm not sure guestmount will never have
            # mounted when it returns EXIT_FAILURE.
            # This is required if discard_warnings=False above
            utils.trycmd('fusermount', '-u', self.mount_dir, run_as_root=True)
            return False

        # More defensiveness as there are edge cases where
        # guestmount can return success while not mounting
        try:
            if not os.listdir(self.mount_dir):
                # Assume we've just got the original empty temp dir
                err = _('unknown guestmount error')
                self.error = _('Failed to mount filesystem: %s') % err
                return False
        except OSError:
            # This is the usual path and means root has
            # probably mounted fine
            pass

        self.mounted = True
        return True
Example #6
0
    def get_dev(self):
        out, err = utils.trycmd('losetup', '--find', '--show', self.image,
                                run_as_root=True)
        if err:
            self.error = _('Could not attach image to loopback: %s') % err
            return False

        self.device = out.strip()
        self.linked = True
        return True
Example #7
0
    def mnt_dev(self):
        """Mount the device into the file system."""
        _out, err = utils.trycmd('mount', self.mapped_device, self.mount_dir,
                                 run_as_root=True)
        if err:
            self.error = _('Failed to mount filesystem: %s') % err
            return False

        self.mounted = True
        return True
Example #8
0
    def mnt_dev(self):
        """Mount the device into the file system."""
        _out, err = utils.trycmd('mount',
                                 self.mapped_device,
                                 self.mount_dir,
                                 run_as_root=True)
        if err:
            self.error = _('Failed to mount filesystem: %s') % err
            return False

        self.mounted = True
        return True
Example #9
0
    def map_dev(self):
        """Map partitions of the device to the file system namespace."""
        assert(os.path.exists(self.device))

        if self.partition:
            map_path = '/dev/mapper/%sp%s' % (self.device.split('/')[-1],
                                              self.partition)
            assert(not os.path.exists(map_path))

            # Note kpartx can output warnings to stderr and succeed
            # Also it can output failures to stderr and "succeed"
            # So we just go on the existence of the mapped device
            _out, err = utils.trycmd('kpartx', '-a', self.device,
                                     run_as_root=True, discard_warnings=True)

            # Note kpartx does nothing when presented with a raw image,
            # so given we only use it when we expect a partitioned image, fail
            if not os.path.exists(map_path):
                if not err:
                    err = _('no partitions found')
                self.error = _('Failed to map partitions: %s') % err
            else:
                self.mapped_device = map_path
                self.mapped = True
        else:
            self.mapped_device = self.device
            self.mapped = True

        # This is an orthogonal operation
        # which only needs to be done once
        if self.disable_auto_fsck and self.mapped:
            self.disable_auto_fsck = False
            # attempt to set ext[234] so that it doesn't auto-fsck
            _out, err = utils.trycmd('tune2fs', '-c', 0, '-i', 0,
                                     self.mapped_device, run_as_root=True)
            if err:
                LOG.info(_('Failed to disable fs check: %s') % err)

        return self.mapped
Example #10
0
    def get_dev(self):
        out, err = utils.trycmd('losetup',
                                '--find',
                                '--show',
                                self.image,
                                run_as_root=True)
        if err:
            self.error = _('Could not attach image to loopback: %s') % err
            return False

        self.device = out.strip()
        self.linked = True
        return True