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
This commit is contained in:
Benjamin Drung 2025-03-25 12:40:14 +01:00
parent 8722bb830a
commit 40694280ef
2 changed files with 31 additions and 1 deletions

View File

@ -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

View File

@ -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.