--- title: Building bhyve Images using makefs and mkimg description: > A walk through on building bootable images that do not require the user to be root. posted: !!timestamp '2014-10-29' created: !!timestamp '2014-10-29' time: 8:57 PM tags: - bhyve - FreeBSD --- Recently Neel Natu [committed](https://svnweb.freebsd.org/changeset/base/r273375) work to enable bhyve to run on AMD processors. My main development machine is an AMD A10-5700, so the commit enables me to use bhyve for testing. EDIT: Anish Gupta did the work that Neel Natu committed. Thanks Anish! I had previously built images using `makefs` and `mkimg` for a CF card for use in another machine, so being able to build images to use with bhyve makes sense. First, you need to make sure that you have a complete source check out along with a completed buildworld and buildkernel. Then follow these steps: 1. Install world and distribution into a temporary directory using the `NO_ROOT` option:

    make installworld DESTDIR=<tmpdir> -DDB_FROM_SRC -DNO_ROOT
    make distribution DESTDIR=<tmpdir> -DDB_FROM_SRC -DNO_ROOT
    
This preps everything with the defaults as necessary. 2. Install a kernel either into a different directory (I do this) or into the same directory above:

    make installkernel DESTDIR=<tmpkerndir> -DNO_ROOT KERNCONF=<conf>
    
3. Make a directory with your custom configuration files. The basics are `/etc/rc.conf` and `/etc/fstab` and you might want `/firstboot` on there too. You will also need a `METALOG` file which contains the permissions for the files. This is just a standard `mtree` file, so you could use `mtree` to generate this instead of creating it by hand. The file contents are below. 4. Build the ufs image using the `makeroot.sh` script in the src tree at `tools/tools/makeroot/makeroot.sh`:

    /usr/src/tools/tools/makeroot/makeroot.sh  -e <custdir>/METALOG -e <tmpkerndir>/METALOG -p <tmpdir>/etc/master.passwd -s 2g ufs.img root
    
5. Build the disc image:

    mkimg -s gpt -b <tmpdir>/boot/pmbr -p freebsd-boot:=<tmpdir>/boot/gptboot -p freebsd-swap::1G -p freebsd-ufs:=ufs.img -o disc.img
    
6. Run the image:

    sh /usr/share/examples/bhyve/vmrun.sh -d disc.img vm0
    
There you have it. Besides running the image, all the other steps can be done as a normal user w/o root access. EDIT: You also might want to include an `/entropy` file (populated with 4k from `/dev/random`) in your custom directory so that the image has a good seed for entropy at first boot for things such as sshd key generation. File contents: * `/etc/fstab`:

  /dev/vtbd0p3    /               ufs     rw              1 1
  
* Custom `METALOG`:

  #mtree 2.0
  ./etc/rc.conf type=file uname=root gname=wheel mode=0644
  ./etc/fstab type=file uname=root gname=wheel mode=0644
  ./firstboot type=file uname=root gname=wheel mode=0644