[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: fake rtc



On Thu, Oct 21, 2010, peter green wrote:
> Many arm systems don't have a rtc. This means every time they reboot
> the clock goes back to the epoch. Clocks going back to the epoch
> causes all sorts of issues (some versions of fsck fail, make won't
> work properly, logs will be impossible to relate to each other, and
> very annoying forced password changes).

 [ Or sometimes they do have a clock, but no battery to back it up while
 the device is off (e.g. beagle, which has an optional battery) ]

 Your approach would work, or you could borrow the approach implemented
 in the Ubuntu version of initramfs-tools: Ubuntu added a
 scripts/local-premount/fixrtc which does nothing by default but if you
 pass "fixrtc" on the kernel cmdline, it sets the current system time to
 the last (u)mount time of the root filesystem.

 I'm attaching the relevant script

 I don't know if it was submitted to Debian; if you find it useful and
 you're using Debian, that would be a good occasion to do so

   Cheers
-- 
Loïc Minier
#!/bin/sh -e
# initramfs local-premount 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=""
ROOTDEV=""

for x in $(cat /proc/cmdline); do
        case ${x} in
        root=*)
                value=${x#*=}

                # Find the device node path depending on the form of root= :

                case ${value} in
                UUID=*)
                        ROOTDEV=/dev/disk/by-uuid/${value#UUID=}
                        ;;
                LABEL=*)
                        ROOTDEV=/dev/disk/by-label/${value#LABEL=}
                        ;;
                *)
                        ROOTDEV=${value}
                        ;;
                esac
        ;;
        fixrtc)
                BROKEN_CLOCK=1
        ;;
        esac
done

if [ -n "$BROKEN_CLOCK" -a -n "$ROOTDEV" ];then
        ROOTDISK=$(readlink -f "$ROOTDEV") &&

        TIMESTR=$(dumpe2fs -h "$ROOTDISK" 2>/dev/null|grep "Last mount time") &&
        TIME=${TIMESTR#*:} &&

        date --set="${TIME} 1 minute" >/dev/null 2>&1
fi

# This script is best-effort.  If we couldn't fudge the clock as desired,
# just try to carry on boot anyway:
# It will probably fail, but we won't have made the situation any worse.
exit 0

Reply to: