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/fbopt

240 lines
7.4 KiB

#!/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 via SSH
-b, --backup backup the vhd
-c, --create create a new vhd
-d, --delete delete the vhd backup
-h, --help print this help
-i, --install install the image on a vhd
-m, --mkdir-shared create a shared directory
-n, --nox run vm without graphical output
4 years ago
-o, --orig run from the original vhd
-r, --remote connect to a remote instance of QVM
-v, --vnc use VNC
-V, --version print the software version
-x, --run run the vm
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
}
# TODO better FIXME.
show_version()
{
local program_name="${0}"
local program_version="$(cat version)"
wd="$(pwd)"
printf "%s\n" "${program_name} version ${program_version}"
}
# 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}")
4 years ago
# Set the options.
local getopt_short_options='abcdhimnorvVx'
4 years ago
local getopt_long_options='attach,backup,create,delete,help,install,run,\
remote,vnc,mkdir-shared,nox,print-flags,version'
# Set the default values for the flags.
4 years ago
local attach='false'
local backup='false'
local create='false'
4 years ago
local delete='false'
local install='false'
local nox='false'
local origin='false'
4 years ago
local remote='false'
4 years ago
local run='false'
local mkdir_shared='false'
local version='false'
4 years ago
local vnc='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
4 years ago
-a | --attach ) attach='true' ;;
4 years ago
-b | --backup ) backup='true' ;;
4 years ago
-c | --create ) create='true' ;;
4 years ago
-d | --delete ) delete='true' ;;
-h | --help ) help='true' ;;
4 years ago
-i | --install ) install='true' ;;
4 years ago
-m | --mkdir-shared ) shared='true' ;;
4 years ago
-n | --nox ) nox='nox' ;;
4 years ago
-o | --origin ) origin='origin' ;;
4 years ago
--print-flags ) print_flags='true' ;;
4 years ago
-r | --remote ) remote='remote' ;;
4 years ago
-v | --vnc ) vnc='vnc' ;;
-V | --version ) version='true' ;;
4 years ago
-x | --run ) run='true' ;;
esac
# Iterate through all arguments.
4 years ago
shift 1
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.
4 years ago
# NOOP.
[ "${print_flags}" = 'true' ] \
&& show_flags \
4 years ago
'attach' \
'backup' \
'create' \
'delete' \
'help' \
'install' \
'mkdir_shared' \
'nox' \
4 years ago
'origin' \
'run' \
'remote' \
'version' \
'vnc' \
&& return 0
[ "${help}" = 'true' ] && show_help && return 0
[ "${version}" = 'true' ] && show_version && return 0
# Override values of optional parameters.
4 years ago
# NOOP.
# From now on you should call a function or an external program
# using the values of the flag variables.
local place=''
local display=''
4 years ago
check_dependencies || exit ${?}
{ [ "${attach}" = 'false' ] && [ "${backup}" = 'false' ] \
&& [ "${create}" = 'false' ] && [ "${delete}" = 'false' ] \
&& [ "${install}" = 'false' ] && [ "${run}" = 'false' ]; } \
&& printf "%s\n" 'an action needs to be specified' 1>&2 2>&- \
&& return 1
if [ "${attach}" = 'true' ]; then
4 years ago
[ "${remote}" = 'remote' ] && place='remote' || place='local'
[ "${vnc}" = 'vnc' ] && display='vnc' || display=''
{ attach "${place}" "${display}"; return ${?}; }
fi
4 years ago
[ "${backup}" = 'true' ] && { backup; return ${?}; }
[ "${create}" = 'true' ] && { create; return ${?}; }
[ "${delete}" = 'true' ] && { delete "${origin}"; return ${?}; }
[ "${install}" = 'true' ] && { installs "${vnc}"; return ${?}; }
if [ "${run}" = 'true' ]; then
4 years ago
if [ "${remote}" = 'remote' ]; then
# Start remote instance.
[ "${vnc}" = 'vnc' ] && display='vnc'
{ run_remote_instance "${display}"; return ${?}; }
else
[ "${vnc}" = 'vnc' ] && display='vnc'
[ "${nox}" = 'nox' ] && display='none'
[ -z "${display}" ] && display='gtk'
{ run "${display}" "${origin}"; return ${?}; }
fi
fi
[ "${mkdir_shared}" = 'true' ] && { mkdir_shared; 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[@]'