Saturday, 8 April 2017

VirtualBox Test Lab

Recreating a Lab can be painful, however, it needn't be.
By creating and using images you can have your test environment up and running in minutes for when you need to work on a configuration.

For example, I often like to test my scripts and deployments in the lab environment before letting them near a production, mostly to check that I've not made any typo, and because you might want to do something simpler here is how to setup a Lab using VirtualBox.

The first step is to disable the DHCP on VirtualBox, this might sound strange but unless you want to manually set the DNS on all of the images you are planning to use the best option is to run a DHCP from the first server you setup.

Now I like to use the 172.16.1.x range you might want to use another and honestly, it doesn't matter what range you use just as long as its no the same IP as a resource you will need later.




After that you'll need to create your images, this means setting up one of each OS you plan to use, now in my case, that's Windows 2016, Windows 2012R2 and Ubuntu Server.

This part is where you invest your time, for example, Windows 2016 take about 30minutes for me to patch to the current level, however, Windows 2012R2 take close to 1 hour so by updating it to current level and then templating it this will save me hours later of windows updates, or you could choose to just turn off the Windows updates.

Same goes for Ubuntu, while the overall update process is faster I still don't want to have to spend the time to update to current level a large number of patches each time.

So go ahead and create the new virtual server of your choice and finish the install of the base OS.
Now in Windows case, I will do three things after the install

1) I will run the windows updates and that might take some time depending on your internet and how much CPU you've assigned to the VM.

2) I will copy any post-startup configuration I plan to use like scripts to the images.

3) I run the two commands needed to let it start up cleanly each time.

dism /online /cleanup-image /StartComponentCleanup /ResetBase
C:\Windows\System32\Sysprep\Sysprep /generalize /oobe /mode:vm /shutdown

Now the "dism /online /cleanup-image /StartComponentCleanup /ResetBase" cleans out anything I might of done and have left over before I save the image and the second command "C:\Windows\System32\Sysprep\Sysprep /generalize /oobe /mode:vm /shutdown" resets the SID so that if you plan to use this a domain controller you won't run into duplicate SID issues.

At this point you should have an up to date image ready to export as a template, now this is where you use the Virualbox export appliance to create a nice OVA file, this is your final template file.

The process is almost the same for Ubuntu

1) running the sudo apt-get update && apt-get upgrade

2) installing any packages I want to be there at the start like open-ssl and ssh-server

3) cleaning up the server by running the following batch file.

The batch file looks like this removing the logs and cleaning up the history ready for fresh use by the next session.

#!/bin/bash

#update apt-cache
apt-get update

#Stop services for cleanup
service rsyslog stop

#clear audit logs
if [ -f /var/log/audit/audit.log ]; then
    cat /dev/null > /var/log/audit/audit.log
fi
if [ -f /var/log/wtmp ]; then
    cat /dev/null > /var/log/wtmp
fi
if [ -f /var/log/lastlog ]; then
    cat /dev/null > /var/log/lastlog
fi

#cleanup persistent udev rules
if [ -f /etc/udev/rules.d/70-persistent-net.rules ]; then
    rm /etc/udev/rules.d/70-persistent-net.rules
fi

#cleanup /tmp directories
rm -rf /tmp/*
rm -rf /var/tmp/*

#cleanup current ssh keys
rm -f /etc/ssh/ssh_host_*

#add check for ssh keys on reboot...regenerate if neccessary
sed -i -e 's|exit 0||' /etc/rc.local
sed -i -e 's|.*test -f /etc/ssh/ssh_host_dsa_key.*||' /etc/rc.local
bash -c 'echo "test -f /etc/ssh/ssh_host_dsa_key || dpkg-reconfigure openssh-server" >> /etc/rc.local'
bash -c 'echo "exit 0" >> /etc/rc.local'

#reset hostname
cat /dev/null > /etc/hostname

#cleanup apt
apt-get clean

#cleanup shell history
history -w
history -c

This is still a very simple example as you might want to import repositries or copy ssh keys over for quick setup.