Updated libs/virtualization.bash

This commit is contained in:
Xavier Logerais (Numericable - Ingénierie Système) 2014-08-29 14:36:28 +02:00
parent 34f4933dc2
commit c671ab5e1a

View File

@ -36,23 +36,36 @@ vm_create() {
echo "Creating VM with virt-install" echo "Creating VM with virt-install"
virt-install --connect=qemu+ssh://${USER}@${hypervisor}/system \ virt-install --connect=qemu+ssh://${USER}@${hypervisor}/system \
--name=${name} \ --name=${name} \
--description="${name}" \ --description="${name}" \
--cpu=host \ --cpu=host \
--ram=${ramsize} \ --ram=${ramsize} \
--disk=/dev/lvm_guests_system/${name},size=${disksize},bus=virtio,cache=writethrough,io=native \ --disk=/dev/lvm_guests_system/${name},size=${disksize},bus=virtio,cache=writethrough,io=native \
--network=bridge=br192,mac=${mac},model=virtio \ --network=bridge=br192,mac=${mac},model=virtio \
--os-type=linux \ --os-type=linux \
--os-variant=debiansqueeze \ --os-variant=debiansqueeze \
--boot=network,hd,menu=on \ --boot=network,hd,menu=on \
--pxe \ --pxe \
--graphics=vnc \ --graphics=vnc \
--noautoconsole --noautoconsole
echo "Done" echo "Done"
} }
vm_delete() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name"; return 1; fi
hypervisor=${1}
name=${2}
echo "Deleting VM ${name}"
virsh destroy ${name}
virsh undefine ${name}
virsh vol-delete --pool default ${name}
virsh vol-delete --pool lvm_guests_system ${name}
virsh vol-delete --pool lvm_guests_data ${name}
}
vm_start() { vm_start() {
if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name"; return 1; fi if [ $# -ne 2 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name"; return 1; fi
@ -121,7 +134,7 @@ vm_change_bridge() {
vm_add_data_disk() { vm_add_data_disk() {
if [ $# -ne 3 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name size (in Gb)"; return 1; fi if [ $# -ne 3 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name size (in Gb)"; return 1; fi
hypervisor=${1} hypervisor=${1}
name=${2} name=${2}
size=$(( ${3} * 1024 * 1024 * 1024 )) size=$(( ${3} * 1024 * 1024 * 1024 ))
@ -150,33 +163,106 @@ vm_create_magick() {
echo " * mac = ${mac}" echo " * mac = ${mac}"
echo " * ip = ${ip}" echo " * ip = ${ip}"
virt-install --connect=qemu+ssh://${USER}@${hypervisor}/system \ virt-install --connect=qemu+ssh://${USER}@${hypervisor}/system \
--name=${name} \ --name=${name} \
--description="${name}" \ --description="${name}" \
--cpu=host \ --cpu=host \
--ram=${ramsize} \ --ram=${ramsize} \
--disk=/dev/lvm_guests_system/${name},size=${disksize},bus=virtio,cache=writethrough,io=native \ --disk=/dev/lvm_guests_system/${name},size=${disksize},bus=virtio,cache=writethrough,io=native \
--network=bridge=br192,mac=${mac},model=virtio \ --network=bridge=br192,mac=${mac},model=virtio \
--os-type=linux \ --os-type=linux \
--os-variant=debianwheezy \ --os-variant=debianwheezy \
--boot=network,hd,menu=on \ --boot=network,hd,menu=on \
--pxe \ --pxe \
--graphics=vnc \ --graphics=vnc \
--noautoconsole --noautoconsole
echo "Done" echo "Done"
} }
vm_create_magick2() {
if [ $# -ne 3 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name vm_template"; return 1; fi
hypervisor=${1}
name=${2}
template=${3}
# Define ressources based on template
case $template in
small)
vcpus="1,maxvcpus=2"
memory="1024,maxmemory=2048"
disk="/dev/lvm_guests_system/${name},size=10,bus=virtio,cache=writethrough,io=native"
;;
medium)
vcpus="vcpus=2,maxvcpus=4"
memory="memory=2048,maxmemory=4096"
disk="/dev/lvm_guests_system/${name},size=20,bus=virtio,cache=writethrough,io=native"
;;
large)
vcpus="vcpus=4,maxvcpus=8"
memory="memory=4096,maxmemory=8192"
disk="/dev/lvm_guests_system/${name},size=40,bus=virtio,cache=writethrough,io=native"
;;
esac
# Get ip and compute MAC
IFS='.' read -a array <<< "$(ssh admin dig +search +short ${name})"
ip=$(printf "%3d.%3d.%3d.%3d\n" ${array[0]} ${array[1]} ${array[2]} ${array[3]})
mac=$(printf "52:54:00:%02X:%02X:%02X\n" ${array[1]} ${array[2]} ${array[3]})
echo "Creating VM with virt-install"
echo " * mac = ${mac}"
echo " * ip = ${ip}"
virt-install --connect=qemu+ssh://${USER}@${hypervisor}/system \
--name="${name}" \
--description="${name}" \
--cpu=host \
--vcpus=${vcpus} \
--memory=${memory} \
--disk=${disk} \
--network=bridge=br192,mac=${mac},model=virtio \
--os-type=linux \
--os-variant=debianwheezy \
--boot=menu=on,useserial=on,network,hd \
--pxe \
--graphics=vnc \
--noautoconsole
echo "Done"
}
vm_change_bridge_magick() {
if [ $# -ne 3 ]; then echo "Usage : $0 hypervisor_name_or_ip vm_name vm_new_bridge"; return 1; fi
hypervisor=${1}
name=${2}
bridge=${3}
IFS='.' read -a array <<< "$(ssh admin dig +search +short ${name})"
ip=$(printf "%3d.%3d.%3d.%3d\n" ${array[0]} ${array[1]} ${array[2]} ${array[3]})
mac=$(printf "52:54:00:%02X:%02X:%02X\n" ${array[1]} ${array[2]} ${array[3]})
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system domiflist ${name} --inactive
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system detach-interface --config --domain ${name} --type bridge --mac ${mac}
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system attach-interface --config --domain ${name} --type bridge --model virtio --mac ${mac} --source ${bridge}
virsh --connect=qemu+ssh://${USER}@${hypervisor}/system domiflist ${name} --inactive
}
#### bash completion #### bash completion
_hypervisors() { _hypervisors() {
if [ -e ~/.ssh/config ]; then if [ -e ~/.ssh/config ]; then
configured_hypervisors=$(cat ~/.ssh/config | egrep -i "^\s*host\s+[a-zA-Z]" | sed -e "s/^host\s*//i" | grep -i hypervisor) configured_hypervisors=$(cat ~/.ssh/config | egrep -i "^\s*host\s+[a-zA-Z]" | sed -e "s/^host\s*//i" | grep -i hypervisor)
fi fi
if [ -e ~/.ssh/known_hosts ]; then if [ -e ~/.ssh/known_hosts ]; then
known_hypervisors=$(cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e 's/,.*//g' | grep -v "\[" | grep -i hypervisor | uniq) known_hypervisors=$(cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e 's/,.*//g' | grep -v "\[" | grep -i hypervisor | uniq)
fi fi
echo $configured_hypervisors $known_hypervisors echo $configured_hypervisors $known_hypervisors
} }
#complete -W "$(cat ~/.ssh/config | egrep -i "^\s*host\s+[a-zA-Z]" | sed -e "s/^host\s*//i" | grep -i hypervisor)" hypervisor_connect #complete -W "$(cat ~/.ssh/config | egrep -i "^\s*host\s+[a-zA-Z]" | sed -e "s/^host\s*//i" | grep -i hypervisor)" hypervisor_connect
@ -189,3 +275,5 @@ complete -W "$(_hypervisors)" vm_connect
complete -W "$(_hypervisors)" vm_change_bridge complete -W "$(_hypervisors)" vm_change_bridge
complete -W "$(_hypervisors)" vm_add_data_disk complete -W "$(_hypervisors)" vm_add_data_disk
complete -W "$(_hypervisors)" vm_create_magick complete -W "$(_hypervisors)" vm_create_magick
complete -W "$(_hypervisors)" vm_change_bridge_magick
complete -W "$(_hypervisors)" vm_change_bridge_magick2