Virtualizing headless Windows on headless Linux server

I have remote Linux server and needed to run some Windows-only utilities. Since Wine is as great as always I decided to virtualize Win7. Since remote Linux is headless not only I needed headless Windows but also headless virtualization.



# Add Oracle repo with VirtualBox
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
echo "deb http://download.virtualbox.org/virtualbox/debian precise contrib" >> /etc/apt/sources.list

# Install VirtualBox
apt-get update
apt-get install virtualbox-4.2

# Verify build installed
dpkg -l | grep virtualbox-4.2

# Download extensions, build must match with one we just installed
mkdir -p /usr/src/vbox
cd /usr/src/vbox
wget http://download.virtualbox.org/virtualbox/4.2.10/Oracle_VM_VirtualBox_Extension_Pack-4.2.10-84104.vbox-extpack
VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.2.10-84104.vbox-extpack

# Grant your user account access to VirtualBox
# or just be root
adduser foobar vboxusers

# Create new virtual machine with passwordless RDP on port 3051
# Passwordless because VirtualBox password authentication with
# VRDE can't do authentication with Windows clients
VBoxManage createvm --name "Win7" --ostype Windows7 --register
VBoxManage modifyvm "Win7" --vrdeport 3051 --vrde on --vrdeextpack default --vrdeauthtype null
VBoxManage modifyvm "Win7" --memory 1024 --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 eth0
VBoxManage createhd --filename "/root/VirtualBox VMs/Win7/Win7.vdi" --size 10000
VBoxManage storagectl "Win7" --name "Storage" --add sata
VBoxManage storageattach "Win7" --storagectl "Storage" --port 0 --device 0 --type hdd --medium "/root/VirtualBox VMs/Win7/Win7.vdi"
VBoxManage storageattach "Win7" --storagectl "Storage" --port 1 --device 0 --type dvddrive --medium /opt/iso/Win7x86.iso

# Start virtual machine
VBoxManage startvm "Win7" --type headless

# Check status
VBoxManage showvminfo Win7 | less

# Connect to new virtual machine from Windows client and perform OS install
# Start > Run > mstsc.exe /v:192.168.1.99:3051

# After install enable built-in Windows RDP and disable VirtualBox VRDE. Also remove virtual CD. VM must be powered off.
VBoxManage modifyvm "Win7" --vrde off
VBoxManage storageattach "Win7" --storagectl "Storage" --port 1 --device 0 --type dvddrive --medium none

# VirtualBox 4.2 has some undocumented autostart features that likely work as well as PAM authentication. Therefore we use the old way and start VMs on rc.local
echo '(sleep 60; VBoxManage startvm "Win7" --type headless)' >>/etc/rc.local

# Poweroff, pause, reset, remove examples
# VBoxManage controlvm "Win7" poweroff
# VBoxManage controlvm "Win7" pause
# VBoxManage controlvm "Win7" reset
# VBoxManage unregistervm "Win7"


Don't forget to grab VirtualBox Tools and install them on Win7 VM. Check http://download.virtualbox.org/virtualbox/4.2.10/ and get VBoxGuestAdditions_4.2.10.iso. You can extract it with WinRAR or mount ISO as-is to VM.


Comments