Skip to content

Instantly share code, notes, and snippets.

@mapi68
Last active April 11, 2024 18:58
Show Gist options
  • Save mapi68/c170f5b0844dbedbaff72a9e3e6c689c to your computer and use it in GitHub Desktop.
Save mapi68/c170f5b0844dbedbaff72a9e3e6c689c to your computer and use it in GitHub Desktop.
This script performs a full system upgrade for Raspberry Pi, migrating from Debian Bullseye to Bookworm, installs NetworkManager, removes dhcpcd, upgrades kernel and firmware, cleans unnecessary packages, and optionally configures NetworkManager with static or DHCP IP.
#!/bin/bash
clear
echo && echo Update package lists and upgrade packages
sudo apt update
sudo apt dist-upgrade -y
echo && echo Install NetworkManager without recommended packages
sudo apt install --no-install-recommends network-manager -y
echo && echo Enable and start NetworkManager, disable, stop and remove dhcpcd
sudo systemctl enable --now NetworkManager
sudo systemctl disable --now dhcpcd
sudo apt --purge autoremove dhcpcd* -y
###########################
sudo shutdown -r +1
###########################
clear
echo && echo Replace 'bullseye' with 'bookworm' in package sources
sudo sed -i -e 's/bullseye/bookworm/g' /etc/apt/sources.list /etc/apt/sources.list.d/raspi.list
echo && echo Update package lists again and perform a full system upgrade
sudo apt update
sudo apt full-upgrade -y
echo && echo Remove unnecessary packages and clean up
sudo apt --purge autoremove -y && sudo apt clean all
###########################
sudo shutdown -r +1
###########################
clear
echo && echo Purge and forcibly remove dependencies of Raspberry Pi kernel and bootloader
echo PROCEED WITH CAUTION, USE AT YOUR OWN RISK!
sleep 10
sudo dpkg --purge --force-depends raspberrypi-kernel raspberrypi-bootloader
echo && echo Unmount /boot, perform file system check and mount /boot/firmware
sudo umount /boot
sudo fsck -y /boot
sudo mkdir /boot/firmware
sudo sed -i.bak -e "s#boot#boot/firmware#" /etc/fstab
sudo systemctl daemon-reload
sudo mount /boot/firmware
echo && echo Install Raspberry Pi firmware package
sudo apt install raspi-firmware -y
echo && echo Install appropriate kernel image and headers based on the architecture
kernel_version=$(uname -r)
if [[ $kernel_version == *"v6"* ]]; then
sudo apt install linux-image-rpi-v6 linux-headers-rpi-v6 -y
elif [[ $kernel_version == *"v7"* ]]; then
sudo apt install linux-image-rpi-v7l linux-headers-rpi-v7l -y
elif [[ $kernel_version == *"v8"* ]]; then
sudo apt install linux-image-rpi-v8 linux-headers-rpi-v8 -y
fi
echo && echo Add 'auto_initramfs=1' to config.txt if not already present
if ! grep -q 'auto_initramfs=1' /boot/firmware/config.txt; then
echo "auto_initramfs=1" | sudo tee -a /boot/firmware/config.txt
fi
###########################
sudo shutdown -r +1
###########################
### BONUS: CONFIGURE NETWORK MANAGER
clear
sudo nmcli -p connection show
INTERF=$(sudo nmcli -p connection show | awk '/eth0/{match($0, /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/); print substr($0, 1, RSTART-2)}' | sed 's/[[:space:]]\+$//')
echo && echo Your eth0 interface is \"$INTERF\"
# Static IP
sudo nmcli con mod "$INTERF" ipv4.method manual ipv4.addresses 192.168.1.222/24 ipv4.gateway 192.168.1.1 ipv4.dns 192.168.1.100
sudo systemctl restart NetworkManager.service
sleep 3
echo && sudo nmcli device show
# DHCP IP
sudo nmcli con mod "$INTERF" ipv4.addresses '' ipv4.gateway '' ipv4.dns ''
sudo nmcli con mod "$INTERF" ipv4.method auto
sudo systemctl restart NetworkManager.service
sleep 3
echo && sudo nmcli device show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment