Last Updated: February 25, 2016
·
983
· MidnightLightning

VirtualBox Vagrant Box to Gitian VM

Both Vagrant and Gitian are tools that use Virtual Machines to assist with development, though Gitian is more focused on the final building of the tool, while Vagrant is more in the iterative development.

However both need some setup of the VM before use. Following this guide is good, but that installs an OS from scratch, taking quite a while to do. Part of the Vagrant community is to create Box files which can be distributed as a starting point to work from. The default boxes that Vagrant suggests (precise64 and precise32) are both Ubuntu boxes, perfect for what Gitian needs too. So, can we shortcut some of the Gitian setup by bootstrapping it with Vagrant? Sure!

Clone Vagrant Box

If you've ever used theprecise64 Vagrant box for any project, Vagrant has already downloaded the base Box file for you and saved it to ~/.vagrant.d/boxes. If not, download the box file directly and extract it (it's a ZIP with a renamed extension).

Open VirtualBox and choose File > Import Appliance.... Drill down in the precise64 folder to find the box.ovf file and select it.

Rename

Gitian expects the VM to be named "Gitian-{suite}-{architecture}", so check your descriptor YML file and rename the VM appropriately (Probably "Gitian-precise-amd64").

System resources

The default Vagrant precise64 box is set to only use 386 MB of RAM, which might be fine for a small Apache server, but is not nearly enough for resource-intensive build processes. Click the "Settings" icon and adjust the system memory to 1024 MB (1 GB) at least.

SSH setup

On the host machine, run:

$ VBoxManage modifyvm Gitian-precise-amd64 --natpf1 "guestssh,tcp,,2223,,22"

That sets up port forwarding, so localhost:2223 connects to the virtual machine's port 22, the default SSH port.

Now boot the virtual machine up.

Next we'll need a SSH key to allow password-less logins. On your host machine do:

$ mkdir var
$ ssh-keygen -t dsa -f var/id_dsa -N ""
$ ssh -p 2223 -oNoHostAuthenticationForLocalhost=yes vagrant@localhost 'mkdir -p .ssh && chmod 700 .ssh && cat >> .ssh/authorized_keys' < var/id_dsa.pub

NOTE: That last line should be "ssh -p 2223 -oNoHostAuthenticationForLocalhost=yes vagrant@localhost 'mkdir -p .ssh && chmod 700 .ssh && cat >> .ssh/authorizedkeys' < var/iddsa.pub", there's an issue with the markdown fenced code blocks with at symbols...

Enter vagrant as the password for the vagrant user when prompted. Now the vagrant user should be able to log in without a password. But...

User setup

...Gitian expects there to be a user named "ubuntu" on the machine, while Vagrant sets theirs up under the username "vagrant". So, let's use the Vagrant user to create one for Gitian: Launch the Virtual Machine, log in as vagrant/vagrant, and do the following:

$ sudo adduser ubuntu
$ sudo adduser ubuntu sudo
$ sudo mkdir -p /home/ubuntu/.ssh
$ sudo chmod 700 /home/ubuntu/.ssh
$ sudo cp ~/.ssh/authorized_keys /home/ubuntu/.ssh/
$ sudo chown -R ubuntu:ubuntu /home/ubuntu/.ssh
$ sudo mkdir -p /root/.ssh
$ sudo chmod 700 /root/.ssh
$ sudo cp ~/.ssh/authorized_keys /root/.ssh/

That copies the SSH key from the Vagrant user to the Ubuntu user as well as the root user.

Packages

To ensure the proper packages for gitian building are already present on the machine before you snapshot it (to speed up bootstrapping later, log in as the ubuntu user and run:

$ sudo apt-get update
$ sudo apt-get install autoconf2.13 automake build-essential bsdmainutils faketime g++ g++-mingw-w64 git-core libqt4-dev libtool libz-dev mingw-w64 nsis pciutils pkg-config psmisc subversion unzip zip

Gitian takes a SHA fingerprint of the installed packages ,to ensure consistency, and does so by taking the hash of the *.deb files stored in /var/cache/apt/archives, but the Vagrant Precise boxes don't have those cached by default, so let's build that so they're not re-downloaded each time:

$ sudo apt-get upgrade
$ sudo apt-get clean
$ sudo dpkg-query -W -f '${Package}\n' | sudo xargs -n 50 apt-get install --reinstall -y -d

Snapshot

Gitian always restores the VM to a known state before rebuilding the project, so we need to create a snapshot for it. Logout of the VM and choose Machine > Take Snapshot.... Name the snapshot Gitian-Clean

Test

On your host machine, navigate to your gitian-builder folder, and run the following:

$ export USE_VBOX=1
$ export PATH=$PATH:$(pwd)/libexec
$ make-clean-vm --suite precise --arch amd64   # VM in VirtualBox should restore itself to Snapshot state
$ start-target 64 precise-amd64  # VM should power on
$ on-target ls -la  # Should output a listing of the Ubuntu user's home folder, without prompting for a password
$ stop-target  # Should power off the VM

Errors

If you get a timeout waiting for banner error, that's an indication that SSH isn't working properly in the VM. In setting up several machines via this process, sometimes the VirtualBox snapshot doesn't catch SSH in a proper state, and when restoring the snapshot, the SSH service is hung. Try deleting the "Gitian-Clean" snapshot and recreating it, and that should solve the problem.