Skip to content

Instantly share code, notes, and snippets.

@codedipper
Last active November 15, 2024 12:16
Show Gist options
  • Save codedipper/2a959ba77b8ec27a998caab5961d3c83 to your computer and use it in GitHub Desktop.
Save codedipper/2a959ba77b8ec27a998caab5961d3c83 to your computer and use it in GitHub Desktop.
Terminal-only Alpine Linux in QEMU; output directed to stdio
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin
VHDD=alpinelinux-vm.qcow2
VCD=alpine-virt-3.20.3-x86_64.iso
CDLINE=
clean(){
echo "Do you really want to remove everything?"
read -r l
rm -f mac.txt OVMF_VARS.4m.fd $VHDD
}
driveinit(){
qemu-img create -f qcow2 $VHDD 7G
chmod 0600 $VHDD
}
macinit(){
echo "52:54:00:$(cat /dev/urandom | tr -cd '1234567890abcdef' | head -c 6 | fold -w 2 | paste -sd ':')" > mac.txt
chmod 0400 mac.txt
}
biosinit(){
cp /usr/share/edk2/x64/OVMF_VARS.4m.fd OVMF_VARS.4m.fd
}
bootcd(){
CDLINE="-drive file=$VCD,if=virtio,media=cdrom,format=raw,readonly=on"
boot
}
boot(){
qemu-system-x86_64 -machine q35,usb=off,accel=kvm,kernel-irqchip=split \
-cpu host,vmx=on -boot order=c,once=d -display curses -m 4G \
-nodefaults -nographic -no-user-config -serial mon:stdio -k en-us \
-drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.4m.fd \
-drive if=pflash,format=raw,file=OVMF_VARS.4m.fd \
-drive file=$VHDD,if=virtio,media=disk,format=qcow2,cache=none $CDLINE \
-netdev user,id=n1 -device e1000,netdev=n1,mac=$(cat mac.txt) \
-object rng-random,id=rng,filename=/dev/urandom
}
install(){
clean
driveinit
macinit
biosinit
bootcd
}
$1
if [ -z "$1" ]; then
echo "Invalid command"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment