def _mount_target(self, mount_dir, os_id=None, pseudo=True, treat_mtab=True): LOG.debug('Mounting target file systems: %s', mount_dir) # Here we are going to mount all file systems in partition schema. for fs in self.driver.partition_scheme.fs_sorted_by_depth(os_id): if fs.mount == 'swap': continue mount = os.path.join(mount_dir, fs.mount.strip(os.sep)) utils.makedirs_if_not_exists(mount) fu.mount_fs(fs.type, str(fs.device), mount) if pseudo: for path in ('/sys', '/dev', '/proc'): utils.makedirs_if_not_exists( os.path.join(mount_dir, path.strip(os.sep))) fu.mount_bind(mount_dir, path) if treat_mtab: mtab = utils.execute('chroot', mount_dir, 'grep', '-v', 'rootfs', '/proc/mounts')[0] mtab_path = os.path.join(mount_dir, 'etc/mtab') if os.path.islink(mtab_path): os.remove(mtab_path) with open(mtab_path, 'wt', encoding='utf-8') as f: f.write(six.text_type(mtab))
def mount_target(self, chroot, treat_mtab=True, pseudo=True): """Mount a set of file systems into a chroot :param chroot: Directory where to mount file systems :param treat_mtab: If mtab needs to be actualized (Default: True) :param pseudo: If pseudo file systems need to be mounted (Default: True) """ LOG.debug('Mounting target file systems: %s', chroot) # Here we are going to mount all file systems in partition scheme. for fs in self.driver.partition_scheme.fs_sorted_by_depth(): if fs.mount == 'swap': continue mount = chroot + fs.mount utils.makedirs_if_not_exists(mount) fu.mount_fs(fs.type, str(fs.device), mount) if pseudo: for path in ('/sys', '/dev', '/proc'): utils.makedirs_if_not_exists(chroot + path) fu.mount_bind(chroot, path) if treat_mtab: mtab = utils.execute( 'chroot', chroot, 'grep', '-v', 'rootfs', '/proc/mounts')[0] mtab_path = chroot + '/etc/mtab' if os.path.islink(mtab_path): os.remove(mtab_path) with open(mtab_path, 'wt', encoding='utf-8') as f: f.write(six.text_type(mtab))
def _make_configdrive_image(self, src_files): bs = 4096 configdrive_device = self.driver.partition_scheme.configdrive_device() size = utils.execute('blockdev', '--getsize64', configdrive_device)[0] size = int(size.strip()) utils.execute('truncate', '--size=%d' % size, CONF.config_drive_path) fu.make_fs( fs_type='ext2', fs_options=' -b %d -F ' % bs, fs_label='config-2', dev=six.text_type(CONF.config_drive_path)) mount_point = tempfile.mkdtemp(dir=CONF.tmp_path) try: fu.mount_fs('ext2', CONF.config_drive_path, mount_point) for file_path in src_files: name = os.path.basename(file_path) if os.path.isdir(file_path): shutil.copytree(file_path, os.path.join(mount_point, name)) else: shutil.copy2(file_path, mount_point) except Exception as exc: LOG.error('Error copying files to configdrive: %s', exc) raise finally: fu.umount_fs(mount_point) os.rmdir(mount_point)
def mount_target(self, chroot, treat_mtab=True, pseudo=True): """Mount a set of file systems into a chroot :param chroot: Directory where to mount file systems :param treat_mtab: If mtab needs to be actualized (Default: True) :param pseudo: If pseudo file systems need to be mounted (Default: True) """ LOG.debug('Mounting target file systems: %s', chroot) # Here we are going to mount all file systems in partition scheme. for fs in self.driver.partition_scheme.fs_sorted_by_depth(): if fs.mount == 'swap': continue mount = chroot + fs.mount utils.makedirs_if_not_exists(mount) fu.mount_fs(fs.type, str(fs.device), mount) if pseudo: for path in ('/sys', '/dev', '/proc'): utils.makedirs_if_not_exists(chroot + path) fu.mount_bind(chroot, path) if treat_mtab: mtab = utils.execute('chroot', chroot, 'grep', '-v', 'rootfs', '/proc/mounts')[0] mtab_path = chroot + '/etc/mtab' if os.path.islink(mtab_path): os.remove(mtab_path) with open(mtab_path, 'wt', encoding='utf-8') as f: f.write(six.text_type(mtab))
def test_mount_fs(self, mock_exec): fu.mount_fs('ext3', '/dev/fake', '/target') mock_exec.assert_called_once_with('mount', '-t', 'ext3', '/dev/fake', '/target', check_exit_code=[0])
def _mount_bootloader(self, mount_dir): fs = filter(lambda fss: fss.mount == 'multiboot', self.driver.partition_scheme.fss) if len(fs) > 1: raise errors.WrongPartitionSchemeError( 'Multiple multiboot partitions found') utils.makedirs_if_not_exists(mount_dir) fu.mount_fs(fs[0].type, str(fs[0].device), mount_dir) yield pu.get_uuid(fs[0].device) fu.umount_fs(mount_dir)
def run_mksquashfs(chroot, output_name=None, compression_algorithm='xz'): """Pack the target system as squashfs using mksquashfs :param chroot: chroot system, to be squashfs'd :param output_name: output file name, might be a relative or an absolute path The kernel squashfs driver has to match with the user space squasfs tools. Use the mksquashfs provided by the target distro to achieve this. (typically the distro maintainers are smart enough to ship the correct version of mksquashfs) Use mksquashfs installed in the target system 1)Mount tmpfs under chroot/mnt 2)run mksquashfs inside a chroot 3)move result files to dstdir """ if not output_name: output_name = 'root.squashfs' + six.text_type(uuid.uuid4()) utils.makedirs_if_not_exists(os.path.dirname(output_name)) dstdir = os.path.dirname(output_name) temp = '.mksquashfs.tmp.' + six.text_type(uuid.uuid4()) s_dst = os.path.join(chroot, 'mnt/dst') s_src = os.path.join(chroot, 'mnt/src') try: fu.mount_fs('tmpfs', 'mnt_{0}'.format(temp), (os.path.join(chroot, 'mnt')), 'rw,nodev,nosuid,noatime,mode=0755,size=4M') utils.makedirs_if_not_exists(s_src) utils.makedirs_if_not_exists(s_dst) # Bind mount the chroot to avoid including various temporary/virtual # files (/proc, /sys, /dev, and so on) into the image fu.mount_fs(None, chroot, s_src, opts='bind') fu.mount_fs(None, None, s_src, 'remount,bind,ro') fu.mount_fs(None, dstdir, s_dst, opts='bind') # run mksquashfs chroot_squash = os.path.join('/mnt/dst/' + temp) long_squash = os.path.join(chroot, 'mnt/dst/{0}'.format(temp)) utils.execute('chroot', chroot, 'mksquashfs', '/mnt/src', chroot_squash, '-comp', compression_algorithm, '-no-progress', '-noappend', logged=True) # move to result name LOG.debug('Moving file: %s to: %s', long_squash, output_name) shutil.move(long_squash, output_name) except Exception as exc: LOG.error('squashfs_image build failed: %s', exc) raise finally: LOG.info('squashfs_image clean-up') stop_chrooted_processes(chroot, signal=signal.SIGTERM) fu.umount_fs(os.path.join(chroot, 'mnt/dst')) fu.umount_fs(os.path.join(chroot, 'mnt/src')) fu.umount_fs(os.path.join(chroot, 'mnt'))
def run_mksquashfs(chroot, output_name=None, compression_algorithm='xz'): """Pack the target system as squashfs using mksquashfs :param chroot: chroot system, to be squashfs'd :param output_name: output file name, might be a relative or an absolute path The kernel squashfs driver has to match with the user space squasfs tools. Use the mksquashfs provided by the target distro to achieve this. (typically the distro maintainers are smart enough to ship the correct version of mksquashfs) Use mksquashfs installed in the target system 1)Mount tmpfs under chroot/mnt 2)run mksquashfs inside a chroot 3)move result files to dstdir """ if not output_name: output_name = 'root.squashfs' + six.text_type(uuid.uuid4()) utils.makedirs_if_not_exists(os.path.dirname(output_name)) dstdir = os.path.dirname(output_name) temp = '.mksquashfs.tmp.' + six.text_type(uuid.uuid4()) s_dst = os.path.join(chroot, 'mnt/dst') s_src = os.path.join(chroot, 'mnt/src') try: fu.mount_fs( 'tmpfs', 'mnt_{0}'.format(temp), (os.path.join(chroot, 'mnt')), 'rw,nodev,nosuid,noatime,mode=0755,size=4M') utils.makedirs_if_not_exists(s_src) utils.makedirs_if_not_exists(s_dst) # Bind mount the chroot to avoid including various temporary/virtual # files (/proc, /sys, /dev, and so on) into the image fu.mount_fs(None, chroot, s_src, opts='bind') fu.mount_fs(None, None, s_src, 'remount,bind,ro') fu.mount_fs(None, dstdir, s_dst, opts='bind') # run mksquashfs chroot_squash = os.path.join('/mnt/dst/' + temp) long_squash = os.path.join(chroot, 'mnt/dst/{0}'.format(temp)) utils.execute( 'chroot', chroot, 'mksquashfs', '/mnt/src', chroot_squash, '-comp', compression_algorithm, '-no-progress', '-noappend', logged=True) # move to result name LOG.debug('Moving file: %s to: %s', long_squash, output_name) shutil.move(long_squash, output_name) except Exception as exc: LOG.error('squashfs_image build failed: %s', exc) raise finally: LOG.info('squashfs_image clean-up') stop_chrooted_processes(chroot, signal=signal.SIGTERM) fu.umount_fs(os.path.join(chroot, 'mnt/dst')) fu.umount_fs(os.path.join(chroot, 'mnt/src')) fu.umount_fs(os.path.join(chroot, 'mnt'))
def test_mount_fs(self, mock_exec): fu.mount_fs('ext3', '/dev/fake', '/target') mock_exec.assert_called_once_with( 'mount', '-t', 'ext3', '/dev/fake', '/target', check_exit_code=[0])