shell syntax optimized and more options for rebase-vm.sh
This commit is contained in:
parent
c1ff7319f4
commit
354075f530
7 changed files with 79 additions and 40 deletions
|
@ -3,9 +3,8 @@
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
# if less than one arguments supplied, display usage
|
# if less or more than one arguments supplied, display usage
|
||||||
if [ "$#" -ne 1 ]
|
if [[ $# -ne 1 ]]; then
|
||||||
then
|
|
||||||
echo "This script takes as input the name of the VM to clone"
|
echo "This script takes as input the name of the VM to clone"
|
||||||
echo "Usage: $0 vm_name"
|
echo "Usage: $0 vm_name"
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -16,7 +15,7 @@ cd /var/lib/libvirt/images
|
||||||
|
|
||||||
VM_NAME=$1
|
VM_NAME=$1
|
||||||
|
|
||||||
if [ ! -f "xml/${VM_NAME}.xml" -a -f "${VM_NAME}.gcow2" ]; then
|
if [[ ! -f "xml/${VM_NAME}.xml" ]] || [[ ! -f "${VM_NAME}.gcow2" ]]; then
|
||||||
echo "xml or qcow2 File does not exists."
|
echo "xml or qcow2 File does not exists."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -2,10 +2,8 @@
|
||||||
# create 1st level-Clones
|
# create 1st level-Clones
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
# if less than two arguments supplied, display usage
|
# if less than two arguments supplied, display usage
|
||||||
if [ $# -ne 2 ]
|
if [[ $# -ne 2 ]]; then
|
||||||
then
|
|
||||||
echo "This script takes as input the name of the VM to clone"
|
echo "This script takes as input the name of the VM to clone"
|
||||||
echo "Usage: $0 vm_name_orig vm_name_clone"
|
echo "Usage: $0 vm_name_orig vm_name_clone"
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -14,21 +12,24 @@ fi
|
||||||
VM_NAME=$1
|
VM_NAME=$1
|
||||||
VM_CLONE=$2
|
VM_CLONE=$2
|
||||||
|
|
||||||
if [ ! -f "xml/$VM_NAME.xml" -a -f "$VM_NAME.gcow2" ]; then
|
# change to image-directory
|
||||||
|
cd /var/lib/libvirt/images
|
||||||
|
|
||||||
|
if [[ ! -f "xml/${VM_NAME}.xml" ]] || [[ ! -f "${VM_NAME}.gcow2" ]]; then
|
||||||
echo "xml or qcow2 File does not exists."
|
echo "xml or qcow2 File does not exists."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
qemu-img create -f qcow2 -F qcow2 -b $VM_NAME.qcow2 $VM_NAME-$VM_CLONE.qcow2
|
qemu-img create -f qcow2 -F qcow2 -b "${VM_NAME}.qcow2" "${VM_NAME}-${VM_CLONE}.qcow2"
|
||||||
chmod a-w $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
|
# virsh --connect=qemu:///system dumpxml "${VM_NAME}" > "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||||
cp xml/$VM_NAME.xml 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
|
# hardware addresses need to be removed, libvirt will assign
|
||||||
# new addresses automatically
|
# new addresses automatically
|
||||||
sed -i /uuid/d xml/$VM_NAME-$VM_CLONE.xml
|
sed -i /uuid/d "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||||
sed -i '/mac address/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)
|
# 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
|
sed -i "s/${VM_NAME}/${VM_NAME}-${VM_CLONE}/" "xml/${VM_NAME}-${VM_CLONE}.xml"
|
||||||
|
|
1
roles/lmn_vm/files/lmn-create-vm-sudo
Normal file
1
roles/lmn_vm/files/lmn-create-vm-sudo
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%role-teacher ALL=(root) NOPASSWD: /usr/local/bin/create-vm.sh
|
1
roles/lmn_vm/files/lmn-rebase-vm-sudo
Normal file
1
roles/lmn_vm/files/lmn-rebase-vm-sudo
Normal file
|
@ -0,0 +1 @@
|
||||||
|
%role-teacher ALL=(root) NOPASSWD: /usr/local/bin/rebase-vm.sh
|
|
@ -1,44 +1,79 @@
|
||||||
#!/usr/bin/bash
|
#!/usr/bin/bash
|
||||||
|
|
||||||
# Rebase one level down
|
# Rebase one level down
|
||||||
|
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
# if less than one arguments supplied, display usage
|
show_help() {
|
||||||
if [ $# -ne 1 ]
|
cat << EOF
|
||||||
then
|
Usage: $(basename "$0") [-n newname] vmname"
|
||||||
echo "This script takes as input the name of the VM to rebase one level down"
|
This script takes as input the name of the VM to rebase one level down
|
||||||
echo "Usage: $0 vm_name"
|
-n new name of the rebased image
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
while getopts ':n:' OPTION; do
|
||||||
|
case "$OPTION" in
|
||||||
|
n)
|
||||||
|
NEWNAME=$OPTARG
|
||||||
|
;;
|
||||||
|
?)
|
||||||
|
show_help
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
shift "$((OPTIND -1))"
|
||||||
|
|
||||||
|
# if less or more than one arguments supplied, display usage
|
||||||
|
if [[ $# -ne 1 ]]; then
|
||||||
|
show_help
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
VM_NAME=$1
|
# change to Images directory
|
||||||
|
cd /var/lib/libvirt/images
|
||||||
|
|
||||||
if [ ! -f $VM_NAME.qcow2 ]
|
VM_NAME="$1"
|
||||||
then
|
|
||||||
echo "File not found $VM_NAME.qcow2"
|
# check if VM-Diskimage exists
|
||||||
|
if [[ ! -f "${VM_NAME}.qcow2" ]]; then
|
||||||
|
echo "File not found ${VM_NAME}.qcow2"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
shopt -s lastpipe
|
# check if new VM-Diskimage exists
|
||||||
qemu-img info --backing-chain $VM_NAME.qcow2 | grep image | wc -l | read NUMBASES
|
if [[ -v NEWNAME ]] && [[ -f "${NEWNAME}.qcow2" ]]; then
|
||||||
qemu-img info --backing-chain $VM_NAME.qcow2 | grep image | head -n 3 | tail -n 1 | cut -d' ' -f2 | read NEWBASE
|
echo "New Base already exists: ${NEWNAME}.qcow2"
|
||||||
qemu-img info --backing-chain $VM_NAME.qcow2 | grep image | head -n 2 | tail -n 1 | cut -d' ' -f2 | read CURRENTBASE
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [ ! $NUMBASES -ge 3 ]
|
NUMBASES=$(qemu-img info --backing-chain "${VM_NAME}.qcow2" | grep -c image)
|
||||||
then
|
NEWBASE=$(qemu-img info --backing-chain "${VM_NAME}.qcow2" | grep image | head -n 3 | tail -n 1 | cut -d' ' -f2)
|
||||||
|
CURRENTBASE=$(qemu-img info --backing-chain "${VM_NAME}.qcow2" | grep image | head -n 2 | tail -n 1 | cut -d' ' -f2)
|
||||||
|
|
||||||
|
if [[ ! "${NUMBASES}" -ge 3 ]]; then
|
||||||
echo "Image must have at least 2 backing-files"
|
echo "Image must have at least 2 backing-files"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -f $NEWBASE -a -f $CURRENTBASE ]
|
# check if base Diskimage exists
|
||||||
then
|
if [[ ! -f "${NEWBASE}" ]] || [[ ! -f "${CURRENTBASE}" ]]; then
|
||||||
echo "Backingfiles not found $CURRENTBASE, $NEWBASE"
|
echo "Backingfiles not found ${CURRENTBASE}, ${NEWBASE}"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
qemu-img rebase -f qcow2 -b $NEWBASE -F qcow2 $VM_NAME.qcow2
|
# rebasing disk image
|
||||||
rm $CURRENTBASE
|
qemu-img rebase -f qcow2 -b "${NEWBASE}" -F qcow2 "${VM_NAME}.qcow2"
|
||||||
mv $VM_NAME.qcow2 $CURRENTBASE
|
if [[ -v NEWNAME ]]; then
|
||||||
chmod a-w $CURRENTBASE
|
# copy and adapt machine definition file
|
||||||
qemu-img create -f qcow2 -F qcow2 -b $CURRENTBASE $VM_NAME.qcow2
|
CURRENTNAME="${CURRENTBASE/.qcow2/}"
|
||||||
|
cp "xml/${CURRENTNAME}.xml" "xml/${NEWNAME}.xml"
|
||||||
|
sed -i "s/${CURRENTNAME}/${NEWNAME}/" "xml/${NEWNAME}.xml"
|
||||||
|
NEWNAME="${NEWNAME}.qcow2"
|
||||||
|
else
|
||||||
|
rm "${CURRENTBASE}"
|
||||||
|
NEWNAME="${CURRENTBASE}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
mv "${VM_NAME}.qcow2" "${NEWNAME}"
|
||||||
|
chmod a-w "${NEWNAME}"
|
||||||
|
|
0
roles/lmn_vm/files/sync-vm.sh
Normal file → Executable file
0
roles/lmn_vm/files/sync-vm.sh
Normal file → Executable file
|
@ -61,6 +61,8 @@
|
||||||
- lmn-mounthome-sudo
|
- lmn-mounthome-sudo
|
||||||
- lmn-create-clone-sudo
|
- lmn-create-clone-sudo
|
||||||
- lmn-sync-vm-sudo
|
- lmn-sync-vm-sudo
|
||||||
|
- lmn-create-vm-sudo
|
||||||
|
- lmn-rebase-vm-sudo
|
||||||
|
|
||||||
- name: deploy mount home script
|
- name: deploy mount home script
|
||||||
copy:
|
copy:
|
||||||
|
|
Loading…
Add table
Reference in a new issue