Setting up VirtualBox to run Debian and FreeBSD VMs headlessly, with the system console accessible over a virtual serial port.
First set up a serial port in VirtualBox. In the Details section for the virtual machine, click Serial Ports, check Enable Serial Port, set Port Mode to Host Pipe, check Create Pipe and put e.g. /tmp/freebsd8vm-socket in Port/File Path.
Now start the virtual machine. Use socat to connect the domain socket created by VirtualBox to a pty.
socat UNIX-CONNECT:/tmp/freebsd8vm-socket PTY,link=/tmp/freebsd8vm-pty &
It should be possible to now connect to the serial port with screen. If you disconnect the screen session the pty will be removed, so the above socat command will need to be run again.
screen /tmp/freebsd8vm-pty
This worked for me on FreeBSD 8.0-RELEASE. It should work similarly on other releases, but device names might change or something.
Tell the boot loader to use the serial console, which also makes the kernel do so. To make the console more responsive, we tell it to use a higher speed connection than the default 9600bps.
echo 'console="comconsole"' >> /boot/loader.conf
echo 'comconsole_speed="38400"' >> /boot/loader.conf
Edit /etc/ttys and change off to on, dialup to vt100, and the speed to 38400 on the entry for ttyu0. The relevant line should look like this after editing.
ttyu0 "/usr/libexec/getty std.38400" vt100 on secure
Reboot for /etc/ttys and /boot/loader.conf be re-read.
These instructions apply to Debian Lenny, but probably will at least mostly work on other Linux systems as well.
Uncomment/modify this line in /etc/inittab for serial console.
T0:23:respawn:/sbin/getty -L ttyS0 38400 vt100
Tell init to re-read inittab, which will make the serial console start working immediately.
telinit q
Now we have to tell grub and the kernel (via grub) to use a serial console as well. Add the following to /boot/grub/menu.lst.
# Serial console
serial --unit=0 --speed=38400 --word=8 --parity=no --stop=1
terminal --timeout=10 serial console
Then find the kernel boot options line, which should look like this
# kopt=root=/dev/hda1 ro
and add arguments to use a serial console.
# kopt=root=/dev/hda1 ro console=tty0 console=ttyS0,38400n8
Now update the Debian automagic kernel sections
update-grub
I also added noapic to the kernel options to prevent the system from periodically freezing at boot
# kopt=root=/dev/hda1 ro console=tty0 console=ttyS0,38400n8 noapic
It will be necessary to reboot so you can see that grub and the kernel are using serial.
Start the virtual machine from the command line, with no graphical UI
VBoxManage startvm freebsd8 -type headless
If rebooting the VM is necessary
VBoxManage controlvm freebsd8 reset
It is handy to be able to ssh to the VM. When using the default NAT network configuration for VirtualBox, the only way to make network connections from the host to the guest system is with port forwarding. The following commands will forward port 2222 on the host to 22 on the guest VM. Unfortunately this makes the host listen on 0.0.0.0, and I have not found a way to make it only listen locally.
VBoxManage setextradata freebsd8 "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/Protocol" TCP
VBoxManage setextradata freebsd8 "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/GuestPort" 22
VBoxManage setextradata freebsd8 "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/HostPort" 2222
http://www.freebsd.org/doc/handbook/serialconsole-setup.html http://www.howtoforge.com/setting_up_a_serial_console http://download.virtualbox.org/virtualbox/3.1.2/UserManual.pdf
If you want only local SSH access, you can add a network adapter to the VM and set it to use a 'Host-only Adapter.', and set a local virtual network interface to use. Under File -> Preferences -> Network -> Host-Only Network, you can fiddle with the virtual interfaces and enable a DHCP server. Now when the VM starts up, you can SSH into it by accessing it by its host-only adapter IP address without exposing it to the network.