Last active
October 6, 2024 10:44
-
-
Save gpolitis/5c66b96ee28acfad702a794877f5f3f1 to your computer and use it in GitHub Desktop.
A kiss wrapper script for qemu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
#Copyright 2024 Georgios Politis | |
# | |
#Permission is hereby granted, free of charge, to any person obtaining a copy | |
#of this software and associated documentation files (the “Software”), to deal | |
#in the Software without restriction, including without limitation the rights | |
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
#copies of the Software, and to permit persons to whom the Software is | |
#furnished to do so, subject to the following conditions: | |
# | |
#The above copyright notice and this permission notice shall be included in all | |
#copies or substantial portions of the Software. | |
# | |
#THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
#SOFTWARE. | |
QEMU_ENV="$1" | |
if [ ! -f "$QEMU_ENV" ]; then | |
echo "Usage: | |
$0 QEMU_ENV | |
Example QEMU_ENV file | |
--------------------- | |
QEMU_BIN=/opt/local/bin/qemu-system-x86_64 | |
QEMU_ACCEL=hvf | |
QEMU_CPU=host,-pdpe1gb | |
QEMU_SMP=2 | |
QEMU_MEM=4G | |
QEMU_NIC=user | |
QEMU_DRIVE=file="$HOME/Virtual Machines/root.qcow2",if=virtio | |
QEMU_CDROM="$HOME/Donwloads/ubuntu-24.04.1-desktop-amd64.iso" | |
Notes | |
----- | |
Some env variables are optional. You can grep qemu.sh to see all the available | |
env variables. | |
Please note that this is just an example env file. You should check the QEMU | |
man page for the available values for the different env variables and make sure | |
they match your environment and needs. For example, hvf acceleration is only | |
available on macOS, you need to adjust this for other host operating systems. | |
Getting started | |
--------------- | |
Assuming you have QEMU installed on your system, you need to : | |
1. Download the ISO image of your favorite ditro. | |
2. Create a root disk for your VM with: | |
qemu-img create -f qcow2 root.qcow2 10G | |
3. Create an env file to describe your VM using the example above as a template. | |
4. Run your VM with qemu.sh" 1>&2 | |
exit 1 | |
fi | |
set -a | |
source "$QEMU_ENV" | |
set +a | |
if [ -z "$QEMU_BIN" ] | |
then | |
echo "QEMU_BIN environment variable is not set." 1>&2 | |
exit 1 | |
fi | |
if [ ! -x "$QEMU_BIN" ] | |
then | |
echo "$QEMU_BIN is not executable." 1>&2 | |
exit 1 | |
fi | |
# I am using associative arrays for the optional arguments. This is a bashism | |
# that I would love to remove so that the script can be run in any shell. | |
declare -a QEMU_OPTIONAL_ARGS | |
if [ ! -z "$QEMU_USBDEVICE" ] | |
then | |
QEMU_OPTIONAL_ARGS+=("-usbdevice" "$QEMU_USBDEVICE") | |
fi | |
if [ ! -z "$QEMU_CDROM" ] | |
then | |
QEMU_OPTIONAL_ARGS+=("-cdrom" "$QEMU_CDROM") | |
fi | |
if [ ! -z "$QEMU_DISPLAY" ] | |
then | |
QEMU_OPTIONAL_ARGS+=("-display" "$QEMU_DISPLAY") | |
fi | |
exec "$QEMU_BIN" -accel "$QEMU_ACCEL" -cpu "$QEMU_CPU" \ | |
-smp "$QEMU_SMP" -m "$QEMU_MEM" -nic "$QEMU_NIC" -drive "$QEMU_DRIVE" \ | |
"${QEMU_OPTIONAL_ARGS[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment