Skip to content

Instantly share code, notes, and snippets.

@highercomve
Last active May 29, 2020 16:29
Show Gist options
  • Select an option

  • Save highercomve/bc1fd3536108164bc5de752c533c30d0 to your computer and use it in GitHub Desktop.

Select an option

Save highercomve/bc1fd3536108164bc5de752c533c30d0 to your computer and use it in GitHub Desktop.

Run VM with qemu with your TPM

example of how to use it

./run_vm_with_drive disk1.qcow2 /dev/sda NUMBER_CPU_YOU_WANT /dev/tpm0

Arguments:

  • disk1.qcow2 if the disk doesn't exist will create a new disk
  • /dev/sda your pendrive or any other aditional media to use as installation media
  • NUMBER_CPU_YOU_WANT (OPTIONAL) set the number of core you want the VM to have (default: 1)
  • /dev/tpm0 (OPTIONAL) set the tpm device used by the VM (default: /dev/tpm0)
#!/bin/bash -e
# USAGE:
# ./start.sh disk1.qcow2 /dev/sda NUMBER_OF_CORES
[ "$UID" -eq 0 ] || exec sudo "$0" "$@"
if [ -z "$1" ]; then
echo "No image supplied"
exit 1
fi
if [ -z "$2" ]; then
echo "Supply USB installer device"
exit 1
fi
if [ ! -f "$1" ]; then
qemu-img create -f qcow2 $1 4G
fi
csc=`cat /sys/devices/system/clocksource/clocksource0/current_clocksource`
if [ "$csc" != "hpet" ]; then
echo "hpet" > /sys/devices/system/clocksource/clocksource0/current_clocksource
fi
cancelpath="/sys/class/tpm/tpm0/device/cancel"
if [ ! -e "$cancelpath" ]; then
cancelpath="/dev/null"
fi
tpmdev="/dev/tpm0"
if [ -n "$4" ] && [ -e "$4" ]; then
tpmdev=$3
fi
if [ -n "$3" ]; then
smp="cpus=$3,cores=$3,threads=1,sockets=1"
else
smp="cpus=1,cores=1,threads=1,sockets=1"
fi
if [ "$2" == "null" ]; then
disks="-hda $1"
else
disks="-hda $1 -hdb $2"
fi
qemu-system-x86_64 \
-enable-kvm \
-cpu host \
-smp $smp \
-m 1024 \
-rtc base=localtime \
-machine accel=kvm \
-L /usr/share/ovmf/ \
--bios /usr/share/ovmf/OVMF.fd \
-vga std \
-net nic \
-net user \
-tpmdev passthrough,id=tpm0,path=$tpmdev,cancel-path=$cancelpath \
-device tpm-tis,tpmdev=tpm0 \
-boot order=c,menu=on \
$disks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment