📜 ⬆️ ⬇️

Autostart and auto power off VM in VmWare ESXi 5.0 Update 1

I decided to start implementing virtualization in one government institution. My first choice was Citrix XenServer, because it was possible to organize a software RAID1 in it (since due to the lack of a budget, a hardware RAID controller was not installed in the server), but, poking with it, I still knocked out money for a hardware RAID controller and switched to VmWare ESXi 5.0. What is good, both are free.

Everything was fine, the necessary tasks were virtualized, the virtual machines worked fine, but there was Update 1 for ESXi 5.0. After the update, the autorun and auto off functions of virtual machines stopped working. It would seem a trifle, but the food we left much to be desired, and from time to time the question arose with the automatic start of the virtual machines after the server was rebooted. Well, sometimes the server also has to be turned off, and for this it was necessary to connect the VMware vSphere Client to the hypervisor and one by one to extinguish the virtuals, which was very lazy.

Having rummaged through the network, a way was found to turn on the VM when starting the server by writing a script and calling it from /etc/rc.local .

But I wanted to, that the shutdown would work correctly, the edits in /etc/inittab did not help. Editing files like /sbin/shutdown.sh and /sbin/vmware-autostart.sh did not bring /sbin/vmware-autostart.sh benefit, since It turned out that the entire environment of the hypervisor is loaded into the RAM from the images and lives there.
')
It was decided to make changes to the very image of the hypervisor.

Especially for these purposes, autorun and auto-shutdown scripts were written, which take information about automatically started and shut down machines from the hypervisor itself, i.e. having made changes in the configuration in the “Virtual Machines Startup / Shutdown” section, the scripts will correctly process them (unless the order is not observed, that is, it works on the principle of “Any Order”).

1) autostart.sh:

#!/bin/sh

sleep_time=`/bin/vim-cmd hostsvc/autostartmanager/get_defaults | grep startDelay | sed "s/ //g" | sed "s/,//g" | awk 'FS="=" {print $2}'`

for i in `/bin/vim-cmd hostsvc/autostartmanager/get_autostartseq | grep vim.VirtualMachine | sed "s/',//g" | awk 'FS=":" {print $2}'`; do
state=`/bin/vim-cmd vmsvc/power.getstate $i | grep Power`
if [ "$state" = "Powered off" ]; then
/bin/vim-cmd vmsvc/power.on $i
j=0
while [ $j -le 3 ]; do
sleep $((sleep_time))
state=`/bin/vim-cmd vmsvc/get.guestheartbeatStatus $i`
if [ "$state" = "green" ]; then
break
fi
j=$((j+1))
done
fi
done


2) autostop.sh:

#!/bin/sh

sleep_time=`/bin/vim-cmd hostsvc/autostartmanager/get_defaults | grep stopDelay | sed "s/ //g" | sed "s/,//g" | awk 'FS="=" {print $2}'`

for i in `/bin/vim-cmd hostsvc/autostartmanager/get_autostartseq | grep vim.VirtualMachine | sed "s/',//g" | awk 'FS=":" {print $2}'`; do
state=`/bin/vim-cmd vmsvc/power.getstate $i | grep Power`
if [ "$state" = "Powered on" ]; then
/bin/vim-cmd vmsvc/power.shutdown $i
j=0
while [ $j -le 3 ]; do
sleep $((sleep_time))
state=`/bin/vim-cmd vmsvc/power.getstate $i | grep Power`
if [ "$state" = "Powered off" ]; then
break
fi
j=$((j+1))
done
fi
done


!!! All changes are made at your own risk, the author does not bear any responsibility for the consequences !!!

Initially, we will unpack gunzip the image file to the main storage:
~ # gunzip < /bootbank/s.v00 > /vmfs/volumes/MainStorage/temp/tmp.vmtar
Let's go to the main repository and unpack once again what we got with the help of VmWare tar:
~ # cd /vmfs/volumes/MainStorage/temp/
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp # vmtar -x tmp.vmtar -o tmp.tar

And only now just unpack tar'om (that's such a tricky nested doll):
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp # tar -xf tmp.tar
Delete the extra archives and see what happened:
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp # rm -f tmp.tar tmp.vmtar
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp # ls -l
drwxr-xr-x 1 201 201 7280 Jun 5 16:18 bin
drwxr-xr-x 1 201 201 280 Apr 30 03:16 dev
drwxr-xr-x 1 201 201 6580 Jun 5 16:18 etc
drwxr-xr-x 1 201 201 17080 Jun 5 16:15 lib
drwxr-xr-x 1 201 201 420 Jun 5 16:15 lib32
drwxr-xr-x 1 201 201 4900 Jun 5 16:15 lib64
drwxr-xr-x 1 201 201 420 Jun 5 16:15 opt
drwxr-xr-x 1 201 201 280 Apr 30 03:21 proc
lrwxrwxrwx 1 201 201 23 Jun 5 16:15 productLocker -> /locker/packages/5.0.0/
drwxr-xr-x 1 201 201 19040 Jun 5 16:18 sbin
drwxrwxrwt 1 201 201 280 Apr 30 03:33 tmp
drwxr-xr-x 1 201 201 980 Jun 5 16:16 usr
drwxr-xr-x 1 201 201 1400 Jun 5 16:18 var
drwxr-xr-x 1 201 201 560 Jun 5 16:18 vmfs
drwxr-xr-x 1 201 201 560 Jun 5 16:18 vmimages
lrwxrwxrwx 1 201 201 18 Jun 5 16:18 vmupgrade -> /locker/vmupgrade/

We are very interested in the contents of the sbin directory, go into it and copy the files for autorun and auto shutdown VM, change the owner and access rights like everyone else (like we are local):
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp # cd sbin/
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # cp ../../autostart.sh .
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # cp ../../autostop.sh .
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # chown 201:201 autostart.sh autostop.sh
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # chmod a=rx autostart.sh autostop.sh

Let us change the vmware-autostart.sh file, add the autorun script call to the vmware_autostart_vms function end and add the auto-shutdown script call to the beginning of the vmware_autostop_vms function:
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # chmod a+w vmware-autostart.sh
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # vi vmware-autostart.sh
# AutoStart vms if any ( Execute the subshell in the background )
vmware_autostart_vms() {
(
logger -t 'VMware[startup]' " Starting VMs"
-----8<------------------------------------------------------------------------
done # wait to start
) &

/sbin/autostart.sh

}
# AutoStop vms if any
vmware_autostop_vms() {
logger -t 'VMware[shutdown]' " Stopping VMs"

/sbin/autostop.sh

val=$("$VIMSH" -U $ROOT_USER $AUTOSTOP_CMD 2>&1 > /dev/null)
-----8<------------------------------------------------------------------------
fi
}

Then we will return the rights back and pack the matryoshka back:
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # chmod aw vmware-autostart.sh
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp/sbin # cd ..
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp # tar -cf ../tmp.tar *
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed/temp # cd ..
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed # vmtar -c tmp.tar -o tmp.vmtar
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed # gzip < tmp.vmtar > s.v00

Change the access rights as they were and replace the existing file with ours, without forgetting to make backup beforehand:
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed # chmod 700 s.v00
/vmfs/volumes/4f72a981-4f940db0-18e9-001517ecc0ed # cp -f s.v00 /bootbank/s.v00

That's all, reboot, check and work.

Source: https://habr.com/ru/post/145428/


All Articles