frnmst-archives
/
qvm
Archived
1
0
Fork 0
Browse Source

Simplified scripts. Fixes.

dev
Franco Masotti 4 years ago
parent
commit
3141c7bbcf
  1. 2
      README.md
  2. 104
      automatic_remote_startup.sh
  3. 44
      fbopt
  4. 42
      qvm

2
README.md

@ -128,6 +128,8 @@ network filesystems.
### Automatic remote startup
# FIXME
To automatically start the virtual machine from a non-host computer you can
use the `automatic_remote_startup.sh` script. Make sure that both the local
non-host and the remote host computer have a copy of the QVM repository with

104
automatic_remote_startup.sh

@ -1,104 +0,0 @@
#!/usr/bin/env bash
# qvm - Trivial management of 64 bit virtual machines with qemu.
#
# Written in 2018 by Franco Masotti/frnmst <franco.masotti@live.com>
#
# 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/>.
######################################################################
# NOTE: This script is intended to be used along with a shell alias. #
######################################################################
program_name="${0}"
local_path="${program_name%/automatic_remote_startup.sh}"
help()
{
cat <<-EOF
Usage: automatic_remote_startup.sh [OPTION]
Start QEMU on a remote host computer and connect to it
Options:
-d, --default start the vm in headless mode and connect to it
with SSH
-h, --help print this help
-u, --use-vnc start the vm in VNC mode and connect to it with the
VNC client
Only a single option is accepted.
By default, a headless connection will be initialized.
Preconditions for this to work is to setup SSH correctly on both
computers.
CC0
Written in 2018 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
EOF
}
connect()
{
local display="${1}"
if [ -z "$(ssh -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" "pgrep qemu")" ]; then
if [ "${display}" = "use_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 -n" &
fi
sleep ${SECONDS_BEFORE_CONNECTION_ATTEMPT}
fi
if [ "${display}" = "use_vnc" ]; then
"${local_path}"/qvm --remote &
else
"${local_path}"/qvm --attach-remote
fi
}
check_dependencies()
{
which \
ssh
}
main()
{
local argc="$1"
local options="dhu"
local long_options="default,help,use-vnc"
local opts
local opt
[ -z "$argc" ] && argc="--default"
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
-- ) ;;
-h | --help ) help ;;
-u | --use-vnc ) connect use_vnc ;;
-d | --default ) connect ;;
esac
done
}
main "$*"

44
fbopt_qvm → fbopt

@ -27,23 +27,18 @@ By default, the backup vhd is run.
Mandatory arguments to long options are mandatory for short options too.
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
-n, --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
--vnc run vm with vnc
--orig run from original vhd
Exit status:
0 if OK,
@ -81,9 +76,9 @@ main()
declare -a argc=("${!1}")
# Set the options.
local getopt_short_options='abcdhinrx'
local getopt_short_options='abcdhimnrvx'
local getopt_long_options='attach,backup,create,delete,help,install,run,\
remote,install,vnc,nox'
remote,vnc,mkdir-shared,nox'
# Set the default values for the flags.
local attach='false'
@ -91,10 +86,10 @@ remote,install,vnc,nox'
local delete='false'
local install='false'
local nox='false'
local orig='false'
local remotely=''
local origin='false'
local remote=''
local run='false'
local shared='false'
local mkdir_shared='false'
local vnc='false'
local program_name="${0}"
@ -159,6 +154,15 @@ remote,install,vnc,nox'
&& show_flags \
'attach' \
'backup' \
'create' \
'delete' \
'help' \
'install' \
'mkdir_shared' \
'nox' \
'run' \
'remote' \
'vnc' \
&& return 0
[ "${help}" = 'true' ] && show_help && return 0
# Override values of optional parameters.
@ -166,8 +170,14 @@ remote,install,vnc,nox'
# From now on you should call a function or an external program
# using the values of the flag variables.
local display='gtk'
[ "${attach}" = 'true' ] && { attach "${remote}"; return ${?}; }
local place=''
local display=''
if [ "${attach}" = 'true' ]; then
[ "${remote}" = 'true' ] && place='remote' || place=''
[ "${vnc}" = 'true' ] && display='vnc' || display=''
{ attach "${place}" "${display}"; return ${?}; }
fi
[ "${backup}" = 'true' ] && { backup; return ${?}; }
[ "${create}" = 'true' ] && { create; return ${?}; }
[ "${delete}" = 'true' ] && { delete "${origin}"; return ${?}; }
@ -175,8 +185,10 @@ remote,install,vnc,nox'
if [ "${run}" = 'true' ]; then
[ "${vnc}" = 'vnc' ] && display='vnc'
[ "${nox}" = 'nox' ] && display='none'
[ -z "${display}" ] && display='gtk'
{ run "${display}" "${origin}"; return ${?}; }
[ "${shared}" = 'true' ] && { shared; return ${?}; }
fi
[ "${mkdir_shared}" = 'true' ] && { shared; return ${?}; }
}
# Test dependencies and versions.

42
qvm

@ -1,5 +1,5 @@
#!/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>
@ -16,6 +16,7 @@ check_dependencies()
{
which \
qemu-system-x86_64 \
qemu-img \
ssh \
vncviewer
}
@ -23,10 +24,15 @@ check_dependencies()
attach()
{
local place="${1}"
local display="${2}"
local address=''
[ "${place}" = 'locally' ] && address='127.0.0.1' || address="${HOST_IP_ADDRESS}"
ssh -p "${SSH_HOST_PORT}" -l "${SSH_GUEST_USERNAME}" "${address}"
[ "${place}" = 'remote' ] && address="${HOST_IP_ADDRESS}" || address='127.0.0.1'
[ -n "${place}" ] && ssh -p "${SSH_HOST_PORT}" -l "${SSH_GUEST_USERNAME}" "${address}"
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()
@ -104,15 +110,37 @@ hostfwd=tcp::"${SSH_HOST_PORT}"-:"${SSH_GUEST_PORT}" \
-drive file="${vhd}" &
}
remote()
# -d, --default start the vm in headless mode and connect to it
# with SSH
# -h, --help print this help
# -u, --use-vnc start the vm in VNC mode and connect to it with the
# VNC client
# Start and connect to a remote QVM instance hosted on a different machine.
run_remote_instance()
{
ssh -N -f -L 5901:127.0.0.1:5900 -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}"
vncviewer 127.0.0.1:1
local display="${1}"
if [ -z "$(ssh -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}" "pgrep qemu")" ]; then
if [ "${display}" = "use_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 -n" &
fi
sleep ${SECONDS_BEFORE_CONNECTION_ATTEMPT}
fi
if [ "${display}" = "vnc" ]; then
"${local_path}"/qvm --remote &
else
"${local_path}"/qvm --attach-remote
fi
}
program_name="${0}"
local_path="${program_name%/qvm}"
# Source variables globally.
. "${local_path}"/configvmrc
. "${local_path}"/fbopt_qvm
. "${local_path}"/fbopt