Skip to content

Instantly share code, notes, and snippets.

@MitchiLaser
Created October 10, 2023 12:51
Show Gist options
  • Save MitchiLaser/b7f7f37b85b9b4e9ff977d6c4a1222ca to your computer and use it in GitHub Desktop.
Save MitchiLaser/b7f7f37b85b9b4e9ff977d6c4a1222ca to your computer and use it in GitHub Desktop.
QEMU VM Startup script
#!/bin/bash
# This is a script which uses QEMU to start a VM or set up the VM with a pre-defined installer
# when the virtual hard-drive cannot be found. It was made to quickly recreate a new VM in case
# the old one was deleted (maybe because someone messed arround with it)
# All the arguments QEMU needs to start the VM with the right parameters
# are also stored in this script and can be modified.
# The default OS installer is currently a Link to the OpenSUSE net installer
###################
## Configuration ##
###################
# Specify SSH, SPICE and Telnet interface
SPICE_PORT=5924
TELNET_PORT=5925
SPICE_PASSWORD=$(echo -n '4atan1' | base64) # password has to be base64 encoded (ore something else)
SSH_PORT=5926
# Specify parameters of the virtual hard drive
DRIVE_SIZE=30G # default: 30GB
DRIVE_NAME="drive.qcow2"
# parameters for the running vm
NUM_CPU_CORES=2 # number of cpu cores
RAM=6G # memory size
# this is the URL of the installation media file for the OS
# it will be downloaded into a temporary file and the vm will be started in installation mode, when the virtual hard drive was not found
INSTALLER_URL="https://mirror1.hs-esslingen.de/pub/Mirrors/ftp.opensuse.org/distribution/leap/15.5/iso/openSUSE-Leap-15.5-NET-x86_64-Build491.1-Media.iso"
####################
## QEMU Arguments ##
####################
arguments=( # the command line arguments for the QEMU instance
-nodefaults # remove default configuration, p.ex. a floppy-interface
-cpu host,hv_relaxed,hv_spinlocks=0x1fff,hv_vapic,hv_time # the host is running on the same cpu-model as the guest. Default-CPU would be "QQemu virtual CPU"
-enable-kvm # enables virtualisation through KVM (kernel virtualised machine) if this module is available, root-privileges currently needed and therfore disabled
-smp $NUM_CPU_CORES # the amount of CPU-Cores
-m $RAM # The amoung of Memory. "M" and "G" can be used as units for Megabytes ans Gigabytes
-drive format=qcow2,file=$(dirname "$0")/drive.qcow2
# include a file or device in the VM which is presented as a physical drive to the guest.
-k de # Keyboard-Layout
-name "OpenSUSE-VM"
# specify network-interface-controller. There are many options. I tried this one and it works
-net user,hostfwd=tcp::${SSH_PORT}-:22 # SSH Access to the VM
-net nic
-object secret,id=secvnc0,data=$SPICE_PASSWORD,format=base64 # spice password has to be passed as a QEMU secret
-vga qxl # specify a graphics-card. "qxl" is recommended for usage with SPICE
-spice port=${SPICE_PORT},password-secret=secvnc0 # enable a SPCIE-interface with specified port and password
# disabled because this option is not supported by the current qemu version
#-soundhw hda # add a virtual high definition audio interface to the spice server
-monitor telnet:127.0.0.1:${TELNET_PORT},server,nowait
# provide a QEMU-Terminal to communicate with the hyper visor.
# "telnet" provides a serial terminal via Telnet.
# Listening on 127.0.0.1 means that only the host can connect to the Telnet terminal
-daemonize # start VM as a daemon in the background
# configure the mouse as a touchscreen / tablet and not as a classic mouse. This improves the interface for the spice client.
-device nec-usb-xhci,id=xhci
-device usb-tablet,bus=xhci.0
-object rng-random,filename=/dev/urandom,id=rng0 # emulate a hardware random number generator. Use as virtio devie with:
-device virtio-rng-pci,rng=rng0 # host frontend for the random number generator
)
# if the virtual hard drive is missing: Create the file and start qemu in the installation mode
if [ ! -f $(dirname "$0")/drive.qcow2 ];
then
Drive_Exists="false"
echo "Creating virtual hdd 'drive.qcow2'"
qemu-img create -f qcow2 $DRIVE_NAME $DRIVE_SIZE
echo "Downloadng OpenSUSE installer"
TEMPFILE=$(mktemp -t XXXXXXXX.iso)
wget $INSTALLER_URL -O $TEMPFILE
arguments+=(-cdrom $TEMPFILE -boot d) # add installer as cdrom and specify the first boot-device. "d" means: boot from the first cd-rom-drive
else
Drive_Exists="true"
arguments+=(-boot c)
fi
# execute QEMU vm
qemu-system-x86_64 "${arguments[@]}" \
"$@" # add all parameters given to this bash-script as a string to this command
# remove temp file
[[ $Drive_Exists == "false" ]] && rm $TEMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment