From 40694280efc5c3d40dbb6d89dbdc7b2ae69f4968 Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Tue, 25 Mar 2025 12:40:14 +0100 Subject: [PATCH] update-initramfs: add -s parameter When installing/upgrading packages (e. g. a kernel and initramfs-tools), the same initrd is generated twice: ``` $ apt-get install --no-install-recommends -y zstd initramfs-tools linux-image-generic [...] Setting up initramfs-tools-core (0.145) ... Setting up initramfs-tools (0.145) ... update-initramfs: deferring update (trigger activated) Setting up linux-image-6.12.16-amd64 (6.12.16-1) ... I: /vmlinuz.old is now a symlink to boot/vmlinuz-6.12.16-amd64 I: /initrd.img.old is now a symlink to boot/initrd.img-6.12.16-amd64 I: /vmlinuz is now a symlink to boot/vmlinuz-6.12.16-amd64 I: /initrd.img is now a symlink to boot/initrd.img-6.12.16-amd64 /etc/kernel/postinst.d/initramfs-tools: update-initramfs: Generating /boot/initrd.img-6.12.16-amd64 Setting up linux-image-amd64 (6.12.16-1) ... Processing triggers for libc-bin (2.40-7) ... Processing triggers for initramfs-tools (0.145) ... update-initramfs: Generating /boot/initrd.img-6.12.16-amd64 ``` Add a `-s` parameter to `update-initramfs` to set a Unix time stamp (seconds since 1970) for the update mode. An existing initramfs will only be updated if it is not newer than the specified Unix time stamp. This feature is the first step to avoid generating the initrd twice. LP: #1466965 --- update-initramfs | 23 ++++++++++++++++++++++- update-initramfs.8 | 9 +++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/update-initramfs b/update-initramfs index ffb84f5..7c543d5 100755 --- a/update-initramfs +++ b/update-initramfs @@ -229,6 +229,12 @@ set_highest_version() version=${1} } +has_been_updated_since_timestamp() { + local initramfs_timestamp timestamp="$1" + initramfs_timestamp=$(stat -c %Y "${initramfs}") + test "$initramfs_timestamp" -gt "$timestamp" +} + create() { if [ -z "${version}" ]; then @@ -268,6 +274,11 @@ update() set_initramfs + if [ -n "${SINCE_TIMESTAMP-}" ] && has_been_updated_since_timestamp "$SINCE_TIMESTAMP"; then + echo "update-initramfs: ${initramfs} has already been updated since $(date -d "@$SINCE_TIMESTAMP" +%c)." + exit 0 + fi + ro_boot_check backup_initramfs @@ -297,7 +308,7 @@ verbose=0 ## -OPTIONS=$(getopt -o "k:cudvtb:h?" --long help,version -n "$0" -- "$@") || usage_error +OPTIONS=$(getopt -o "k:cudvtb:s:h?" --long help,version -n "$0" -- "$@") || usage_error eval set -- "$OPTIONS" @@ -335,6 +346,16 @@ while true; do fi shift 2 ;; + -s) + SINCE_TIMESTAMP="$2" + case "$SINCE_TIMESTAMP" in + *[!0-9]*) + echo "E: '${SINCE_TIMESTAMP}' is not a Unix time (seconds since 1970)." >&2 + exit 1 + ;; + esac + shift 2 + ;; -h|-\?|--help) usage exit 0 diff --git a/update-initramfs.8 b/update-initramfs.8 index 0350a07..4337c05 100644 --- a/update-initramfs.8 +++ b/update-initramfs.8 @@ -11,6 +11,8 @@ update\-initramfs \- generate an initramfs image .RB [ \-v ] .RB [ \-b .IR directory ] +.RB [ \-s +.IR timestamp ] .br .BR update\-initramfs " " \-\-version .br @@ -64,6 +66,13 @@ the chosen action. \fB\-b \fI directory Set an different bootdir for the image creation. +.TP +\fB-s \fI timestamp +This parameter sets a Unix time stamp (seconds since 1970) for the update mode. +An existing initramfs will only be updated +if it is not newer than the specified Unix time stamp. +This parameter will be ignored if set to an empty string. + .TP \fB--version\fR Print the version information of the underlying initrd generation tool and exit.