From 1e5b06be5b7351ba4b9b398ac4d5431348ae6c03 Mon Sep 17 00:00:00 2001
From: Colin Walters <walters@verbum.org>
Date: Fri, 18 Aug 2017 20:51:10 -0400
Subject: [PATCH] lib/deploy: Add .img to end of initramfs in /usr/lib/modules

Follow up to <https://github.com/ostreedev/ostree/pull/1079>; I was working on
the rpm-ostree updates for this, and I think it's more consistent if we have
`.img` here, since that's a closer match to the "remove $kver" that results in
`vmlinuz`. Also just best practice to have file suffix types where they make
sense.

The astute reader might notice this sneaks in a change where we'd crash if the
legacy bootdir didn't have an initramfs...yeah, should probably have test
coverage of that.

Closes: #1095
Approved by: jlebon
---
 docs/manual/deployment.md             |  2 +-
 src/libostree/ostree-sysroot-deploy.c | 27 +++++++++++++++++++++------
 tests/libtest.sh                      | 20 +++++++++++---------
 tests/test-admin-deploy-syslinux.sh   |  6 +++---
 4 files changed, 36 insertions(+), 19 deletions(-)

diff --git a/docs/manual/deployment.md b/docs/manual/deployment.md
index 0db03117..013a2d0e 100644
--- a/docs/manual/deployment.md
+++ b/docs/manual/deployment.md
@@ -45,7 +45,7 @@ distinguish it from the concept of a deployment.
 
 First, the tree must include a kernel (and optionally an initramfs).  The
 current standard locations for these are `/usr/lib/modules/$kver/vmlinuz` and
-`/usr/lib/modules/$kver/initramfs`.  The "boot checksum" will be computed
+`/usr/lib/modules/$kver/initramfs.img`.  The "boot checksum" will be computed
 automatically.  This follows the current Fedora kernel layout, and is
 the current recommended path.  However, older versions of libostree don't
 support this; you may need to also put kernels in the previous (legacy)
diff --git a/src/libostree/ostree-sysroot-deploy.c b/src/libostree/ostree-sysroot-deploy.c
index 8b2f5e4b..f50f2457 100644
--- a/src/libostree/ostree-sysroot-deploy.c
+++ b/src/libostree/ostree-sysroot-deploy.c
@@ -994,13 +994,27 @@ get_kernel_from_tree_usrlib_modules (int                  deployment_dfd,
   g_clear_object (&in);
   (void) close (fd); fd = -1;
 
-  /* Look for an initramfs, but it's optional */
-  if (!ot_openat_ignore_enoent (ret_layout->boot_dfd, "initramfs", &fd, error))
-    return FALSE;
+  /* Look for an initramfs, but it's optional; since there wasn't any precedent
+   * for this, let's be a bit conservative and support both `initramfs.img` and
+   * `initramfs`.
+   */
+  const char *initramfs_paths[] = {"initramfs.img", "initramfs"};
+  const char *initramfs_path = NULL;
+  for (guint i = 0; i < G_N_ELEMENTS(initramfs_paths); i++)
+    {
+      initramfs_path = initramfs_paths[i];
+      if (!ot_openat_ignore_enoent (ret_layout->boot_dfd, initramfs_path, &fd, error))
+        return FALSE;
+      if (fd != -1)
+        break;
+      else
+        initramfs_path = NULL;
+    }
   if (fd != -1)
     {
-      ret_layout->initramfs_srcpath = g_strdup ("initramfs");
-      ret_layout->initramfs_namever = g_strdup_printf ("initramfs-%s", kver);
+      g_assert (initramfs_path);
+      ret_layout->initramfs_srcpath = g_strdup (initramfs_path);
+      ret_layout->initramfs_namever = g_strdup_printf ("initramfs-%s.img", kver);
       in = g_unix_input_stream_new (fd, FALSE);
       if (!ot_gio_splice_update_checksum (NULL, in, checksum, cancellable, error))
         return FALSE;
@@ -1108,9 +1122,10 @@ get_kernel_from_tree_legacy_layouts (int                  deployment_dfd,
       g_assert (initramfs_checksum != NULL);
       if (strcmp (kernel_checksum, initramfs_checksum) != 0)
         return glnx_throw (error, "Mismatched kernel checksum vs initrd");
-      ret_layout->bootcsum = g_steal_pointer (&kernel_checksum);
     }
 
+  ret_layout->bootcsum = g_steal_pointer (&kernel_checksum);
+
   *out_layout = g_steal_pointer (&ret_layout);
   return TRUE;
 }
diff --git a/tests/libtest.sh b/tests/libtest.sh
index ce7cc3d9..1381a69e 100755
--- a/tests/libtest.sh
+++ b/tests/libtest.sh
@@ -381,14 +381,15 @@ setup_os_repository () {
     cd ${test_tmpdir}
     mkdir osdata
     cd osdata
-    mkdir -p usr/bin ${bootdir} usr/lib/modules/3.6.0 usr/share usr/etc
+    kver=3.6.0
+    mkdir -p usr/bin ${bootdir} usr/lib/modules/${kver} usr/share usr/etc
     kernel_path=${bootdir}/vmlinuz
-    initramfs_path=${bootdir}/initramfs
+    initramfs_path=${bootdir}/initramfs.img
     # /usr/lib/modules just uses "vmlinuz", since the version is in the module
     # directory name.
     if [[ $bootdir != usr/lib/modules/* ]]; then
-        kernel_path=${kernel_path}-3.6.0
-        initramfs_path=${initramfs_path}-3.6.0
+        kernel_path=${kernel_path}-${kver}
+        initramfs_path=${bootdir}/initramfs-${kver}.img
     fi
     echo "a kernel" > ${kernel_path}
     echo "an initramfs" > ${initramfs_path}
@@ -472,8 +473,9 @@ os_repository_new_commit ()
     branch=${3:-testos/buildmaster/x86_64-runtime}
     echo "BOOT ITERATION: $boot_checksum_iteration"
     cd ${test_tmpdir}/osdata
-    if test -f usr/lib/modules/3.6.0/vmlinuz; then
-        bootdir=usr/lib/modules/3.6.0
+    kver=3.6.0
+    if test -f usr/lib/modules/${kver}/vmlinuz; then
+        bootdir=usr/lib/modules/${kver}
     else
         if test -d usr/lib/ostree-boot; then
             bootdir=usr/lib/ostree-boot
@@ -483,10 +485,10 @@ os_repository_new_commit ()
     fi
     rm ${bootdir}/*
     kernel_path=${bootdir}/vmlinuz
-    initramfs_path=${bootdir}/initramfs
+    initramfs_path=${bootdir}/initramfs.img
     if [[ $bootdir != usr/lib/modules/* ]]; then
-        kernel_path=${kernel_path}-3.6.0
-        initramfs_path=${initramfs_path}-3.6.0
+        kernel_path=${kernel_path}-${kver}
+        initramfs_path=${bootdir}/initramfs-${kver}.img
     fi
     echo "new: a kernel ${boot_checksum_iteration}" > ${kernel_path}
     echo "new: an initramfs ${boot_checksum_iteration}" > ${initramfs_path}
diff --git a/tests/test-admin-deploy-syslinux.sh b/tests/test-admin-deploy-syslinux.sh
index 2a3c497d..3516c478 100755
--- a/tests/test-admin-deploy-syslinux.sh
+++ b/tests/test-admin-deploy-syslinux.sh
@@ -39,10 +39,10 @@ for test_bootdir in "boot" "usr/lib/ostree-boot"; do
     assert_file_has_content sysroot/boot/loader/entries/ostree-testos-0.conf 'options.* root=LABEL=MOO'
     assert_file_has_content sysroot/boot/loader/entries/ostree-testos-0.conf 'options.* quiet'
     assert_file_has_content sysroot/boot/ostree/testos-${bootcsum}/vmlinuz-3.6.0 'a kernel'
-    assert_file_has_content sysroot/boot/ostree/testos-${bootcsum}/initramfs-3.6.0 'an initramfs'
+    assert_file_has_content sysroot/boot/ostree/testos-${bootcsum}/initramfs-3.6.0.img 'an initramfs'
     # kernel/initrams should also be in the tree's /boot with the checksum
     assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/$test_bootdir/vmlinuz-3.6.0-${bootcsum} 'a kernel'
-    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/$test_bootdir/initramfs-3.6.0-${bootcsum} 'an initramfs'
+    assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/$test_bootdir/initramfs-3.6.0.img-${bootcsum} 'an initramfs'
     assert_file_has_content sysroot/ostree/deploy/testos/deploy/${rev}.0/etc/os-release 'NAME=TestOS'
     assert_file_has_content sysroot/ostree/boot.1/testos/${bootcsum}/0/etc/os-release 'NAME=TestOS'
     ${CMD_PREFIX} ostree admin status
@@ -64,7 +64,7 @@ rev=$(${CMD_PREFIX} ostree --repo=sysroot/ostree/repo rev-parse testos/buildmast
 ${CMD_PREFIX} ostree admin deploy --karg=root=LABEL=MOO --karg=quiet --os=testos testos:testos/buildmaster/x86_64-runtime
 assert_file_has_content sysroot/boot/loader/entries/ostree-testos-0.conf 'options.* root=LABEL=MOO'
 assert_file_has_content sysroot/boot/ostree/testos-${bootcsum}/vmlinuz-3.6.0 'a kernel'
-assert_file_has_content sysroot/boot/ostree/testos-${bootcsum}/initramfs-3.6.0 'an initramfs'
+assert_file_has_content sysroot/boot/ostree/testos-${bootcsum}/initramfs-3.6.0.img 'an initramfs'
 # Note this bootcsum shouldn't be the modules one
 assert_not_streq "${bootcsum}" "${usrlib_modules_bootcsum}"
 echo "ok kernel in /usr/lib/modules and /usr/lib/ostree-boot"