Ejemplo n.º 1
0
    def test_has_option_in_help_command_failure_exception(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.has_option_in_help('command_that_fails',
                                               '--non-existing-flag')
 def test_has_option_in_help(self, mock_run):
     command_type = namedtuple('command', ['output', 'error'])
     mock_run.return_value = command_type(
         output="Dummy line\n\t--some-flag\n\t--some-other-flag",
         error="Dummy line\n\t--error-flag\n\t--some-other-flag"
     )
     assert CommandCapabilities.has_option_in_help('command', '--some-flag')
     assert CommandCapabilities.has_option_in_help('command', '--error-flag')
     assert CommandCapabilities.has_option_in_help(
         'command', '--some-flag', help_flags=['subcommand', '-h']
     )
     assert CommandCapabilities.has_option_in_help(
         'command', '--some-other-flag',
         help_flags=['subcommand', '-h'], root='root_dir'
     )
     assert not CommandCapabilities.has_option_in_help(
         'command', '--non-existing-flag'
     )
     mock_run.assert_has_calls([
         call(['command', '--help']),
         call(['command', '--help']),
         call(['command', 'subcommand', '-h']),
         call(['chroot', 'root_dir', 'command', 'subcommand', '-h']),
         call(['command', '--help'])
     ])
Ejemplo n.º 3
0
    def test_check_version_failure_exception(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.check_version('command_that_fails',
                                          '--non-existing-flag')
Ejemplo n.º 4
0
    def test_check_version_failure_warning(self, mock_warn, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.check_version('command_that_fails', (1, 2),
                                          raise_on_error=False)
        assert mock_warn.called
Ejemplo n.º 5
0
    def test_check_version_failure_warning(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        with self._caplog.at_level(logging.WARNING):
            CommandCapabilities.check_version('command_that_fails', (1, 2),
                                              raise_on_error=False)
    def test_check_version_failure_exception(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.check_version(
            'command_that_fails', '--non-existing-flag'
        )
    def test_has_option_in_help_command_failure_exception(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.has_option_in_help(
            'command_that_fails', '--non-existing-flag'
        )
    def test_check_version_failure_warning(self, mock_warn, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.check_version(
            'command_that_fails', (1, 2), raise_on_error=False
        )
        assert mock_warn.called
Ejemplo n.º 9
0
    def test_has_option_in_help_command_failure_warning(self, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        with self._caplog.at_level(logging.WARNING):
            CommandCapabilities.has_option_in_help('command_that_fails',
                                                   '--non-existing-flag',
                                                   raise_on_error=False)
Ejemplo n.º 10
0
    def test_has_option_in_help_command_failure_warning(
            self, mock_warn, mock_run):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.has_option_in_help('command_that_fails',
                                               '--non-existing-flag',
                                               raise_on_error=False)
        assert mock_warn.called
    def test_has_option_in_help_command_failure_warning(
        self, mock_warn, mock_run
    ):
        def side_effect():
            raise Exception("Something went wrong")

        mock_run.side_effect = side_effect
        CommandCapabilities.has_option_in_help(
            'command_that_fails', '--non-existing-flag', raise_on_error=False
        )
        assert mock_warn.called
Ejemplo n.º 12
0
 def setup_keyboard_map(self):
     """
     Setup console keyboard
     """
     if 'keytable' in self.preferences:
         log.info('Setting up keytable: {0}'.format(
             self.preferences['keytable']))
         if CommandCapabilities.has_option_in_help('systemd-firstboot',
                                                   '--keymap',
                                                   root=self.root_dir,
                                                   raise_on_error=False):
             Path.wipe(self.root_dir + '/etc/vconsole.conf')
             Command.run([
                 'chroot', self.root_dir, 'systemd-firstboot',
                 '--keymap=' + self.preferences['keytable']
             ])
         elif os.path.exists(self.root_dir + '/etc/sysconfig/keyboard'):
             Shell.run_common_function('baseUpdateSysConfig', [
                 self.root_dir + '/etc/sysconfig/keyboard', 'KEYTABLE',
                 '"' + self.preferences['keytable'] + '"'
             ])
         else:
             log.warning(
                 'keyboard setup skipped no capable '
                 'systemd-firstboot or etc/sysconfig/keyboard found')
Ejemplo n.º 13
0
 def setup_locale(self):
     """
     Setup UTF8 system wide locale
     """
     if 'locale' in self.preferences:
         if 'POSIX' in self.preferences['locale'].split(','):
             locale = 'POSIX'
         else:
             locale = '{0}.UTF-8'.format(
                 self.preferences['locale'].split(',')[0])
         log.info('Setting up locale: %s', self.preferences['locale'])
         if CommandCapabilities.has_option_in_help('systemd-firstboot',
                                                   '--locale',
                                                   root=self.root_dir,
                                                   raise_on_error=False):
             Path.wipe(self.root_dir + '/etc/locale.conf')
             Command.run([
                 'chroot', self.root_dir, 'systemd-firstboot',
                 '--locale=' + locale
             ])
         if os.path.exists(self.root_dir + '/etc/sysconfig/language'):
             Shell.run_common_function('baseUpdateSysConfig', [
                 self.root_dir + '/etc/sysconfig/language', 'RC_LANG',
                 locale
             ])
Ejemplo n.º 14
0
 def setup_locale(self):
     """
     Setup UTF8 system wide locale
     """
     if 'locale' in self.preferences:
         if 'POSIX' in self.preferences['locale'].split(','):
             locale = 'POSIX'
         else:
             locale = '{0}.UTF-8'.format(
                 self.preferences['locale'].split(',')[0]
             )
         log.info('Setting up locale: %s', self.preferences['locale'])
         if CommandCapabilities.has_option_in_help(
             'systemd-firstboot', '--locale',
             root=self.root_dir, raise_on_error=False
         ):
             Path.wipe(self.root_dir + '/etc/locale.conf')
             Command.run([
                 'chroot', self.root_dir, 'systemd-firstboot',
                 '--locale=' + locale
             ])
         elif os.path.exists(self.root_dir + '/etc/sysconfig/language'):
             Shell.run_common_function(
                 'baseUpdateSysConfig', [
                     self.root_dir + '/etc/sysconfig/language',
                     'RC_LANG', locale
                 ]
             )
         else:
             log.warning(
                 'locale setup skipped no capable '
                 'systemd-firstboot or etc/sysconfig/language not found'
             )
Ejemplo n.º 15
0
    def add_efi_loader_parameters(self):
        """
        Add ISO creation parameters to embed the EFI loader

        In order to boot the ISO from EFI, the EFI binary is added as
        alternative loader to the ISO creation parameter list. The
        EFI binary must be included into a fat filesystem in order
        to become recognized by the firmware. For details about this
        file refer to _create_embedded_fat_efi_image() from
        bootloader/config/grub2.py
        """
        loader_file = self.boot_path + '/efi'
        if os.path.exists(os.sep.join([self.source_dir, loader_file])):
            self.iso_loaders.append('-eltorito-alt-boot')
            iso_tool = self.get_tool_name()
            if iso_tool and CommandCapabilities.has_option_in_help(
                iso_tool, '-eltorito-platform', raise_on_error=False
            ):
                self.iso_loaders += ['-eltorito-platform', 'efi']
            self.iso_loaders += [
                '-b', loader_file, '-no-emul-boot', '-joliet-long'
            ]
            loader_file_512_byte_blocks = os.path.getsize(
                os.sep.join([self.source_dir, loader_file])
            ) / 512
            # boot-load-size is stored in a 16bit range, thus we only
            # set the value if it fits into that range
            if loader_file_512_byte_blocks <= 0xffff:
                self.iso_loaders.append(
                    '-boot-load-size'
                )
                self.iso_loaders.append(
                    format(int(loader_file_512_byte_blocks))
                )
Ejemplo n.º 16
0
    def add_efi_loader_parameters(self):
        """
        Add ISO creation parameters to embed the EFI loader

        In order to boot the ISO from EFI, the EFI binary is added as
        alternative loader to the ISO creation parameter list. The
        EFI binary must be included into a fat filesystem in order
        to become recognized by the firmware. For details about this
        file refer to _create_embedded_fat_efi_image() from
        bootloader/config/grub2.py
        """
        loader_file = self.boot_path + '/efi'
        if os.path.exists(os.sep.join([self.source_dir, loader_file])):
            self.iso_loaders.append('-eltorito-alt-boot')
            iso_tool = self.get_tool_name()
            if iso_tool and CommandCapabilities.has_option_in_help(
                    iso_tool, '-eltorito-platform', raise_on_error=False):
                self.iso_loaders += ['-eltorito-platform', 'efi']
            self.iso_loaders += [
                '-b', loader_file, '-no-emul-boot', '-joliet-long'
            ]
            loader_file_512_byte_blocks = os.path.getsize(
                os.sep.join([self.source_dir, loader_file])) / 512
            # boot-load-size is stored in a 16bit range, thus we only
            # set the value if it fits into that range
            if loader_file_512_byte_blocks <= 0xffff:
                self.iso_loaders.append('-boot-load-size')
                self.iso_loaders.append(
                    format(int(loader_file_512_byte_blocks)))
Ejemplo n.º 17
0
 def setup_keyboard_map(self):
     """
     Setup console keyboard
     """
     if 'keytable' in self.preferences:
         log.info(
             'Setting up keytable: %s', self.preferences['keytable']
         )
         if CommandCapabilities.has_option_in_help(
             'systemd-firstboot', '--keymap',
             root=self.root_dir, raise_on_error=False
         ):
             Path.wipe(self.root_dir + '/etc/vconsole.conf')
             Command.run([
                 'chroot', self.root_dir, 'systemd-firstboot',
                 '--keymap=' + self.preferences['keytable']
             ])
         elif os.path.exists(self.root_dir + '/etc/sysconfig/keyboard'):
             Shell.run_common_function(
                 'baseUpdateSysConfig', [
                     self.root_dir + '/etc/sysconfig/keyboard', 'KEYTABLE',
                     '"' + self.preferences['keytable'] + '"'
                 ]
             )
         else:
             log.warning(
                 'keyboard setup skipped no capable '
                 'systemd-firstboot or etc/sysconfig/keyboard found'
             )
Ejemplo n.º 18
0
    def __init__(self, filename, create_from_file_list=True, file_list=None):
        self.filename = filename
        self.create_from_file_list = create_from_file_list
        self.file_list = file_list

        if CommandCapabilities.check_version('tar', (1, 27)):
            self.xattrs_options = ['--xattrs', '--xattrs-include=*']
        else:
            self.xattrs_options = []
Ejemplo n.º 19
0
 def test_check_version(self, mock_run):
     command_type = namedtuple('command', ['output'])
     mock_run.return_value = command_type(
         output="Dummy line\ncommand v1.2.3\n")
     assert CommandCapabilities.check_version('command', (1, 2, 3))
     assert CommandCapabilities.check_version('command', (1, 1, 3))
     assert not CommandCapabilities.check_version('command', (1, 3))
     assert CommandCapabilities.check_version('command', (1, 2, 3),
                                              version_flags=['-v'])
     assert CommandCapabilities.check_version('command', (1, 2, 3),
                                              version_flags=['-v'],
                                              root='root_dir')
     mock_run.assert_has_calls([
         call(['command', '--version']),
         call(['command', '--version']),
         call(['command', '--version']),
         call(['command', '-v']),
         call(['chroot', 'root_dir', 'command', '-v'])
     ])
Ejemplo n.º 20
0
    def __init__(self, filename, create_from_file_list=True, file_list=None):
        self.filename = filename
        self.create_from_file_list = create_from_file_list
        self.file_list = file_list

        if CommandCapabilities.check_version('tar', (1, 27)):
            self.xattrs_options = [
                '--xattrs', '--xattrs-include=*'
            ]
        else:
            self.xattrs_options = []
Ejemplo n.º 21
0
 def post_init(self):
     """
     Initializes some umoci parameters and options
     """
     self.container_name = ':'.join(
         [self.container_dir, self.container_tag])
     if CommandCapabilities.has_option_in_help('umoci', '--no-history',
                                               ['repack', '--help']):
         self.no_history_flag = ['--no-history']
     else:
         self.no_history_flag = []
Ejemplo n.º 22
0
 def test_has_option_in_help(self, mock_run):
     command_type = namedtuple('command', ['output'])
     mock_run.return_value = command_type(
         output="Dummy line\n\t--some-flag\n\t--some-other-flag")
     assert CommandCapabilities.has_option_in_help('command', '--some-flag')
     assert CommandCapabilities.has_option_in_help(
         'command', '--some-flag', help_flags=['subcommand', '-h'])
     assert CommandCapabilities.has_option_in_help(
         'command',
         '--some-other-flag',
         help_flags=['subcommand', '-h'],
         root='root_dir')
     assert not CommandCapabilities.has_option_in_help(
         'command', '--non-existing-flag')
     mock_run.assert_has_calls([
         call(['command', '--help']),
         call(['command', 'subcommand', '-h']),
         call(['chroot', 'root_dir', 'command', 'subcommand', '-h']),
         call(['command', '--help'])
     ])
 def test_check_version(self, mock_run):
     command_type = namedtuple('command', ['output'])
     mock_run.return_value = command_type(
         output="Dummy line\ncommand v1.2.3\n"
     )
     assert CommandCapabilities.check_version('command', (1, 2, 3))
     assert CommandCapabilities.check_version('command', (1, 1, 3))
     assert not CommandCapabilities.check_version('command', (1, 3))
     assert CommandCapabilities.check_version(
         'command', (1, 2, 3), version_flags=['-v']
     )
     assert CommandCapabilities.check_version(
         'command', (1, 2, 3), version_flags=['-v'], root='root_dir'
     )
     mock_run.assert_has_calls([
         call(['command', '--version']),
         call(['command', '--version']),
         call(['command', '--version']),
         call(['command', '-v']),
         call(['chroot', 'root_dir', 'command', '-v'])
     ])
Ejemplo n.º 24
0
    def check_container_tool_chain_installed(self) -> None:
        """
        When creating container images the specific tools are used in order
        to import and export OCI or Docker compatible images. This check
        searches for those tools to be installed in the build system and
        fails if it can't find them
        """
        message_tool_not_found = dedent('''\n
            Required tool {name} not found in caller environment

            Creation of OCI or Docker images requires the tools {name} and
            skopeo to be installed on the build system. For SUSE based systems
            you can find the tools at:

            http://download.opensuse.org/repositories/Virtualization:/containers
        ''')
        message_version_unsupported = dedent('''\n
            {name} tool found with unknown version
        ''')
        message_unknown_tool = dedent('''\n
            Unknown tool: {0}.

            Please configure KIWI with an appropriate value (umoci or buildah).
            Consider this runtime configuration file syntax (/etc/kiwi.yml):

            oci:
                - archive_tool: umoci | buildah
        ''')

        expected_version = (0, 1, 0)

        if self.xml_state.get_build_type_name() in ['docker', 'oci']:
            runtime_config = RuntimeConfig()
            tool_name = runtime_config.get_oci_archive_tool()
            if tool_name == 'buildah':
                oci_tools = ['buildah', 'skopeo']
            elif tool_name == 'umoci':
                oci_tools = ['umoci', 'skopeo']
            else:
                raise KiwiRuntimeError(message_unknown_tool.format(tool_name))
            for tool in oci_tools:
                if not Path.which(filename=tool, access_mode=os.X_OK):
                    raise KiwiRuntimeError(
                        message_tool_not_found.format(name=tool)
                    )
                elif not CommandCapabilities.check_version(
                    tool, expected_version, raise_on_error=False
                ):
                    raise KiwiRuntimeError(
                        message_version_unsupported.format(name=tool)
                    )
            self._check_multitag_support()
Ejemplo n.º 25
0
 def post_init(self):
     """
     Initializes some umoci parameters and options
     """
     self.oci_dir = mkdtemp(prefix='kiwi_oci_dir.')
     self.container_dir = os.sep.join([self.oci_dir, 'oci_layout'])
     self.working_image = '{0}:{1}'.format(
         self.container_dir, Defaults.get_container_base_image_tag())
     if CommandCapabilities.has_option_in_help('umoci', '--no-history',
                                               ['config', '--help']):
         self.no_history_flag = ['--no-history']
     else:
         self.no_history_flag = []
Ejemplo n.º 26
0
 def _check_multitag_support(self) -> None:
     message = dedent('''\n
         Using additionaltags attribute requires skopeo tool to be
         capable to handle it, it must include the '--additional-tag'
         option for copy subcommand (check it running 'skopeo copy
         --help').\n
         It is known to be present since v0.1.30
     ''')
     if 'additional_names' in self.xml_state.get_container_config():
         if not CommandCapabilities.has_option_in_help(
             'skopeo', '--additional-tag', ['copy', '--help'],
             raise_on_error=False
         ):
             raise KiwiRuntimeError(message)
Ejemplo n.º 27
0
    def create_image_format(self):
        """
        Create ova disk format using ovftool from
        https://www.vmware.com/support/developer/ovf
        """
        # Check for required ovftool
        ovftool = Path.which(filename='ovftool', access_mode=os.X_OK)
        if not ovftool:
            tool_not_found_message = dedent('''\n
                Required tool {0} not found in PATH on the build host

                Building OVA images requires VMware's {0} tool which
                can be installed from the following location

                https://www.vmware.com/support/developer/ovf
            ''')
            raise KiwiCommandNotFound(
                tool_not_found_message.format(ovftool)
            )

        # Create the vmdk disk image and vmx config
        super(DiskFormatOva, self).create_image_format()

        # Convert to ova using ovftool
        vmx = self.get_target_file_path_for_format('vmx')
        ova = self.get_target_file_path_for_format('ova')
        try:
            os.unlink(ova)
        except OSError:
            pass
        ovftool_options = []
        if CommandCapabilities.has_option_in_help(
            ovftool, '--shaAlgorithm', raise_on_error=False
        ):
            ovftool_options.append('--shaAlgorithm=SHA1')
        Command.run(
            [ovftool] + ovftool_options + [vmx, ova]
        )
        # ovftool ignores the umask and creates files with 0600
        # apply file permission bits set in the vmx file to the
        # ova file
        st = os.stat(vmx)
        os.chmod(ova, stat.S_IMODE(st.st_mode))
Ejemplo n.º 28
0
Archivo: ova.py Proyecto: hwoarang/kiwi
    def create_image_format(self):
        """
        Create ova disk format using ovftool from
        https://www.vmware.com/support/developer/ovf
        """
        # Check for required ovftool
        ovftool = Path.which(filename='ovftool', access_mode=os.X_OK)
        if not ovftool:
            tool_not_found_message = dedent('''\n
                Required tool {0} not found in PATH on the build host

                Building OVA images requires VMware's {0} tool which
                can be installed from the following location

                https://www.vmware.com/support/developer/ovf
            ''')
            raise KiwiCommandNotFound(
                tool_not_found_message.format(ovftool)
            )

        # Create the vmdk disk image and vmx config
        self.vmdk.create_image_format()

        # Convert to ova using ovftool
        vmx = self.get_target_file_path_for_format('vmx')
        ova = self.get_target_file_path_for_format('ova')
        try:
            os.unlink(ova)
        except OSError:
            pass
        ovftool_options = []
        if CommandCapabilities.has_option_in_help(
            ovftool, '--shaAlgorithm', raise_on_error=False
        ):
            ovftool_options.append('--shaAlgorithm=SHA1')
        Command.run(
            [ovftool] + ovftool_options + [vmx, ova]
        )
        # ovftool ignores the umask and creates files with 0600
        # apply file permission bits set in the vmx file to the
        # ova file
        st = os.stat(vmx)
        os.chmod(ova, stat.S_IMODE(st.st_mode))
Ejemplo n.º 29
0
 def setup_timezone(self):
     """
     Setup timezone symlink
     """
     if 'timezone' in self.preferences:
         log.info('Setting up timezone: %s', self.preferences['timezone'])
         if CommandCapabilities.has_option_in_help('systemd-firstboot',
                                                   '--timezone',
                                                   root=self.root_dir,
                                                   raise_on_error=False):
             Path.wipe(self.root_dir + '/etc/localtime')
             Command.run([
                 'chroot', self.root_dir, 'systemd-firstboot',
                 '--timezone=' + self.preferences['timezone']
             ])
         else:
             zoneinfo = '/usr/share/zoneinfo/' + self.preferences['timezone']
             Command.run([
                 'chroot', self.root_dir, 'ln', '-s', '-f', zoneinfo,
                 '/etc/localtime'
             ])
Ejemplo n.º 30
0
 def setup_locale(self):
     """
     Setup UTF8 system wide locale
     """
     if 'locale' in self.preferences:
         if 'POSIX' in self.preferences['locale'].split(','):
             locale = 'POSIX'
         else:
             locale = '{0}.UTF-8'.format(
                 self.preferences['locale'].split(',')[0])
         log.info('Setting up locale: {0}'.format(
             self.preferences['locale']))
         if CommandCapabilities.has_option_in_help('systemd-firstboot',
                                                   '--locale',
                                                   root=self.root_dir,
                                                   raise_on_error=False):
             Path.wipe(self.root_dir + '/etc/locale.conf')
             Command.run([
                 'chroot', self.root_dir, 'systemd-firstboot',
                 '--locale=' + locale
             ])
Ejemplo n.º 31
0
 def setup_timezone(self):
     """
     Setup timezone symlink
     """
     if 'timezone' in self.preferences:
         log.info(
             'Setting up timezone: %s', self.preferences['timezone']
         )
         if CommandCapabilities.has_option_in_help(
             'systemd-firstboot', '--timezone',
             root=self.root_dir, raise_on_error=False
         ):
             Path.wipe(self.root_dir + '/etc/localtime')
             Command.run([
                 'chroot', self.root_dir, 'systemd-firstboot',
                 '--timezone=' + self.preferences['timezone']
             ])
         else:
             zoneinfo = '/usr/share/zoneinfo/' + self.preferences['timezone']
             Command.run([
                 'chroot', self.root_dir,
                 'ln', '-s', '-f', zoneinfo, '/etc/localtime'
             ])
Ejemplo n.º 32
0
    def check_luksformat_options_valid(self) -> None:
        """
        Options set via the luksformat element are passed along
        to the cryptsetup tool. Only options that are known to
        the tool should be allowed. Thus this runtime check looks
        up the provided option names if they exist in the cryptsetup
        version used on the build host
        """
        message = dedent('''\n
            Option {0!r} not found in cryptsetup

            The Option {0!r} could not be found in the help output
            of the cryptsetup tool.
        ''')
        luksformat = self.xml_state.build_type.get_luksformat()
        if luksformat:
            for option in luksformat[0].get_option():
                argument = option.get_name()
                if not CommandCapabilities.has_option_in_help(
                    'cryptsetup', argument, ['--help'],
                    raise_on_error=False
                ):
                    raise KiwiRuntimeError(message.format(argument))
Ejemplo n.º 33
0
Archivo: ova.py Proyecto: agraf/kiwi
 def create_image_format(self):
     # Creates the vmdk disk image and vmx config
     super(DiskFormatOva, self).create_image_format()
     # Converts to ova using ovftool
     vmx = self.get_target_file_path_for_format('vmx')
     ova = self.get_target_file_path_for_format('ova')
     try:
         os.unlink(ova)
     except OSError:
         pass
     ovftool_cmd = ['ovftool']
     if CommandCapabilities.has_option_in_help(
             'ovftool', '--shaAlgorithm', raise_on_error=False):
         ovftool_cmd.append('--shaAlgorithm=SHA1')
     ovftool_cmd.extend([vmx, ova])
     try:
         Command.run(ovftool_cmd)
     except KiwiCommandNotFound as e:
         log.info('Building OVA images requires VMware\'s ovftool, get it from https://www.vmware.com/support/developer/ovf/')
         raise e
     # ovftool ignores the umask and creates files with 0600 for some reason
     st = os.stat(vmx)
     os.chmod(ova, stat.S_IMODE(st.st_mode))
Ejemplo n.º 34
0
    def create(self, overwrite: bool = True):
        """
        Setup a loop device of the blocksize given in the constructor
        The file to loop is created with the size specified in the
        constructor unless an existing one should not be overwritten

        :param bool overwrite: overwrite existing file to loop
        """
        if overwrite:
            qemu_img_size = format(self.filesize_mbytes) + 'M'
            Command.run(['qemu-img', 'create', self.filename, qemu_img_size])
        loop_options = []
        if self.blocksize_bytes and self.blocksize_bytes != 512:
            if CommandCapabilities.has_option_in_help('losetup',
                                                      '--sector-size',
                                                      raise_on_error=False):
                loop_options.append('--sector-size')
            else:
                loop_options.append('--logical-blocksize')
            loop_options.append(format(self.blocksize_bytes))
        loop_call = Command.run(['losetup'] + loop_options +
                                ['-f', '--show', self.filename])
        self.node_name = loop_call.output.rstrip('\n')
Ejemplo n.º 35
0
    def setup_disk_image_config(
        self, boot_uuid=None, root_uuid=None, hypervisor=None,
        kernel=None, initrd=None, boot_options={}
    ):
        """
        Create grub2 config file to boot from disk using grub2-mkconfig

        :param string boot_uuid: unused
        :param string root_uuid: unused
        :param string hypervisor: unused
        :param string kernel: unused
        :param string initrd: unused
        :param dict boot_options:

        options dictionary that has to contain the root and boot
        device and optional volume configuration. KIWI has to
        mount the system prior to run grub2-mkconfig.

        .. code:: python

            {
                'root_device': string,
                'boot_device': string,
                'efi_device': string,
                'system_volumes': volume_manager_instance.get_volumes()
            }
        """
        self._mount_system(
            boot_options.get('root_device'),
            boot_options.get('boot_device'),
            boot_options.get('efi_device'),
            boot_options.get('system_volumes')
        )
        config_file = os.sep.join(
            [
                self.root_mount.mountpoint, 'boot',
                self.boot_directory_name, 'grub.cfg'
            ]
        )
        Command.run(
            [
                'chroot', self.root_mount.mountpoint,
                os.path.basename(self._get_grub2_mkconfig_tool()), '-o',
                config_file.replace(self.root_mount.mountpoint, '')
            ]
        )

        if self.validate_use_of_linuxefi:
            # On systems that uses GRUB_USE_LINUXEFI with grub2 version
            # less than 2.04 there is no support for dynamic EFI
            # environment checking. In this condition we change the
            # grub config to add this support as follows:
            #
            # * Apply only on grub < 2.04
            #    1. Modify grub.cfg to set linux/initrd as variables
            #    2. Prepend hybrid setup to select linux vs. linuxefi on demand
            #
            # Please note this is a one time modification done by kiwi
            # Any subsequent call of the grub config tool will overwrite
            # the setup and disables dynamic EFI environment checking
            # at boot time
            if not CommandCapabilities.check_version(
                self._get_grub2_mkconfig_tool(),
                version_waterline=(2, 4), raise_on_error=False
            ):
                with open(config_file) as grub_config_file:
                    grub_config = grub_config_file.read()
                    grub_config = re.sub(
                        r'([ \t]+)linux[efi]*([ \t]+)', r'\1$linux\2',
                        grub_config
                    )
                    grub_config = re.sub(
                        r'([ \t]+)initrd[efi]*([ \t]+)', r'\1$initrd\2',
                        grub_config
                    )
                with open(config_file, 'w') as grub_config_file:
                    grub_config_file.write(
                        Template(self.grub2.header_hybrid).substitute()
                    )
                    grub_config_file.write(grub_config)

        if self.root_reference:
            if self.root_filesystem_is_overlay or \
               Defaults.is_buildservice_worker():
                # grub2-mkconfig has no idea how the correct root= setup is
                # for disk images created with overlayroot enabled or in a
                # buildservice worker environment. Because of that the mkconfig
                # tool just finds the raw partition loop device and includes it
                # which is wrong. In this particular case we have to patch the
                # written config file and replace the wrong root= reference with
                # the correct value.
                with open(config_file) as grub_config_file:
                    grub_config = grub_config_file.read()
                    grub_config = grub_config.replace(
                        'root={0}'.format(boot_options.get('root_device')),
                        self.root_reference
                    )
                with open(config_file, 'w') as grub_config_file:
                    grub_config_file.write(grub_config)

        if self.firmware.efi_mode():
            self._copy_grub_config_to_efi_path(
                self.efi_mount.mountpoint, config_file
            )
Ejemplo n.º 36
0
 def test_check_version_no_match(self, mock_run):
     command_type = namedtuple('command', ['output'])
     mock_run.return_value = command_type(
         output="Dummy line\ncommand someother stuff\n")
     with raises(KiwiCommandCapabilitiesError):
         CommandCapabilities.check_version('command', (1, 2, 3))
 def test_check_version_no_match(self, mock_run):
     command_type = namedtuple('command', ['output'])
     mock_run.return_value = command_type(
         output="Dummy line\ncommand someother stuff\n"
     )
     CommandCapabilities.check_version('command', (1, 2, 3))
Ejemplo n.º 38
0
 def test_check_version_complex_pattern(self, mock_run):
     command_type = namedtuple('command', ['output'])
     mock_run.return_value = command_type(
         output="grub2-mkconfig (GRUB2) 2.02\n")
     assert CommandCapabilities.check_version('command', (2, 2)) is True
     assert CommandCapabilities.check_version('command', (2, 4)) is False