lmn-client/roles/lmn_vm/files/rebase-vm.sh
2023-02-07 19:18:57 +01:00

44 lines
1.1 KiB
Bash
Executable file

#!/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