frnmst-archives
/
qvm
Archived
1
0
Fork 0
Trivial management of 64 bit virtual machines with qemu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
qvm/qvm

187 lines
5.4 KiB

6 years ago
#!/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()
{
6 years ago
cat <<-EOF
Usage: qvm [OPTION]
Trivial management of 64 bit virtual machines with qemu.
Options:
-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
--delete-orig delete original vhd
-h, --help print this help
-i, --install install img on vhd
--install-vnc install img on vhd via vnc
-n, --run-nox run vm without opening a graphical window
which is useful for background jobs like SSH
--run-nox-orig '--run-orig' and '--run-nox' combined
-s, --mkdir-shared create shared directory
-r, --remote connect to a vnc instance via ssh
-x, --run run vm
--run-vnc run vm with vnc
--run-orig run from original vhd
--run-orig-vnc run from original vhd with vnc
6 years ago
Only a single option is accepted.
By default, the backup vhd is run.
6 years ago
CC0 License
6 years ago
Written in 2016 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
EOF
}
6 years ago
check_dependencies()
6 years ago
{
which \
qemu-system-x86_64 \
ssh \
vncviewer
}
6 years ago
attach()
{
local place="${1}"
local address=''
[ "${place}" = 'locally' ] && address='127.0.0.1' || address="${HOST_IP_ADDRESS}"
ssh -p "${SSH_HOST_PORT}" -l "${SSH_GUEST_USERNAME}" "${address}"
}
backup()
{
qemu-img create -f qcow2 -b "${VHD_NAME}" "${VHD_NAME}".mod
}
create()
{
qemu-img create -f qcow2 "${VHD_NAME}" "${VHD_SIZE}"
}
6 years ago
delete()
{
local vhd_type="${1}"
local vhd=''
[ "${vhd_type}" = 'orig' ] && vhd="${VHD_NAME}" || vhd=""${VHD_NAME}".mod"
rm "${vhd}"
}
6 years ago
installs()
{
local display="${1}"
local enable_vnc=''
[ "${display}" = 'vnc' ] && enable_vnc='-monitor pty -vnc 127.0.0.1:0'
qemu-system-x86_64 -m "${VM_MEMORY}" \
-device e1000,netdev=user.0 \
-netdev user,\
id=user.0,hostfwd=tcp::"${SSH_HOST_PORT}"-:"${SSH_GUEST_PORT}" \
-enable-kvm \
${enable_vnc} \
-cdrom "${IMG_NAME}" \
-boot order=d \
"${VHD_NAME}" &
}
6 years ago
shared()
{
mkdir -p "${SHARED_DATA_PATH}"
}
run()
{
local display="${1}"
local vhd_type="${2}"
local vhd=''
local display=''
local enable_vnc=''
[ "${display}" = 'vnc' ] \
&& display='none' \
&& enable_vnc="-monitor pty -vnc 127.0.0.1:0"
[ "${vhd_type}" = 'orig' ] && vhd="${VHD_NAME}" || vhd=""${VHD_NAME}".mod"
# Adds ALSA audio support.
export QEMU_AUDIO_DRV=alsa
# No spaces between the parameters of each option.
qemu-system-x86_64 \
-m "${VM_MEMORY}" \
-enable-kvm \
${enable_vnc} \
-device e1000,netdev=user.0 \
-netdev user,id=user.0,\
hostfwd=tcp::"${HOST_PORT_1}"-:"${GUEST_PORT_1}",\
hostfwd=tcp::"${HOST_PORT_2}"-:"${GUEST_PORT_2}",\
hostfwd=tcp::"${SSH_HOST_PORT}"-:"${SSH_GUEST_PORT}" \
-virtfs local,path="$SHARED_DATA_PATH",security_model=none,mount_tag="${MOUNT_TAG}" \
-smp ${NUMBER_OF_CORES} \
-soundhw ac97 \
-display "${display}" \
-drive file="${vhd}" &
}
remote()
{
ssh -N -f -L 5901:127.0.0.1:5900 "${HOST_USERNAME}"@"${HOST_IP_ADDRESS}"
vncviewer 127.0.0.1:1
}
main()
{
local options="abcdhinrx"
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"
[ -z "$argc" ] && argc='-x'
# Source variables globally.
. "${local_path}"/configvmrc
for opt in $opts; do
case "$opt" in
-- ) ;;
-a | --attach ) attach locally ;;
--attach-remote ) attach remotely ;;
-b | --backup ) backup ;;
-c | --create ) create ;;
-d | --delete ) delete ;;
--delete-orig ) delete orig ;;
-h | --help ) help ;;
-i | --install ) installs ;;
--install-vnc ) installs vnc ;;
-n | --run-nox ) run none ;;
--run-nox-orig ) run none orig ;;
-r | --remote ) remote ;;
-s | --mkdir-shared ) shared ;;
-x | --run ) run gtk ;;
--run-orig ) run gtk orig ;;
--run-vnc ) run vnc ;;
--run-orig-vnc ) run vnc orig ;;
esac
done
}