Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -122,6 +124,14 @@ case $1 in
led)
led "$2" "$3"
;;
rtc)
checkroot
rtc "$2" "$3"
;;
ntp)
checkroot
ntp "$2"
;;
help)
help "$2"
;;
Expand Down
4 changes: 3 additions & 1 deletion modules/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function help_default {
echo " sshtunnel <add|remove|show> helps adding an sshtunnel"
echo " <portinterval> [user@host]"
echo " led [green|red] [mode] sets the led mode"
echo " rtc <on|off> [rasclock|ds3231] sets up the rtc clock specified"
echo " ntp <on|off> enables or disables the ntp service"
echo
}

Expand All @@ -43,4 +45,4 @@ function help {
help_default
fi
fi
}
}
44 changes: 44 additions & 0 deletions modules/ntp.sh
Original file line number Diff line number Diff line change
@@ -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 <on|off>"
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."
}
94 changes: 94 additions & 0 deletions modules/rtc.sh
Original file line number Diff line number Diff line change
@@ -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 <on|off> [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 ""
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down