Example #1
0
    def __calculate_volume_mbytes(self):
        """
            calculate the number of mbytes each volume path currently
            consumes and also provide a total number of these values
        """
        volume_mbytes_type = namedtuple(
            'volume_mbytes_type', ['volume', 'total']
        )
        volume_mbytes = {}
        volume_total = 0
        for volume in self.volumes:
            if volume.realpath and not volume.realpath == '/':
                path_to_volume = self.root_dir + '/' + volume.realpath
                if os.path.exists(path_to_volume):
                    volume_size = SystemSize(path_to_volume)
                    volume_mbytes[volume.realpath] = volume_size.customize(
                        volume_size.accumulate_mbyte_file_sizes(),
                        self.filesystem
                    )
                    volume_total += volume_mbytes[volume.realpath]

        return volume_mbytes_type(
            volume=volume_mbytes,
            total=volume_total
        )
Example #2
0
 def get_volume_mbsize(self, mbsize, size_type, realpath, filesystem_name):
     """
         Implements size lookup for the given path and desired
         filesystem according to the specified size type
     """
     if size_type == 'freespace':
         # Please note for nested volumes which contains other volumes
         # the freespace calculation is not correct. Example:
         # /usr is a volume and /usr/lib is a volume. If freespace is
         # set for the /usr volume the data size calculated also
         # contains the data of the /usr/lib path which will live in
         # an extra volume later. The result will be more freespace
         # than expected ! Especially for the root volume this matters
         # most because it always nests all other volumes. Thus it is
         # better to use a fixed size for the root volume if it is not
         # configured to use all rest space
         #
         # You are invited to fix it :)
         volume_size = SystemSize(
             self.root_dir + '/' + realpath
         )
         mbsize = int(mbsize) + \
             Defaults.get_min_volume_mbytes()
         mbsize += volume_size.customize(
             volume_size.accumulate_mbyte_file_sizes(),
             filesystem_name
         )
     return mbsize
Example #3
0
 def __init__(self, xml_state, root_dir):
     self.configured_size = xml_state.get_build_type_size()
     self.size = SystemSize(root_dir)
     self.requested_image_type = xml_state.get_build_type_name()
     if self.requested_image_type in Defaults.get_filesystem_image_types():
         self.requested_filesystem = self.requested_image_type
     else:
         self.requested_filesystem = xml_state.build_type.get_filesystem()
Example #4
0
 def __init__(self, xml_state, root_dir):
     self.configured_size = xml_state.get_build_type_size()
     self.size = SystemSize(root_dir)
     self.requested_image_type = xml_state.get_build_type_name()
     if self.requested_image_type in Defaults.get_filesystem_image_types():
         self.requested_filesystem = self.requested_image_type
     else:
         self.requested_filesystem = xml_state.build_type.get_filesystem()
Example #5
0
    def __init__(self, xml_state, root_dir):
        self.configured_size = xml_state.get_build_type_size()
        self.build_type_name = xml_state.get_build_type_name()
        self.filesystem = xml_state.build_type.get_filesystem()
        self.bootpart_requested = xml_state.build_type.get_bootpartition()
        self.bootpart_mbytes = xml_state.build_type.get_bootpartsize()
        self.mdraid = xml_state.build_type.get_mdraid()
        self.luks = xml_state.build_type.get_luks()
        self.volume_manager = xml_state.get_volume_management()
        self.bootloader = xml_state.build_type.get_bootloader()
        self.oemconfig = xml_state.get_build_type_oemconfig_section()
        self.volumes = xml_state.get_volumes()

        self.firmware = FirmWare(xml_state)
        self.rootsize = SystemSize(root_dir)

        self.root_dir = root_dir
        self.xml_state = xml_state
Example #6
0
    def __calculate_volume_mbytes(self):
        """
            calculate the number of mbytes each volume path currently
            consumes and also provide a total number of these values
        """
        volume_mbytes_type = namedtuple('volume_mbytes_type',
                                        ['volume', 'total'])
        volume_mbytes = {}
        volume_total = 0
        for volume in self.volumes:
            if volume.realpath and not volume.realpath == '/':
                path_to_volume = self.root_dir + '/' + volume.realpath
                if os.path.exists(path_to_volume):
                    volume_size = SystemSize(path_to_volume)
                    volume_mbytes[volume.realpath] = volume_size.customize(
                        volume_size.accumulate_mbyte_file_sizes(),
                        self.filesystem)
                    volume_total += volume_mbytes[volume.realpath]

        return volume_mbytes_type(volume=volume_mbytes, total=volume_total)
Example #7
0
class FileSystemSetup(object):
    """
        Implement filesystem setup methods providing information
        from the root directory required before building a
        filesystem image
    """
    def __init__(self, xml_state, root_dir):
        self.configured_size = xml_state.get_build_type_size()
        self.size = SystemSize(root_dir)
        self.requested_image_type = xml_state.get_build_type_name()
        if self.requested_image_type in Defaults.get_filesystem_image_types():
            self.requested_filesystem = self.requested_image_type
        else:
            self.requested_filesystem = xml_state.build_type.get_filesystem()

    def get_size_mbytes(self):
        root_dir_mbytes = self.size.accumulate_mbyte_file_sizes()
        filesystem_mbytes = self.size.customize(root_dir_mbytes,
                                                self.requested_filesystem)

        if not self.configured_size:
            log.info('Using calculated size: %d MB', filesystem_mbytes)
            return filesystem_mbytes
        elif self.configured_size.additive:
            result_filesystem_mbytes = \
                self.configured_size.mbytes + filesystem_mbytes
            log.info('Using configured size: %d MB + %d MB calculated = %d MB',
                     self.configured_size.mbytes, filesystem_mbytes,
                     result_filesystem_mbytes)
            return result_filesystem_mbytes
        else:
            log.info('Using configured size: %d MB',
                     self.configured_size.mbytes)
            if self.configured_size.mbytes < filesystem_mbytes:
                log.warning(
                    '--> Configured size smaller than calculated size: %d MB',
                    filesystem_mbytes)
            return self.configured_size.mbytes
Example #8
0
class FileSystemSetup(object):
    """
        Implement filesystem setup methods providing information
        from the root directory required before building a
        filesystem image
    """

    def __init__(self, xml_state, root_dir):
        self.configured_size = xml_state.get_build_type_size()
        self.size = SystemSize(root_dir)
        self.requested_image_type = xml_state.get_build_type_name()
        if self.requested_image_type in Defaults.get_filesystem_image_types():
            self.requested_filesystem = self.requested_image_type
        else:
            self.requested_filesystem = xml_state.build_type.get_filesystem()

    def get_size_mbytes(self):
        root_dir_mbytes = self.size.accumulate_mbyte_file_sizes()
        filesystem_mbytes = self.size.customize(root_dir_mbytes, self.requested_filesystem)

        if not self.configured_size:
            log.info("Using calculated size: %d MB", filesystem_mbytes)
            return filesystem_mbytes
        elif self.configured_size.additive:
            result_filesystem_mbytes = self.configured_size.mbytes + filesystem_mbytes
            log.info(
                "Using configured size: %d MB + %d MB calculated = %d MB",
                self.configured_size.mbytes,
                filesystem_mbytes,
                result_filesystem_mbytes,
            )
            return result_filesystem_mbytes
        else:
            log.info("Using configured size: %d MB", self.configured_size.mbytes)
            if self.configured_size.mbytes < filesystem_mbytes:
                log.warning("--> Configured size smaller than calculated size: %d MB", filesystem_mbytes)
            return self.configured_size.mbytes
Example #9
0
 def get_volume_mbsize(self, mbsize, size_type, realpath, filesystem_name):
     """
         Implements size lookup for the given path and desired
         filesystem according to the specified size type
     """
     if size_type == 'freespace':
         # Please note for nested volumes which contains other volumes
         # the freespace calculation is not correct. Example:
         # /usr is a volume and /usr/lib is a volume. If freespace is
         # set for the /usr volume the data size calculated also
         # contains the data of the /usr/lib path which will live in
         # an extra volume later. The result will be more freespace
         # than expected ! Especially for the root volume this matters
         # most because it always nests all other volumes. Thus it is
         # better to use a fixed size for the root volume if it is not
         # configured to use all rest space
         #
         # You are invited to fix it :)
         volume_size = SystemSize(self.root_dir + '/' + realpath)
         mbsize = int(mbsize) + \
             Defaults.get_min_volume_mbytes()
         mbsize += volume_size.customize(
             volume_size.accumulate_mbyte_file_sizes(), filesystem_name)
     return mbsize
Example #10
0
    def __init__(self, xml_state, root_dir):
        self.configured_size = xml_state.get_build_type_size()
        self.build_type_name = xml_state.get_build_type_name()
        self.filesystem = xml_state.build_type.get_filesystem()
        self.bootpart_requested = xml_state.build_type.get_bootpartition()
        self.bootpart_mbytes = xml_state.build_type.get_bootpartsize()
        self.mdraid = xml_state.build_type.get_mdraid()
        self.luks = xml_state.build_type.get_luks()
        self.volume_manager = xml_state.get_volume_management()
        self.bootloader = xml_state.build_type.get_bootloader()
        self.oemconfig = xml_state.get_build_type_oemconfig_section()
        self.volumes = xml_state.get_volumes()

        self.firmware = FirmWare(
            xml_state
        )
        self.rootsize = SystemSize(
            root_dir
        )

        self.root_dir = root_dir
        self.xml_state = xml_state
Example #11
0
 def __get_container_filesystem_size_mbytes(self):
     size = SystemSize(self.root_dir)
     root_dir_mbytes = size.accumulate_mbyte_file_sizes()
     return size.customize(root_dir_mbytes, 'ext4')
Example #12
0
class DiskSetup(object):
    """
        Implement disk setup methods providing information required
        before building a disk image
    """
    def __init__(self, xml_state, root_dir):
        self.configured_size = xml_state.get_build_type_size()
        self.build_type_name = xml_state.get_build_type_name()
        self.filesystem = xml_state.build_type.get_filesystem()
        self.bootpart_requested = xml_state.build_type.get_bootpartition()
        self.bootpart_mbytes = xml_state.build_type.get_bootpartsize()
        self.mdraid = xml_state.build_type.get_mdraid()
        self.luks = xml_state.build_type.get_luks()
        self.volume_manager = xml_state.get_volume_management()
        self.bootloader = xml_state.build_type.get_bootloader()
        self.oemconfig = xml_state.get_build_type_oemconfig_section()
        self.volumes = xml_state.get_volumes()

        self.firmware = FirmWare(xml_state)
        self.rootsize = SystemSize(root_dir)

        self.root_dir = root_dir
        self.xml_state = xml_state

    def get_disksize_mbytes(self):
        log.info('Precalculating required disk size')
        calculated_disk_mbytes = 0
        root_filesystem_mbytes = self.rootsize.customize(
            self.rootsize.accumulate_mbyte_file_sizes(), self.filesystem)
        calculated_disk_mbytes += root_filesystem_mbytes
        log.info('--> system data with filesystem overhead needs %s MB',
                 root_filesystem_mbytes)
        if self.volume_manager and self.volume_manager == 'lvm':
            if self.build_type_name == 'vmx':
                # only for vmx types we need to add the configured volume
                # sizes. oem disks are self expandable and will resize to
                # the configured sizes on first boot of the disk image
                volume_mbytes = self.__accumulate_volume_size(
                    root_filesystem_mbytes)
                if volume_mbytes:
                    calculated_disk_mbytes += volume_mbytes
                    log.info('--> volume(s) size setup adding %s MB',
                             volume_mbytes)

        legacy_bios_mbytes = self.firmware.get_legacy_bios_partition_size()
        if legacy_bios_mbytes:
            calculated_disk_mbytes += legacy_bios_mbytes
            log.info('--> legacy bios boot partition adding %s MB',
                     legacy_bios_mbytes)

        boot_mbytes = self.boot_partition_size()
        if boot_mbytes:
            calculated_disk_mbytes += boot_mbytes
            log.info('--> boot partition adding %s MB', boot_mbytes)

        efi_mbytes = self.firmware.get_efi_partition_size()
        if efi_mbytes:
            calculated_disk_mbytes += efi_mbytes
            log.info('--> EFI partition adding %s MB', efi_mbytes)

        recovery_mbytes = self.__inplace_recovery_partition_size()
        if recovery_mbytes:
            calculated_disk_mbytes += recovery_mbytes
            log.info('--> In-place recovery partition adding: %s MB',
                     recovery_mbytes)

        if self.configured_size and self.build_type_name == 'oem':
            log.warning(
                'Fixed disk size setup not used for expandable oem image')
            self.configured_size = None

        if not self.configured_size:
            log.info('Using calculated disk size: %d MB',
                     calculated_disk_mbytes)
            return calculated_disk_mbytes
        elif self.configured_size.additive:
            result_disk_mbytes = \
                self.configured_size.mbytes + calculated_disk_mbytes
            log.info(
                'Using configured disk size: %d MB + %d MB calculated = %d MB',
                self.configured_size.mbytes, calculated_disk_mbytes,
                result_disk_mbytes)
            return result_disk_mbytes
        else:
            log.info('Using configured disk size: %d MB',
                     self.configured_size.mbytes)
            if self.configured_size.mbytes < calculated_disk_mbytes:
                log.warning(
                    '--> Configured size smaller than calculated size: %d MB',
                    calculated_disk_mbytes)
            return self.configured_size.mbytes

    def need_boot_partition(self):
        """
            Decide if an extra boot partition is needed. This is done with
            the bootpartition attribute from the type, however if it is not
            set it depends on some other type configuration parameters if
            we need a boot partition or not
        """
        if self.bootpart_requested is True:
            return True
        if self.bootpart_requested is False:
            return False
        if self.mdraid:
            return True
        if self.volume_manager:
            return True
        if self.filesystem == 'btrfs':
            return True
        if self.filesystem == 'xfs':
            return True
        if self.bootloader == 'grub2_s390x_emu':
            return True
        if self.luks:
            return True

    def get_boot_label(self):
        label = 'BOOT'
        if self.bootloader == 'grub2_s390x_emu':
            label = 'ZIPL'
        return label

    def get_root_label(self):
        return 'ROOT'

    def get_efi_label(self):
        return 'EFI'

    def boot_partition_size(self):
        if self.need_boot_partition():
            if self.bootpart_mbytes:
                return self.bootpart_mbytes
            else:
                return Defaults.get_default_boot_mbytes()

    def __inplace_recovery_partition_size(self):
        """
            in inplace recovery mode the recovery archive is created at
            install time. This requires free space on the disk. The
            amount of free space is specified with the oem-recovery-part-size
            attribute. If specified we add the given size to the disk.
            If not specified an inplace setup at install time will be
            moved to the first boot of an oem image when the recovery
            partition has been created
        """
        if self.oemconfig and self.oemconfig.get_oem_inplace_recovery():
            recovery_mbytes = self.oemconfig.get_oem_recovery_part_size()
            if recovery_mbytes:
                return int(recovery_mbytes[0] * 1.7)

    def __accumulate_volume_size(self, root_mbytes):
        """
            calculate number of mbytes to add to the disk to allow
            the creaton of the volumes with their configured size
        """
        disk_volume_mbytes = 0

        data_volume_mbytes = self.__calculate_volume_mbytes()
        root_volume = self.__get_root_volume_configuration()

        for volume in self.volumes:
            if volume.realpath and not volume.realpath == '/' and volume.size:
                [size_type, req_size] = volume.size.split(':')
                disk_add_mbytes = 0
                if size_type == 'freespace':
                    disk_add_mbytes += int(req_size) + \
                        Defaults.get_min_volume_mbytes()
                else:
                    disk_add_mbytes += int(req_size) - \
                        data_volume_mbytes.volume[volume.realpath]
                if disk_add_mbytes > 0:
                    disk_volume_mbytes += disk_add_mbytes
                else:
                    log.warning(
                        'volume size of %s MB for %s is too small, skipped',
                        int(req_size), volume.realpath)

        if root_volume:
            if root_volume.size_type == 'freespace':
                disk_add_mbytes += root_volume.req_size + \
                    Defaults.get_min_volume_mbytes()
            else:
                disk_add_mbytes = root_volume.req_size - \
                    root_mbytes + data_volume_mbytes.total

            if disk_add_mbytes > 0:
                disk_volume_mbytes += disk_add_mbytes
            else:
                log.warning('root volume size of %s MB is too small, skipped',
                            root_volume.req_size)

        return disk_volume_mbytes

    def __get_root_volume_configuration(self):
        """
            provide LVRoot volume configuration if present and in
            use according to the selected volume management. So far
            this only affects the LVM volume manager
        """
        root_volume_type = namedtuple('root_volume_type',
                                      ['size_type', 'req_size'])
        for volume in self.volumes:
            if volume.name == 'LVRoot':
                if volume.size:
                    [size_type, req_size] = volume.size.split(':')
                    return root_volume_type(size_type=size_type,
                                            req_size=int(req_size))

    def __calculate_volume_mbytes(self):
        """
            calculate the number of mbytes each volume path currently
            consumes and also provide a total number of these values
        """
        volume_mbytes_type = namedtuple('volume_mbytes_type',
                                        ['volume', 'total'])
        volume_mbytes = {}
        volume_total = 0
        for volume in self.volumes:
            if volume.realpath and not volume.realpath == '/':
                path_to_volume = self.root_dir + '/' + volume.realpath
                if os.path.exists(path_to_volume):
                    volume_size = SystemSize(path_to_volume)
                    volume_mbytes[volume.realpath] = volume_size.customize(
                        volume_size.accumulate_mbyte_file_sizes(),
                        self.filesystem)
                    volume_total += volume_mbytes[volume.realpath]

        return volume_mbytes_type(volume=volume_mbytes, total=volume_total)
Example #13
0
 def __get_container_filesystem_size_mbytes(self):
     size = SystemSize(self.root_dir)
     root_dir_mbytes = size.accumulate_mbyte_file_sizes()
     return size.customize(root_dir_mbytes, 'ext4')
Example #14
0
    def create(self):
        # media dir to store CD contents
        self.media_dir = mkdtemp(prefix='live-media.', dir=self.target_dir)
        rootsize = SystemSize(self.media_dir)

        # custom iso metadata
        log.info('Using following live ISO metadata:')
        log.info('--> Application id: %s', self.mbrid.get_id())
        log.info('--> Publisher: %s', Defaults.get_publisher())
        custom_iso_args = [
            '-A',
            self.mbrid.get_id(),
            '-p',
            '"' + Defaults.get_preparer() + '"',
            '-publisher',
            '"' + Defaults.get_publisher() + '"',
        ]
        if self.volume_id:
            log.info('--> Volume id: %s', self.volume_id)
            custom_iso_args.append('-V')
            custom_iso_args.append('"' + self.volume_id + '"')

        # prepare boot(initrd) root system
        log.info('Preparing live ISO boot system')
        self.boot_image_task.prepare()

        # export modprobe configuration to boot image
        self.system_setup.export_modprobe_setup(
            self.boot_image_task.boot_root_directory)

        # pack system into live boot structure
        log.info('Packing system into live ISO type: %s', self.live_type)
        if self.live_type in self.types:
            live_type_image = FileSystem(name=self.types[self.live_type],
                                         device_provider=None,
                                         root_dir=self.root_dir)
            live_type_image.create_on_file(self.live_image_file)
            Command.run(['mv', self.live_image_file, self.media_dir])
            self.__create_live_iso_client_config(self.live_type)
        else:
            raise KiwiLiveBootImageError('live ISO type "%s" not supported' %
                                         self.live_type)

        # setup bootloader config to boot the ISO via isolinux
        log.info('Setting up isolinux bootloader configuration')
        bootloader_config_isolinux = BootLoaderConfig('isolinux',
                                                      self.xml_state,
                                                      self.media_dir)
        bootloader_config_isolinux.setup_live_boot_images(
            mbrid=None, lookup_path=self.boot_image_task.boot_root_directory)
        bootloader_config_isolinux.setup_live_image_config(mbrid=None)
        bootloader_config_isolinux.write()

        # setup bootloader config to boot the ISO via EFI
        if self.firmware.efi_mode():
            log.info('Setting up EFI grub bootloader configuration')
            bootloader_config_grub = BootLoaderConfig('grub2', self.xml_state,
                                                      self.media_dir)
            bootloader_config_grub.setup_live_boot_images(
                mbrid=self.mbrid,
                lookup_path=self.boot_image_task.boot_root_directory)
            bootloader_config_grub.setup_live_image_config(mbrid=self.mbrid)
            bootloader_config_grub.write()

        # create initrd for live image
        log.info('Creating live ISO boot image')
        self.__create_live_iso_kernel_and_initrd()

        # calculate size and decide if we need UDF
        if rootsize.accumulate_mbyte_file_sizes() > 4096:
            log.info('ISO exceeds 4G size, using UDF filesystem')
            custom_iso_args.append('-allow-limited-size')
            custom_iso_args.append('-udf')

        # create iso filesystem from media_dir
        log.info('Creating live ISO image')
        iso_image = FileSystemIsoFs(device_provider=None,
                                    root_dir=self.media_dir,
                                    custom_args=custom_iso_args)
        iso_header_offset = iso_image.create_on_file(self.isoname)

        # make it hybrid
        if self.hybrid:
            Iso.create_hybrid(iso_header_offset, self.mbrid, self.isoname)

        self.result.add('live_image', self.isoname)
        return self.result
Example #15
0
class DiskSetup(object):
    """
        Implement disk setup methods providing information required
        before building a disk image
    """
    def __init__(self, xml_state, root_dir):
        self.configured_size = xml_state.get_build_type_size()
        self.build_type_name = xml_state.get_build_type_name()
        self.filesystem = xml_state.build_type.get_filesystem()
        self.bootpart_requested = xml_state.build_type.get_bootpartition()
        self.bootpart_mbytes = xml_state.build_type.get_bootpartsize()
        self.mdraid = xml_state.build_type.get_mdraid()
        self.luks = xml_state.build_type.get_luks()
        self.volume_manager = xml_state.get_volume_management()
        self.bootloader = xml_state.build_type.get_bootloader()
        self.oemconfig = xml_state.get_build_type_oemconfig_section()
        self.volumes = xml_state.get_volumes()

        self.firmware = FirmWare(
            xml_state
        )
        self.rootsize = SystemSize(
            root_dir
        )

        self.root_dir = root_dir
        self.xml_state = xml_state

    def get_disksize_mbytes(self):
        log.info('Precalculating required disk size')
        calculated_disk_mbytes = 0
        root_filesystem_mbytes = self.rootsize.customize(
            self.rootsize.accumulate_mbyte_file_sizes(), self.filesystem
        )
        calculated_disk_mbytes += root_filesystem_mbytes
        log.info(
            '--> system data with filesystem overhead needs %s MB',
            root_filesystem_mbytes
        )
        if self.volume_manager and self.volume_manager == 'lvm':
            if self.build_type_name == 'vmx':
                # only for vmx types we need to add the configured volume
                # sizes. oem disks are self expandable and will resize to
                # the configured sizes on first boot of the disk image
                volume_mbytes = self.__accumulate_volume_size(
                    root_filesystem_mbytes
                )
                if volume_mbytes:
                    calculated_disk_mbytes += volume_mbytes
                    log.info(
                        '--> volume(s) size setup adding %s MB', volume_mbytes
                    )

        legacy_bios_mbytes = self.firmware.get_legacy_bios_partition_size()
        if legacy_bios_mbytes:
            calculated_disk_mbytes += legacy_bios_mbytes
            log.info(
                '--> legacy bios boot partition adding %s MB',
                legacy_bios_mbytes
            )

        boot_mbytes = self.boot_partition_size()
        if boot_mbytes:
            calculated_disk_mbytes += boot_mbytes
            log.info(
                '--> boot partition adding %s MB', boot_mbytes
            )

        efi_mbytes = self.firmware.get_efi_partition_size()
        if efi_mbytes:
            calculated_disk_mbytes += efi_mbytes
            log.info(
                '--> EFI partition adding %s MB', efi_mbytes
            )

        recovery_mbytes = self.__inplace_recovery_partition_size()
        if recovery_mbytes:
            calculated_disk_mbytes += recovery_mbytes
            log.info(
                '--> In-place recovery partition adding: %s MB',
                recovery_mbytes
            )

        if self.configured_size and self.build_type_name == 'oem':
            log.warning(
                'Fixed disk size setup not used for expandable oem image'
            )
            self.configured_size = None

        if not self.configured_size:
            log.info(
                'Using calculated disk size: %d MB',
                calculated_disk_mbytes
            )
            return calculated_disk_mbytes
        elif self.configured_size.additive:
            result_disk_mbytes = \
                self.configured_size.mbytes + calculated_disk_mbytes
            log.info(
                'Using configured disk size: %d MB + %d MB calculated = %d MB',
                self.configured_size.mbytes,
                calculated_disk_mbytes,
                result_disk_mbytes
            )
            return result_disk_mbytes
        else:
            log.info(
                'Using configured disk size: %d MB',
                self.configured_size.mbytes
            )
            if self.configured_size.mbytes < calculated_disk_mbytes:
                log.warning(
                    '--> Configured size smaller than calculated size: %d MB',
                    calculated_disk_mbytes
                )
            return self.configured_size.mbytes

    def need_boot_partition(self):
        """
            Decide if an extra boot partition is needed. This is done with
            the bootpartition attribute from the type, however if it is not
            set it depends on some other type configuration parameters if
            we need a boot partition or not
        """
        if self.bootpart_requested is True:
            return True
        if self.bootpart_requested is False:
            return False
        if self.mdraid:
            return True
        if self.volume_manager:
            return True
        if self.filesystem == 'btrfs':
            return True
        if self.filesystem == 'xfs':
            return True
        if self.bootloader == 'grub2_s390x_emu':
            return True
        if self.luks:
            return True

    def get_boot_label(self):
        label = 'BOOT'
        if self.bootloader == 'grub2_s390x_emu':
            label = 'ZIPL'
        return label

    def get_root_label(self):
        return 'ROOT'

    def get_efi_label(self):
        return 'EFI'

    def boot_partition_size(self):
        if self.need_boot_partition():
            if self.bootpart_mbytes:
                return self.bootpart_mbytes
            else:
                return Defaults.get_default_boot_mbytes()

    def __inplace_recovery_partition_size(self):
        """
            in inplace recovery mode the recovery archive is created at
            install time. This requires free space on the disk. The
            amount of free space is specified with the oem-recovery-part-size
            attribute. If specified we add the given size to the disk.
            If not specified an inplace setup at install time will be
            moved to the first boot of an oem image when the recovery
            partition has been created
        """
        if self.oemconfig and self.oemconfig.get_oem_inplace_recovery():
            recovery_mbytes = self.oemconfig.get_oem_recovery_part_size()
            if recovery_mbytes:
                return int(recovery_mbytes[0] * 1.7)

    def __accumulate_volume_size(self, root_mbytes):
        """
            calculate number of mbytes to add to the disk to allow
            the creaton of the volumes with their configured size
        """
        disk_volume_mbytes = 0

        data_volume_mbytes = self.__calculate_volume_mbytes()
        root_volume = self.__get_root_volume_configuration()

        for volume in self.volumes:
            if volume.realpath and not volume.realpath == '/' and volume.size:
                [size_type, req_size] = volume.size.split(':')
                disk_add_mbytes = 0
                if size_type == 'freespace':
                    disk_add_mbytes += int(req_size) + \
                        Defaults.get_min_volume_mbytes()
                else:
                    disk_add_mbytes += int(req_size) - \
                        data_volume_mbytes.volume[volume.realpath]
                if disk_add_mbytes > 0:
                    disk_volume_mbytes += disk_add_mbytes
                else:
                    log.warning(
                        'volume size of %s MB for %s is too small, skipped',
                        int(req_size), volume.realpath
                    )

        if root_volume:
            if root_volume.size_type == 'freespace':
                disk_add_mbytes += root_volume.req_size + \
                    Defaults.get_min_volume_mbytes()
            else:
                disk_add_mbytes = root_volume.req_size - \
                    root_mbytes + data_volume_mbytes.total

            if disk_add_mbytes > 0:
                disk_volume_mbytes += disk_add_mbytes
            else:
                log.warning(
                    'root volume size of %s MB is too small, skipped',
                    root_volume.req_size
                )

        return disk_volume_mbytes

    def __get_root_volume_configuration(self):
        """
            provide LVRoot volume configuration if present and in
            use according to the selected volume management. So far
            this only affects the LVM volume manager
        """
        root_volume_type = namedtuple(
            'root_volume_type', ['size_type', 'req_size']
        )
        for volume in self.volumes:
            if volume.name == 'LVRoot':
                if volume.size:
                    [size_type, req_size] = volume.size.split(':')
                    return root_volume_type(
                        size_type=size_type,
                        req_size=int(req_size)
                    )

    def __calculate_volume_mbytes(self):
        """
            calculate the number of mbytes each volume path currently
            consumes and also provide a total number of these values
        """
        volume_mbytes_type = namedtuple(
            'volume_mbytes_type', ['volume', 'total']
        )
        volume_mbytes = {}
        volume_total = 0
        for volume in self.volumes:
            if volume.realpath and not volume.realpath == '/':
                path_to_volume = self.root_dir + '/' + volume.realpath
                if os.path.exists(path_to_volume):
                    volume_size = SystemSize(path_to_volume)
                    volume_mbytes[volume.realpath] = volume_size.customize(
                        volume_size.accumulate_mbyte_file_sizes(),
                        self.filesystem
                    )
                    volume_total += volume_mbytes[volume.realpath]

        return volume_mbytes_type(
            volume=volume_mbytes,
            total=volume_total
        )
Example #16
0
    def create(self):
        # media dir to store CD contents
        self.media_dir = mkdtemp(
            prefix='live-media.', dir=self.target_dir
        )
        rootsize = SystemSize(self.media_dir)

        # custom iso metadata
        log.info('Using following live ISO metadata:')
        log.info('--> Application id: %s', self.mbrid.get_id())
        log.info('--> Publisher: %s', Defaults.get_publisher())
        custom_iso_args = [
            '-A', self.mbrid.get_id(),
            '-p', '"' + Defaults.get_preparer() + '"',
            '-publisher', '"' + Defaults.get_publisher() + '"',
        ]
        if self.volume_id:
            log.info('--> Volume id: %s', self.volume_id)
            custom_iso_args.append('-V')
            custom_iso_args.append('"' + self.volume_id + '"')

        # prepare boot(initrd) root system
        log.info('Preparing live ISO boot system')
        self.boot_image_task.prepare()

        # export modprobe configuration to boot image
        self.system_setup.export_modprobe_setup(
            self.boot_image_task.boot_root_directory
        )

        # pack system into live boot structure
        log.info('Packing system into live ISO type: %s', self.live_type)
        if self.live_type in self.types:
            live_type_image = FileSystem(
                name=self.types[self.live_type],
                device_provider=None,
                root_dir=self.root_dir
            )
            live_type_image.create_on_file(self.live_image_file)
            Command.run(
                ['mv', self.live_image_file, self.media_dir]
            )
            self.__create_live_iso_client_config(self.live_type)
        else:
            raise KiwiLiveBootImageError(
                'live ISO type "%s" not supported' % self.live_type
            )

        # setup bootloader config to boot the ISO via isolinux
        log.info('Setting up isolinux bootloader configuration')
        bootloader_config_isolinux = BootLoaderConfig(
            'isolinux', self.xml_state, self.media_dir
        )
        bootloader_config_isolinux.setup_live_boot_images(
            mbrid=None,
            lookup_path=self.boot_image_task.boot_root_directory
        )
        bootloader_config_isolinux.setup_live_image_config(
            mbrid=None
        )
        bootloader_config_isolinux.write()

        # setup bootloader config to boot the ISO via EFI
        if self.firmware.efi_mode():
            log.info('Setting up EFI grub bootloader configuration')
            bootloader_config_grub = BootLoaderConfig(
                'grub2', self.xml_state, self.media_dir
            )
            bootloader_config_grub.setup_live_boot_images(
                mbrid=self.mbrid,
                lookup_path=self.boot_image_task.boot_root_directory
            )
            bootloader_config_grub.setup_live_image_config(
                mbrid=self.mbrid
            )
            bootloader_config_grub.write()

        # create initrd for live image
        log.info('Creating live ISO boot image')
        self.__create_live_iso_kernel_and_initrd()

        # calculate size and decide if we need UDF
        if rootsize.accumulate_mbyte_file_sizes() > 4096:
            log.info('ISO exceeds 4G size, using UDF filesystem')
            custom_iso_args.append('-allow-limited-size')
            custom_iso_args.append('-udf')

        # create iso filesystem from media_dir
        log.info('Creating live ISO image')
        iso_image = FileSystemIsoFs(
            device_provider=None,
            root_dir=self.media_dir,
            custom_args=custom_iso_args
        )
        iso_header_offset = iso_image.create_on_file(self.isoname)

        # make it hybrid
        if self.hybrid:
            Iso.create_hybrid(
                iso_header_offset, self.mbrid, self.isoname
            )

        self.result.add(
            'live_image', self.isoname
        )
        return self.result