|
- ---
- 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:
- <pre class="fullwidth"><code>
- make installworld DESTDIR=<tmpdir> -DDB_FROM_SRC -DNO_ROOT
- make distribution DESTDIR=<tmpdir> -DDB_FROM_SRC -DNO_ROOT
- </code></pre>
- 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:
- <pre class="fullwidth"><code>
- make installkernel DESTDIR=<tmpkerndir> -DNO_ROOT KERNCONF=<conf>
- </code></pre>
- 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`:
- <pre class="fullwidth"><code>
- /usr/src/tools/tools/makeroot/makeroot.sh -e <custdir>/METALOG -e <tmpkerndir>/METALOG -p <tmpdir>/etc/master.passwd -s 2g ufs.img root
- </code></pre>
- 5. Build the disc image:
- <pre class="fullwidth"><code>
- 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
- </code></pre>
- 6. Run the image:
- <pre class="fullwidth"><code>
- sh /usr/share/examples/bhyve/vmrun.sh -d disc.img vm0
- </code></pre>
-
- 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`:
- <pre class="fullwidth"><code>
- /dev/vtbd0p3 / ufs rw 1 1
- </code></pre>
- * Custom `METALOG`:
- <pre class="fullwidth"><code>
- #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
- </code></pre>
|