Skip to content

Instantly share code, notes, and snippets.

@summersab
Last active November 7, 2021 20:41
Show Gist options
  • Save summersab/2ce1dd88d6f1d8369a45615c284f20dc to your computer and use it in GitHub Desktop.
Save summersab/2ce1dd88d6f1d8369a45615c284f20dc to your computer and use it in GitHub Desktop.
Simple script to install Debian on a variety of architectures with QEMU
#!/bin/bash
# The architecture you want to install. Options:
# amd64 arm64 armel armhf i386 mips64el mipsel ppc64el s390x
arch=armhf
disk=16G
cores=2
RAM=4G
# Determine the correct QEMU platform based on the value of $arch
case $arch in
amd64) platform=x86_64 ;;
arm64) platform=aarch64 ;;
armel | armhf) platform=arm ;;
i386) platform=i386 ;;
mips64el | mipsel) platform=mips ;;
ppc64el) platform=ppc ;;
s390x) platform=s390x ;;
esac
# Install the right QEMU platform binaries and QEMU utils so we can mount qcow images
apt-get install qemu-system-$platform qemu-utils
# Download the kernel, initrd, and netboot ISO
wget -O initrd-$arch.gz http://ftp.debian.org/debian/dists/stable/main/installer-$arch/current/images/netboot/initrd.gz &
wget -O vmlinuz--$arch.gz http://ftp.debian.org/debian/dists/stable/main/installer-$arch/current/images/netboot/vmlinuz &
wget -O mini-$arch.iso http://ftp.debian.org/debian/dists/stable/main/installer-$arch/current/images/netboot/mini.iso
# Create a raw disk
qemu-img create -f qcow2 debian-$arch.qcow2 $disk
# Boot and install the OS. Node that GRUB will fail to install (that's expected)
qemu-system-$platform -M virt -cpu max -smp cores=$cores -m $RAM -serial stdio \
-kernel vmlinuz-$arch -initrd initrd-$arch.gz \
-drive file=mini-$arch.iso,if=none,id=cd,media=cdrom,read-only -device virtio-blk-device,drive=cd \
-drive file=debian-$arch.qcow2,if=none,id=hd -device virtio-blk-device,drive=hd \
-device virtio-net-device,netdev=net0 -netdev user,id=net0
# Mount the disk image to copy off the boot kernel and initrd
modprobe nbd max_part=8
qemu-nbd --connect=/dev/nbd0 debian-$arch.qcow2
mount /dev/nbd0p1 /mnt/
cp /mnt/* boot-$arch/ -rv
# Remove the ISO and change the kernel and initrd to what was copied using the above commands
qemu-system-$platform -M virt -cpu max -smp cores=$cores -m $RAM -nographic \
-kernel ./boot-$arch/vmlinuz -initrd ./boot-$arch/initrd.img -append "root=/dev/vda2" \
-drive file=debian-$arch.qcow2,if=none,id=hd -device virtio-blk-device,drive=hd \
-device virtio-net-device,netdev=net0 -netdev user,id=net0,hostfwd=tcp::5022-:22 &
# Add `-serial stdio` to params and `console=ttyAMA0` to `-append` kernel args to redirect guest's console to localhost
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment