Files
shell_sequencer/seqs/debianrpi.sh

299 lines
7.0 KiB
Bash
Executable File

#!/bin/bash
#toolName=mytool
readonly toolDownSite="https://raspi.debian.net/tested-images"
toolDownUrl=
toolDownFile=
readonly toolEssentialDeps="vim bash-completion man-db locales wget"
SDDEV=
SDBOOT=
SDBOOTPUUID=
SDROOT=
SDROOTDEV=
SDROOTPUUID=
sq_aptOpt=
seq_config() {
interactive || sq_aptOpt="-y"
return 0
}
step_1_info() {
echo "Download latest xz-compressed image"
echoinfo "$toolDownSite"
}
step_1_options() { echo "[IMAGE FILE URL] [SD CARD DEVICE]"; }
step_1_alias() { echo "install"; }
step_1() {
shift
SDDEV="$2"
if [ -z "$1" ] && interactive ; then
read -p "Provide image download url from $toolDownSite: " toolDownUrl
elif [ -n "${1:-}" ] ; then
toolDownUrl="$1"
else
error -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
info "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"; }
step_2_options() { echo "[DEVICE]"; }
step_2() {
shift
if [ -z "$SDDEV" ] ; then
SDDEV="$1"
fi
read_sd_dev "$SDDEV"
# check if device was confirmed
endReturn -o $? "SD card device not found"
info "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() {
color 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] Disable ipv6
[etc/sysctl.conf]
net.ipv6.conf.eth0.disable_ipv6 = 1
[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() { echo "setup"; }
step_10() {
if [ -z "$(ls /etc/default | grep raspi)" ] ; then
error -e "Not on a Raspberry pi"
return 1
fi
exe apt update
exe apt full-upgrade ${sq_aptOpt}
exe apt install $toolEssentialDeps ${sq_aptOpt}
}
step_11_info() { echo "Set timezone"; }
step_11() {
exe dpkg-reconfigure tzdata
}
step_12_info() { echo "Setup locales to en_US.UTF-8"; }
step_12() {
local localUs="en_US.UTF-8"
local localUsDefault="$localUs UTF-8"
if [ ! -f "$localesConfigLoc" ] ; then
error -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
info "Logout and login for changes to be active"
}
localesConfigLoc="/etc/locale.gen"
step_14_info() { echo "Change hostname"; }
step_14_options() { echo "[HOSTNAME]"; }
step_14_alias() { echo "hostname"; }
step_14() {
shift
local localHostname=$1
endIfEmpty localHostname "No hostname provided"
if [ $(grep -r "$localHostname" "$hostsLoc">>/dev/null; echo $?) -eq 0 ] ; then
info "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"
step_16_info() { echo "Install cifs mounting requirements"; }
step_16_alias() { echo "cifs"; }
step_16() {
exe apt install cifs-utils ${sq_aptOpt}
}
step_17_info() { echo "Cifs notes"; }
step_17() {
color green
cat <<CIFS_EOF
# Example fstab entry
[/etc/fstab]
//192.168.0.10/Qdownload /mnt/Qdownload cifs vers=1.0,credentials=/root/.smbpasswd,uid=username,gid=groupname,file_mode=0775,dir_mode=0775 0 0
# Mounting during boot may fail on Rpi4. This changes the entry to automount
(note "_netdev" and "comment=systemd.automount")
//192.168.0.10/Qdownload /mnt/Qdownload cifs _netdev,vers=2.0,credentials=/root/.smbpasswd,uid=username,gid=groupname,file_mode=0775,dir_mode=0775,comment=systemd.automount 0 0
## Alternatively this options were reported to work
auto,x-systemd.automount
# Example .smbpasswd
Don't forget to chown 600 this file.
[/root/.smbpasswd]
username=REMOTEUSER
password=REMOTEPASSWORD
CIFS_EOF
}
step_19_info() { echo "Setup notes"; }
step_19_alias() { echo "setupnotes"; }
step_19() {
color green
cat <<SETUPNOTES_EOF
# Secure root with a password
passwd
# Create a regular user
adduser USERNAME
SETUPNOTES_EOF
}
step_40_info() { echo "Boot from HD notes"; }
step_40_alias() { echo "hdboot"; }
step_40() {
color green
cat <<HDBOOT_EOF
[I] Raspberry pi 4 boots from USB automatically if there is no SD available
(needs boot loader version September 3rd)
[I] Adding a swap partition
* [RASPIROOTSSD/etc/fstab]
LABEL=RASPISWAP none swap sw 0 3
* Debian enables a swap partition automatically
because by default there is no swap file
[I] Procedure for raspi 3 is similar to the one for raspbian
(boot on SD and root on SSD)
* Clone RAPSIROOT from SD to SSD
rsync -axHAX RASPIROOT/ RASPIROOTSSD/
* [/boot/firmware/cmdline.txt]
root=LABEL=RASPIROOTSSD
* [RASPIROOTSSD/etc/fstab]
LABEL=RASPIROOTSSD / ...
HDBOOT_EOF
}
read_sd_dev() {
local partExt=""
if [ -n "${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
info -e "$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
# shellcheck disable=SC2034 # Appears unused
readonly sqr_minVersion=16
# shellcheck disable=SC1091 # Don't follow this source
. /usr/local/bin/sequencer.sh