Implement VM configuration and deploy the images.
This commit is contained in:
parent
396a91fb40
commit
18928f2818
14 changed files with 315 additions and 9 deletions
37
roles/lmn_vm/files/create-clone.sh
Executable file
37
roles/lmn_vm/files/create-clone.sh
Executable file
|
|
@ -0,0 +1,37 @@
|
|||
#!/usr/bin/bash
|
||||
# create VM clone
|
||||
|
||||
set -eu
|
||||
|
||||
# if less than one arguments supplied, display usage
|
||||
if [ "$#" -ne 1 ]
|
||||
then
|
||||
echo "This script takes as input the name of the VM to clone"
|
||||
echo "Usage: $0 vm_name"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# change to image-directory
|
||||
cd /var/lib/libvirt/images
|
||||
|
||||
VM_NAME=$1
|
||||
|
||||
if [ ! -f "xml/${VM_NAME}.xml" -a -f "${VM_NAME}.gcow2" ]; then
|
||||
echo "xml or qcow2 File does not exists."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
qemu-img create -f qcow2 -F qcow2 -b "${VM_NAME}.qcow2" "${VM_NAME}-clone.qcow2"
|
||||
|
||||
cp "xml/${VM_NAME}.xml" "xml/${VM_NAME}-clone.xml"
|
||||
|
||||
# and actually rename the vm: (this also updates the storage path)
|
||||
sed -i "s/${VM_NAME}/${VM_NAME}-clone/" "xml/${VM_NAME}-clone.xml"
|
||||
|
||||
# set Username for mounting data from correct user
|
||||
sed -i "s/USER/${SUDO_USER}/" "xml/${VM_NAME}-clone.xml"
|
||||
|
||||
# delete the old vm
|
||||
virsh --connect=qemu:///system undefine "${VM_NAME}-clone" || echo "${VM_NAME}-clone did not exist"
|
||||
# finally, create the new vm
|
||||
virsh --connect=qemu:///system define "xml/${VM_NAME}-clone.xml"
|
||||
34
roles/lmn_vm/files/create-vm.sh
Executable file
34
roles/lmn_vm/files/create-vm.sh
Executable file
|
|
@ -0,0 +1,34 @@
|
|||
#!/usr/bin/bash
|
||||
# create 1st level-Clones
|
||||
|
||||
set -eu
|
||||
|
||||
# if less than two arguments supplied, display usage
|
||||
if [ $# -ne 2 ]
|
||||
then
|
||||
echo "This script takes as input the name of the VM to clone"
|
||||
echo "Usage: $0 vm_name_orig vm_name_clone"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VM_NAME=$1
|
||||
VM_CLONE=$2
|
||||
|
||||
if [ ! -f "xml/$VM_NAME.xml" -a -f "$VM_NAME.gcow2" ]; then
|
||||
echo "xml or qcow2 File does not exists."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
qemu-img create -f qcow2 -F qcow2 -b $VM_NAME.qcow2 $VM_NAME-$VM_CLONE.qcow2
|
||||
chmod a-w $VM_NAME-$VM_CLONE.qcow2
|
||||
|
||||
# virsh --connect=qemu:///system dumpxml $VM_NAME > xml/$VM_NAME_$VM_CLONE.xml
|
||||
cp xml/$VM_NAME.xml xml/$VM_NAME-$VM_CLONE.xml
|
||||
|
||||
# hardware addresses need to be removed, libvirt will assign
|
||||
# new addresses automatically
|
||||
sed -i /uuid/d xml/$VM_NAME-$VM_CLONE.xml
|
||||
sed -i '/mac address/d' xml/$VM_NAME-$VM_CLONE.xml
|
||||
|
||||
# and actually rename the vm: (this also updates the storage path)
|
||||
sed -i s/$VM_NAME/$VM_NAME-$VM_CLONE/ xml/$VM_NAME-$VM_CLONE.xml
|
||||
5
roles/lmn_vm/files/images.list
Normal file
5
roles/lmn_vm/files/images.list
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
win10.qcow2
|
||||
win10-SolidWorks.qcow2
|
||||
win10-Elektro.qcow2
|
||||
deb11.qcow2
|
||||
deb11-virtualbox.qcow2
|
||||
3
roles/lmn_vm/files/lmn-create-clone-sudo
Normal file
3
roles/lmn_vm/files/lmn-create-clone-sudo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
%examusers ALL=(root) NOPASSWD: /usr/local/bin/create-clone.sh
|
||||
%role-student ALL=(root) NOPASSWD: /usr/local/bin/create-clone.sh
|
||||
%role-teacher ALL=(root) NOPASSWD: /usr/local/bin/create-clone.sh
|
||||
3
roles/lmn_vm/files/lmn-mounthome-sudo
Normal file
3
roles/lmn_vm/files/lmn-mounthome-sudo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
%examusers ALL=(root) NOPASSWD: /usr/local/bin/mounthome.sh
|
||||
%role-student ALL=(root) NOPASSWD: /usr/local/bin/mounthome.sh
|
||||
%role-teacher ALL=(root) NOPASSWD: /usr/local/bin/mounthome.sh
|
||||
3
roles/lmn_vm/files/lmn-proxy.sh
Executable file
3
roles/lmn_vm/files/lmn-proxy.sh
Executable file
|
|
@ -0,0 +1,3 @@
|
|||
export http_proxy='http://firewall:3128'
|
||||
export https_proxy=$http_proxy
|
||||
export ftp_proxy=$http_proxy
|
||||
15
roles/lmn_vm/files/mounthome.sh
Executable file
15
roles/lmn_vm/files/mounthome.sh
Executable file
|
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/bash
|
||||
set -eu
|
||||
|
||||
share="$(getent passwd "$SUDO_UID" | cut -d : -f 6 | sed 's/.*default-school//')"
|
||||
|
||||
if [ "$#" -gt 0 ] && [ "$1" = '-u' ]; then
|
||||
umount "/media/${SUDO_USER}/home" && rmdir "/media/${SUDO_USER}/home"
|
||||
else
|
||||
chgrp 1010 "/media/${SUDO_USER}"
|
||||
chmod 0770 "/media/${SUDO_USER}"
|
||||
mkdir -p "/media/${SUDO_USER}/home"
|
||||
mount -t cifs -o "sec=krb5i,cruid=${SUDO_UID},username=${SUDO_USER},uid=${SUDO_UID},\
|
||||
gid=1010,file_mode=0770,dir_mode=0770,forceuid,forcegid,user=${SUDO_USER}" \
|
||||
"//server/default-school/${share}" "/media/${SUDO_USER}/home"
|
||||
fi
|
||||
2
roles/lmn_vm/files/pulseaudio-override.conf
Normal file
2
roles/lmn_vm/files/pulseaudio-override.conf
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[Service]
|
||||
Environment=HOME=/tmp/pulse.%u
|
||||
44
roles/lmn_vm/files/rebase-vm.sh
Executable file
44
roles/lmn_vm/files/rebase-vm.sh
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
#!/usr/bin/bash
|
||||
|
||||
# Rebase one level down
|
||||
|
||||
set -eu
|
||||
|
||||
# if less than one arguments supplied, display usage
|
||||
if [ $# -ne 1 ]
|
||||
then
|
||||
echo "This script takes as input the name of the VM to rebase one level down"
|
||||
echo "Usage: $0 vm_name"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
VM_NAME=$1
|
||||
|
||||
if [ ! -f $VM_NAME.qcow2 ]
|
||||
then
|
||||
echo "File not found $VM_NAME.qcow2"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
shopt -s lastpipe
|
||||
qemu-img info --backing-chain $VM_NAME.qcow2 | grep image | wc -l | read NUMBASES
|
||||
qemu-img info --backing-chain $VM_NAME.qcow2 | grep image | head -n 3 | tail -n 1 | cut -d' ' -f2 | read NEWBASE
|
||||
qemu-img info --backing-chain $VM_NAME.qcow2 | grep image | head -n 2 | tail -n 1 | cut -d' ' -f2 | read CURRENTBASE
|
||||
|
||||
if [ ! $NUMBASES -ge 3 ]
|
||||
then
|
||||
echo "Image must have at least 2 backing-files"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f $NEWBASE -a -f $CURRENTBASE ]
|
||||
then
|
||||
echo "Backingfiles not found $CURRENTBASE, $NEWBASE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
qemu-img rebase -f qcow2 -b $NEWBASE -F qcow2 $VM_NAME.qcow2
|
||||
rm $CURRENTBASE
|
||||
mv $VM_NAME.qcow2 $CURRENTBASE
|
||||
chmod a-w $CURRENTBASE
|
||||
qemu-img create -f qcow2 -F qcow2 -b $CURRENTBASE $VM_NAME.qcow2
|
||||
67
roles/lmn_vm/files/run-vm.sh
Executable file
67
roles/lmn_vm/files/run-vm.sh
Executable file
|
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/bash
|
||||
# create and run clone
|
||||
|
||||
set -eu
|
||||
|
||||
NEWCLONE=0
|
||||
|
||||
show_help() {
|
||||
cat << EOF
|
||||
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
|
||||
EOF
|
||||
}
|
||||
|
||||
NEWCLONE=0
|
||||
|
||||
while getopts ':n' OPTION; do
|
||||
case "$OPTION" in
|
||||
n)
|
||||
NEWCLONE=1
|
||||
;;
|
||||
?)
|
||||
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
|
||||
|
||||
if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" ]]; then
|
||||
echo "no base VM disk '${VM_NAME}.qcow2' found"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# check, if we have to start squid
|
||||
if [[ ! -f "/tmp/squid.pid" ]]; then
|
||||
echo "starting squid."
|
||||
/usr/sbin/squid -f /etc/squid/squid-usermode.conf
|
||||
fi
|
||||
|
||||
# check, if we have to mount home
|
||||
if [[ ! -d "/media/${USER}/home" ]]; then
|
||||
echo "mounting home."
|
||||
sudo mounthome.sh
|
||||
fi
|
||||
|
||||
if ! virsh --connect=qemu:///system list | grep "${VM_NAME}"; 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}"
|
||||
fi
|
||||
virsh --connect=qemu:///system start "${VM_NAME}-clone"
|
||||
fi
|
||||
echo "starting viewer"
|
||||
virt-viewer --connect=qemu:///system --full-screen "${VM_NAME}-clone"
|
||||
Loading…
Add table
Add a link
Reference in a new issue