Last active
October 1, 2024 20:39
-
-
Save paalbra/f623b9d55007b2f413f072ef5644d5db to your computer and use it in GitHub Desktop.
Initialization script for Raspberry PI (first boot)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Steps on a clean Raspberry Pi OS memory card: | |
# 0. Edit variables at the start of this script | |
# 1. Add this script to /init.sh | |
# 2. Add a cronjob /etc/cron.d/init: echo '@reboot root /bin/bash /init.sh 1> /init.log 2>&1 && rm /etc/cron.d/init' | |
# 3. Add userconf to boot pratition: echo "pi:$(echo 'mypassword' | openssl passwd -6 -stdin)" | |
# 4. Add configuration for wifi: | |
# SSID="MySSID" | |
# PSK="mypassword" | |
# ROOTFS="/run/media/*/rootfs/" | |
# cat << EOF > ${ROOTFS}etc/NetworkManager/system-connections/preconfigured.nmconnection | |
# [connection] | |
# id=preconfigured | |
# uuid=$(uuid) | |
# type=wifi | |
# autoconnect=true | |
# | |
# [wifi] | |
# mode=infrastructure | |
# ssid=${SSID} | |
# | |
# [wifi-security] | |
# key-mgmt=wpa-psk | |
# psk=${PSK} | |
# | |
# [ipv4] | |
# method=auto | |
# | |
# [ipv6] | |
# method=auto | |
# EOF | |
# chmod 600 ${ROOTFS}etc/NetworkManager/system-connections/preconfigured.nmconnection | |
TIME_ZONE="Europe/Oslo" # timedatectl list-timezones | |
PACKAGES=( | |
lsof | |
python3-gpiozero | |
python3-picamera | |
python3-venv | |
vim | |
) | |
function header() { | |
echo -e "\n=========================" | |
date --iso-8601=s | |
echo -e "=========================\n" | |
} | |
function perform() { | |
header | |
echo "$1" | |
shift | |
echo Performing: "${@}" | |
"$@" | |
} | |
function set_hostname() { | |
if [[ "$HOSTNAME" != "raspberrypi" ]]; then | |
echo "Custom hostname already set: $HOSTNAME" | |
return | |
fi | |
# Notes: | |
# Example content from /proc/device-tree/model: | |
# * 1b: Raspberry Pi Model B Rev 2 | |
# * 3b+: Raspberry Pi 3 Model B Plus Rev 1.3 | |
# * 4b: Raspberry Pi 4 Model B Rev 1.1 | |
LASTMAC=$(perl -pe 's/://g' /sys/class/net/wlan0/address | tail -c 5) | |
SHORTNAME=$(perl -pe 's/(Raspberry Pi|Model|Rev .*|Plus| )//g' /proc/device-tree/model | tr '[:upper:]' '[:lower:]') | |
NEW_HOSTNAME="pi-$SHORTNAME-$LASTMAC" | |
echo "HOSTNAME: \"$HOSTNAME\", NEW_HOSTNAME: \"$NEW_HOSTNAME\"" | |
hostnamectl set-hostname "$NEW_HOSTNAME" && perl -i -pe "s/raspberrypi/$NEW_HOSTNAME/g" /etc/hosts | |
} | |
function install_and_upgrade() { | |
apt-get update | |
apt-get install -y "$@" | |
apt-get upgrade -y | |
} | |
perform "Setting hostname:" set_hostname | |
perform "Setting timezone:" timedatectl set-timezone "$TIME_ZONE" | |
perform "Enabling SSH:" systemctl enable --now ssh.service | |
perform "Current ENV:" env | |
perform "Sleeping 30s. Hoping for network..." sleep 30 | |
perform "IP info:" ip address | |
header | |
if ! ping -c 4 -W 1 8.8.8.8; then | |
echo "No network? Exiting" | |
exit 1 | |
fi | |
perform "Performing apt-get update, install and upgrade:" install_and_upgrade ${PACKAGES[*]} | |
perform "Done. Rebooting..." /usr/sbin/reboot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment