Hi, because I need to control the start order of VMs and set a delay between them in some circunstances, I write a little bash script to accomplish this with KVM vms in proxmox.
/usr/sbin/qmcontrol
The script replace the /usr/sbin/qm startall and stopall commands to introduce two new settings in the vm configuration, order: and delay:
You need to put this settings manually inside the configuration files you find at /etc/qemu-server.
If you miss any of them or both, the script assigns the latest order to this vms and set his delay to DEFLAY value in script.
To activate the script you need to edit the /etc/init.d/qemu-server file and change the variable PROG to use qmcontrol instead of qm
Need to look like this
I'm a newbie bash scripter, so any advice or modification will be appreciated !
/usr/sbin/qmcontrol
Code:
#!/bin/bash
#
# qmcontrol v0.03
#
# - changed Starting VM Message to something more meaningful (zextra comments)
# - added DEFLAY (default delay for VMs without delay)
# - check only for VMid.conf files (udo suggestion)
#
DEFLAY=5
PROG=/usr/sbin/qm
if [[ -z "$1" || "$1" != "startall" && "$1" != "stopall" ]]; then
echo "Usage: qmcontrol <startall>|<stopall>"
exit
fi
for file in `ls /etc/qemu-server/ | grep conf$`
do
if [ 1 -eq `cat /etc/qemu-server/$file | grep onboot: | awk '{print $2}'` ]; then
vmboot="$vmboot $file"
fi
done
for file in $vmboot
do
order=`cat /etc/qemu-server/$file | grep order: | awk '{print $2}'`
if [ -z "$order" ]; then
order="9999"
fi
vmsort="$vmsort $order $file"
done
if [ "$1" = "startall" ]; then
vmselect=`echo ${vmsort//.conf/.} | tr "." "\n" | sort -bg`
else
vmselect=`echo ${vmsort//.conf/.} | tr "." "\n" | sort -bgr`
fi
count=0
for vmid in $vmselect
do
if [ `expr $count % 2` -eq 1 ]; then
if [ "$1" = "startall" ]; then
delay=`cat /etc/qemu-server/$vmid.conf | grep delay: | awk '{print $2}'`
if [ -z $delay ]; then
delay=$DEFLAY
fi
echo "Starting VM $vmid, waiting $delay seconds to allow VM $vmid services to start"
$PROG start $vmid 2>/dev/null
sleep $delay
else
echo "Stopping VM $vmid"
$PROG shutdown $vmid 2>/dev/null
$PROG wait $vmid 2>/dev/null
fi
fi
((count++))
done
if [ "$1" = "stopall" ]; then
echo "Stopping any other VMs"
$PROG stopall 2>/dev/null
fi
The script replace the /usr/sbin/qm startall and stopall commands to introduce two new settings in the vm configuration, order: and delay:
You need to put this settings manually inside the configuration files you find at /etc/qemu-server.
Code:
ostype: other
memory: 2048
sockets: 1
name: nas
ide2: none,media=cdrom
vlan0: virtio=42:00:66:00:4C:00
bootdisk: virtio0
boot: cd
freeze: 0
cpuunits: 1000
acpi: 1
kvm: 1
virtio0: local:101/vm-101-disk-1.raw
onboot: 1
cores: 1
order: 2
delay: 30
If you miss any of them or both, the script assigns the latest order to this vms and set his delay to DEFLAY value in script.
To activate the script you need to edit the /etc/init.d/qemu-server file and change the variable PROG to use qmcontrol instead of qm
Need to look like this
Code:
...
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PROG=/usr/sbin/qmcontrol
DESC="Qemu Server"
...
I'm a newbie bash scripter, so any advice or modification will be appreciated !
Last edited: