Since the advent of the IaaS and PaaS providers, the decline in usage of monolithic architectures (e.g.: application server + relational DB) and the need for distributed and automatically scalable applications, as software developers we have to be increasingly more aware and skilled in the tools and techniques used to orchestrate and automate the deployment of all the different components that our architecture might end up with.
Among these tools, normally encompassed in the DevOps space, I am currently very interested in Vagrant, Docker, Ansible and Ubuntu JuJu. Although I already use Vagrant, today I decided I wanted to know how to create my own Base Boxes that I can reuse. My idea is to have two Linux Base Boxes with Ubuntu Server and Ubuntu Desktop respectively and then another two with Windows 7 and 8. Once Vagrant is covered I want to get deeper with Docker because I see a lot of potential in lightweight containers instead of using heavy virtual machines.
Most of the guides out there focus on Linux distributions without GUI, so I decided that while I am creating my Vagrant Base Box with Ubuntu 14.04 Desktop I would document all the relevant steps next:
Download relevant VirtualBox installable package for your host system from the website: https://www.virtualbox.org/
I have downloaded the 64bit Desktop ISO: http://www.ubuntu.com/download/desktop
Before installing the Guest Additions, you will have to prepare your guest system for building external kernel modules:
$ sudo apt-get install dkms
$ sudo /etc/init.d/vboxadd setup
and install the build-essential package and linux headers to compile the Guest Additions propertly
$ sudo apt-get install -y build-essential linux-headers-server
Then Mount guest additions ISO via virtualbox window and follow the indications to run the installation script :
If the sytems does not install it automatically you can use the following commands:
$ sudo mount /dev/cdrom /media/cdrom
$ sudo /media/cdrom/VBoxLinuxAdditions.run
$ sudo umount /media/cdrom
$ sudo apt-get clean
$ sudo apt-get update -y
$ sudo apt-get upgrade -y
# Restart the machine
$ sudo shutdown -r now
$ sudo su -
$ visudo
# Add the following line to the end of the file.
vagrant ALL=(ALL) NOPASSWD:ALL
This way we can SSH to the machine without entering the password:
$ mkdir -p /home/vagrant/.ssh
$ wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
# Ensure we have the correct permissions set
$ chmod 0700 /home/vagrant/.ssh
$ chmod 0600 /home/vagrant/.ssh/authorized_keys
$ chown -R vagrant /home/vagrant/.ssh
$ sudo apt-get install -y openssh-server
$ sudo vi /etc/ssh/sshd_config
Ensure the following is set:
$ sudo service ssh restart
Save & close the file then restart SSH.
You can remove those packages that you don't consider necessary (e.g.: LibreOffice) and execute the following commands to save space and shut the VM down:
$ sudo dd if=/dev/zero of=/EMPTY bs=1M
$ sudo rm -f /EMPTY
# Shutdown the machine
$ sudo shutdown -h now
Asuming you already have Vagrant installed in the host system, just run the following command:
$ vagrant package –-base
in my case:
$ vagrant package –-base Ubuntu-14.04-64-Desktop
this will create a file called package.box
$ vagrant box add {boxname} package.box
in my case:
$ vagrant box add Ubuntu-14.04-64-Desktop package.box
Go to a different folder and type:
$ vagrant init {boxname}
in my case:
$ vagrant init Ubuntu-14.04-64-Desktop
Edit the generated Vagrantfile by uncommenting the following lines:
config.vm.provider "virtualbox" do |v|
v.gui = true
end
$ vagrant up
and you should be able to SSH the machine and see the VM running:
Please let me know if you find something that is incorrect or inaccurate.
Cheers.