This repository has been archived on 2021-09-04. You can view files and clone it, but cannot push or open issues/pull-requests.
qvm/qvm

177 lines
4.2 KiB
Bash
Executable File

#!/usr/bin/env sh
# qvm - Trivial management of 64 bit virtual machines with qemu.
#
# Written in 2016 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the public
# domain worldwide. This software is distributed without any warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication along
# with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
help()
{
cat <<-EOF
Usage: qvm [OPTION]
Trivial management of 64 bit virtual machines with qemu.
Options:
-a, --attach connect via SSH
-b, --backup backup vhd
-c, --create create new vhd
-d, --delete delete vhd backup
--delete-orig delete original vhd
-h, --help print this help
-i, --install install img on vhd
-n, --run-nox run vm without opening a graphical window
(useful for background jobs like SSH)
--run-nox-orig run-orig and run-nox combined
-s, --mkdir-shared create shared directory
-x, --run run vm
--run-orig run from original vhd
Only a single option is accepted.
By default, the backup vhd is run.
CC0
Written in 2016 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
EOF
}
unrecognized_option()
{
printf "%s\n" "Try 'qvm --help' for more information"
} 1>&2-
attach()
{
ssh -p "$ssh_host_port" -l "$ssh_guest_username" 127.0.0.1
}
backup()
{
qemu-img create -f "$vhd_type" \
-b "$vhd_name" \
"$vhd_name".mod
}
create()
{
qemu-img create -f "$vhd_type" \
"$vhd_name" \
"$vhd_size"
}
delete()
{
local argc="$1"
local vhd=""
if [ "$argc" = "orig" ]; then
vhd="$vhd_name"
else
vhd=""$vhd_name".mod"
fi
rm "$vhd"
}
installs()
{
qemu-system-x86_64 -m "$vm_memory" \
-enable-kvm \
-cdrom "$img_name" \
-boot order=d \
"$vhd_name" &
}
shared()
{
mkdir -p "$shared_data_path"
}
run()
{
local argc1="$1"
local argc2="$2"
local vhd=""
local display=""
if [ "$argc2" = "orig" ]; then
vhd="$vhd_name"
else
vhd=""$vhd_name".mod"
fi
if [ "$argc1" = "none" ]; then
display="none"
else
display="gtk"
fi
export QEMU_AUDIO_DRV=alsa
if [ -f "$vhd" ]; then
qemu-system-x86_64 \
-m "$vm_memory" \
-enable-kvm \
-device e1000,netdev=user.0 \
-netdev user,\
id=user.0,hostfwd=tcp::"$host_port"-:"$guest_port",\
hostfwd=tcp::"$ssh_host_port"-:"$ssh_guest_port" \
-virtfs local,path="$shared_data_path",\
security_model=none,mount_tag="$mount_tag" \
-soundhw ac97 \
-display "$display" \
"$vhd" &
else
printf "VHD file \""$vhd"\" is missing.\n"
exit 1
fi
}
main()
{
local argc="$1"
local options="abcdhinrx"
local long_options="attach,backup,create,delete,delete-orig\
,help,install,run,run-orig,run-nox,run-nox-orig"
local opts
local opt
[ -z "$argc" ] && argc="-x"
opts="$(getopt --options $options --longoptions $long_options -- $argc)"
[ $? -ne 0 ] && unrecognized_option && return 1
eval set -- "$opts"
# Source variables globally.
. ./configvmrc
for opt in $opts; do
case "$opt" in
-- ) ;;
-a | --attach ) attach ;;
-b | --backup ) backup ;;
-c | --create ) create ;;
-d | --delete ) delete ;;
--delete-orig ) delete orig ;;
-h | --help ) help ;;
-i | --install ) installs ;;
-n | --run-nox ) run none ;;
--run-nox-orig ) run none orig ;;
-s | --mkdir-shared ) shared ;;
-x | --run ) run gtk ;;
--run-orig ) run gtk orig ;;
esac
done
}
main "$*"