Files
shell_sequencer/seqs/debianrpi.sh

230 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
#toolName=mytool
toolDownSite="https://raspi.debian.net/tested-images"
toolDownUrl=
toolDownFile=
toolEssentialDeps="vim bash-completion man-db locales wget"
SDDEV=
SDBOOT=
SDBOOTPUUID=
SDROOT=
SDROOTDEV=
SDROOTPUUID=
# Get script working directory
# (when called from a different directory)
WDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >>/dev/null 2>&1 && pwd )"
CONFIG=0
SCRIPT_NAME=$(basename -- $0)
SCRIPT_NAME=${SCRIPT_NAME%%.*}
CONFIG_FILE_NAME="${SCRIPT_NAME}.cfg"
CONFIG_FILE_TEMPLATE="$WDIR/${CONFIG_FILE_NAME}.example"
#step_config() {
# echo "Called once before executing steps."
## e.g. to source a config file manually:
#. "$CONFIG_FILE"
## or to use sequencer api with global config file:
#initSeqConfig "$CONFIG_FILE_NAME" "$CONFIG_FILE_TEMPLATE"
## or to use sequencer api with profile config file support:
#initSeqConfig -p "$SCRIPT_NAME" "$CONFIG_FILE_TEMPLATE"
#if [ $? -eq 0 ] ; then
# CONFIG=1
#fi
#}
step_1_info() {
echo "Download latest xz-compressed image [IMAGE FILE URL] [SD CARD DEVICE]"
echoinfo "$toolDownSite"
}
step_1_alias() { ALIAS="install"; }
step_1() {
shift
SDDEV="$2"
if [ -z "$1" ] && [ $QUIET -eq 0 ] ; then
read -p "Provide image download url from $toolDownSite: " toolDownUrl
elif [ ! -z "$1" ] ; then
toolDownUrl="$1"
else
echoerr " [E] No image file provided for download"
exit -1
fi
toolDownFile="/tmp/${toolDownUrl//*\//}"
local downSha256="${toolDownUrl%.img.xz}.xz.sha256"
local downShaFile="${toolDownFile%.img.xz}.xz.sha256"
echo Url: ${toolDownUrl}
echo Sha256: $downSha256
echo "Files: $toolDownFile"
echo " $downShaFile"
if [ ! -f "$downShaFile" ] ; then
exe wget -O "$downShaFile" "$downSha256"
endReturn -o $? "Error downloading $downShaFile"
fi
if [ ! -f "$toolDownFile" ] ; then
exe wget -O "$toolDownFile" "$toolDownUrl"
endReturn -o $? "Error downloading $downDownFile"
fi
echoseq " [I] Checking SHA256 checksum"
exe cd $(dirname "$toolDownFile")
exe sha256sum -c "$downShaFile" >>/dev/null
endReturn -o $? "SHA256 checksum error"
}
step_2_info() { echo "Write image to device [DEVICE]"; }
step_2() {
shift
if [ -z "$SDDEV" ] ; then
SDDEV="$1"
fi
read_sd_dev "$SDDEV"
# check if device was confirmed
if [ $? -ne 0 ] ; then
endReturn -o 1 "SD card device not found"
fi
echoseq " [I] Writing $(basename "$toolDownFile")"
exep "xzcat \"$toolDownFile\" | dd of=$SDDEV bs=64k oflag=dsync status=progress"
exe sync
}
step_3_info() { echo "Prepare SD card for first run"; }
step_3() {
outColor green
cat <<PREPARE_EOF
[I] Setup static IP
[etc/network/interfaces.d/eth0]
#iface eth0 inet dhcp
iface eth0 inet static
address 192.168.0.38/24
gateway 192.168.0.1
iface eth0 inet6 static
address fd00::38/72
gateway fd00::1/64
[I] Setup dns nameserver
[etc/resolv.conf]
nameserver 192.168.0.1
nameserver fd00::1
[I] Setup root ssh access
[etc/ssh/sshd_config]
PermitRootLogin yes
[root/.ssh/authorized_keys]
# Root login only possible with an ssh key
PREPARE_EOF
}
step_10_info() { echo "Essential debian setup"; }
step_10_alias() { ALIAS="setup"; }
step_10() {
if [ -z "$(ls /etc/default | grep raspi)" ] ; then
echoerr " [E] Not on a Raspberry pi"
return -1
fi
exe apt update
exe apt full-upgrade
exe apt install $toolEssentialDeps
}
step_11_info() { echo "Setup locales to en_US.UTF-8"; }
step_11() {
local localUs="en_US.UTF-8"
local localUsDefault"$localUs UTF-8"
if [ ! -f "$localesConfigLoc" ] ; then
echoerr " [E] Install packages locales first"
exit -1
fi
exe sed -i "s/#[[:space:]]*\($localUsDefault\)/\1/" "$localesConfigLoc"
endReturn -o $? "Changing locales failed"
exe locale-gen
exe update-locale LANG=$localUs
echoseq " [I] Logout and login for changes to be active"
}
localesConfigLoc="/etc/locale.gen"
step_13_info() { echo "Change hostname [HOSTNAME]"; }
step_13_alias() { ALIAS="hostname"; }
step_13() {
shift
local localHostname=$1
endCheckEmpty localHostname "No hostname provided"
if [ $(grep -r "$localHostname" "$hostsLoc">>/dev/null; echo $?) -eq 0 ] ; then
echoseq " [I] Hostname $localHostname already defined"
return 0
fi
exe hostnamectl set-hostname "$localHostname"
endReturn -o $? "Couldn't set hostname"
exe sed -i "s/^\(127\.0\.0\.1[[:space:]]*\)\(localhost.*\)/\1\2\n\1$localHostname/" "$hostsLoc"
}
hostsLoc="/etc/hosts"
read_sd_dev() {
local partExt=""
if [ ! -z "$1" ] ; then
SDBOOT=
SDROOT=
SDROOTDEV=
SDROOTPUUID=
SDBOOTPUUID=
SDDEV="$1"
elif [ -z $SDDEV ] || [ ! -b "$SDDEV" ] ; then
SDDEV=
SDBOOT=
SDROOT=
SDROOTDEV=
SDBOOTPUUID=
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
echoerr " [I] $SDDEV not a block device"
SDDEV=
return 1
fi
if [[ "$SDDEV" =~ .*blk.* ]] ; then
partExt="p"
fi
if [ -z $SDBOOT ] ; then
SDBOOT=$(findmnt -no TARGET "${SDDEV}${partExt}${SDBOOTPARTNO}")
fi
if [ -z $SDBOOTPUUID ] ; then
IFS=\" read -r _ vPARTUUID _ < <(blkid "${SDDEV}${partExt}${SDBOOTPARTNO}" -s PARTUUID)
SDBOOTPUUID=$vPARTUUID
fi
SDROOTDEV=${SDDEV}${partExt}${SDROOTPARTNO}
if [ -z $SDROOT ] ; then
SDROOT=$(findmnt -no TARGET "${SDROOTDEV}")
fi
if [ -z $SDROOTPUUID ] ; then
SDROOTPUUID=$(findmnt -no PARTUUID "${SDROOTDEV}")
fi
return 0
}
SDBOOTPARTNO=1
SDROOTPARTNO=2
VERSION_SEQREV=12
. /usr/local/bin/sequencer.sh