From 0edc3d6b0fb432c36c81dec5332ad43a419e1f07 Mon Sep 17 00:00:00 2001 From: Ignacio Rodriguez Date: Wed, 22 Aug 2018 23:14:19 -0300 Subject: [PATCH] treehouses rtc/ntp --- cli.sh | 10 ++++++ modules/help.sh | 4 ++- modules/ntp.sh | 44 +++++++++++++++++++++++ modules/rtc.sh | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 5 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 modules/ntp.sh create mode 100644 modules/rtc.sh diff --git a/cli.sh b/cli.sh index 4973913ef2..24536d3460 100755 --- a/cli.sh +++ b/cli.sh @@ -16,8 +16,10 @@ source "$SCRIPTFOLDER/modules/hotspot.sh" source "$SCRIPTFOLDER/modules/image.sh" source "$SCRIPTFOLDER/modules/led.sh" source "$SCRIPTFOLDER/modules/locale.sh" +source "$SCRIPTFOLDER/modules/ntp.sh" source "$SCRIPTFOLDER/modules/password.sh" source "$SCRIPTFOLDER/modules/rename.sh" +source "$SCRIPTFOLDER/modules/rtc.sh" source "$SCRIPTFOLDER/modules/ssh.sh" source "$SCRIPTFOLDER/modules/sshkeyadd.sh" source "$SCRIPTFOLDER/modules/sshtunnel.sh" @@ -122,6 +124,14 @@ case $1 in led) led "$2" "$3" ;; + rtc) + checkroot + rtc "$2" "$3" + ;; + ntp) + checkroot + ntp "$2" + ;; help) help "$2" ;; diff --git a/modules/help.sh b/modules/help.sh index eb192b315d..8fa1a78e78 100644 --- a/modules/help.sh +++ b/modules/help.sh @@ -30,6 +30,8 @@ function help_default { echo " sshtunnel helps adding an sshtunnel" echo " [user@host]" echo " led [green|red] [mode] sets the led mode" + echo " rtc [rasclock|ds3231] sets up the rtc clock specified" + echo " ntp enables or disables the ntp service" echo } @@ -43,4 +45,4 @@ function help { help_default fi fi -} \ No newline at end of file +} diff --git a/modules/ntp.sh b/modules/ntp.sh new file mode 100644 index 0000000000..b4ef289e17 --- /dev/null +++ b/modules/ntp.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +function ntp { + status="$1" + + if [ "$status" = "on" ]; then + rm -rf /boot/time &> /dev/null + sed -i "s/server 127\.127\.1\.0//" /etc/ntp.conf + sed -i "s/fudge 127\.127\.1\.0 stratum 10//" /etc/ntp.conf + sed -i "s/restrict 192\.168\.0\.0 mask 255\.255\.0\.0 nomodify notrap//" /etc/ntp.conf + + echo "Success: please reboot you rpi to apply changes." + elif [ "$status" = "off" ]; then + service ntp restart + date > /boot/time + hwclock -w + hwclock -r >> /boot/time + hwclock -s + date >> /boot/time + { + echo "server 127.127.1.0" + echo "fudge 127.127.1.0 stratum 10" + echo "restrict 192.168.0.0 mask 255.255.0.0 nomodify notrap" + } >> /etc/ntp.conf + echo "Success: please reboot you rpi to apply changes." + else + echo "Error: only on, off options are supported" + exit 0 + fi +} + +function ntp_help { + echo "" + echo "Usage: $(basename "$0") ntp " + echo "" + echo "Enables or disables time through ntp servers" + echo "" + echo "Example:" + echo " $(basename "$0") ntp on" + echo " Enables the ntp service." + echo "" + echo " $(basename "$0") ntp off" + echo " Disables the ntp service." +} \ No newline at end of file diff --git a/modules/rtc.sh b/modules/rtc.sh new file mode 100644 index 0000000000..31d0048fe3 --- /dev/null +++ b/modules/rtc.sh @@ -0,0 +1,94 @@ +#!/bin/bash + +declare -A rtcclockdata +rtcclockdata["rasclock"]="dtoverlay=i2c-rtc,pcf2127" +rtcclockdata["ds3231"]="dtoverlay=i2c-rtc,ds3231" + +function get_current_clock { + for i in "${rtcclockdata[@]}" + do + if grep -q "$i" "/boot/config.txt" 2>/dev/null; then + prevClock="$i" + break + fi + done + + echo "$prevClock" +} + +function write_rtc { + clock="$1" + + prevClock=$(get_current_clock) + + if [ ! -z "$prevClock" ]; then + sed -i -e "s/$prevClock/$clock/g" /boot/config.txt + else + echo "$clock" >> /boot/config.txt + fi +} + +function rtc { + status="$1" + clock="$2" + + if [ "$status" = "on" ]; then + if [ -z "$clock" ]; then + echo "Error: you need to specify a clock" + exit 0 + elif [ -z "${rtcclockdata[$clock]}" ]; then + echo "Error: the clock is not supported." + exit 0 + else + write_rtc "${rtcclockdata[$clock]}" + + if [ -f "/lib/udev/hwclock-set.old" ]; then + cp /lib/udev/hwclock-set.old /lib/udev/hwclock-set + else + cp /lib/udev/hwclock-set /lib/udev/hwclock-set.old + fi + + # https://i.imgur.com/aq0rLJl.png + sed -e '/run\/systemd\/system/,+2 s/^#*/#/' -i /lib/udev/hwclock-set + sed -e '/--systz/ s/^#*/#/' -i /lib/udev/hwclock-set + + apt-get --force-yes -y remove fake-hwclock -qq &> /dev/null + update-rc.d -f fake-hwclock remove &> /dev/null + + echo "Success: clock changed. Please reboot" + fi + elif [ "$status" = "off" ]; then + currentClock=$(get_current_clock) + if [ ! -z "$currentClock" ]; then + sed -i "s/$currentClock//" /boot/config.txt + fi; + + cp /lib/udev/hwclock-set.old /lib/udev/hwclock-set + + apt-get -y install fake-hwclock -qq &> /dev/null + update-rc.d -f fake-hwclock defaults &> /dev/null + + echo "Success: clock changed. Please reboot" + else + echo "Error: only on, off options are supported" + exit 0 + fi +} + +function rtc_help { + echo "" + echo "Usage: $(basename "$0") rtc [ds3231|rasclock]" + echo "" + echo "Enables or disables the rtc clock" + echo "" + echo "Example:" + echo " $(basename "$0") rtc off" + echo " Disables the rtc clock." + echo "" + echo " $(basename "$0") rtc on rasclock" + echo " Set ups the system to make the 'rasclock' clock work." + echo "" + echo " $(basename "$0") rtc on ds3231" + echo " Set ups the system to make the 'ds3231' clock work." + echo "" +} \ No newline at end of file diff --git a/package.json b/package.json index cd49ea19cf..b3c1878acc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@treehouses/cli", - "version": "1.1.0", + "version": "1.1.0-rtc2", "description": "Thin command-line interface for Raspberry Pi low level configuration.", "main": "cli.sh", "bin": {