Sep 12, 2012
0 notes

fake-hwclock for Arch Linux ARM on Raspberry Pi

UPDATE: If your installation uses systemd, please refer to this post for the alternate installation method.

UPDATE 2: I have re-written this in C. You can find it here: https://github.com/xanmanning/alarm-fake-hwclock

 

Prepare yourself for another geeky post. So, I have been using the Raspberry Pi on-and-off for a few months now. It is a lovely device but it is missing the hardware clock. This isn’t a problem for most people as ntpd will fix this immediately when using ethernet. For those of us using a WiFi adaptor it is often an inconvenience as it can take anything up to 10 minutes to update the clock. The Raspian distribution uses a Debian specific tool called fake-hwclock which sets the clock to the last time recorded before system halt, rather useful when you don’t want to be playing with files that appear to have been created 40-odd years in the future.

Here is my simple bash script implementation of fake-hwclock for Arch Linux ARM (and possibly others).

Recommended packages

I recommend that you install ntpd rather than openntpd that comes pre-installed on Arch Linux ARM, this is because ntpd is a bit more responsive when it comes to updating the time.

sudo pacman -Sy ntpd

The Code

Create, make executable, and open up the file /etc/rc.d/fake-hwclock

sudo touch /etc/rc.d/fake-hwclock ; sudo chmod +x /etc/rc.d/fake-hwclock ; sudo vim /etc/rc.d/fake-hwclock

You will need copy and paste this into /etc/rc.d/fake-hwclock

#!/bin/bash

	. /etc/rc.conf
	. /etc/rc.d/functions

MYSELF=$0

setclock() {
	stat_busy "Setting hardware clock"
	MYTIME=$(date -r $MYSELF '+%Y-%m-%d %T')
	date --set="$MYTIME" &>/dev/null
	if [ $? = 0 ] ; then
		add_daemon fake-hwclock
		stat_done
      	else
		stat_fail
		exit 1
	fi
}

saveclock() {
	stat_busy "Saving current time"
	
	# Homage to my friend Ian, who dislikes
	# people who code dirty.
	touch $MYSELF &>/dev/null

	if [ $? = 0 ] ; then
		rm_daemon fake-hwclock
		stat_done
	else
		stat_fail
		exit 1
	fi
}

case "$1" in
	start)
		setclock
		;;
	stop)
		saveclock
		;;
	*)
		echo "Usage: $MYSELF {start|stop}"
		exit 1
		;;
esac

Once this is done we will have to edit the file /etc/rc.conf so that fake-hwclock loads when the system loads. To do this you need to edit the array variable DAEMONS usually located at the bottom of the file.

sudo vim /etc/rc.conf

In my file, mine looks a little bit like this (I have edited some junk bits out)

DAEMONS=(!hwclock fake-hwclock syslog-ng dbus network ntpd @crond @sshd)

Note that the ! suffix before hwclock disables hwclock (this is not necessary in a device without a hardware clock). A note above this tells you to disable hwclock if you are using ntpd, this does not apply to my fake-hwclock script.

Done, reboot and give it a go!

Blog comments powered by Disqus

About
The (rant)log of an RIBA part II architectural assistant; jamming architecture and technology together into one incoherant mess.. Subscribe via RSS.