I've written this code to automatically start KVM Virtual Machine at boot of host system, and stop Virtual Machine at shutdown or reboot of host system.
start on startup
start on started networking and started qemu-kvm and started libvirt-bin
stop on runlevel [016]
kill timeout 70
expect fork
pre-start exec /KVM/luc.sh prepara
exec /KVM/luc.sh avvia
pre-stop script
/KVM/luc.sh ferma
/bin/sleep 20
end script
luc.sh is a script with these functions:
- prepara() – prepare the environment
- avvia() – start KVM
- ferma() – shutdown KVM
It works except for the sleep
command. I need to delay the shutdown/reboot operation of the host system to wait for the guest shutdown to complete. In other words, I want insert a wait of ~20 sec after the /KVM/luc.sh ferma
command. It seems that the sleep
call in the upstart script is asynchronous. Is it possible to make a synchronous call in the upstart script?
Best Answer
Yes, somehow, sleep doesn't work for me either (I'm using Ubuntu 10.04). I'm not sure that the following solution is the best one but it seems to be working.
Create an additional upstart task, say, post-sleep.conf (logging added for clarity):
Add the following to the job that needs to sleep after shutdown:
Use instances in the first script if several jobs need to wait before full shutdown. However, I think an even better and more robust solution would be to rely on events.