Files
shell_sequencer/seqs/raspberry.sh

116 lines
3.0 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"
#step_config() {
# echo "Called once before executing steps."
# echo "e.g. to source a config file:"
# #. "$CONFIG_FILE"
#}
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 "$2"; else echo "[SD CARD DEVICE]"; fi }
step_2_alias() { ALIAS="writesd"; }
step_2() {
local sdDev=""
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
if [ ! -z $2 ] && [ "$2" != "" ] ; then
sdDev="$2"
else
exe lsblk -p
exe read -p "Provide SD card device (e.g. /dev/sdb): " sdDev
fi
# check if device is a block special file
if [ ! -b "$sdDev" ] ; then
echoerr " [E] $sdDev not found"
return 1
fi
exe dd bs=4M if="$downImgName" of="$sdDev" conv=fsync
exe sync
#x 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"; }
step_3() {
#TODO
echo exe touch "$partSdBoot"/ssh
}
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}"
VERSION_SEQREV=9
. /usr/local/bin/sequencer.sh