Not sure of the details of how your VM is initiated, but assuming you can call on a different script instead:
#!/bin/bash
function cleanup () {
sudo shutdown --now
}
trap cleanup HUP INT TERM QUIT
original-vm-startup-command
cleanup
Then when the controlling terminal is closed, "sudo shutdown --now" will run.
Whether or not it is closed by the application itself or otherwise (like ctrl-C ctrl-D) it should still shutdown. The trap statement will make sure this happens in the latter case, the last line for the former.
You probably want sudo to run for just shutdown without prompting for password:
In /etc/sudoers
%admin ALL=NOPASSWD: /sbin/shutdown
replace 'admin' with the appropriate username.
If this solution doesn't work, would need more details on how you are starting things up.