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
|
@ -1,44 +1,79 @@
|
|||
#!/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"
|
||||
show_help() {
|
||||
cat << EOF
|
||||
Usage: $(basename "$0") [-n newname] vmname"
|
||||
This script takes as input the name of the VM to rebase one level down
|
||||
-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
|
||||
fi
|
||||
|
||||
VM_NAME=$1
|
||||
# change to Images directory
|
||||
cd /var/lib/libvirt/images
|
||||
|
||||
if [ ! -f $VM_NAME.qcow2 ]
|
||||
then
|
||||
echo "File not found $VM_NAME.qcow2"
|
||||
VM_NAME="$1"
|
||||
|
||||
# check if VM-Diskimage exists
|
||||
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
|
||||
# check if new VM-Diskimage exists
|
||||
if [[ -v NEWNAME ]] && [[ -f "${NEWNAME}.qcow2" ]]; then
|
||||
echo "New Base already exists: ${NEWNAME}.qcow2"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! $NUMBASES -ge 3 ]
|
||||
then
|
||||
NUMBASES=$(qemu-img info --backing-chain "${VM_NAME}.qcow2" | grep -c image)
|
||||
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"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ ! -f $NEWBASE -a -f $CURRENTBASE ]
|
||||
then
|
||||
echo "Backingfiles not found $CURRENTBASE, $NEWBASE"
|
||||
# check if base Diskimage exists
|
||||
if [[ ! -f "${NEWBASE}" ]] || [[ ! -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
|
||||
# rebasing disk image
|
||||
qemu-img rebase -f qcow2 -b "${NEWBASE}" -F qcow2 "${VM_NAME}.qcow2"
|
||||
if [[ -v NEWNAME ]]; then
|
||||
# copy and adapt machine definition file
|
||||
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}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue