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

139 lines
3.6 KiB
Plaintext
Raw Normal View History

2016-10-08 18:30:05 +02:00
#!/usr/bin/env sh
2018-12-28 18:59:44 +01:00
#
2016-10-08 18:30:05 +02:00
# 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/>.
2018-12-27 13:05:08 +01:00
check_dependencies()
2016-10-08 18:30:05 +02:00
{
2018-12-27 13:05:08 +01:00
which \
qemu-system-x86_64 \
2018-12-28 18:59:44 +01:00
qemu-img \
2018-12-27 13:05:08 +01:00
ssh \
vncviewer
}
2016-10-08 18:30:05 +02:00
attach()
{
2018-12-27 13:05:08 +01:00
local place="${1}"
2018-12-28 18:59:44 +01:00
local display="${2}"
2018-12-27 13:05:08 +01:00
local address=''
2017-10-21 13:28:42 +02:00
2018-12-28 18:59:44 +01:00
[ "${place}" = 'remote' ] && address="${HOST_IP_ADDRESS}" || address='127.0.0.1'
2019-01-09 18:53:53 +01:00
# FIXME:::
# [ -n "${place}" ] && ssh -p "${SSH_HOST_PORT}" -l "${SSH_GUEST_USERNAME}" "${address}"
2018-12-28 18:59:44 +01:00
if [ "${display}" = 'vnc' ]; then
ssh -N -f -L 5901:127.0.0.1:5900 -l "${HOST_USERNAME}" "${address}"
vncviewer 127.0.0.1:1
fi
}
backup()
{
2018-12-27 13:05:08 +01:00
qemu-img create -f qcow2 -b "${VHD_NAME}" "${VHD_NAME}".mod
}
create()
{
2018-12-27 13:05:08 +01:00
qemu-img create -f qcow2 "${VHD_NAME}" "${VHD_SIZE}"
}
2016-10-08 18:30:05 +02:00
delete()
{
2018-12-27 13:05:08 +01:00
local vhd_type="${1}"
local vhd=''
2016-11-14 18:02:47 +01:00
2018-12-27 19:57:01 +01:00
[ "${vhd_type}" = 'origin' ] && vhd="${VHD_NAME}" || vhd=""${VHD_NAME}".mod"
2018-12-27 13:05:08 +01:00
rm "${vhd}"
}
2016-10-08 18:30:05 +02:00
installs()
{
2018-12-27 13:05:08 +01:00
local display="${1}"
local enable_vnc=''
2017-10-18 11:41:15 +02:00
2018-12-27 13:05:08 +01:00
[ "${display}" = 'vnc' ] && enable_vnc='-monitor pty -vnc 127.0.0.1:0'
2017-10-18 11:41:15 +02:00
2018-12-28 19:19:38 +01:00
qemu-system-x86_64 -m "${MEMORY}" \
2018-12-27 16:37:24 +01:00
-device e1000,netdev=user.0 \
-netdev user,id=user.0,\
hostfwd=tcp::"${SSH_HOST_PORT}"-:"${SSH_GUEST_PORT}" \
-enable-kvm \
${enable_vnc} \
2018-12-28 19:19:38 +01:00
-smp ${NUMBER_OF_CORES} \
2018-12-27 16:37:24 +01:00
-cdrom "${IMG_NAME}" \
-boot order=d \
"${VHD_NAME}" &
}
2016-10-08 18:30:05 +02:00
2018-12-28 19:37:31 +01:00
mkdir_shared()
2016-11-14 18:02:47 +01:00
{
2018-12-27 13:05:08 +01:00
mkdir -p "${SHARED_DATA_PATH}"
2016-11-14 18:02:47 +01:00
}
run()
{
2018-12-27 13:05:08 +01:00
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"
2018-12-27 19:57:01 +01:00
[ "${vhd_type}" = 'origin' ] && vhd="${VHD_NAME}" || vhd=""${VHD_NAME}".mod"
2018-12-27 13:05:08 +01:00
# Adds ALSA audio support.
2017-09-02 23:07:03 +02:00
export QEMU_AUDIO_DRV=alsa
2018-12-27 13:05:08 +01:00
# No spaces between the parameters of each option.
qemu-system-x86_64 \
2018-12-27 13:05:08 +01:00
-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}" \
2019-01-04 12:37:24 +01:00
-virtfs local,path="${SHARED_DATA_PATH}",security_model=none,mount_tag="${MOUNT_TAG}" \
2018-12-27 13:05:08 +01:00
-smp ${NUMBER_OF_CORES} \
-soundhw ac97 \
-display "${display}" \
-drive file="${vhd}" &
}
2018-12-28 18:59:44 +01:00
# Start and connect to a remote QVM instance hosted on a different machine.
run_remote_instance()
2017-10-18 11:41:15 +02:00
{
2018-12-28 18:59:44 +01:00
local display="${1}"
if [ -z "$(ssh -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" "pgrep qemu")" ]; then
2018-12-31 16:47:59 +01:00
if [ "${display}" = 'vnc' ]; then
2018-12-28 18:59:44 +01:00
ssh -p ${SSH_EXPOSED_PORT} -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" \
2019-01-04 12:37:24 +01:00
"cd "${HOST_QVM_SCRIPT_DIRECTORY}" && ./qvm --run --vnc" &
2018-12-28 18:59:44 +01:00
else
ssh -p ${SSH_EXPOSED_PORT} -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" \
2019-01-04 12:37:24 +01:00
"cd "${HOST_QVM_SCRIPT_DIRECTORY}" && ./qvm --run --nox" &
2018-12-28 18:59:44 +01:00
fi
sleep ${SECONDS_BEFORE_CONNECTION_ATTEMPT}
fi
2019-01-04 12:20:38 +01:00
attach 'remote' "${display}"
2017-10-18 11:41:15 +02:00
}
2018-12-27 13:26:08 +01:00
program_name="${0}"
local_path="${program_name%/qvm}"
2018-12-27 19:57:01 +01:00
# Source variables globally.
. "${local_path}"/configvmrc
2018-12-28 18:59:44 +01:00
. "${local_path}"/fbopt
2018-12-27 13:26:08 +01:00