How to have all your dev-machines always with you

Introduction and Rationale

I am traveling a lot and would like to have everything with me to work on trains, planes or wherever I find some time to hack. Since I don't have time/space/money to run a server farm somewhere, but still need several setups, I decided to use virtualization. Disk space is no issue these days. My beloved Thinkpad X61s has a 320GB hard disk that came at around 80 Euros, and it has 3GB RAM which is just enough to run one virtual machine next to whatever else I do. For Gentoo development and committing to the main tree I need one machine running ~x86, the unstable branch. I also need an amd64-system to test ebuilds there. For my daily work I still want to use a mostly stable system. For archtesting purposes another "empty" stable x86-system is good to have. One cool thing about VirtualBox is that it now supports 64-Bit systems on a 32-Bit host, when the CPU supports 64-Bit of course. For historical reasons I'm still running x86 because when I set up this system 3 years ago amd64 was not where it is today.

General information on Virtualbox in Gentoo are here in the Gentoo wiki.

Point of departure and requirements

  • My production system is mostly stable x86-system and must be rock solid
  • I need 32 and 64 bit systems for testing and development.
  • I must be able to run X-applications on every virtual machine

Implementation

I have implemented the above setup using app-emulation/virtualbox-bin, the closed-source variant of Virtualbox. I'm using the closed source variant because it offers better performance and features. Oracle also supports an Open Source version which usually lacks around a year behind. For the Gentoo install

Setup

  1. Install Virtualbox
  2. Make sure the kernel modules are loaded, e.g. by adding
modules="${modules} vboxdrv vboxpci vboxnetflt vboxnetadp"

to your /etc/conf.d/modules.

  1. Create a new virtual machine using the VirtualBox gui, I have mine set up to use 1GB of RAM and 1 CPU.
  2. In the settings, enable "VT-x/AMD-V" and "Nested Paging" to make use of hardware virtualization features.
  3. Virtually enter (the .iso of) any Live-CD and boot it. I used systemrescuecd.
  4. Perform a normal Gentoo install from a live system.
  5. When compiling a kernel you can use my kernel configuration (for 2.6.35). It is not very optimized, but works well.

X-Forwarding

Of course we want to use X-applications run on the virtual machine 'seamlessly' on our production system. VirtualBox offers a so called 'seamless desktop' integration, but you should instantly forget about it since it is complicated to set up and error prone. X is a network protocol and supports 'seamless integration' for more than 20 years. We will let ssh do the hard work for us.

  1. Install openssh on the virtual machine
  2. In /etc/ssh\_config make sure you have "ForwardX11 yes" for any host, and in /etc/sshd\_config make sure you have "X11Forwarding yes"
  3. Use 'rc-update sshd default' to start sshd automatically

Next we want to log in via ssh to the virtual machine, and since it has no physical network address we will map the virtual machine's port 22 to some unused port of our real machine. This is commonly called "port forwarding". VirtualBox comes with a program "VboxManage" to do this sort of setup. Here are the commands to enable port forwarding of port 2222 of the local machine to port 22 (the default port of ssh) of the virtual machine:

VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/HostPort" 2222
VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/GuestPort" 22
VBoxManage setextradata <guestname> "VBoxInternal/Devices/pcnet/0/LUN#0/Config/ssh/Protocol" TCP

In any of these commands replace <guestname> by the name that you gave the virtual machine during setup. You can use

VBoxManage getextradata <guestname> enumerate

to show which values are set. This is just one possibility, another one is to use

VBoxManage modifyvm <guestname> --natpf1 "guestssh,tcp,,2222,,22"

to set things up and

VBoxManage showvminfo <guestname>

to show the current setup.

  1. you can try to log in now using
ssh -Y -p2222 youruser@localhost

The "-Y" option enables X11-forwarding, "-p" specifies the port. If everything worked out nicely (and you have the basic X-packages on the virtual machine installed) you can run any X program and it will appear on the screen of the physical machine without any noticeable performance loss. If you want to transfer files to your virtual machine you can use "scp" which (weirdly) uses "-P" to specify the port. For example

scp -P2222 -r mydir youruser@localhost:~

will copy mydir to the home directory of your user on the virtual machine. When all this works you will certainly want to set up passwordless ssh. After this it is most convenient to set up an ssh-alias by editing ~/.ssh/config as follows

Host devbox
     Port 2222
     HostName localhost
     User youruser

With this ssh/scp/git+ssh and other goodies will just work, e.g. 'ssh devbox' will log you in. Finally, to make key management even easier you might want to set up net-misc/keychain.

Performance & Maintenance

The virtual machine is somewhat slower on I/O and when forking new processes. In pure number crunching it seems to be more or less equivalent to the physical machine, thanks to hardware support. One handbrake seems to be the choice of the I/O scheduler. Make sure that the virtual machine uses the noop scheduler (kernel boot option 'elevator=noop', at runtime via /sys/block/*/queue/scheduler ).

Another hint from Jesse Adelman was to use RAM disks for portage's tempdir. Read about it here.

In general I tree to keep the virtual machines 'as empty as possible' to ease the pain of world updates. On the unstable systems I also mask gcc and other heavy system packages and update them just once in a while, not taking every version that comes out.

Time synchronization

I constantly use suspend-to-ram on my laptop. This works well with virtualbox and is very stable. After resume the clock of the virtualmachine is messed up as it stood still during the sleep time. The so called virtualbox-guest-additions contain a time synchronization tool. Just install app-emulation/virtualbox-guest-additions and add /etc/init.d/vboxadd to your default runlevel.

Kudos

Contributions to this guide have been made by

Questions ?

I offer support for this guide via email: tomka@g.o and will post questions and answers on this page.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Date: 2010-12-25

Validate