Hello,
Here is what I did (and it is working perfectly at the moment)
vi /etc/rc.conf
I have put the following in the file
# this is required for networking (replaces loading remaining modules manually)
vboxnet_enable="YES"
# start VirtualBox
vboxheadless_enable="YES"
# VMs to start (whitespace-separated list):
vboxheadless_machines="NanobootXPEnology"
# definition for VM with acronym "NanobootXPEnology":
vboxheadless_NanobootXPEnology_name="NanobootXPEnology"
vboxheadless_NanobootXPEnology_user="vbox"
vboxheadless_NanobootXPEnology_stop="poweroff"
This way, the virtual machine will automatically start and stop with the jail.
As my virtual machine uses the bridged networking, I have seen that freenas host will always crash when it is "shut downed" or "restarted". I have reported it as a bug (5263). The only way I saw to solve the problem was to force a shutdown of the virtual machine before the vboxheadless shot downs the machine. So I do
mount -uw /
(to be able to change the /conf...)
In the /conf/base/etc/rc.shutdown (and also /etc/rc.shutdown)
I add the following in the beginning of the file (just after the comments)
/usr/sbin/jexec 1 /mnt/VHD/shutdown.sh
sleep 10
I put jexec 1 because I have no idea about how to find the id of a jail from its name and the jail always has an ID of 1
Without the sleep I have a crash of freenas
The file shutdown.sh content (it must be in the jail as jexec works in the jail):
I must do a su vbox as phpvirtualbox works as vbox.
Don't forget the chmod 777 /mnt/VHD/shutdown.sh
(777 is perhaps a bit too much.
#!bin/sh
su vbox -c "/usr/local/bin/VBoxManage controlvm NanobootXPEnology savestate"