Sunday, 19 May 2013

setting system time at startup from ext4 filesystem

setting system time at startup from ext4 filesystem

I have a pc with no battery backup and using ubuntu 12.04 with ext4 it will constantly ask me to handle the situation by saying that my superblocks last write is in the future.
So I wonder - can I cheat with something like this - setting the time from the superblock before getting in trouble. Is it a dumb idea - and where should I put it?
#!/bin/bash
# set the time based on last mount of disk

DEVICE="$1"

[ "${DEVICE}" = "" ] && echo "Won't operate without device" > 2 && exit 3

LAST_MOUNT_DATE=$(\
    sudo dumpe2fs -h $DEVICE |\
    grep 'Last mount time:' |\
    sed 's/^[^:]*: *//'
    )
LAST_MOUNT_YEAR=${LAST_MOUNT_DATE//* } # remove all upto last space

THIS_BOOT_DATE=$(date)
THIS_BOOT_YEAR=${THIS_BOOT_DATE//* } # remove all upto last space


if [[ "$THIS_BOOT_YEAR" < "$LAST_MOUNT_YEAR" ]] ; then
    date -s "$LAST_MOUNT_DATE"
fi

No comments:

Post a Comment