From ed3eeeeebb1e4ea7b93c48280bd6a5bd94b44865 Mon Sep 17 00:00:00 2001 From: Minecrell Date: Mon, 24 Feb 2020 11:53:59 +0100 Subject: [PATCH] main/postmarketos-mkinitfs: append extra initfs hash to file contents (!983) Partial revert of 24fa68c9 ("avoid storing initramfs-extra twice"). Some people have (rightfully) complained that including the hash in the filename of the initramfs-extra prevents the device from booting whenever the boot partition gets out of sync with the boot image. In general we should assume that those two are in sync, but there is no need to unnecessarily break this if previously booting the outdated extra initramfs worked just fine. At the end, the hash is just an implementation detail for the caching mechanism - we need it when (re-)generating the initramfs, but it does not have to be within the filename. Instead, we can simply append the hash to the file contents. GZIP will simply ignore the trailing garbage. The boot partition will then look like: $ cat /boot/initramfs--extra d3c7b449c6fc811d97351bbc46852b66 (the hash) This makes the filename of initramfs-extra nice and stable again. It also fixes a regression where the initramfs-extra would no longer get symlinked by "pmbootstrap export" (I was going to fix that in pmbootstrap but now this solution is more appealing...) While we're at it: Make the script more resilient again by writing to a temporary file first, then move it (atomically) to the real path. --- main/postmarketos-mkinitfs/APKBUILD | 4 ++-- main/postmarketos-mkinitfs/mkinitfs.sh | 28 ++++++++++++++------------ 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/main/postmarketos-mkinitfs/APKBUILD b/main/postmarketos-mkinitfs/APKBUILD index 8c61c2dd9..44b5c9415 100644 --- a/main/postmarketos-mkinitfs/APKBUILD +++ b/main/postmarketos-mkinitfs/APKBUILD @@ -1,5 +1,5 @@ pkgname=postmarketos-mkinitfs -pkgver=0.9.1 +pkgver=0.9.2 pkgrel=1 pkgdesc="Tool to generate initramfs images for postmarketOS" url="https://postmarketos.org" @@ -26,4 +26,4 @@ package() { sha512sums="1d49db8a48ad513cc548b8a0ea23cc64518e71c93863155b4e9d2271fb46090506331c03d6955d693c8568c248ecc76b218efe4a6f6bba57c41c5f6d775dc61b init.sh.in 3bcec7b35ced7e87c301f71a892e54aa40983396e2ebaa3c8cbd84c91f711b3ca0e30fbc3104b1a1018ec1af51844ba90a63d380359b51db6242562d21776ed0 init_functions.sh -88cda58aed10e7a8997b117b222b55d19d82e4317604f38fa82cf564858fef5bab94d2ef52b233df1ca262c1fc2c97f02dbe490cde0da7acf678804bf38f95a8 mkinitfs.sh" +7f7e32588a3658f707a483e2a14ec20396dfabd25f5e86ef2be3055e05ddafcba1285dc9b233c4cdf47424eb71f35dfdc8fd2dc92ff0c76b970981bdfc576475 mkinitfs.sh" diff --git a/main/postmarketos-mkinitfs/mkinitfs.sh b/main/postmarketos-mkinitfs/mkinitfs.sh index 6273e6346..813d7fe41 100644 --- a/main/postmarketos-mkinitfs/mkinitfs.sh +++ b/main/postmarketos-mkinitfs/mkinitfs.sh @@ -339,10 +339,10 @@ append_or_copy_dtb() } # Create the initramfs-extra archive -# Updates $outfile_extra with path to cached file (hash appended to filename) +# $1: outfile generate_initramfs_extra() { - echo "==> initramfs: creating $outfile_extra" + echo "==> initramfs: creating $1" osk_conf="$(get_osk_config)" if [ $? -eq 1 ]; then @@ -350,26 +350,28 @@ generate_initramfs_extra() exit 1 fi - # Generate output filename (initfs_extra_cache) by hashing all input files + # Hash all input files for caching initfs_extra_files=$(echo "$BINARIES_EXTRA$osk_conf" | xargs -0 -I{} sh -c 'ls $1 2>/dev/null' -- {} | sort -u) initfs_extra_files_hashes="$(md5sum $initfs_extra_files)" initfs_extra_hash="$(echo "$initfs_extra_files_hashes" | md5sum | awk '{ print $1 }')" - initfs_extra_cache="${outfile_extra}_${initfs_extra_hash}" - - if ! [ -e "$initfs_extra_cache" ]; then - # Delete old initramfs-extra_ files - rm -f "$outfile_extra"_* + # The hash is appended to the initramfs, check if up-to-date + if [ "$initfs_extra_hash" != "$(tail -c 32 "$1")" ]; then # Set up initramfs-extra in temp folder tmpdir_extra=$(mktemp -d /tmp/mkinitfs.XXXXXX) mkdir -p "$tmpdir_extra" copy_files "$(get_binaries_extra)" "$tmpdir_extra" copy_files "$osk_conf" "$tmpdir_extra" - create_cpio_image "$tmpdir_extra" "$initfs_extra_cache" + create_cpio_image "$tmpdir_extra" "$1.new" rm -rf "$tmpdir_extra" - fi - outfile_extra="$initfs_extra_cache" + # Append hash to initramfs (used to check if up-to-date) + echo -n "$initfs_extra_hash" >> "$1.new" + + # Replace old initramfs-extra *after* we are done to make sure + # it does not become corrupted if something goes wrong. + mv "$1.new" "$1" + fi } # initialize @@ -377,8 +379,6 @@ source_deviceinfo parse_commandline "$1" "$2" "$3" check_hook_files -generate_initramfs_extra - echo "==> initramfs: creating $outfile" tmpdir=$(mktemp -d /tmp/mkinitfs.XXXXXX) @@ -405,4 +405,6 @@ create_bootimg rm -rf "$tmpdir" +generate_initramfs_extra "$outfile_extra" + exit 0