diff --git a/seqs/raspberry.sh b/seqs/raspberry.sh index 2c0c104..01b3310 100755 --- a/seqs/raspberry.sh +++ b/seqs/raspberry.sh @@ -11,6 +11,7 @@ WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )" osName= distName= +RPI_BOOT_CONFIG="/boot/config.txt" step_config() { if [ "$(which lsb_release)" == "" ] ; then @@ -62,9 +63,11 @@ downDay=$(date +%Y%m%d) downDir="/tmp" downLoc="${downDir}/raspios_$downDay.zip" -step_2_info() { echo -n "Write Raspberry Pi OS image to SD card " - if [ -z $2 ] ; then echo "[SD CARD DEVICE]"; else echo "$2"; fi +step_2_info() { + echoinfoArgs "[SD CARD DEVICE]" + echo "Write Raspberry Pi OS image to SD card" echoinfo "This operation will delete all data previously on the SD card!" + [ $CONTEXT_EXE -ne 0 ] && [ -z $2 ] && echoinfo " [SD CARD DEVICE]: $2" } step_2_alias() { ALIAS="writesd"; } step_2() { @@ -101,7 +104,14 @@ step_2() { exe read -p " Press enter to contiue." } -step_3_info() { echo "Prepare SD for first run [SD CARD DEVICE]"; } +step_3_info() { + echoinfoArgs "[SD CARD DEVICE]" + echo "Prepare SD for first run" + if [ $CONTEXT_EXE -ne 0 ]; then + shift + [ ! -z $1 ] && echoinfo " [SD CARD DEVICE]: $1" + fi +} step_3() { if [ -z $2 ] && [ ! -z $SDDEV ] ; then if [ $QUIET -ne 0 ] ; then @@ -141,15 +151,13 @@ step_3() { } step_4_info() { - local sd="[SD CARD DEVICE]" - local hd="[HD DEVICE]" - if [ ! -z $2 ] ; then - sd="SD: \"$2\"" + echoinfoArgs "[SD CARD DEVICE] [HD DEVICE]" + echo "Prepare SD card to boot from hard disk" + if [ $CONTEXT_EXE -ne 0 ]; then + shift + [ ! -z $1 ] && echoinfo " [SD CARD DEVICE]: $1" + [ ! -z $2 ] && echoinfo " [HD DEVICE]: $2" fi - if [ ! -z $3 ] ; then - hd="HD: \"$3\"" - fi - echo "Prepare $sd to boot from $hd" } step_4_alias() { ALIAS="hdboot"; } step_4() { @@ -209,15 +217,14 @@ step_4() { } step_5_info() { - local sd="[SD CARD DEVICE]" - local hd="[HD DEVICE]" - if [ ! -z $2 ] ; then - sd="SD: \"$2\"" + echoinfoArgs "[SD CARD DEVICE] [HD DEVICE]" + echo "Prepare HD for boot (TODO)" + + if [ $CONTEXT_EXE -ne 0 ]; then + shift + [ ! -z $1 ] && echoinfo " [SD CARD DEVICE]: $1" + [ ! -z $2 ] && echoinfo " [HD DEVICE]: $2" fi - if [ ! -z $3 ] ; then - hd="HD: \"$3\"" - fi - echo "Prepare HD for boot $sd $hd (TODO)" } step_5() { #TODO @@ -258,86 +265,6 @@ step_5() { echo " [I] modify $HDROOT/etc/dhcpcd.conf for static IPs" } -step_20_info() { echo "Disable swap file and remove it"; } -step_20_alias() { ALIAS="disableswap"; } -step_20() { - exe swapoff -a - exe systemctl disable dphys-swapfile - exe rm -rf "$rpiSwapFile" - echoerr " [W] Reboot to apply changes" - if [ $QUIET -eq 0 ] ; then - exe read -p " Reboot now ([n]/y)? " answer - case $answer in - [yY]) - echoerr " [I] Rebooting now ..." - exe reboot - ;; - *) - ;; - esac - fi -} -rpiSwapFile="/var/swap" - -step_22_info() { echo "Resize second SD card partition [SD CARD DEVICE]"; } -step_22_alias() { ALIAS="resizesd"; } -step_22() { - shift - read_sd_dev "$1" - if [ -z $SDDEV ] || [ "$SDDEV" = "" ] || [ "$SDROOT" == "/" ] ; then - echoerr " [E] No SD found" - return 1 - fi - echo " [I] Device to be resized: $SDROOTDEV" - - exe umount -q "$SDROOT" - exe parted -s "$SDDEV" "resizepart $SDROOTPARTNO -1" quit - saveReturn $? - exe e2fsck -f "$SDROOTDEV" - saveReturn $? - exe resize2fs "$SDROOTDEV" - saveReturn $? - if [ -z $SDROOT ] ; then - read -p "Please provide a mountpoint: " SDROOT - exe mkdir -p "$SDROOT" - fi - exe mount "$SDROOTDEV" "$SDROOT" - saveReturn $? - getReturn - return $? -} - -step_24_info() { - echo "Turn off power LED [OPTION]" - echoinfo " [OPTION]" - echoinfo " -p : Turn off permanentely (/etc/rc.local)" -} -step_24_alias() { ALIAS="powerledoff"; } -step_24() { - exep "${ledOffCmd}" - if [ ! -z $2 ] ; then - exep "grep \"$ledPowerBright\" $startScript >>/dev/null" - if [ $? -eq 0 ] ; then - echo " [I] Power LED brightness already used. Please check $startScript manually for:" - echo " $ledOffCmd" - return 1 - fi - exe sed -i "s/^exit 0/# Turn off power LED/" "$startScript" - exep "echo \"${ledOffCmd}\" >> $startScript" - exep "echo -e \"\nexit 0\" >> $startScript" - fi -} -startScript="/etc/rc.local" -ledPowerBright="/sys/class/leds/led1/brightness" -ledOffCmd="sudo sh -c 'echo 0 > ${ledPowerBright}'" - -step_26_info() { echo "Restart network without reboot"; } -step_26_alias() { ALIAS="netrestart"; } -step_26() { - exep "sudo ip link set eth0 down && sudo ip link set eth0 up" -} - - read_sd_dev() { local partExt="" if [ ! -z "$1" ] ; then @@ -429,5 +356,160 @@ read_hd_dev() { return 0 } -VERSION_SEQREV=9 +step_20_info() { echo "Disable swap file and remove it"; } +step_20_alias() { ALIAS="disable_swap"; } +step_20() { + exe swapoff -a + exe systemctl disable dphys-swapfile + exe rm -rf "$rpiSwapFile" + echoerr " [W] Reboot to apply changes" + if [ $QUIET -eq 0 ] ; then + exe read -p " Reboot now ([n]/y)? " answer + case $answer in + [yY]) + echoerr " [I] Rebooting now ..." + exe reboot + ;; + *) + ;; + esac + fi +} +rpiSwapFile="/var/swap" + +step_22_info() { echoinfoArgs "[SD CARD DEVICE]"; echo "Resize second SD card partition"; } +step_22_alias() { ALIAS="resizesd"; } +step_22() { + shift + read_sd_dev "$1" + if [ -z $SDDEV ] || [ "$SDDEV" = "" ] || [ "$SDROOT" == "/" ] ; then + echoerr " [E] No SD found" + return 1 + fi + echo " [I] Device to be resized: $SDROOTDEV" + + exe umount -q "$SDROOT" + exe parted -s "$SDDEV" "resizepart $SDROOTPARTNO -1" quit + saveReturn $? + exe e2fsck -f "$SDROOTDEV" + saveReturn $? + exe resize2fs "$SDROOTDEV" + saveReturn $? + if [ -z $SDROOT ] ; then + read -p "Please provide a mountpoint: " SDROOT + exe mkdir -p "$SDROOT" + fi + exe mount "$SDROOTDEV" "$SDROOT" + saveReturn $? + getReturn + return $? +} + +step_24_info() { + echoinfoArgs "[OPTION]" + echo "Turn off power LED" + echoinfo " [OPTION]" + echoinfo " -p : Turn off permanentely (/etc/rc.local)" +} +step_24_alias() { ALIAS="disable_powerled"; } +step_24() { + exep "${ledOffCmd}" + if [ ! -z "$2" ] && [ "$2" == "-p" ] ; then + exep "grep \"$ledPowerBright\" $startScript >>/dev/null" + if [ $? -eq 0 ] ; then + echo " [I] Power LED brightness already used. Please check $startScript manually for:" + echo " $ledOffCmd" + return 1 + fi + exe sed -i "s/^exit 0/# Turn off power LED/" "$startScript" + exep "echo \"${ledOffCmd}\" >> $startScript" + exep "echo -e \"\nexit 0\" >> $startScript" + fi +} +startScript="/etc/rc.local" +ledPowerBright="/sys/class/leds/led1/brightness" +ledOffCmd="sudo sh -c 'echo 0 > ${ledPowerBright}'" + +step_26_info() { echo "Restart network without reboot"; } +step_26_alias() { ALIAS="netrestart"; } +step_26() { + exep "sudo ip link set eth0 down && sudo ip link set eth0 up" +} + +step_28_info() { echo "Disable bluetooth"; } +step_28_alias() { ALIAS="disable_bluetooth"; } +step_28() { + checkBootConfig "$RPI_CONFIG_DTOVERLAY" "$RPI_CONF_DI_BLUETOOTH" + [ $? -eq 0 ] && endReturn -o 1 "Bluetooth already disabled" + + addConf -a "${RPI_CONFIG_DTOVERLAY}=$RPI_CONF_DI_BLUETOOTH" "$RPI_BOOT_CONFIG" +} +RPI_CONFIG_DTOVERLAY="dtoverlay" +RPI_CONF_DI_BLUETOOTH="disable-bt" +RPI_CONF_DI_WIFI="disable-wifi" + +step_29_info() { echo "Disable bluetooth services"; } +step_29() { + exe systemctl disable hciuart.service + exe systemctl disable bluealsa.service + exe systemctl disable bluetooth.service + + echoseq " [I] Consider uninstalling bluetooth software:" + echoseq "apt purge --autoremove -y bluez" + echoseq + echoseq " [W] Reboot to make changes active" +} + + +step_31_info() { echo "Disable Wifi"; } +step_31_alias() { ALIAS="disable_wifi"; } +step_31() { + checkBootConfig "$RPI_CONFIG_DTOVERLAY" "$RPI_CONF_DI_WIFI" + [ $? -eq 0 ] && endReturn -o 1 "Wifi already disabled" + + addConf -a "${RPI_CONFIG_DTOVERLAY}=$RPI_CONF_DI_WIFI" "$RPI_BOOT_CONFIG" + + echoseq " [W] Reboot to make changes active" +} + +step_33_info() { echo "Disable HDMI"; } +step_33_alias() { ALIAS="disable_hdmi"; } +step_33() { + checkBootConfig "$RPI_CONFIG_HDMI_BLANK" "$RPI_CONF_DI_HDMI" + [ $? -eq 0 ] && endReturn -o 1 "HDMI already disabled" + + addConf -a "${RPI_CONFIG_HDMI_BLANK}=$RPI_CONF_DI_HDMI" "$RPI_BOOT_CONFIG" +} +RPI_CONFIG_HDMI_BLANK="hdmi_blanking" +RPI_CONF_DI_HDMI="1" + +step_34_info() { echo "Disable TV service"; } +step_34() { + exe $tvserviceOffCmd + exep "grep \"$tvserviceBin\" $startScript >>/dev/null" + if [ $? -eq 0 ] ; then + echo " [I] Tvservice already used. Please check $startScript manually for:" + echo " $tvserviceOffCmd" + return 1 + fi + + exe sed -i "s/^exit 0/# Turn off tvservice/" "$startScript" + exep "echo \"${tvserviceOffCmd}\" >> $startScript" + exep "echo -e \"\nexit 0\" >> $startScript" + + echoseq " [W] Reboot to make changes active" +} +tvserviceBin="/usr/bin/tvservice" +tvserviceOffCmd="${tvserviceBin} -o'" + +# checkBootConfig [VALUE] +checkBootConfig() { + [ -z "$1" ] && return 1 + + local re_check="^[[:space:]]*[^#]*${1}[[:space:]]*=[[:space:]]*$2" + grep -rqE "$re_check" "$RPI_BOOT_CONFIG" + return $? +} + +VERSION_SEQREV=14 . /usr/local/bin/sequencer.sh