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

Fixing the program to adhere to fbopt.

dev
Franco Masotti 4 years ago
parent
commit
eedd3bddef
  1. 8
      README.md
  2. 103
      automatical_remote_startup.sh
  3. 21
      configvmrc
  4. 146
      qvm

8
README.md

@ -127,10 +127,10 @@ package (if it's not already present in the QEMU package itself).
You should consult the QEMU's manual to learn about all possible compatible
network filesystems.
## Automatical remote startup
## Automatic remote startup
To automatically start the virtual machine from a non-host computer you can
use the `automatical_remote_startup.sh` script. Make sure that both the local
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
the variables correctly set in the `configrc` file.
@ -143,7 +143,7 @@ This can be verified by using the `--attach-remote` option.
Once you have checked that everyting works, you can add a command alias in
your shell configuration file (e.g: `~/.bashrc`), something like:
alias vm='/home/user/scripts/qvm/automatical_remote_startup.sh'
alias vm='/home/user/scripts/qvm/automatic_remote_startup.sh'
# Help
@ -202,4 +202,4 @@ your shell configuration file (e.g: `~/.bashrc`), something like:
# License
CC0.
Creative Commons Zero (CC0).

103
automatical_remote_startup.sh

@ -1,103 +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@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/>.
################################################################
# This script is intended to be used along with a shell alias. #
################################################################
program_name="$0"
local_path="${program_name%/automatical_remote_startup.sh}"
help()
{
cat <<-EOF
Usage: automatical_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 argc1="$1"
if [ -z "$(ssh ${host_username}@${host_ip_address} "pgrep qemu")" ]; then
if [ "$argc1" = "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 [ "$argc1" = "use_vnc" ]; then
"${local_path}"/qvm --remote &
else
"${local_path}"/qvm --attach-remote
fi
}
unrecognized_option()
{
printf "%s\n" "Try 'automatical_remote_startup.sh --help' for more information"
} 1>&2-
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 "$*"

21
configvmrc

@ -12,9 +12,11 @@
img_name="parabola-mate-2016.07.27-dual.iso"
# Size assigned to the vm's HDD.
vhd_size="16G"
vhd_type="qcow2"
vhd_name=""$img_name"."$vhd_type""
# You should leave the following as-is.
vhd_name=""$img_name".qcow2"
# Total number of cores assigned to the vm.
# You should set the appropriate number to have a better experience.
@ -24,16 +26,21 @@ number_of_cores="1"
vm_memory="4G"
# Shared port.
host_port="5555"
host_port_1="5555"
# vm port.
guest_port_1="3050"
# Another shared port.
host_port_2="5556"
# vm port.
guest_port="3050"
guest_port_2="3051"
# Shared port.
ssh_host_port="2222"
# vm port.
ssh_guest_port="22"
# guest user name useful to conect with ssh.
# guest user name useful to connect with ssh.
ssh_guest_username="vm"
shared_data_path="/home/user/qvm/shared_host_guest"
@ -46,10 +53,10 @@ host_ip_address="192.168.1.1"
host_username="parabola"
#
# Automatical remote startup stuff only
# Automatical remote startup stuff only.
#
# The port used to connecting directly to the host computer.
ssh_exposed_port="22"
host_qvm_script_directory="/home/remote/qvm"
seconds_before_connection_attempt="30"
seconds_before_connection_attempt="25"

146
qvm

@ -12,7 +12,7 @@
# with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
program_name="$0"
program_name="${0}"
local_path="${program_name%/qvm}"
help()
@ -32,8 +32,8 @@ Options:
-i, --install install img on vhd
--install-vnc install img on vhd via vnc
-n, --run-nox run vm without opening a graphical window
(useful for background jobs like SSH)
--run-nox-orig run-orig and run-nox combined
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
@ -45,147 +45,117 @@ Options:
Only a single option is accepted.
By default, the backup vhd is run.
CC0
CC0 License
Written in 2016 by Franco Masotti/frnmst <franco.masotti@student.unife.it>
EOF
}
unrecognized_option()
check_dependencies()
{
printf "%s\n" "Try 'qvm --help' for more information"
} 1>&2-
which \
qemu-system-x86_64 \
ssh \
vncviewer
}
attach()
{
local argc="$1"
local address=""
if [ "$argc" = "locally" ]; then
address="127.0.0.1"
elif [ "$argc" = "remotely" ]; then
address="$host_ip_address"
fi
local place="${1}"
local address=''
ssh -p "$ssh_host_port" -l "$ssh_guest_username" "$address"
[ "${place}" = 'locally' ] && address='127.0.0.1' || address="${HOST_IP_ADDRESS}"
ssh -p "${SSH_HOST_PORT}" -l "${SSH_GUEST_USERNAME}" "${address}"
}
backup()
{
qemu-img create -f "$vhd_type" \
-b "$vhd_name" \
"$vhd_name".mod
qemu-img create -f qcow2 -b "${VHD_NAME}" "${VHD_NAME}".mod
}
create()
{
qemu-img create -f "$vhd_type" \
"$vhd_name" \
"$vhd_size"
qemu-img create -f qcow2 "${VHD_NAME}" "${VHD_SIZE}"
}
delete()
{
local argc="$1"
local vhd=""
if [ "$argc" = "orig" ]; then
vhd="$vhd_name"
else
vhd=""$vhd_name".mod"
fi
local vhd_type="${1}"
local vhd=''
rm "$vhd"
[ "${vhd_type}" = 'orig' ] && vhd="${VHD_NAME}" || vhd=""${VHD_NAME}".mod"
rm "${vhd}"
}
installs()
{
local argc1="$1"
local enable_vnc=""
local display="${1}"
local enable_vnc=''
if [ "$argc1" = "vnc" ]; then
enable_vnc="-monitor pty -vnc 127.0.0.1:0"
fi
[ "${display}" = 'vnc' ] && enable_vnc='-monitor pty -vnc 127.0.0.1:0'
qemu-system-x86_64 -m "$vm_memory" \
qemu-system-x86_64 -m "${VM_MEMORY}" \
-device e1000,netdev=user.0 \
-netdev user,\
id=user.0,hostfwd=tcp::"$ssh_host_port"-:"$ssh_guest_port" \
id=user.0,hostfwd=tcp::"${SSH_HOST_PORT}"-:"${SSH_GUEST_PORT}" \
-enable-kvm \
$enable_vnc \
-cdrom "$img_name" \
${enable_vnc} \
-cdrom "${IMG_NAME}" \
-boot order=d \
"$vhd_name" &
"${VHD_NAME}" &
}
shared()
{
mkdir -p "$shared_data_path"
mkdir -p "${SHARED_DATA_PATH}"
}
run()
{
local argc1="$1"
local argc2="$2"
local vhd=""
local display=""
local enable_vnc=""
if [ "$argc1" = "none" ]; then
display="none"
elif [ "$argc1" = "gtk" ]; then
display="gtk"
elif [ "$argc1" = "vnc" ]; then
display="none"
enable_vnc="-monitor pty -vnc 127.0.0.1:0"
fi
if [ "$argc2" = "orig" ]; then
vhd="$vhd_name"
else
vhd=""$vhd_name".mod"
fi
local display="${1}"
local vhd_type="${2}"
local vhd=''
local display=''
local enable_vnc=''
[ "${display}" = 'vnc' ] \
&& display='none' \
&& enable_vnc="-monitor pty -vnc 127.0.0.1:0"
[ "${vhd_type}" = 'orig' ] && 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 "$vm_memory" \
-enable-kvm \
$enable_vnc \
-device e1000,netdev=user.0 \
-netdev user,\
id=user.0,hostfwd=tcp::"$host_port"-:"$guest_port",\
hostfwd=tcp::"$ssh_host_port"-:"$ssh_guest_port" \
-virtfs local,path="$shared_data_path",\
security_model=none,mount_tag="$mount_tag" \
-smp "$number_of_cores" \
-soundhw ac97 \
-display "$display" \
-drive file="$vhd" &
-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}" \
-virtfs local,path="$SHARED_DATA_PATH",security_model=none,mount_tag="${MOUNT_TAG}" \
-smp ${NUMBER_OF_CORES} \
-soundhw ac97 \
-display "${display}" \
-drive file="${vhd}" &
}
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 "${HOST_USERNAME}"@"${HOST_IP_ADDRESS}"
vncviewer 127.0.0.1:1
}
main()
{
local argc="$1"
local options="abcdhinrx"
local 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"
local opts
local opt
[ -z "$argc" ] && argc="-x"
opts="$(getopt --options $options --longoptions $long_options -- $argc)"
[ $? -ne 0 ] && unrecognized_option && return 1
eval set -- "$opts"
[ -z "$argc" ] && argc='-x'
# Source variables globally.
. "${local_path}"/configvmrc
@ -214,5 +184,3 @@ attach-remote"
esac
done
}
main "$*"