Virtualbox headless

Maybe you know, maybe you don't, but I'm the one who builds pf-kernel for Ubuntu.
In this post I'll teach you something about Virtualbox's virtual machines(VMs). Since the title is about Virtualbox and not kernel building, I will not explain anything about the latter.

A little history

On a shiny day I decided to build pf kernel for Ubuntu. Since I run a 64 bits machine and people also wanted the 32 bits kernel, I made a 32 bits Ubuntu virtual machine to help me in building the binary package.
A few weeks later, I told myself that it was a good idea to keep two machines just for building and to not use my computer for this task. I wanted a clean enviroment, so I made a Ubuntu 64 bits VM.

Managing the machines

Managing the machines is actually an easy task. That's because I SSH into them. I do that because there's no need for a display. There is no X, so all I need is a terminal and I build via a script, then upload the resulting debs in my Dropbox folder.

I start them using a script. I always start them as a group and if soemthing fails, I get feedback from the script. Starting a Virtualbox virtual machine from the command line is easy. You just have to use VBoxManage startvm "VM name". By default, this will run the VM in GUI mode, meaning that you will actually see the VM's window.
It's basically the same thing you see when you run it from VBox, and then close VBox's window. It's also what I didn't want. So I added a little switch to the command: --type headless. This switch tells VBox not to display any windows.
Exactly what I wanted.

VBoxManage startvm "ubuntu32" --type headless
VBoxManage startvm "ubuntu64" --type headless

"OK, but now you can't see the VMs! How do you build the kernel?"
Easy. I connect via SSH. It's the same for me, even if I see a machine window or even if I connect via SSH to it. Remember? There's no X, so no GUI, only CLI.
Connecting via SSH is a little trickier. You have to forward the port to the VM. On *nix systems, you can't map a port under 1024 if you're not root. So forwarding port 22(ssh) won't work. Note that VBox won't complain about this, but you can't connect to SSH either. So use this command to forward the port:

VBoxManage modifyvm "VM name" --natpf1 "guestssh,tcp,,2222,,22"

"guestssh,tcp,,2222,,22" means the following:

  • guestssh = rule name
  • tcp = protocol. SSH runs on tcp.
  • ,, = nothing between those commas means "host IP". Leave it blank if you don't know what you are doing.
  • 2222 = host port to forward. Aka "the port you will connect to the remote host"
  • ,, = second blank commas means guest IP. Leave it blank if you don't know what you are doing.
  • 22 = guest port. Aka "the port on which the VM ssh server listens"

I changed the port from 2222 to 2232 for the 32 bits VM and to 2264 for the 64 bits VM.
When I want to connect to the machines I simply run ssh name@localhost -p 2232 or ssh name@localhost -p 2264. Note: You must connect to localhost ( or else this won't work.
You can see them in action in this picture.

I stop the machines via scripts, or I save the states if I don't want to shutdown them.


VBoxManage controlvm "ubuntu32" savestate
VBoxManage controlvm "ubuntu64" savestate


VBoxManage controlvm "ubuntu32" acpipowerbutton
VBoxManage controlvm "ubuntu64" acpipowerbutton

That's all.

Comments !