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.
138 lines
3.8 KiB
138 lines
3.8 KiB
#!/usr/bin/env bash |
|
# |
|
# 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/>. |
|
|
|
check_dependencies() |
|
{ |
|
command -V qemu-system-x86_64 \ |
|
&& command -V qemu-img \ |
|
&& command -V ssh \ |
|
&& command -V vncviewer |
|
} |
|
|
|
attach() |
|
{ |
|
local place="${1}" |
|
local display="${2}" |
|
local address='' |
|
|
|
[ "${place}" = 'remote' ] && address="${HOST_IP_ADDRESS}" || address='127.0.0.1' |
|
[ -n "${place}" ] && [ "${display}" != 'vnc' ] && ssh -p "${SSH_HOST_PORT}" -l "${SSH_GUEST_USERNAME}" "${address}" |
|
if [ "${display}" = 'vnc' ]; then |
|
# Forward "${address}":5900 to localhost:5901 through SSH. |
|
ssh -N -f -L 5901:127.0.0.1:5900 -l "${HOST_USERNAME}" "${address}" |
|
# Connect to port 5901. |
|
vncviewer 127.0.0.1:1 |
|
fi |
|
} |
|
|
|
backup() |
|
{ |
|
qemu-img create -f qcow2 -b "${VHD_NAME}" "${VHD_NAME}".mod |
|
} |
|
|
|
create() |
|
{ |
|
qemu-img create -f qcow2 "${VHD_NAME}" "${VHD_SIZE}" |
|
} |
|
|
|
delete() |
|
{ |
|
local vhd_type="${1}" |
|
local vhd='' |
|
|
|
[ "${vhd_type}" = 'origin' ] && vhd="${VHD_NAME}" || vhd=""${VHD_NAME}".mod" |
|
rm "${vhd}" |
|
} |
|
|
|
installs() |
|
{ |
|
local display="${1}" |
|
local enable_vnc='' |
|
|
|
[ "${display}" = 'vnc' ] && enable_vnc='-monitor pty -vnc 127.0.0.1:0' |
|
|
|
qemu-system-x86_64 -m "${MEMORY}" \ |
|
-device e1000,netdev=user.0 \ |
|
-netdev user,id=user.0,\ |
|
hostfwd=tcp::"${SSH_HOST_PORT}"-:"${SSH_GUEST_PORT}" \ |
|
-enable-kvm \ |
|
${enable_vnc} \ |
|
-smp ${NUMBER_OF_CORES} \ |
|
-cdrom "${IMG_NAME}" \ |
|
-boot order=d \ |
|
"${VHD_NAME}" & |
|
} |
|
|
|
mkdir_shared() |
|
{ |
|
mkdir -p "${SHARED_DATA_PATH}" |
|
} |
|
|
|
run() |
|
{ |
|
local display="${1}" |
|
local vhd_type="${2}" |
|
local vhd='' |
|
local enable_vnc='' |
|
|
|
[ "${display}" = 'vnc' ] \ |
|
&& display='none' \ |
|
&& enable_vnc="-monitor pty -vnc 127.0.0.1:0" |
|
[ "${vhd_type}" = 'origin' ] && 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 "${MEMORY}" \ |
|
-cpu host \ |
|
-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=passthrough,mount_tag="${MOUNT_TAG}" \ |
|
-smp ${NUMBER_OF_CORES} \ |
|
-device AC97 \ |
|
-display "${display}" \ |
|
-drive file="${vhd}" & |
|
} |
|
|
|
# Start and connect to a remote QVM instance hosted on a different machine. |
|
run_remote_instance() |
|
{ |
|
local display="${1}" |
|
|
|
if [ -z "$(ssh -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" "pgrep qemu")" ]; then |
|
if [ "${display}" = 'vnc' ]; then |
|
ssh -p ${SSH_EXPOSED_PORT} -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" \ |
|
"cd "${HOST_QVM_SCRIPT_DIRECTORY}" && ./qvm --run --vnc" & |
|
else |
|
ssh -p ${SSH_EXPOSED_PORT} -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" \ |
|
"cd "${HOST_QVM_SCRIPT_DIRECTORY}" && ./qvm --run --nox" & |
|
fi |
|
sleep ${SECONDS_BEFORE_CONNECTION_ATTEMPT} |
|
fi |
|
|
|
attach 'remote' "${display}" |
|
} |
|
|
|
program_name="${0}" |
|
local_path="${program_name%/qvm}" |
|
# Source variables globally. |
|
. "${local_path}"/configvmrc |
|
. "${local_path}"/fbopt
|
|
|