Skip to content

Instantly share code, notes, and snippets.

@codedipper
Created November 15, 2024 12:20
Show Gist options
  • Save codedipper/a744de1a7886f36821a4230508d248ed to your computer and use it in GitHub Desktop.
Save codedipper/a744de1a7886f36821a4230508d248ed to your computer and use it in GitHub Desktop.
GUI Arch Linux in QEMU; PulseAudio, TPM 2.0 and secure boot
#!/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin
VHDD=archlinux-vm.qcow2
VCD=archlinux-2024.10.12-x86_64.iso
CDLINE=
clean(){
echo "Do you really want to remove everything?"
read -r l
killall -s 15 swtpm
rm -rf tpmstate/
rm -f swtpm-sock mac.txt OVMF_VARS.4m.fd $VHDD
}
driveinit(){
qemu-img create -f qcow2 $VHDD 50G
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(){
mkdir tpmstate
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(){
swtpm socket --tpm2 --tpmstate dir=tpmstate --ctrl type=unixio,path=swtpm-sock & > /dev/null
qemu-system-x86_64 -machine q35,usb=off,accel=kvm,kernel-irqchip=split -boot order=c,once=d \
-cpu host,vmx=on -display gtk \
-nodefaults -no-user-config -k en-us \
-drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.secboot.4m.fd \
-drive if=pflash,format=raw,file=OVMF_VARS.4m.fd \
-cpu host,vmx=on -m 4G -nodefaults -vga virtio \
-object rng-random,id=rng,filename=/dev/urandom \
-drive file=$VHDD,if=virtio,media=disk,format=qcow2,cache=none $CDLINE \
-audiodev pa,id=ad0 -device ich9-intel-hda -device hda-duplex,audiodev=ad0 \
-netdev user,id=n1 -device e1000,netdev=n1,mac=$(cat mac.txt) \
-chardev socket,id=chrtpm,path=swtpm-sock \
-tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis,tpmdev=tpm0
}
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