Files
shell_sequencer/seqs/raspberry.sh

306 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# Collection of setup steps for a new raspberry pi
# (e.g. boot from HD, disable swap file, ...)
# Get script working directory
# (when called from a different directory)
WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
#CONFIG_FILE="$WDIR/${toolName}.cfg"
#CONFIG_FILE_DEFAULT="${CONFIG_FILE}.example"
osName=
distName=
step_config() {
if [ "$(which lsb_release)" == "" ] ; then
echoerr " [W] Cannot detect OS. Assuming Ubuntu"
osName="Ubuntu"
else
osName=$(lsb_release -is)
distName=$(lsb_release -cs)
fi
if [ "$osName" == "" ] ; then
echoerr " [W] Error dedecting OS. Assuming Ubuntu"
osName="Ubuntu"
fi
echo " [I] Detected OS: $osName $distName"
}
SDDEV=
SDBOOT=
SDROOT=
SDROOTPUUID=
HDDEV=
HDROOT=
HDROOTPUUID=
step_1_info() { echo "Download latest raspbian lite image from"
echoinfo "$downUrl"; }
step_1_alias() { ALIAS="setup"; }
step_1() {
if [ ! -f "$downLoc" ] ; then
exe wget "$downUrl" -O "$downLoc"
echo -ne " [I] sha256 sum of download:\n "
exe sha256sum "$downLoc"
fi
downImgName="${downDir}/$(zipinfo -1 "$downLoc")"
if [ ! -f "$downImgName" ] ; then
exe unzip "$downLoc" -d "$downDir"
endReturn -o $? "Unzip raspbian image failed"
fi
}
downUrl="https://downloads.raspberrypi.org/raspbian_lite_latest"
downImgName=""
downDay=$(date +%Y%m%d)
downDir="/tmp"
downLoc="${downDir}/raspbian_$downDay.zip"
step_2_info() { echo -n "Write raspbian image to SD card "
if [ -z $2 ] ; then echo "[SD CARD DEVICE]"; else echo "$2"; fi
echoinfo "This operation will delete all data previously on the SD card!"
}
step_2_alias() { ALIAS="writesd"; }
step_2() {
if [ "$downImgName" == "" ] ; then
# called again to make sure $downImgName is populated
step setup
fi
if [ ! -f "$downImgName" ] ; then
echoerr " [E] No raspian image found"
return 1
fi
read_sd_dev "$2"
# check if device was confirmed
if [ $? -ne 0 ] ; then
echoerr " [E] SD card device not found"
return 1
fi
# write image
exe dd bs=4M if="$downImgName" of="$SDDEV" conv=fsync
endReturn -o "$?" "Writing image to $SDDEV failed"
exe sync
# SD partitions will have new informations now
# Leave SD card device intact for next step
SDBOOT=""
SDROOT=""
# TODO ? automatic remount
echoerr " [W] Please remove SD now and plug it back in."
exe read -p " Press enter to contiue."
}
step_3_info() { echo "Prepare SD for first run [SD CARD DEVICE]"; }
step_3() {
if [ -z $2 ] && [ ! -z $SDDEV ] ; then
if [ $QUIET -ne 0 ] ; then
answer="n"
else
exe lsblk -p "$SDDEV"
echo
exe read -p "Is $SDDEV still the SD card (y/[n])? " answer
fi
case $answer in
[yY])
# Nothing todo
;;
*)
SDDEV=""
;;
esac
fi
read_sd_dev "$2"
endReturn -o $? "SD card device not found"
if [ ! -w "$SDBOOT" ] ; then
echoerr " [E] SD card boot partion not writeable"
return 1
fi
# enable systemd ssh service
echo " [I] Enable SSH access"
exe touch "$SDBOOT"/ssh
# save SD boot settings for easy emergency start from SD
echo " [I] Save SD card boot information for later use"
exe cp -ar "$SDBOOT"/cmdline.txt "$SDBOOT"/cmdline.txt.sd
exe cp -ar "$SDROOT"/etc/fstab "$SDROOT"/etc/fstab.sd
}
step_4_info() {
local sd="[SD CARD DEVICE]"
local hd="[HD DEVICE]"
if [ ! -z $2 ] ; then
sd="SD: \"$2\""
fi
if [ ! -z $3 ] ; then
hd="HD: \"$3\""
fi
echo "TODO Prepare $sd to boot from $hd"
}
step_4_alias() { ALIAS="hdboot"; }
step_4() {
#TODO
echoerr " [E] Not ready yet...TODO"
#return 1
read_sd_dev "$2"
endReturn -o $? "SD detection error $?"
echo " [I] SD: $SDDEV"
echo " $SDBOOT"
echo " $SDROOT"
echo " $SDROOTPUUID"
read_hd_dev "$3"
endReturn -o $? "HD detection error"
echo " [I] HD: $HDDEV"
echo " $HDROOT"
echo " $HDROOTPUUID"
echo
echo " [I] modify PARTUUID of $SDBOOT/cmdline.txt"
echo " root=PARTUUID=******00-01"
}
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 [MOUNTPOINT]"; }
step_22_alias() { ALIAS="resizesd"; }
step_22() {
local mountP="$resizeMount"
if [ ! -z $2 ] && [ "$2" != "" ] && [ "$2" != "/" ] ; then
mountP=$2
fi
exe umount -q "$mountP"
exe parted -s "$resizeDevice" "resizepart $resizePartNo -1" quit
exe resize2fs "$resizePart"
exe mount "$mountP"
}
resizeMount="/backup"
resizeDevice="/dev/mmcblk0"
resizePartNo=2
resizePart="${resizeDevice}p${resizePartNo}"
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
SDBOOT=
SDROOT=
SDROOTPUUID=
SDDEV="$1"
elif [ -z $SDDEV ] || [ ! -b "$SDDEV" ] ; then
SDDEV=
SDBOOT=
SDROOT=
SDROOTPUUID=
echo " [I] Available devices:"
echo
exe lsblk -p
echo
exe read -p "Provide SD card device (e.g. /dev/sdb): " SDDEV
fi
if [ ! -b "$SDDEV" ] ; then
SDDEV=
return 1
fi
if [ "$osName" == "Raspbian" ] ; then
partExt="p"
fi
if [ -z $SDBOOT ] ; then
SDBOOT=$(findmnt -no TARGET "${SDDEV}${partExt}1")
fi
if [ -z $SDROOT ] ; then
SDROOT=$(findmnt -no TARGET "${SDDEV}${partExt}2")
fi
if [ -z $SDROOTPUUID ] ; then
SDROOTPUUID=$(findmnt -no PARTUUID "${SDDEV}${partExt}2")
fi
}
read_hd_dev() {
if [ ! -z "$1" ] ; then
HDROOT=
HDROOTPUUID=
HDDEV="$1"
elif [ -z $HDDEV ] || [ ! -b "$HDDEV" ] ; then
HDROOT=
HDROOTPUUID=
echo " [I] Available devices:"
echo
exe lsblk -p
echo
exe read -p "Provide HD device (e.g. /dev/sdf): " HDDEV
fi
if [ ! -b "$HDDEV" ] ; then
HDDEV=
return 1
fi
if [ -z $HDROOT ] ; then
HDROOT=$(findmnt -no TARGET "${HDDEV}1")
fi
if [ -z $HDROOTPUUID ] ; then
HDROOTPUUID=$(findmnt -no PARTUUID "${HDDEV}1")
fi
}
VERSION_SEQREV=9
. /usr/local/bin/sequencer.sh