so here the fixed code, seems the above did hit a race where date was still finishing while fsck already started, so date needed to have 1 min added to the last mount time ... also the check for the year limits to only certain usecases so i dropped it. the code below works reliable now (together with /usr/share/initramfs-tools/hooks/fixclock from above and indeed fixrtc needs to be set permanently on the cmdline) :
# use the fixrtc cmdline option in your bootloader to
# automatically set the hardware clock to the date of
# the last mount of your root filesystem to avoid fsck
# to get confused by the superblock being in the future
BROKEN_CLOCK=""
for x in $(cat /proc/cmdline); do
case ${x} in
root=*) UUID=${x#*=} UUID="${UUID#*=}"
;;
fixrtc) BROKEN_CLOCK=1
;;
esac
done
if [ "$BROKEN_CLOCK" ];then ROOTDISK=$(readlink -f /dev/disk/by-uuid/$UUID)
TIMESTR=$(dumpe2fs -h $ROOTDISK 2>/dev/null|grep "Last mount time") TIME=${TIMESTR#*:}
date --set="${TIME} 1 minute"
hwclock --systohc
fi
so here the fixed code, seems the above did hit a race where date was still finishing while fsck already started, so date needed to have 1 min added to the last mount time ... also the check for the year limits to only certain usecases so i dropped it. the code below works reliable now (together with /usr/share/ initramfs- tools/hooks/ fixclock from above and indeed fixrtc needs to be set permanently on the cmdline) :
ogra@ubuntu:~$ cat /usr/share/ initramfs- tools/scripts/ local-premount/ fixrtc
#!/bin/sh -e
# initramfs init-top script for fixrtc
PREREQ=""
# Output pre-requisites
prereqs()
{
echo "$PREREQ"
}
case "$1" in
prereqs)
prereqs
exit 0
;;
esac
# use the fixrtc cmdline option in your bootloader to
# automatically set the hardware clock to the date of
# the last mount of your root filesystem to avoid fsck
# to get confused by the superblock being in the future
BROKEN_CLOCK=""
for x in $(cat /proc/cmdline); do
UUID= ${x#*=}
UUID= "${UUID# *=}"
BROKEN_ CLOCK=1
case ${x} in
root=*)
;;
fixrtc)
;;
esac
done
if [ "$BROKEN_CLOCK" ];then
ROOTDISK= $(readlink -f /dev/disk/ by-uuid/ $UUID)
date --set="${TIME} 1 minute"
hwclock --systohc
fi