Translated from the Japanese version here:
./setup-ja.md
When I was reading "Designing BSD Rootkits", I set up a virtual machine so I could follow along. In this file, I've recorded the steps to setting up a FreeBSD VM using Vagrant. At first it was going to be a note so I didn't forget, but I thought it might be useful to others so I decided to clean it and upload it.
I'm using Vagrant to manage the virtual machine. Vagrant supports many Hypervisors (incl. VirtualBox and KVM), and takes care of most of the configuration for us. The VM itself is defined in a file called Vagrantfile.
$provision_script = <<-SCRIPT
echo 'Downloading sources...'
fetch -qo /tmp https://download.freebsd.org/ftp/releases/amd64/12.2-RELEASE/src.txz
echo 'Extracting sources...'
tar Jxf /tmp/src.txz -C /
SCRIPT
Vagrant.configure("2") do |config|
config.vm.box = "freebsd/FreeBSD-12.2-RELEASE"
config.vm.synced_folder "./chapters", "/home/vagrant/chapters",
type: "rsync", rsync__args: ["-zz", "--delete", "--archive"]
config.vm.provision "shell", inline: $provision_script
endgeneric/freebsd12is the template namesynced_folderadds a shared folder.rsyncis a personal preferenceprovisionis a script that is run when the VM is created. A variable is used here for readability
With the above settings, rsync will only sync in one direction. Changes made from inside the VM will be overwritten on the next sync, so please remember to only make changes on the host side. The advantage in this setup is that the development folders on the host will always stay clean, even if the build procedure generates many files on the VM. When this method is used, a command must be run after the VM is started to initiate the sync feature.
Basic Vagrant commands
vagrant upStart VMvagrant haltPower off the VMvagrant reloadReread the settings file and restart the VMvagrant rsync-autoStart rsync
※ vagrant rsync-auto & may be useful
To verify the dev environment, try compiling and loading chapters/01/hello_world/. If there are no problems, the output should look something like the following:
vagrant@freebsd:~/chapters/01/hello_world % sudo kldload ./hello_world.ko
vagrant@freebsd:~/chapters/01/hello_world % sudo kldunload ./hello_world.ko
vagrant@freebsd:~/chapters/01/hello_world % dmesg | tail -n 2
[Addr: 0xffffffff82772119] Hello, world!
Goodbye, cruel world...
vagrant@freebsd:~/chapters/01/hello_world %