Clean up VM scripts a bit.
This commit is contained in:
parent
3ea922c263
commit
da71eed94b
5 changed files with 66 additions and 81 deletions
|
@ -1,43 +0,0 @@
|
|||
#!/usr/bin/bash
|
||||
# create VM clone
|
||||
|
||||
set -eu
|
||||
|
||||
# if less or more than one arguments supplied, display usage
|
||||
if [[ $# -ne 1 ]]; then
|
||||
echo "This script takes as input the name of the VM to clone" >&2
|
||||
echo "Usage: $0 vm_name" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# change to image-directory
|
||||
cd /lmn/vm
|
||||
|
||||
VM_NAME=$1
|
||||
VM_DIR="/tmp/${UID}/vm"
|
||||
VM_XML="${VM_DIR}/${VM_NAME}-clone.xml"
|
||||
|
||||
if ! [[ -f "${VM_NAME}.xml" && -f "${VM_NAME}.qcow2" ]] && ! [[ -f "${VM_DIR}/${VM_NAME}.xml" && -f "${VM_DIR}/${VM_NAME}.qcow2" ]]; then
|
||||
echo "xml or qcow2 File does not exists." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create User-VM-Dir and link system VM-Images
|
||||
[[ -d "${VM_DIR}" ]] || mkdir -p "${VM_DIR}"
|
||||
sudo /usr/local/bin/link-images.sh
|
||||
|
||||
# Create backing file
|
||||
cd "${VM_DIR}"
|
||||
qemu-img create -f qcow2 -F qcow2 -b "${VM_NAME}.qcow2" "${VM_NAME}-clone.qcow2"
|
||||
|
||||
# Create machine-definition-file
|
||||
cp "${VM_DIR}/${VM_NAME}.xml" "${VM_XML}"
|
||||
|
||||
# set VM_DIR:
|
||||
sed -i "s:VMIMAGEDIR:${VM_DIR}:" "${VM_XML}"
|
||||
|
||||
# and actually rename the vm (this also updates part of the storage path):
|
||||
sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "${VM_XML}"
|
||||
|
||||
# set virtiofs-Socket
|
||||
sed -i "s:VIRTIOFSSOCKET:/run/virtiofsd/${VM_NAME}-clone.sock:" "${VM_XML}"
|
|
@ -21,36 +21,6 @@ exit_script() {
|
|||
kill -- -$$ # Sends SIGTERM to child/sub processes
|
||||
}
|
||||
|
||||
QEMU='qemu:///session'
|
||||
|
||||
NEWCLONE=0
|
||||
|
||||
while getopts ':ns' OPTION; do
|
||||
case "$OPTION" in
|
||||
n)
|
||||
NEWCLONE=1
|
||||
;;
|
||||
s)
|
||||
QEMU='qemu:///system'
|
||||
;;
|
||||
?)
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift "$((OPTIND -1))"
|
||||
|
||||
# if less than one arguments supplied, display usage
|
||||
if [[ $# -ne 1 ]] ; then
|
||||
show_help
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VM_NAME=$1
|
||||
VM_DIR="/tmp/${UID}/vm"
|
||||
|
||||
check_images() {
|
||||
# sync vm-torrents and machine definition file
|
||||
sudo -u lmnsynci /usr/local/bin/sync-vm.sh -t
|
||||
|
@ -99,7 +69,65 @@ check_images() {
|
|||
echo "VM-Image and required backingfiles available and checked"
|
||||
}
|
||||
|
||||
#################
|
||||
create-clone() {
|
||||
local VM_NAME="$1"
|
||||
local VM_DIR="/tmp/${UID}/vm"
|
||||
local VM_XML="${VM_DIR}/${VM_NAME}-clone.xml"
|
||||
local VM_SYSDIR="/lmn/vm"
|
||||
|
||||
if ! [[ -f "$VM_SYSDIR/${VM_NAME}.xml" && -f "$VM_SYSDIR/${VM_NAME}.qcow2" ]] && ! [[ -f "${VM_DIR}/${VM_NAME}.xml" && -f "${VM_DIR}/${VM_NAME}.qcow2" ]]; then
|
||||
echo "xml or qcow2 File does not exists." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create User-VM-Dir and link system VM-Images
|
||||
[[ -d "${VM_DIR}" ]] || mkdir -p "${VM_DIR}"
|
||||
sudo /usr/local/bin/link-images.sh
|
||||
|
||||
# Create backing file
|
||||
cd "${VM_DIR}"
|
||||
qemu-img create -f qcow2 -F qcow2 -b "${VM_NAME}.qcow2" "${VM_NAME}-clone.qcow2"
|
||||
|
||||
# Create machine-definition-file
|
||||
cp "${VM_DIR}/${VM_NAME}.xml" "${VM_XML}"
|
||||
|
||||
# set VM_DIR:
|
||||
sed -i "s:VMIMAGEDIR:${VM_DIR}:" "${VM_XML}"
|
||||
# and actually rename the vm (this also updates part of the storage path):
|
||||
sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "${VM_XML}"
|
||||
# set virtiofs-Socket
|
||||
sed -i "s:VIRTIOFSSOCKET:/run/virtiofs/${VM_NAME}-clone.sock:" "${VM_XML}"
|
||||
}
|
||||
|
||||
QEMU='qemu:///session'
|
||||
|
||||
NEWCLONE=0
|
||||
|
||||
while getopts ':ns' OPTION; do
|
||||
case "$OPTION" in
|
||||
n)
|
||||
NEWCLONE=1
|
||||
;;
|
||||
s)
|
||||
QEMU='qemu:///system'
|
||||
;;
|
||||
?)
|
||||
show_help
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift "$((OPTIND -1))"
|
||||
|
||||
# if less than one arguments supplied, display usage
|
||||
if [[ $# -ne 1 ]] ; then
|
||||
show_help
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VM_NAME=$1
|
||||
VM_DIR="/tmp/${UID}/vm"
|
||||
|
||||
# check, if we have to start squid
|
||||
if ! killall -s 0 squid; then
|
||||
|
@ -114,7 +142,7 @@ if ! virsh --connect="${QEMU}" list | grep "${VM_NAME}-clone"; then
|
|||
echo "VM not yet running."
|
||||
check_images
|
||||
if [[ "${NEWCLONE}" = 1 ]] || [[ ! -f "${VM_DIR}/${VM_NAME}-clone.qcow2" ]]; then
|
||||
create-clone.sh "${VM_NAME}"
|
||||
create-clone "${VM_NAME}"
|
||||
fi
|
||||
# delete the old vm
|
||||
virsh --connect=qemu:///session undefine "${VM_NAME}-clone" || echo "${VM_NAME}-clone did not exist"
|
||||
|
|
|
@ -13,4 +13,4 @@ VM_NAME="$1"
|
|||
## Make sure VMs can read the base directory:
|
||||
chgrp 1010 "/lmn/media/${SUDO_USER}"
|
||||
|
||||
systemctl restart virtiofs@${VM_NAME}-clone:${SUDO_USER}.service
|
||||
systemctl start virtiofs@${VM_NAME}-clone:${SUDO_USER}.service
|
||||
|
|
|
@ -4,10 +4,11 @@ Description=share home folders via virtiofsd
|
|||
|
||||
[Service]
|
||||
Type=exec
|
||||
ExecStart=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; exec /usr/lib/qemu/virtiofsd --socket-path="/run/virtiofsd/$${soc}.sock" -o source="/lmn/media/$${usr}"'
|
||||
ExecStartPost=chmod 755 /run/virtiofsd/
|
||||
ExecStartPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; chown "$$usr" "/run/virtiofsd/$${soc}.sock"'
|
||||
ExecStopPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; rm "/run/virtiofsd/$${soc}.sock"'
|
||||
ExecStartPre=/usr/bin/mkdir -p /run/virtiofs/
|
||||
ExecStart=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; exec /usr/lib/qemu/virtiofsd --socket-path="/run/virtiofs/$${soc}.sock" -o source="/lmn/media/$${usr}"'
|
||||
ExecStartPost=/usr/bin/chmod 755 /run/virtiofs/
|
||||
ExecStartPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; usr="$${v##*:}"; chown "$$usr" "/run/virtiofs/$${soc}.sock"'
|
||||
ExecStopPost=/usr/bin/sh -c 'v="%i"; soc="$${v%%:*}"; rm "/run/virtiofs/$${soc}.sock"'
|
||||
SuccessExitStatus=1
|
||||
|
||||
[Install]
|
||||
|
|
|
@ -114,7 +114,6 @@
|
|||
- mounthome.sh
|
||||
- create-vm.sh
|
||||
- rebase-vm.sh
|
||||
- create-clone.sh
|
||||
- run-vm.sh
|
||||
- upload-vm.sh
|
||||
- sync-vm.sh
|
||||
|
|
Loading…
Add table
Reference in a new issue