def create_initrd(self, mbrid: Optional[SystemIdentifier] = None, basename: Optional[str] = None, install_initrd: bool = False) -> None: """ Create initrd from prepared boot system tree and compress the result :param SystemIdentifier mbrid: instance of ImageIdentifier :param str basename: base initrd file name :param bool install_initrd: installation media initrd """ if self.is_prepared(): log.info('Creating initrd cpio archive') # we can't simply exclude boot when building the archive # because the file boot/mbrid must be preserved. Because of # that we create a copy of the boot directory and remove # everything in boot/ except for boot/mbrid. The original # boot directory should not be changed because we rely # on other data in boot/ e.g the kernel to be available # for the entire image building process if basename: kiwi_initrd_basename = basename else: kiwi_initrd_basename = self.initrd_base_name temp_boot_root = Temporary(prefix='kiwi_boot_root_copy.').new_dir() temp_boot_root_directory = temp_boot_root.name os.chmod(temp_boot_root_directory, 0o755) data = DataSync(self.boot_root_directory + '/', temp_boot_root_directory) data.sync_data(options=['-a']) boot_directory = temp_boot_root_directory + '/boot' Path.wipe(boot_directory) if mbrid: log.info('--> Importing mbrid: %s', mbrid.get_id()) Path.create(boot_directory) image_identifier = boot_directory + '/mbrid' mbrid.write(image_identifier) cpio = ArchiveCpio( os.sep.join([self.target_dir, kiwi_initrd_basename])) # the following is a list of directories which were needed # during the process of creating an image but not when the # image is actually booting with this initrd exclude_from_archive = [ '/' + Defaults.get_shared_cache_location(), '/image', '/usr/lib/grub*' ] # the following is a list of directories to exclude which # are not needed inside of the initrd exclude_from_archive += [ '/usr/share/doc', '/usr/share/man', '/home', '/media', '/srv' ] cpio.create(source_dir=temp_boot_root_directory, exclude=exclude_from_archive) log.info('--> xz compressing archive') compress = Compress( os.sep.join([self.target_dir, kiwi_initrd_basename])) compress.xz(['--check=crc32', '--lzma2=dict=1MiB', '--threads=0']) self.initrd_filename = compress.compressed_filename
class TestArchiveCpio(object): def setup(self): self.archive = ArchiveCpio('foo.cpio') @patch('kiwi.archive.cpio.Command.run') def test_create(self, mock_command): self.archive.create('source-dir', ['/boot', '/var/cache']) find_command = \ 'find . -path ./boot -prune -or -path ./var/cache -prune -o -print' cpio_command = 'cpio --quiet -o -H newc' mock_command.assert_called_once_with( [ 'bash', '-c', 'cd source-dir && ' + find_command + ' | ' + cpio_command + ' > foo.cpio' ] ) @patch('kiwi.archive.cpio.Command.run') def test_extract(self, mock_command): self.archive.extract('dest-dir') bash_command = \ 'cd dest-dir && cat foo.cpio | cpio -i --make-directories' mock_command.assert_called_once_with( ['bash', '-c', bash_command] )
class TestArchiveCpio: def setup(self): self.archive = ArchiveCpio('foo.cpio') def setup_method(self, cls): self.setup() @patch('kiwi.archive.cpio.Command.run') def test_create(self, mock_command): self.archive.create('source-dir', ['/boot', '/var/cache']) find_command = \ 'find . -path ./boot -prune -or -path ./var/cache -prune -o -print' cpio_command = 'cpio --quiet -o -H newc' mock_command.assert_called_once_with([ 'bash', '-c', ''.join([ 'cd source-dir && ', find_command, ' | ', cpio_command, ' > foo.cpio' ]) ]) @patch('kiwi.archive.cpio.Command.run') def test_extract(self, mock_command): self.archive.extract('dest-dir') bash_command = \ 'cd dest-dir && cat foo.cpio | cpio -i --make-directories' mock_command.assert_called_once_with(['bash', '-c', bash_command])
def create_initrd(self, mbrid=None): """ Create initrd from prepared boot system tree and compress the result :param object mbrid: instance of ImageIdentifier """ if self.is_prepared(): log.info('Creating initrd cpio archive') # we can't simply exclude boot when building the archive # because the file boot/mbrid must be preserved. Because of # that we create a copy of the boot directory and remove # everything in boot/ except for boot/mbrid. The original # boot directory should not be changed because we rely # on other data in boot/ e.g the kernel to be available # for the entire image building process temp_boot_root_directory = mkdtemp(prefix='kiwi_boot_root_copy.') self.temp_directories.append(temp_boot_root_directory) data = DataSync(self.boot_root_directory + '/', temp_boot_root_directory) data.sync_data(options=['-z', '-a']) boot_directory = temp_boot_root_directory + '/boot' Path.wipe(boot_directory) if mbrid: log.info('--> Importing mbrid: %s', mbrid.get_id()) Path.create(boot_directory) image_identifier = boot_directory + '/mbrid' mbrid.write(image_identifier) cpio = ArchiveCpio( os.sep.join([self.target_dir, self.initrd_base_name])) # the following is a list of directories which were needed # during the process of creating an image but not when the # image is actually booting with this initrd exclude_from_archive = [ '/' + Defaults.get_shared_cache_location(), '/image', '/usr/lib/grub*' ] # the following is a list of directories to exclude which # are not needed inside of the initrd exclude_from_archive += [ '/usr/share/doc', '/usr/share/man', '/home', '/media', '/srv' ] cpio.create(source_dir=temp_boot_root_directory, exclude=exclude_from_archive) log.info('--> xz compressing archive') compress = Compress( os.sep.join([self.target_dir, self.initrd_base_name])) compress.xz() self.initrd_filename = compress.compressed_filename