Setting to provide a "VM always on" feature

Hello community,

at first I would like to wish everyone a happy new year.

I am looking for a solution for people who accidentally poweroff Virtual Machines which are production systems using commands like halt, poweroff or something like this. In shorts I would like to guarantee that some VMs work in a “always on” mode.
Is there a working solution which is easier to handle than my workaround (below)?

Preface: Previously we have used Ganeti which provides a feature to autorestart a VM in case the QEMU system crashes (or a user shuts down its Virtual Machine from the inside of the VM. Only a shutdown from the Ganeti command line leads to a permanent shutdown state.

I already found a workaround to enable such a mechanism in OpenNebula but my solution is not free of fuss unfortunately.

Either way I like to present my workaround in the following in the hope that I can help someone.
At first I created a VM_HOOK that catches every time a VM enters the POWEROFF state.

VM_HOOK = [
   name      = "hook_vm_on_poweroff",
   on        = "CUSTOM",
   state     = "POWEROFF",
   lcm_state = "LCM_INIT",
   command   = "ft/hook_vm_on_poweroff.sh",
   arguments = "$ID $PREV_STATE $PREV_LCM_STATE" ]

Then I created the following script ft/hook_vm_on_poweroff.sh as handler for the hook:

#!/bin/bash

if [ "$2" == "ACTIVE" ] && [ "$3" == "SHUTDOWN_POWEROFF" ]; then
        IS_VM_ALWAYS_ON=$(onevm show $1 | grep VM_ALWAYS_ON | grep -q true && echo "true")
        if [ "x$IS_VM_ALWAYS_ON" == "xtrue" ]; then
                echo "Restarting VM $1 - VM_ALWAYS_ON is enabled"
                onevm resume $1
        fi
fi

This script checks whether the VM was running previously.
If so, it also checks if the VM has set the following variable:
VM_ALWAYS_ON=true (I added that variable using the command onevm update VM_ID)

If all conditions are met the command onevm resume VM_ID is issued to restart the VM.

A major downside of my workaround is that the hook also fires if someone shuts down the VM via the Sunstone or onevm poweroff VM_ID

Do you know a better and easier way to implement this?

Many thanks in advance!

Best regards,

Bernhard J. M. Grün