From 7fa22cb7320519d5ef3018188feada62631735eb Mon Sep 17 00:00:00 2001 From: nagumii Date: Tue, 17 Dec 2024 20:09:09 +0200 Subject: [PATCH] moved time-setting code from rc.local to fire-once service --- .../overlay-luckfox-ubuntu/etc/rc.local | 31 ++--------------- .../lib/systemd/system/settime.service | 11 ++++++ .../usr/local/bin/settime.sh | 34 +++++++++++++++++++ 3 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/lib/systemd/system/settime.service create mode 100644 foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/settime.sh diff --git a/foxbuntu/project/cfg/BoardConfig_IPC/overlay/overlay-luckfox-ubuntu/etc/rc.local b/foxbuntu/project/cfg/BoardConfig_IPC/overlay/overlay-luckfox-ubuntu/etc/rc.local index 511dfc8..b019d89 100755 --- a/foxbuntu/project/cfg/BoardConfig_IPC/overlay/overlay-luckfox-ubuntu/etc/rc.local +++ b/foxbuntu/project/cfg/BoardConfig_IPC/overlay/overlay-luckfox-ubuntu/etc/rc.local @@ -34,32 +34,7 @@ echo 34 > /sys/class/gpio/export && echo out > /sys/class/gpio/gpio34/direction # fi # ) & -# if RTC recognized -if ls /dev/rtc* 1> /dev/null 2>&1; then - rtc="true" - if sudo hwclock -s 2>&1 | grep -a failed > /dev/null 2>&1; then - echo "RTC device found but failed to get time." - else - hwclock -s > /dev/null 2>&1 & - echo "RTC device found! $(date +"%Y-%m-%d %H:%M:%S %Z")." - fi -else - echo "RTC not found." - sudo date -s "1 JAN 2024 00:00:00" > /dev/null 2>&1 & -fi -# try get network time -date_str=$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g') -if [ -n "$date_str" ]; then - sudo date -s "$date_str" > /dev/null 2>&1 - sudo echo "Got network time: $(date +"%Y-%m-%d %H:%M:%S %Z")." - if [ "$rtc" = "true" ]; then - echo "Setting RTC to network time." - sudo hwclock -w & - fi -else - echo "Failed to fetch network time, using $(date +"%Y-%m-%d %H:%M:%S %Z") instead." -fi # necessary for internet over usb in otg mode (see https://web.archive.org/web/20241006173648/https://wiki.luckfox.com/Luckfox-Pico/Luckfox-Pico-Network-Sharing-1/) #sleep 5 && route add default gw 172.32.0.100 && echo "nameserver 8.8.8.8" >> /etc/resolv.conf @@ -67,8 +42,6 @@ fi # does not appear to lower power consumption, but needs more testing #(sleep 5 && cpufreq-set -u 408000 && echo "Boot complete, setting CPU frequency to 408mhz.") & -( - sleep 5 # run USB automount and config tool /usr/local/bin/usbconfig.sh @@ -85,8 +58,8 @@ fi echo 0 > /sys/class/gpio/gpio34/value; sleep 0.5; done - ) + ) echo "Boot complete." -) & + exit 0 \ No newline at end of file diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/lib/systemd/system/settime.service b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/lib/systemd/system/settime.service new file mode 100644 index 0000000..59221fc --- /dev/null +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/lib/systemd/system/settime.service @@ -0,0 +1,11 @@ +[Unit] +Description=Run settime script at startup +Before=meshtasticd.service +After=network.target + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/settime.sh + +[Install] +WantedBy=default.target diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/settime.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/settime.sh new file mode 100644 index 0000000..4938898 --- /dev/null +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/settime.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Check if the RTC (Real-Time Clock) has a valid time +if [[ $(hwclock -r) ]]; then + echo "Got time from RTC: $(hwclock -r)" # Output the time obtained from the RTC + time="$(hwclock -r)" # Store the RTC time in the 'time' variable + hwclock -s # Set the system time from the RTC time +fi + +# Check if we can get the time from a network source (e.g., google.com) +if [[ ! $(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g') ]]; then + echo "Could not get time from network" # If no time is obtained, print an error message +else + # Get the time from the network and store it in the 'time' variable + time="$(curl -s --head http://google.com | grep ^Date: | sed 's/Date: //g')" + echo "Got time from network: ${time}" # Output the time obtained from the network + date -s "${time}" # Set the system time from the network time +fi + +# If no time is set yet, fall back to a default date +if [[ -z ${time} ]]; then + echo "Could not get time from any source. Setting to Jan 1 2024." # Inform the user of the fallback date + date -s "1 JAN 2024 00:00:00" # Set the system time to January 1st, 2024 +fi + +# Check if the RTC device exists +if [[ -z /dev/rtc* ]]; then + echo "No RTC found" # If no RTC device is found, print an error message +else + echo "RTC found, setting time from system clock" # If an RTC is found, set its time from the system clock + hwclock -w # Write the system time to the RTC +fi + +exit 0 # Exit the script successfully