You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.6 KiB
111 lines
2.6 KiB
#!/bin/bash |
|
|
|
XML_SAVE_PATH=/datas/git/computers/libvirt-xml |
|
URI=qemu:///system |
|
|
|
NO_ARGS=0 |
|
E_OPTERROR=85 |
|
|
|
if [ $# -eq "$NO_ARGS" ] # Script invoked with no command-line args? |
|
then |
|
echo "Usage: $(basename "${0}") options (-dpe)" |
|
exit $E_OPTERROR |
|
fi |
|
|
|
usage() { |
|
cat <<EOF |
|
automate some libvirt operations: |
|
general: |
|
-h print this message |
|
-d <domain> domain to operate on |
|
-p <pool> migrate to specified pool |
|
-a <disk> add second img disk to vm |
|
-l |
|
EOF |
|
} |
|
|
|
if [[ $1 == '-h' || $1 == '--help' || $1 == '-?' ]]; then |
|
usage |
|
exit 0 |
|
fi |
|
|
|
# Read args |
|
while getopts "ld:p:a:" opt |
|
do |
|
case $opt in |
|
l) |
|
virsh --connect=${URI} list --all |
|
virsh --connect=${URI} pool-list |
|
exit 0 |
|
;; |
|
d) |
|
domain=$OPTARG |
|
;; |
|
p) |
|
pool=$OPTARG |
|
;; |
|
a) |
|
disk=$OPTARG |
|
;; |
|
*) |
|
echo "Option -$OPTARG requires an argument" |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
|
|
echo "domain=${domain}" |
|
echo "pool=${pool}" |
|
echo "disk=${disk}" |
|
|
|
move_domain() |
|
{ |
|
echo "=== move_domain ===" |
|
# extract domain |
|
xmlsource=${XML_SAVE_PATH}/$(date +%Y%m%d%H%M%S)_${domain}.xml |
|
virsh --connect=${URI} dumpxml "${domain}" > "${xmlsource}" |
|
sourcepath=$(xmlstarlet sel --text --template --value-of "//domain/devices/disk[@device='disk']/source/@file" "${xmlsource}") |
|
# extract pool |
|
poolpath=$(virsh --connect=${URI} pool-dumpxml --pool "${pool}" |grep path |cut -d'>' -f 2 |cut -d'<' -f 1) |
|
# wait |
|
sleep 2 |
|
# compute new xml |
|
xmldestination=${XML_SAVE_PATH}/$(date +%Y%m%d%H%M%S)_${domain}.xml |
|
echo "replace ${sourcepath} by ${poolpath}/${domain}.qcow2" |
|
xmlstarlet ed -u "//domain/devices/disk[@device='disk']/source/@file" -v "${poolpath}/${domain}.qcow2" "${xmlsource}" > "${xmldestination}" |
|
#if [ $(xmlstarlet val --quiet ${xmldestination}) -ne 0 ] |
|
#then |
|
# echo "xml failure" |
|
# exit 1 |
|
#fi |
|
# move disk |
|
mv -i "${sourcepath}" "/${poolpath}/${domain}.qcow2" |
|
# remove old |
|
virsh --connect=${URI} undefine --domain "${domain}" |
|
# define new |
|
virsh --connect=${URI} define --validate --file "${xmldestination}" |
|
} |
|
|
|
if [[ ! ${domain} =~ ^$ ]] |
|
then |
|
echo "=== check domain ===" |
|
if ! virsh --connect=${URI} list --all |grep " ${domain} " |
|
then |
|
echo "domain does not exist :" |
|
virsh --connect=${URI} list --all |
|
exit 1 |
|
else |
|
if [[ ! ${pool} =~ ^$ ]] |
|
then |
|
echo "=== check pool ===" |
|
if ! virsh --connect=${URI} pool-list |grep " ${pool} " |
|
then |
|
echo "pool does not exist :" |
|
virsh --connect=${URI} pool-list |
|
exit 1 |
|
else |
|
move_domain "${domain}" "${pool}" |
|
fi |
|
fi |
|
fi |
|
fi
|
|
|