Просмотр исходного кода

ubuntu osmorphing: move initramfs update to core

The helper that updates the initramfs on Ubuntu is duplicated by
multiple providers, we'll move it to Coriolis core.
Lucian Petrut 1 неделя назад
Родитель
Сommit
ddf05f6337

+ 19 - 0
coriolis/osmorphing/ubuntu.py

@@ -122,3 +122,22 @@ class BaseUbuntuMorphingTools(debian.BaseDebianMorphingTools):
                 "Writing following configuration to '%s': %s" % (
                     config_path_chroot, config_data))
             self._write_file_sudo(config_path_chroot, yaml.dump(config_data))
+
+    def _run_update_initramfs(self):
+        # env LC_ALL=C suppresses Perl/shell locale warnings that would
+        # otherwise appear in stdout and get mixed into the version list.
+        # Using 'env' rather than a shell assignment prefix because
+        # _exec_cmd_chroot runs the command directly via chroot (no shell),
+        # so "LC_ALL=C cmd" would be treated as the binary name.
+        raw = self._exec_cmd_chroot("env LC_ALL=C linux-version list")
+
+        kernel_versions = [v for v in raw.splitlines() if v and v[0].isdigit()]
+        if not kernel_versions:
+            LOG.warning(
+                "No kernel versions found via 'linux-version list'; "
+                "skipping update-initramfs. Raw output was: %r",
+                raw,
+            )
+            return
+        for version in kernel_versions:
+            self._exec_cmd_chroot(f"update-initramfs -k {version} -u")

+ 0 - 1
coriolis/tests/osmorphing/test_debian.py

@@ -383,7 +383,6 @@ deb http://archive.debian.org/debian wheezy-updates main non-free-firmware
         mock_write_file_sudo.assert_not_called()
         mock_exec_cmd_chroot.assert_not_called()
 
-
     @mock.patch.object(debian.BaseDebianMorphingTools, '_test_path_chroot')
     @mock.patch.object(debian.BaseDebianMorphingTools, '_exec_cmd_chroot')
     def test_install_uefi_fallback_bootloader(

+ 31 - 0
coriolis/tests/osmorphing/test_ubuntu.py

@@ -170,3 +170,34 @@ class BaseUbuntuMorphingToolsTestCase(test_base.CoriolisBaseTestCase):
                 self.os_root_dir, self.os_root_dir))
         mock_write_file_sudo.assert_called_once_with(
             'etc/netplan/file1.yaml', ubuntu.yaml.dump(config_data))
+
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot')
+    def test_update_initramfs(self, mock_exec_cmd_chroot):
+        mock_exec_cmd_chroot.side_effect = [
+            # 'linux-version list' output
+            "6.8.0-111-generic\n6.8.0-124-generic\n",
+            # 'update-initramfs' output
+            "",
+            "",
+        ]
+
+        self.morphing_tools._run_update_initramfs()
+
+        mock_exec_cmd_chroot.assert_has_calls([
+            mock.call("env LC_ALL=C linux-version list"),
+            mock.call("update-initramfs -k 6.8.0-111-generic -u"),
+            mock.call("update-initramfs -k 6.8.0-124-generic -u"),
+        ])
+
+    @mock.patch.object(base.BaseLinuxOSMorphingTools, '_exec_cmd_chroot')
+    def test_update_initramfs_no_kernels(self, mock_exec_cmd_chroot):
+        mock_exec_cmd_chroot.side_effect = [
+            # 'linux-version list' output
+            ""
+        ]
+
+        self.morphing_tools._run_update_initramfs()
+
+        mock_exec_cmd_chroot.assert_has_calls([
+            mock.call("env LC_ALL=C linux-version list"),
+        ])