New scripts and sudo rules. Implement qemu session mode.
This commit is contained in:
parent
91aab8e75b
commit
7871936a67
19 changed files with 176 additions and 67 deletions
|
@ -3,29 +3,40 @@
|
|||
|
||||
set -eu
|
||||
|
||||
NEWCLONE=0
|
||||
|
||||
show_help() {
|
||||
cat << EOF
|
||||
cat << EOF >&2
|
||||
Usage: $(basename "$0") [-n] vmname"
|
||||
Create a new clone, start the vm (if not yet running) and run virt-viewer.
|
||||
Squid-Proxy will be started too.
|
||||
User Home will be mounted on /media/USERNAME/home
|
||||
-n new clone will be created, even if exists
|
||||
-s qemu:///session instead of default qemu:///system
|
||||
EOF
|
||||
}
|
||||
|
||||
exit_script() {
|
||||
echo "run-vm.sh ${VM_NAME} terminated by trap!" >> "/tmp/${UID}/exit-run-vm.log"
|
||||
virsh --connect="${QEMU}" destroy "${VM_NAME}-clone"
|
||||
trap - SIGHUP SIGINT SIGTERM # clear the trap
|
||||
kill -- -$$ # Sends SIGTERM to child/sub processes
|
||||
}
|
||||
|
||||
QEMU='qemu:///system'
|
||||
|
||||
NEWCLONE=0
|
||||
|
||||
while getopts ':n' OPTION; do
|
||||
while getopts ':ns' OPTION; do
|
||||
case "$OPTION" in
|
||||
n)
|
||||
NEWCLONE=1
|
||||
;;
|
||||
?)
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
n)
|
||||
NEWCLONE=1
|
||||
;;
|
||||
s)
|
||||
QEMU='qemu:///session'
|
||||
;;
|
||||
?)
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
@ -40,7 +51,7 @@ fi
|
|||
VM_NAME=$1
|
||||
|
||||
if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" ]]; then
|
||||
echo "no base VM disk '${VM_NAME}.qcow2' found"
|
||||
echo "no base VM disk '${VM_NAME}.qcow2' found" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
@ -51,17 +62,26 @@ if [[ ! -f "/tmp/squid.pid" ]]; then
|
|||
fi
|
||||
|
||||
# check, if we have to mount home
|
||||
if [[ ! -d "/media/${USER}/home" ]]; then
|
||||
if ! findmnt "/media/${USER}/home"; then
|
||||
echo "mounting home."
|
||||
sudo mounthome.sh
|
||||
fi
|
||||
|
||||
if ! virsh --connect=qemu:///system list | grep "${VM_NAME}"; then
|
||||
export XDG_CONFIG_HOME="/tmp/${UID}/.config"
|
||||
VM_DIR="/tmp/${UID}/vmimages"
|
||||
|
||||
if ! virsh --connect="${QEMU}" list | grep "${VM_NAME}-clone"; then
|
||||
echo "VM not yet running. Try to clone and start."
|
||||
if [[ "${NEWCLONE}" = 1 ]] || [[ ! -f "/var/lib/libvirt/images/${VM_NAME}-clone.qcow2" ]]; then
|
||||
sudo create-clone.sh "${VM_NAME}"
|
||||
if [[ "${NEWCLONE}" = 1 ]] || [[ ! -f "${VM_DIR}/${VM_NAME}-clone.qcow2" ]]; then
|
||||
create-clone.sh "${VM_NAME}"
|
||||
fi
|
||||
virsh --connect=qemu:///system start "${VM_NAME}-clone"
|
||||
# delete the old vm
|
||||
virsh --connect=qemu:///session undefine "${VM_NAME}-clone" || echo "${VM_NAME}-clone did not exist"
|
||||
# finally, create the new vm
|
||||
virsh --connect=qemu:///session define "${VM_DIR}/xml/${VM_NAME}-clone.xml"
|
||||
trap exit_script SIGHUP SIGINT SIGTERM
|
||||
[[ "${QEMU}" = 'qemu:///session' ]] && sudo /usr/local/bin/start-virtiofsd.sh "${VM_NAME}"
|
||||
virsh --connect="${QEMU}" start "${VM_NAME}-clone"
|
||||
fi
|
||||
echo "starting viewer"
|
||||
virt-viewer --connect=qemu:///system --full-screen "${VM_NAME}-clone"
|
||||
virt-viewer --connect="${QEMU}" --full-screen "${VM_NAME}-clone"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue