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

215 lines
5.4 KiB
Plaintext
Raw Normal View History

2016-10-08 18:30:05 +02:00
#!/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/>.
program_name="$0"
local_path="${program_name%/qvm}"
help()
{
2016-10-08 18:30:05 +02:00
cat <<-EOF
Usage: qvm [OPTION]
Trivial management of 64 bit virtual machines with qemu.
Options:
2017-10-21 13:28:42 +02:00
-a, --attach connect to SSH locally
--attach-remote connect to SSH remotely
-b, --backup backup vhd
-c, --create create new vhd
-d, --delete delete vhd backup
2016-11-14 18:02:47 +01:00
--delete-orig delete original vhd
-h, --help print this help
-i, --install install img on vhd
2017-10-18 11:41:15 +02:00
--install-vnc install img on vhd via vnc
2016-11-14 18:02:47 +01:00
-n, --run-nox run vm without opening a graphical window
2016-11-06 22:15:31 +01:00
(useful for background jobs like SSH)
2016-11-14 18:02:47 +01:00
--run-nox-orig run-orig and run-nox combined
-s, --mkdir-shared create shared directory
2017-10-18 11:41:15 +02:00
-r, --remote connect to a vnc instance via ssh
2016-11-14 18:02:47 +01:00
-x, --run run vm
2017-10-18 11:41:15 +02:00
--run-vnc run vm with vnc
2016-11-14 18:02:47 +01:00
--run-orig run from original vhd
2017-10-18 11:41:15 +02:00
--run-orig-vnc run from original vhd with vnc
2016-11-14 18:02:47 +01:00
2016-10-08 18:30:05 +02:00
Only a single option is accepted.
2016-11-14 18:02:47 +01:00
By default, the backup vhd is run.
2016-10-08 18:30:05 +02:00
CC0
Written in 2016 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
EOF
}
2016-10-08 18:30:05 +02:00
unrecognized_option()
2016-10-08 18:30:05 +02:00
{
printf "%s\n" "Try 'qvm --help' for more information"
} 1>&2-
2016-10-08 18:30:05 +02:00
attach()
{
2017-10-21 13:28:42 +02:00
local argc="$1"
local address=""
if [ "$argc" = "locally" ]; then
address="127.0.0.1"
elif [ "$argc" = "remotely" ]; then
address="$host_ip_address"
fi
ssh -p "$ssh_host_port" -l "$ssh_guest_username" "$address"
}
backup()
{
qemu-img create -f "$vhd_type" \
2016-10-08 18:30:05 +02:00
-b "$vhd_name" \
"$vhd_name".mod
}
create()
{
qemu-img create -f "$vhd_type" \
"$vhd_name" \
"$vhd_size"
}
2016-10-08 18:30:05 +02:00
delete()
{
2016-11-14 18:02:47 +01:00
local argc="$1"
local vhd=""
2016-10-08 18:30:05 +02:00
2016-11-14 18:02:47 +01:00
if [ "$argc" = "orig" ]; then
vhd="$vhd_name"
else
vhd=""$vhd_name".mod"
fi
rm "$vhd"
}
2016-10-08 18:30:05 +02:00
installs()
{
2017-10-18 11:41:15 +02:00
local argc1="$1"
local enable_vnc=""
if [ "$argc1" = "vnc" ]; then
enable_vnc="-monitor pty -vnc 127.0.0.1:0"
fi
qemu-system-x86_64 -m "$vm_memory" \
-enable-kvm \
2017-10-18 11:41:15 +02:00
$enable_vnc \
-cdrom "$img_name" \
-boot order=d \
"$vhd_name" &
}
2016-10-08 18:30:05 +02:00
2016-11-14 18:02:47 +01:00
shared()
{
mkdir -p "$shared_data_path"
}
run()
{
2016-11-14 18:02:47 +01:00
local argc1="$1"
local argc2="$2"
local vhd=""
local display=""
2017-10-18 11:41:15 +02:00
local enable_vnc=""
if [ "$argc1" = "none" ]; then
display="none"
elif [ "$argc1" = "gtk" ]; then
display="gtk"
elif [ "$argc1" = "vnc" ]; then
display="none"
enable_vnc="-monitor pty -vnc 127.0.0.1:0"
fi
2016-11-14 18:02:47 +01:00
if [ "$argc2" = "orig" ]; then
vhd="$vhd_name"
else
vhd=""$vhd_name".mod"
fi
2017-09-02 23:07:03 +02:00
export QEMU_AUDIO_DRV=alsa
qemu-system-x86_64 \
2016-10-08 18:30:05 +02:00
-m "$vm_memory" \
-enable-kvm \
2017-10-18 11:41:15 +02:00
$enable_vnc \
2016-10-08 18:30:05 +02:00
-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" \
2017-09-02 23:07:03 +02:00
-soundhw ac97 \
2016-11-14 18:02:47 +01:00
-display "$display" \
-drive file="$vhd" &
}
2017-10-18 11:41:15 +02:00
remote()
{
ssh -N -f -L 5901:127.0.0.1:5900 "$host_username"@"$host_ip_address"
vncviewer 127.0.0.1:1
}
main()
{
local argc="$1"
2017-02-25 17:55:52 +01:00
local options="abcdhinrx"
2017-10-21 13:28:42 +02:00
local long_options="attach,backup,create,delete,delete-orig,\
help,install,run,run-orig,run-nox,run-nox-orig,run-vnc,run-orig-vnc,remote,install-vnc,\
attach-remote"
local opts
local opt
2016-11-14 18:02:47 +01:00
[ -z "$argc" ] && argc="-x"
opts="$(getopt --options $options --longoptions $long_options -- $argc)"
[ $? -ne 0 ] && unrecognized_option && return 1
eval set -- "$opts"
# Source variables globally.
. "${local_path}"/configvmrc
for opt in $opts; do
case "$opt" in
-- ) ;;
2017-10-21 13:28:42 +02:00
-a | --attach ) attach locally ;;
--attach-remote ) attach remotely ;;
-b | --backup ) backup ;;
-c | --create ) create ;;
-d | --delete ) delete ;;
2016-11-14 18:02:47 +01:00
--delete-orig ) delete orig ;;
-h | --help ) help ;;
-i | --install ) installs ;;
2017-10-18 11:41:15 +02:00
--install-vnc ) installs vnc ;;
2016-11-14 18:02:47 +01:00
-n | --run-nox ) run none ;;
--run-nox-orig ) run none orig ;;
2017-10-18 11:41:15 +02:00
-r | --remote ) remote ;;
2016-11-14 18:02:47 +01:00
-s | --mkdir-shared ) shared ;;
-x | --run ) run gtk ;;
--run-orig ) run gtk orig ;;
2017-10-18 11:41:15 +02:00
--run-vnc ) run vnc ;;
--run-orig-vnc ) run vnc orig ;;
esac
done
}
2016-10-08 18:30:05 +02:00
main "$*"