Skip to content

Instantly share code, notes, and snippets.

@highercomve
Last active July 29, 2021 14:30
Show Gist options
  • Save highercomve/ccec4592072984d11134e3ce5d201c9b to your computer and use it in GitHub Desktop.
Save highercomve/ccec4592072984d11134e3ce5d201c9b to your computer and use it in GitHub Desktop.

How to use this script

This is a very small utility to run a qemu virtual machine for x64 images. It let you start the vm with a qcow2 disk and a image as instalation media.

Requirements

You need to have qemu installed in your PC and a CPU with virtualization enabled.

How to download it

wget https://gist.githubusercontent.com/highercomve/ccec4592072984d11134e3ce5d201c9b/raw/5d52a388db2bb66b700b6181c853d2635bc181ff/vm_with_tpm.sh

if you what to installed you could

chmod +x vm_start && mv vm_start /usr/bin/vm_start

How to use it

Download a image to be use as installation media, for this example i will use the x64 installer from pantavisor.io

then run:

vm_start pantabox.qcow2 ~/Downloads/x64_initial_stable-installer.img

That will start a VM using only 1 core, if you want to run with more cores pass a 3th parameter to the command with the number of commands

Example:

vm_start pantabox.qcow2 ~/Downloads/x64_initial_stable-installer.img 4
#!/bin/bash -e
# USAGE:
# ./start.sh disk1.qcow2 /dev/sda NUMBER_OF_CORES
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" ] && [ ! -z "${1##*/dev/*}" ]; then
qemu-img create -f qcow2 $1 4G
fi
[ "$UID" -eq 0 ] || exec sudo "$0" "$@"
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 \
-netdev user,id=net0,hostfwd=tcp::8022-:8022 \
-device e1000-82545em,netdev=net0,id=net0,mac=bc:cf:94:8c:a2:47 \
-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