#!/usr/bin/env sh # qvm - Trivial management of 64 bit virtual machines with qemu. # # Written in 2016 by Franco Masotti/frnmst # # 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 # . help() { cat <<-EOF Usage: qvm [OPTION] Trivial management of 64 bit virtual machines with qemu. Options: -b, --backup backup vhd -c, --create create new vhd -d, --delete delete vhd backup --delete-original delete original vhd -h, --help print this help -i, --install install img on vhd -r, --run run vm --run-nox run vm without opening a graphical window (useful for background jobs like SSH) Only a single option is accepted. Before running any virtual machine you must create a backup first. CC0 Written in 2016 by Franco Masotti/frnmst EOF } unrecognized_option() { printf "%s\n" "Try 'qvm --help' for more information" } 1>&2- backup() { qemu-img create -f "$vhd_type" \ -b "$vhd_name" \ "$vhd_name".mod } create() { qemu-img create -f "$vhd_type" \ "$vhd_name" \ "$vhd_size" } delete() { rm "$vhd_name".mod } delete_original() { rm "$vhd_name" } installs() { qemu-system-x86_64 -m "$vm_memory" \ -enable-kvm \ -cdrom "$img_name" \ -boot order=d \ "$vhd_name" & } run() { argc="$1" if [ -f "$vhd_name".mod ]; then qemu-system-x86_64 \ -m "$vm_memory" \ -enable-kvm \ -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" \ -display "$argc" \ "$vhd_name".mod & else printf "Make a backup and an installation first.\n" exit 1 fi } main() { local argc="$1" local options="bcdhir" local long_options="backup,create,delete,delete-original\ ,help,install,run,run-nox" local opts local opt [ -z "$argc" ] && argc="-r" opts="$(getopt --options $options --longoptions $long_options -- $argc)" [ $? -ne 0 ] && unrecognized_option && return 1 eval set -- "$opts" # Source variables globally. . ./configvmrc for opt in $opts; do case "$opt" in -- ) ;; -b | --backup ) backup ;; -c | --create ) create ;; -d | --delete ) delete ;; --delete-original ) delete_original ;; -h | --help ) help ;; -i | --install ) installs ;; -r | --run ) run gtk ;; --run-nox ) run none ;; esac done } main "$*"