使用 upstart 设置硬件时钟

使用 upstart 设置硬件时钟

我有一块运行 Ubuntu 14.04 的小型嵌入式主板 (Jetson TX1)。芯片安装的载板没有备用电池或超级电容,因此只要拔掉电源,时钟就会走时(而且通常不会连接互联网来设置时钟)。我有一个以前用过的电池供电的 I2C 时钟模块。时钟需要一个内核模块,我已经成功编译并安装了该模块(我改编了找到的说明这里到 TX1。)系统完全启动后,我可以通过以下方式设置系统时钟:

$> echo ds3231 0x68 | sudo tee /sys/class/i2c-dev/i2c-1/device/new_device
$> sudo hwclock -s -f /dev/rtc2

效果很好。我希望在启动时自动完成此操作,看来 upstart 是正确的工具。事实上,已经有一个任务 /etc/init/hwclock.conf 使用内置 RTC(即由于没有电池而失去时间的 RTC)执行此操作,所以我想我可以劫持它。它的内容是:

# hwclock - adjust system clock and timezone
#
# The hwclock task adjusts the system clock when the hardware clock is
# set to localtime (e.g. when dual-booting with Windows), and also
# ensures that the system timezone is set so that timestamps are written
# to FAT devices.

description "adjust system clock and timezone"

start on starting mountall

task

script
    . /etc/default/rcS
    [ "$UTC" = "yes" ] && tz="--utc" || tz="--localtime"
    [ "$BADYEAR" = "yes" ] && badyear="--badyear"
    exec hwclock --systz $tz --noadjfile $badyear
end script

我已将其更改为:

start on starting mountall

task

script
    /bin/echo "ds3231 0x68" > /sys/class/i2c-dev/i2c-1/device/new_device
    /sbin/hwclock -sf /dev/rtc2
end script

但是重新启动时,系统时间未设置,如果我查看 /var/logs/upstart/hwclock.log,则会显示以下消息:

hwclock: Cannot access the Hardware Clock via any known method
hwclock: Use the --debug option tosee the details of our search for an access method.

在我看来,这听起来像是在加载内核模块之前运行任务。我尝试过其他类似的事件,start on filesystemstart on runlevel [2345]都无济于事。然而,在系统启动后,我可以输入,sudo hwclock -sf /dev/rtc2它就可以正常工作。

更新:我刚刚尝试了该事件start on runlevel [23456],它似乎确实有效。我不确定为什么在运行级别中添加 6 会使其起作用,因为我认为运行级别 6 是用于重新启动的?这是最好的事件吗?还是有更好的事件来确保时钟设置可靠?

相关内容