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

Added missing files.

dev
Franco Masotti 4 years ago
parent
commit
3fa68e7bb9
  1. 104
      automatic_remote_startup.sh
  2. 207
      fbopt_qvm
  3. 2
      qvm

104
automatic_remote_startup.sh

@ -0,0 +1,104 @@
#!/usr/bin/env bash
# qvm - Trivial management of 64 bit virtual machines with qemu.
#
# Written in 2018 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/>.
######################################################################
# 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 "$*"

207
fbopt_qvm

@ -0,0 +1,207 @@
#!/bin/bash
#
# fbopt version 0.4.1
#
# 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/>.
#
#
# See also https://frnmst.gitlab.io/notes/my-bash-option-parsing-template.html
#
show_help()
{
cat <<-EOF
Usage: qvm [OPTION]
Trivial management of 64 bit virtual machines with qemu.
Only a single option is accepted.
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
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
Exit status:
0 if OK,
1 if an error occurred.
CC0 License
Written in 2016-2018 by Franco Masotti/frnmst <franco.masotti@live.com>
EOF
}
# A function that prints the variable name and value of all
# the flags enabled by the user. This is useful to check that
# all the flags are correct, as kind of a dry run.
show_flags()
{
local flags="${*}"
for flag in ${flags}; do
printf "%s='%s'\n" "${flag}" "${!flag}"
done
}
getopt_error()
{
local program_name="${0}"
printf "%s\n" "Try '"${program_name}" --help' for more information"
} 1>&2 2>&-
main()
{
# Create a new array from the reference of the input one.
# See https://stackoverflow.com/questions/1063347/passing-arrays-as-parameters-in-bash
# See some comments below.
declare -a argc=("${!1}")
# Set the options. flag_f has an optional argument.
local getopt_short_options='abcdhinrx'
local getopt_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'
# Set the default values for the flags.
local flag_a=''
local flag_b=''
local flag_c=''
local flag_d='false'
local flag_e='false'
local flag_f='false'
local program_name="${0}"
opts="$(getopt \
--name "${program_name}" \
--shell bash \
--options "${getopt_short_options}" \
--longoptions "${getopt_long_options}" \
-- \
"${argc[@]}")"
getopt_retval=${?}
# Check that getopt works and that some kind of argument
# is passed to the script. This is "quotation hell".
a="'"${argc[@]}"'"
{ [ ${getopt_retval} -ne 0 ] || [ -z "${a}" ]; } && getopt_error && return 1
eval set -- "${opts}"
# Option parsing.
while [ "${1}" != '--' ]; do
case "${1}" 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
# Iterate through all arguments.
# shift 1
# EXCEPTION: break because we only need one argument.
break
done
shift 1
# Everything else after '--' is an argument.
argc="${*}"
# Check that the flags that must be non empty are actually not empty.
# A user might infact circumvent getopt's mechanisms like this
# ./program -flag ''
# This can also be done inside the option parser loop but to avoid nestings
# I prefer it done here.
{ [ -z "${flag_a}" ] \
|| [ -z "${flag_b}" ]; } \
&& getopt_error && return 1
[ "${print_flags}" = 'true' ] \
&& show_flags \
'flag_a' \
'flag_b' \
'flag_c' \
'flag_d' \
'flag_e' \
'flag_f' \
&& return 0
[ "${help}" = 'true' ] && show_help && return 0
# Override values of optional parameters.
# [ -z "${flag_f}" ] && flag_f='true'
# From now on you should call a function or an external program
# using the values of the flag variables.
program_name="${0}"
local_path="${program_name%/qvm}"
# Source variables globally.
. "${local_path}"/configvmrc
[ "${flag_a}" = 'alpha' ] && do_something_alpha && return ${?}
[ "${flag_a}" = 'a' ] && do_something_a && return ${?}
}
# Test dependencies and versions.
# getopt must return 4 to be fully compatible. See getopt's manual.
which bash getopt 1>/dev/null 2>/dev/null && { getopt -T; [ ${?} -eq 4 ]; }
# Get and pass argc to the main function.
# All this work with an array must be done to preserve
# quotations for arguments that have whitespaces.
# See https://lists.debian.org/debian-user/2007/12/msg01244.html
declare -a opts=()
for opt in "${@}"; do
opts=("${opts[@]}" "${opt}")
done
main 'opts[@]'

2
qvm

@ -106,7 +106,7 @@ hostfwd=tcp::"${SSH_HOST_PORT}"-:"${SSH_GUEST_PORT}" \
remote()
{
ssh -N -f -L 5901:127.0.0.1:5900 "${HOST_USERNAME}"@"${HOST_IP_ADDRESS}"
ssh -N -f -L 5901:127.0.0.1:5900 -l "${HOST_USERNAME}" "${HOST_IP_ADDRESS}"
vncviewer 127.0.0.1:1
}