I've written a simple(ish) bhyve management script that some people might find useful. Only supports FreeBSD guests at the moment although that's easy enough to fix with a few spare hours. It's also my first shell script that's longer than about 10 lines (containing more than just a few rsync(1)/mysqldump(1) commands) so it's probably incredibly messy to a sh(1) expert.
I've looked at other management scripts (mainly iohyve & bhyveucl) but they didn't really suit me. iohyve is designed purely for ZFS, which limits its usefulness. It also uses properties to store configuration. This is ok, and as a "port" of sysutils/iocage, its primary design feature is using ZFS & ZFS properies, but I find it gets messy very quickly. Even with simple functionality you end up with loads of ZFS properties and my general view is that one way or another, the way forward for bhyve is to have everything about the vm set in a config file. The command structure of iohyve is really nice (mines similar), but the other niggle I have is that it creates a ZFS dataset for every ISO file(?).
With bhyveucl, I found the configuration really awkward. The sample files contain god knows how many dozens of lines just to configure networking. I wanted something that could be run with the bare minimum of config.
I did originally plan to use UCL (it's much nicer having stuff like interfaces in an array rather than network0="", network1="", etc), but my awkward and terrible development set up left me using only what was in base. So I ended up making overzealous use of sysrc() and storing all config in rc style files. In a way it's nice that nothing needs to be installed.
I'd never actually run bhyve before doing this (just an avid follower). After writing about 300-400 lines I moved the script to the only bhyve capable machine I can play with and tried it. I was quite surprised that I only had to change a line or two to get everything working, and that bhyve works incredibly well.
Anyway onto the script, and how to use it:
Now to actually using it
Disclaimer: Use at your own risk; All testing was done on 10.1-RELEASE. It shouldn't do anything harmful to your computer but as mentioned, it's my first "serious" shell script and was mainly thrown together over a couple of days.
I've looked at other management scripts (mainly iohyve & bhyveucl) but they didn't really suit me. iohyve is designed purely for ZFS, which limits its usefulness. It also uses properties to store configuration. This is ok, and as a "port" of sysutils/iocage, its primary design feature is using ZFS & ZFS properies, but I find it gets messy very quickly. Even with simple functionality you end up with loads of ZFS properties and my general view is that one way or another, the way forward for bhyve is to have everything about the vm set in a config file. The command structure of iohyve is really nice (mines similar), but the other niggle I have is that it creates a ZFS dataset for every ISO file(?).
With bhyveucl, I found the configuration really awkward. The sample files contain god knows how many dozens of lines just to configure networking. I wanted something that could be run with the bare minimum of config.
I did originally plan to use UCL (it's much nicer having stuff like interfaces in an array rather than network0="", network1="", etc), but my awkward and terrible development set up left me using only what was in base. So I ended up making overzealous use of sysrc() and storing all config in rc style files. In a way it's nice that nothing needs to be installed.
I'd never actually run bhyve before doing this (just an avid follower). After writing about 300-400 lines I moved the script to the only bhyve capable machine I can play with and tried it. I was quite surprised that I only had to change a line or two to get everything working, and that bhyve works incredibly well.
Anyway onto the script, and how to use it:
- Make sure vmm is unloaded. Currently I check for vmm and do my own init if it isn't loaded, so my init won't get done if you load it manually or through /boot/loader.conf.
- Install the script at /usr/local/sbin/vm and make root executable
- Create a directory to house all vm data
Use whatever directory you like, although I will use /vm throughout the examples so put in your own directory as needed.Code:mkdir /vm - Add the following to /etc/rc.conf
Code:vm_enable="YES" # enable vm vm_dir="/vm" # vm directory vm_list="" # space separated list of machines to start on boot vm_delay="5" # delay between starting machines - Run the init command which will load all needed modules and create the rest of the directory structure
Code:# vm init - Create /vm/.templates/default.conf
I've designed the system to use templates for the configuration. This way you can have multiple templates (eg freebsd-small.conf, freebsd-web.conf, etc) and use the relevant template when creating virtual machines.
Put the following in it
When a vm is created using the default template, it will have 1 cpu, 256MB of ram and one network interface, connected to the public switch (more on that in a minute)Code:cpu=1 memory=256M network0_type="virtio-net" network0_switch="public"
Now to actually using it
- I've taken a "vmware style" system with the networking in that you create virtual "switches", then tell each vm which switch to connect to. In the default template I specified a switch called "public", so lets create it:
Lets say em0 is an interface on our host connected to our lan, so we want to connect our virtual machines to that:Code:# vm switch create public
Now list the switches to see the config:Code:vm switch add public em0
This has basically created a bridge and added em0 to itCode:# vm switch list NAME IDENT VLAN PORTS public bridge0 - em0 - Download a FreeBSD iso to install from
Code:# vm iso ftp://ftp.freebsd.org/pub/FreeBSD/releases/ISO-IMAGES/10.1/FreeBSD-10.1-RELEASE-amd64-disc1.iso - Create our virtual machine
Both those commands basically do the same thing. Without specifying a template, default will be used, and a 20G disk will be created if no size given.Code:# vm create test [OR] # vm create -t default -s 20G test
- Install the OS
(I only useCode:# vm iso FILENAME FreeBSD-10.1-RELEASE-amd64-disc1.iso # vm install test FreeBSD-10.1-RELEASE-amd64-disc1.isovm isothere to list the ISO name for easy copy/paste).
This will wait for bhyveload to make sure it runs correctly, then will exit once bhyve actually starts. You can now connect to the console to complete the install
(This uses cu() so press ~+Ctrl-D to exit)Code:# vm console test
Once the OS is installed and the machine reboots, it should boot straight into FreeBSD. The script takes care of re-starting bhyve after a guest reboot. - To shutdown, either connect to the VM and shut it down normally, or run one of the following from the host:
The first will send the shutdown command to the guest and exit immediately, the second will wait for the machines to stop. (The second is used in my rc script just in case FreeBSD needs my script to actually wait until bhyve has finished before I exit)Code:# vm stop test # vm stopall - You can start a machine once it's installed by just running
Once it's running, using the same console command as above to connect to it.Code:# vm start test
Disclaimer: Use at your own risk; All testing was done on 10.1-RELEASE. It shouldn't do anything harmful to your computer but as mentioned, it's my first "serious" shell script and was mainly thrown together over a couple of days.
Attachments
Last edited by a moderator: