3 changed files with 186 additions and 0 deletions
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
#!/bin/bash |
||||
|
||||
XML_SAVE_PATH=/datas/git/computers/libvirt-xml |
||||
XML_POOL_SAVE_PATH=/datas/git/computers/libvirt-pool-xml |
||||
URI=qemu:///system |
||||
|
||||
echo "=== dump pools ===" |
||||
virsh --connect=qemu:///system pool-list |
||||
|
||||
for pool in $(virsh pool-list |awk 'NF=1{print $NF}' |tail +3 |grep -v ^$) |
||||
do |
||||
virsh pool-dumpxml ${pool} > ${XML_POOL_SAVE_PATH}/$(date +%Y%m%d%H%M%S)_${pool}.xml |
||||
done |
||||
|
||||
echo "=== dump domains ===" |
||||
virsh --connect=qemu:///system list --all |
||||
|
||||
for domain in $(virsh list --all |awk 'NF=2{print $NF}' |tail +3 |grep -v ^$) |
||||
do |
||||
virsh dumpxml ${domain} > ${XML_SAVE_PATH}/$(date +%Y%m%d%H%M%S)_${domain}.xml |
||||
done |
||||
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
#!/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 |
||||
@ -0,0 +1,54 @@
@@ -0,0 +1,54 @@
|
||||
#!/bin/bash |
||||
|
||||
connection=$1 |
||||
host=${connection##*@} |
||||
#user=${connection%%@*} |
||||
domain=${host} |
||||
vip=0.0.0.0 |
||||
|
||||
if ! virsh --connect=qemu:///system list --all |grep " ${domain} " |
||||
then |
||||
echo "not a existing domain" |
||||
exit 1 |
||||
fi |
||||
|
||||
if ! virsh --connect=qemu:///system list |grep " ${domain} " |
||||
then |
||||
echo "domain is not started" |
||||
echo "virsh --connect=qemu:///system start ${domain}" |
||||
exit 1 |
||||
fi |
||||
|
||||
if ! virsh --connect=qemu:///system guestinfo "${domain}" --interface |
||||
then |
||||
echo "No guest agent on this guest. let's try to find host in dhcp leases." |
||||
if virsh --connect=qemu:///system net-dhcp-leases default |grep "${domain}" |
||||
then |
||||
virsh --connect=qemu:///system net-dhcp-leases default |grep "${domain}" |cut -c '50-80' |sed -e 's/ //g' |cut -d'/' -f 1 |
||||
vip=$(virsh --connect=qemu:///system net-dhcp-leases default |grep "${domain}" |cut -c '50-80' |sed -e 's/ //g' |cut -d'/' -f 1) |
||||
else |
||||
echo "no ip. script error." |
||||
exit 1 |
||||
fi |
||||
else |
||||
virsh --connect=qemu:///system guestinfo "${domain}" --interface |grep 'if.1.addr.0.addr' |cut -d':' -f 2 |
||||
vip=$(virsh --connect=qemu:///system guestinfo "${domain}" --interface |grep 'if.1.addr.0.addr' |cut -d':' -f 2) |
||||
fi |
||||
|
||||
tee ~/.ssh/config.d/"${domain}" <<EOF |
||||
Host ${domain} |
||||
Hostname ${vip} |
||||
EOF |
||||
|
||||
chmod 640 ~/.ssh/config.d/"${domain}" |
||||
|
||||
# local ansible inventory |
||||
if [ -d /datas/git/ansible/inventory/ ] |
||||
then |
||||
tee /datas/git/ansible/inventory/"${domain}" <<EOF |
||||
[${domain}] |
||||
${vip} |
||||
EOF |
||||
fi |
||||
|
||||
ssh "${connection}" |
||||
Loading…
Reference in new issue