Below is a script that you can use to create debian installation media. It downloads the latest released pi bootloader files, the buster netinst.iso, and generates the preseed and config files to make it all work. Once all files have been downloaded/created, copy them to a fat formatted usb drive.
To temporarily enable usb booting on the pi 2 or on the pi3, copy the bootcode.bin file to an SD card. You must still have bootcode.bin on the usb drive as it is used by the installer. Place the SD card and usb drive in the pi and turn on.
The pi3B+ has usb booting enabled by default, but at the time of writing there is no dtb file for the pi 3B+.
The debian-installer will start eventually after a long pause at the rainbow screen and you can install to the SD card, or another usb drive.
No swap partition is created by default. Using a swapfile or using zram are better, but you'll have to sort them out yourself. As you will getting wifi/bluetooth to work (see https://wiki.ubuntu.com/ARM/RaspberryPi#WiFi for wifi). The package flash-kernel is used to copy the kernel,initrd and dtb file to the fat partition (mounted at /boot/firmware). This is usually combined with u-boot, but that isn't done here, but it is something else you could set up if you wish.
Code: Select all
#!/bin/sh
# Construct a debian armhf installer for the Pi2 and Pi3B.
set -e
# Download the latest pi bootloader files
wget http://archive.raspberrypi.org/debian/pool/main/r/raspberrypi-firmware/raspberrypi-bootloader_1.20180417-1_armhf.deb
dpkg-deb -x raspberrypi-bootloader_1.20180417-1_armhf.deb /tmp/pi-bootloader
mkdir pi-installer
cd pi-installer
# Copy the bootloader files
sudo cp /tmp/pi-bootloader/boot/* .
# The cmdline.txt (adjust as necessary)
cat <<'EOF' > cmdline.txt
file=/hd-media/preseed/pi.seed --- quiet net.ifnames=0
EOF
# The config.txt
cat <<'EOF' > config.txt
# For more options and information see:
# https://www.raspberrypi.org/documentation/configuration/config-txt/README.md
# Kernel and initramfs
kernel=vmlinuz
initramfs initrd.gz followkernel
[pi2]
device_tree=bcm2836-rpi-2-b.dtb
[pi3]
device_tree=bcm2837-rpi-3-b.dtb
[pi3+]
device_tree=bcm2837-rpi-3-b-plus.dtb
[all]
EOF
# Run 'unix2dos config.txt' if you want it readable in windows
# Download iso
wget https://cdimage.debian.org/cdimage/weekly-builds/armhf/iso-cd/debian-testing-armhf-netinst.iso
# Extract kernel, initrd and device tree files from iso
# These are also available at http://ftp.uk.debian.org/debian/dists/buster/main/installer-armhf/current/images/
mkdir /tmp/installer-iso
sudo mount -o loop,ro debian-testing-armhf-netinst.iso /tmp/installer-iso
cp /tmp/installer-iso/install/hd-media/vmlinuz .
cp /tmp/installer-iso/install/hd-media/initrd.gz .
cp /tmp/installer-iso/install/device-tree/bcm2836-rpi-2-b.dtb .
cp /tmp/installer-iso/install/device-tree/bcm2837-rpi-3-b.dtb .
#cp /tmp/installer-iso/install/device-tree/bcm2837-rpi-3-b-plus.dtb
sync
sudo umount /tmp/installer-iso
# Create preseed files
mkdir preseed
cd preseed
cat <<'EOF' > pi.seed
# The partitioning scheme to be used
d-i partman-partitioning/default_label select msdos
d-i partman-auto/expert_recipe_file string /hd-media/preseed/pi_recipe
# Kernel options to be used for the insalled system (added to those used with the installer)
d-i debian-installer/add-kernel-opts string ro elevator=deadline fsck.repair=yes rootwait cma=64M
# We'll use the pi's own bootloader
d-i grub-installer/skip boolean true
# Don't pause for the "No boot loader installed" message
d-i nobootloader/confirmation_common note
# Setup fat partition (replicates the work of flash-kernel-installer)
d-i preseed/late_command string /hd-media/preseed/pi_late_command
EOF
cat <<'EOF' > pi_recipe
raspberrypi ::
128 128 128 fat32
$primary{ }
$bootable{ }
method{ format }
format{ }
use_filesystem{ }
filesystem{ fat32 }
label{ boot-part }
mountpoint{ /boot/firmware } .
900 10000 -1 $default_filesystem
$lvmok{ }
$primary{ }
method{ format }
format{ }
use_filesystem{ }
$default_filesystem{ }
label{ root-part }
mountpoint{ / } .
EOF
cat <<'STOP' > pi_late_command
#!/bin/sh
# Install Raspberry Pi firmware. Written by Adam Smith
set -e
findfs () {
mount | grep "on /target${1%/} " | tail -n1 | cut -d' ' -f1
}
logger -t late_command "Removing cups-filters.conf"
# The Pi doesn't have a parallel port
rm /target/etc/modules-load.d/cups-filters.conf || true
logger -t late_command "Copying bootloader files"
# Copy bootloader files
cp /hd-media/bootcode.bin /target/boot/firmware/
cp /hd-media/fixup*.dat /target/boot/firmware/
cp /hd-media/start*.elf /target/boot/firmware/
cp /hd-media/config.txt /target/boot/firmware/
# Update config.txt
sed -i 's/^kernel=.*/kernel=vmlinuz/g' /target/boot/firmware/config.txt
sed -i 's/^initramfs .*/initramfs initrd.img followkernel/g' /target/boot/firmware/config.txt
# Create cmdline.txt file
user_params="$(echo $(user-params))" || true
vol_id=$(blkid -o value -s UUID $(findfs /))
echo "root=UUID=$vol_id $user_params" > /target/boot/firmware/cmdline.txt
logger -t late_command "Running flash-kernel ..."
if ! apt-install flash-kernel u-boot-tools; then
logger -t late_command "error: apt-install flash-kernel u-boot-tools failed"
exit 1
fi
# Add entry to flash-kernel database
machine="$(cat /proc/device-tree/model)"
# Make a guess about the dtb file to use
case "$machine" in
'Raspberry Pi 2 Model B'*)
dtb="bcm2836-rpi-2-b.dtb"
;;
'Raspberry Pi 3 Model B Plus'*)
dtb="bcm2837-rpi-3-b-plus.dtb"
;;
'Raspberry Pi 3 Model B'*)
dtb="bcm2837-rpi-3-b.dtb"
;;
*)
logger -t late_command "Unknown model"
exit 1
esac
cat <<EOF >> /target/etc/flash-kernel/db
# Automatically added by the installer
Machine: $machine
DTB-Id: $dtb
Boot-DTB-Path: /boot/firmware/$dtb
Boot-Kernel-Path: /boot/firmware/vmlinuz
Boot-Initrd-Path: /boot/firmware/initrd.img
EOF
# Use flash-kernel to copy kernel, initrd.img, dtb file, etc to the boot partition
mount -o bind /dev /target/dev
if ! in-target flash-kernel; then
# Run out of space on boot partition or missing dtb file?
logger -t late_command "error: flash-kernel failed"
umount /target/dev || true
exit 1
fi
umount /target/dev || true
logger -t late_command "flash-kernel successful"
STOP
# Finished!
# Copy all files in the pi-installer directory (including the iso file) to a fat formatted usb drive.