Enabling Autostart of VirtualBox-VMs on FreeNAS

Status
Not open for further replies.

zufallsheld

Explorer
Joined
Dec 27, 2013
Messages
54
I created the following guide mainly because I needed a reminder on how to do this myself, but I figured maybe other people need it to.
Here's the original on my blog:
https://www.zufallsheld.de/2015/01/06/enabling-autostart-of-virtualbox-vms-on-freenas/


There’s the great FreeNAS jail phpVirtualBox. It lets you run headless VirtualBox-machines on your FreeNAS server, in case you need a different operating system than FreeBSD. I’m using it to run Icinga and Musicbrainz. There’s only one problem: After a server reboot (yes, that happens) the VMs do not automatically start again.

Luckily there’s a method to to achieve that.

First you’ll have to connect to your FreeNAS (and the Virtualbox-jail) on the shell. If you don’t know how to do it, have a look here. After connecting its just a matter of editing one file: the rc.conf.

The file rc.conf contains descriptive information about the local host name, configuration details for any potential network interfaces and which services should be started up at system initial boot time.

To edit the rc.conf, enter the following command on the shell:

edit /etc/rc.conf
Then you have to put the following code into the now open file (copy&paste it). You’ll have to replace “yourm” with the name of your virtual machine.

Code:
# 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="yourvm"
# definition for VM with acronym "yourvm":
vboxheadless_yourvm_name="yourvm"
vboxheadless_yourvm_user="vbox"
vboxheadless_yourvm_stop="savestate"


Press escape then Enter twice to save and exit the file.

If you want to add another virtual machine, simply add it to the vboxheadless_machines-list and append the three following lines, again replacing the name.

Here’s an example where two virtual machines are automatically started at boot:

Code:
# VMs to start (whitespace-separated list):
vboxheadless_machines="icinga musicbrainz"

# definition for VM with acronym "icinga":
vboxheadless_icinga_name="icinga"
vboxheadless_icinga_user="vbox"
vboxheadless_icinga_stop="savestate"

# definition for VM with acronym "musicbrainz":
vboxheadless_musicbrainz_name="musicbrainz"
vboxheadless_musicbrainz_user="vbox"
vboxheadless_musicbrainz_stop="savestate"



That is all you have to do. Next time your FreeNAS server reboots, the VMs will start, too!
 
Last edited:

TheWoo

Explorer
Joined
Jan 2, 2015
Messages
58
Perfect! Exactly what I was looking for. Works like charm!
 

Hisma

Explorer
Joined
Mar 23, 2013
Messages
87
This is good, thanks. I am surprised this is not able to be done from the vbox GUI, but so be it.

I would like to suggest one improvement. Or, at least, one suggestion.

You have several options for the "stop". Same as you do from the GUI. You can do a "poweroff", which is akin to just pressing the power button on a physical machine, "acpipowerbutton", which is supposed to be a graceful shutdown, "pause", which I guess freezes the state but does not save it, and "savestate", which freezes the the current state and saves it so you can reboot/shutdown without losing your work (my preferred option).

Based on those options, the two that would be better than "shutdown" would be either "acpipowerbutton", or "savestate". I went with "savestate".

So just change the
Code:
vboxheadless_musicbrainz_stop="poweroff"


to

Code:
vboxheadless_musicbrainz_stop="savestate"


And you'll have a very seamless reboot, without having to restart apps, or re-login to your server etc. I tested this and it works well.

I believe that "savestate" is actually the default stop if you don't specify it, but I like being deliberate so I still include this line.

Great guide and thanks again.
 

zufallsheld

Explorer
Joined
Dec 27, 2013
Messages
54
This is good, thanks. I am surprised this is not able to be done from the vbox GUI, but so be it.

I would like to suggest one improvement. Or, at least, one suggestion.

You have several options for the "stop". Same as you do from the GUI. You can do a "poweroff", which is akin to just pressing the power button on a physical machine, "acpipowerbutton", which is supposed to be a graceful shutdown, "pause", which I guess freezes the state but does not save it, and "savestate", which freezes the the current state and saves it so you can reboot/shutdown without losing your work (my preferred option).

Based on those options, the two that would be better than "shutdown" would be either "acpipowerbutton", or "savestate". I went with "savestate".

So just change the
Code:
vboxheadless_musicbrainz_stop="poweroff"


to

Code:
vboxheadless_musicbrainz_stop="savestate"


And you'll have a very seamless reboot, without having to restart apps, or re-login to your server etc. I tested this and it works well.

I believe that "savestate" is actually the default stop if you don't specify it, but I like being deliberate so I still include this line.

Great guide and thanks again.

Thanks, you're right. That's better.
I didn't really think about it since I practically never stop my server. Nonetheless I changed the guide.
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
That looks really useful; thanks for writing it up! One of the VMs I'd want to start has a space in its name, though--how would I deal with that?
 

zufallsheld

Explorer
Joined
Dec 27, 2013
Messages
54
That looks really useful; thanks for writing it up! One of the VMs I'd want to start has a space in its name, though--how would I deal with that?
Spaces shouldn't be a problem, since all the names are quoted in the configuration. Did you encounter any problems?
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
Your example with two machines confused me a bit, since vboxheadless_machines had two values separated only by spaces. I think I've got it, though. In my case, I added this to /etc/rc.conf inside the jail:
Code:
# 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="win7 musicbrainz"

# definition for VM with acronym "win7":
vboxheadless_win7_name="Windows 7"
vboxheadless_win7_user="vbox"
vboxheadless_win7_stop="savestate"

# definition for VM with acronym "musicbrainz":
vboxheadless_musicbrainz_name="musicbrainz-server-20140514"
vboxheadless_musicbrainz_user="vbox"
vboxheadless_musicbrainz_stop="savestate"

I manually set the two VMs to "save state" in the VirtualBox web GUI, then restarted the FreeNAS server. On restart, both VMs were started. Seems to be working fine.
 

htpc guy

Cadet
Joined
Feb 11, 2015
Messages
1
Is the information covered here still accurate? I've got Virtualbox installed in a jail and when I ssh in and do cat /etc/rc.conf I see different information. I'm running build FreeNAS-9.3-STABLE-201503270027.

apache22_enable="YES"
sshd_enable="YES"
vboxwebsrv_user="vbox"
vboxwebsrv_enable="YES"
inet6_enable="YES"
ip6addrctl_enable="YES"
 

zufallsheld

Explorer
Joined
Dec 27, 2013
Messages
54
Is the information covered here still accurate? I've got Virtualbox installed in a jail and when I ssh in and do cat /etc/rc.conf I see different information. I'm running build FreeNAS-9.3-STABLE-201503270027.

apache22_enable="YES"
sshd_enable="YES"
vboxwebsrv_user="vbox"
vboxwebsrv_enable="YES"
inet6_enable="YES"
ip6addrctl_enable="YES"
Yes, it's still accurate. This information you're seeing is the default. Just add the autostart-options under there.
 

ajdales

Cadet
Joined
Apr 24, 2015
Messages
7
Hi Zufallsheld,

This is a great post, but as a 'nix' noob I am still somewhat confused.
How do I edit rc.conf so the modifications are permanent after a reboot? I did do a lot of searching on te forum and google, but did not find any clear instructions.

Also which part of the vbox guest name has to be identical to the name used to name the guest and is it kapital letter sensitive? I named one guest WinXPproSolis, should I use this name on all lines about the guest? What I am triing to write is: should all the xxxx be named WinXPproSolis, or only the second set of x'es on line 5?

  1. # VMs to start (whitespace-separated list):
  2. vboxheadless_machines="xxxx yyyy"

  3. # definition for VM with acronym "xxxx":
  4. vboxheadless_xxxx_name="xxxx"
  5. vboxheadless_xxxx_user="vbox"
  6. vboxheadless_xxxx_stop="savestate"

  7. # definition for VM with acronym "yyyy":
  8. vboxheadless_yyyy_name="yyyy"
  9. vboxheadless_yyyy_user="vbox"
  10. vboxheadless_yyyy_stop="savestate"

Would it be possible to set these commands through a tunable?

PS. I'm running 9.3 stable
 
Last edited:

zufallsheld

Explorer
Joined
Dec 27, 2013
Messages
54
Hi Zufallsheld,

This is a great post, but as a 'nix' noob I am still somewhat confused.
How do I edit rc.conf so the modifications are permanent after a reboot? I did do a lot of searching on te forum and google, but did not find any clear instructions.


Assuming you know how to connect to the jail (jls, then jexec N tcsh), then you have to enter the following command in the shell:
Code:
 edit /etc/rc.conf

Then press escape then Enter twice to save and exit the file.

Also which part of the vbox guest name has to be identical to the name used to name the guest and is it kapital letter sensitive? I named one guest WinXPproSolis, should I use this name on all lines about the guest? What I am triing to write is: should all the xxxx be named WinXPproSolis, or only the second set of x'es on line 5?

  1. # VMs to start (whitespace-separated list):
  2. vboxheadless_machines="xxxx yyyy"

  3. # definition for VM with acronym "xxxx":
  4. vboxheadless_xxxx_name="xxxx"
  5. vboxheadless_xxxx_user="vbox"
  6. vboxheadless_xxxx_stop="savestate"

  7. # definition for VM with acronym "yyyy":
  8. vboxheadless_yyyy_name="yyyy"
  9. vboxheadless_yyyy_user="vbox"
  10. vboxheadless_yyyy_stop="savestate"


PS. I'm running 9.3 stable

The xxxx in line 2 reference the xxxx-part in the variable (vboxheadless_xxxx_name), so these must be identical. But it's best to name them all the same.

Would it be possible to set these commands through a tunable?

I guess, but I don't know if its persistent.
 

ajdales

Cadet
Joined
Apr 24, 2015
Messages
7
Thanks a lot for the clarification.
I tried to edit the freeNAS rc.conf in the freeNAS shell. this edit did not remain after a reboot. Thanks to your extra information I found the shell button in the freeNAS webGUI to go into the virtualbox shell and edit the virtualbox rc.conf. It now works like a dream
 

Axemann

Dabbler
Joined
Jun 6, 2015
Messages
21
EDIT: Nevermind, I was able to resolve the issue. :)
Apparently I can neither read nor type today... :p
I hope this doesn't count as thread-necro, but I'm having a bit of an issue getting VMs to auto-start in my jail.

When I try to manually run '/usr/local/etc/rc.d/vboxheadless start', all I get is the following:

Code:
Starting Virtual Machines:
                      DC2 This account is currently not available.
Unknown machine


For all intents and purposes this is a "virgin" Virtualbox jail, with one Windows VM set up (and working) that I'd like to have boot automatically and shutdown when the NAS does.

The 'vbox' user exists and is specified as the user in /etc/rc.conf, so I'm not sure what's going on...

Any help is appreciated, and let me know if you need additional info.

-Axemann
 
Last edited:

byron

Dabbler
Joined
May 13, 2015
Messages
21
Craziest thing and wanted to post in case any others had an issue. Your recommendations worked prefect for my windows VM named VAMT_1. For whatever reason my freebsd pfsense box would not auto boot. It would always say aborted and I couldnt really find anything in the logs. I created a new VM to see if it was somehow the name or something. I changed the file as below accidentally forgetting to change yonxx-pfsense to "pfsense" in one spot. Imagine my surprise when I saw the original VM boot automatically.

I don't understand why having the WRONG names listed here actually resolves the issue but it does.

# 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="pfsense VAMT_1"

# definition for VM with acronym "yonxx-pfsense":
vboxheadless_pfsense_name="yonxx-pfsense"
vboxheadless_pfsense_user="vbox"
vboxheadless_pfsense_stop="savestate"

# definition for VM with acronym "VAMT_1"
vboxheadless_VAMT_1_name="VAMT_1"
vboxheadless_VAMT_1_user="vbox"
vboxheadless_VAMT_1_stop="savestate"
 

Dudleydogg

Explorer
Joined
Aug 30, 2014
Messages
50
This is not working for me on latest FreeNAS-9.3-STABLE-201509282017 is there another Solution?
Suggestions
 

byron

Dabbler
Joined
May 13, 2015
Messages
21
This works for me. I'm not exactly sure why. But it seems like the first part "vboxheadless_machines=" are like variables. I named them different than the actual VM names as seen below. This has worked consistently for me in every version.
Hope it helps.

-----------------------------------

apache22_enable="YES"

sshd_enable="YES"

vboxwebsrv_user="vbox"

vboxwebsrv_enable="YES"


# 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="pfsense mgmt VAMT"


# definition for VM with acronym "bla":

vboxheadless_pfsense_name="actualVMname1"

vboxheadless_pfsense_user="vbox"

vboxheadless_pfsense_stop="savestate"


# definition for VM with acronym "bla":

vboxheadless_mgmt_name="actualVMname2"

vboxheadless_mgmt_user="vbox"

vboxheadless_mgmt_stop="savestate"


# definition for VM with acronym "VAMT_1":

vboxheadless_VAMT_1_name="actualVMname3"

vboxheadless_VAMT_1_user="vbox"

vboxheadless_VAMT_1_stop="savestate"
 

Dudleydogg

Explorer
Joined
Aug 30, 2014
Messages
50
Craziest thing and wanted to post in case any others had an issue. Your recommendations worked prefect for my windows VM named VAMT_1. For whatever reason my freebsd pfsense box would not auto boot. It would always say aborted and I couldnt really find anything in the logs. I created a new VM to see if it was somehow the name or something. I changed the file as below accidentally forgetting to change yonxx-pfsense to "pfsense" in one spot. Imagine my surprise when I saw the original VM boot automatically.

I don't understand why having the WRONG names listed here actually resolves the issue but it does.

# 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="pfsense VAMT_1"

# definition for VM with acronym "yonxx-pfsense":
vboxheadless_pfsense_name="yonxx-pfsense"
vboxheadless_pfsense_user="vbox"
vboxheadless_pfsense_stop="savestate"

# definition for VM with acronym "VAMT_1"
vboxheadless_VAMT_1_name="VAMT_1"
vboxheadless_VAMT_1_user="vbox"
vboxheadless_VAMT_1_stop="savestate"

so the yonxx-pfsense would be the exact name in VBoxManage list vms ?

Here is my rc.conf the VBoxManage list vms is "VCENTER-SIX"
still cannot get it to start.
Thanks for Answering though I am trying the Named Variables just no luck yet. This all bridges on my Bare Metal VCenter which I want to decommission.

apache22_enable="YES"
sshd_enable="YES"
vboxwebsrv_user="vbox"
vboxwebsrv_enable="YES"
inet6_enable="YES"
ip6addrctl_enable="YES"

# 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="vcenter"

# definition for VM with acronym "VirtualCenter":
vboxheadless_vcenter_name="VCENTER-SIX"
vboxheadless_vcenter_user="vbox"
vboxheadless_vcenter_stop="savestate"
 

Dudleydogg

Explorer
Joined
Aug 30, 2014
Messages
50
I got it to work but I had to clean out the bad network Pair seems issue with Bridge Adapter changing name after reboot.
epair4B did not exist had to reselect another network adapter.
 

byron

Dabbler
Joined
May 13, 2015
Messages
21
That was going to be my next question. If you could start the VM normally. I have had the epair switch out on me as well. Anyway glad its sorted out. And this method does work with multiple VMs. I could never get it to work when using the actual names of the VM's in the first section.

Virtualbox on freenas has been very stable as of late. Just an FYI...I was able to get Windows 10 to work with a later version of virtual guest additions.

Good stuff. My freenas is my everything box now.
 

Dudleydogg

Explorer
Joined
Aug 30, 2014
Messages
50
Byron I wanted to say thanks your Suggestions did resolve. I would like to figure out the issue of the epair issue Im sure it has to do with the fact im using Bridged Networks.
Thanks again Byron for your help.
 
Status
Not open for further replies.
Top