Create a Ubuntu Server 12.04 LTS Vagrant Base Box from Scratch Using VMware Fusion

Thursday, September 12, 2013

Vagrant makes it super easy to spin-up local virtual machines using VirtualBox or VMware Fusion. There are many Vagrant Boxes available to use immediately after downloading and installing Vagrant. However, I prefer to know exactly how my virtual machine image is created. This post will walk you through creating a Ubuntu Server 12.04.4 LTS Vagrant Base Box from scratch using VMware Fusion 5 or 6.

See this post to create a Ubuntu Server 12.04.4 LTS Vagrant Base Box from scratch using VirtualBox.

Install VMware Fusion 5 or 6

Before you begin, you will need to download and install a licensed copy of VMware Fusion 5 or 6 which costs $59.99 (there is a 30 day free trial available).

Install Vagrant and the Vagrant VMware Provider License

Next, download and install the latest version of Vagrant for OS X.

In addition, you will need to purchase the Vagrant VMware Provider License from HashiCorp, the creators of Vagrant, for $79.00 here. This provider license is what allows Vagrant to talk to VMware Fusion.

Once Vagrant is installed, open the Terminal application and download and install the Vagrant VMware Fusion Provider plugin:

vagrant plugin install vagrant-vmware-fusion

HashiCorp should have emailed you the Vagrant VMware Fusion Provider License by now. License the provider with the following command (save the license in a safe place, Vagrant will copy the license to it’s own directory as well):

vagrant plugin license vagrant-vmware-fusion ~/Downloads/license.lic

Verify everything is working by running any of the Vagrant commands. An error message will be thrown if there is something wrong.

Prepare the Ubuntu Server 12.04.4 LTS Virtual Machine

The following steps were written for VMware Fusion 6. They may differ if you are using VMware Fusion 5.

Download Ubuntu Server 12.04.4 LTS.

Open VMware Fusion, go to File > New….

Click Continue without disc.

Click Choose a disc or disc image.

Choose the downloaded ubuntu-12.04.4-server-amd64.iso, click Open, and Continue.

Set Operating System to Linux and Version to Ubuntu 64-bit and click Continue.

Uncheck Easy Install and click Continue.

Click Customize Settings.

Save your virtual machine with whatever name you like, I chose ubuntu-server-12.04.4-lts-x86_64.vmwarevm, and click Save.

The Settings pane should then appear.

Under Sharing, turn Shared Folders Off.

Under Processors & Memory, set Processors to 1, Memory to 512 (Vagrant can change these values later), expand Advanced options and check Enable hypervisor applications in this virtual machine.

Under Display, turn Accelerate 3D Graphics Off.

Under Network Adapter, make sure Share with my Mac is selected.

Under Hard Disk (SCSI), set the Disk size to 40.00 GB, expand Advanced options and uncheck Split into 2 GB files, then click Apply.

Under CD/DVD, expand Advanced options, and change the Bus type to IDE.

Under Sound Card, click Remove Sound Card.

Under USB & Bluetooth, expand Advanced USB options, click Remove USB Controller, and click Remove.

Under Printer, click Remove Printer Port, and click Remove.

Under Compatibility, expand Advanced options, change the Use Hardware Version to 9, uncheck Allow upgrading the virtual hardware for this virtual machine, and click Apply.

Under Isolation, uncheck Enable Drag and Drop and Enable Copy and Paste.

Close the Settings menu.

Start up the virtual machine and begin installation.

Install Ubuntu Server 12.04.4 LTS

You can install the operating system manually, using a Ubuntu Preseed file, or using a Kickstart Profile. I will be providing steps to install the operating system manually and using a Kickstart Profile.

Be aware that no where in the following steps do I install VMware Tools. So far, I have not found a need for them. Feel free to install VMware Tools if you need them.

Follow the steps in one of the following two sections, Manual Installation or Kickstart Profile Installation.

Manual Installation

Install the operating system however you like. Most of the default options can be used.

Once the operating system has finished installing and booted, perform the following post-install steps to make it work with Vagrant.

Login as the vagrant user (password is vagrant).

Install additional repository packages:

sudo apt-get install openssh-client wget curl git man vim ntp

Allow user vagrant to use sudo without entering a password:

sudo bash -c 'echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers'

Create the .ssh keys folder:

mkdir -m 0700 -p /home/vagrant/.ssh

If you want to use your own SSH public/private key then create an SSH public/private key on your workstation (you may already have), and copy the public key to /home/vagrant/.ssh/authorized_keys on the virtual machine.

Otherwise, if you want to use the SSH public/private key provided by Vagrant, run the following command:

curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub >> /home/vagrant/.ssh/authorized_keys

Clean up apt:

sudo apt-get clean

Clean up the tmp directory:

sudo rm -rf /tmp/*

Clean up the last logged in users logs:

sudo rm -f /var/log/wtmp /var/log/btmp

Clean up history:

history -c

sudo history -c

Shutdown the virtual machine:

sudo shutdown -h now

Once the virtual machine is shutdown, open Settings for the virtual machine.

Click the CD/DVD (IDE) button, expand Advanced options, click Remove CD/DVD Drive, and click Remove.

Close the Settings menu.

Next, jump to the Create the Vagrant Box section.

Kickstart Profile Installation

I used this Kickstart Profile to automate the build.

Start up the virtual machine and when the Ubuntu boot menu appears, select your language, hit the F6 key to bring up Other Options, hit the Esc key, and append the following:

ks=http://public.thornelabs.net/ubuntu-server-12.04-lts-x86_64-vagrant-box.txt

Hit the Enter key and wait for the installation to finish.

At the end of the install, shutdown the virtual machine, and again open the Settings for the virtual machine.

Click the CD/DVD (IDE) button, expand Advanced options, click Remove CD/DVD Drive, and click Remove.

Close the Settings menu.

Next, jump to the Create the Vagrant Box section.

Create the Vagrant Box

Go to the directory where your virtual machines are stored. By default this will be ~/Documents/Virtual\ Machines or ~/Documents/Virtual\ Machines.localized. In my case it was the latter.

cd ~/Documents/Virtual\ Machines.localized/ubuntu-server-12.04.4-lts-x86_64.vmwarevm

Defrag and shrink the VMDK as much as possible:

/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -d Virtual\ Disk.vmdk

/Applications/VMware\ Fusion.app/Contents/Library/vmware-vdiskmanager -k Virtual\ Disk.vmdk

Stay in the current directory, and add a metadata.json file with the following contents:

{
    "provider": "vmware_fusion"
}

Remove the VMware log files:

rm -f vmware*.log

Finally, tar everything up into a box file:

tar cvzf ubuntu-server-12.04.4-lts-x86_64.vmware.box ./*

Remove the metadata.json file:

rm -f metadata.json

Adding the Vagrant Box

Vagrant appears to have problems with spaces in absolute paths, so move the Vagrant Box somewhere else:

mv ubuntu-server-12.04.4-lts-x86_64.vmware.box ~/Desktop

Add the newly created Vagrant Box to vagrant (this will copy the Vagrant Box to another location):

vagrant box add ubuntu-server-12.04.4-lts-x86_64 ~/Desktop/ubuntu-server-12.04.4-lts-x86_64.vmware.box

Once the Vagrant Box has been copied, the original Vagrant Box file can be deleted.

rm -f ~/Desktop/ubuntu-server-12.04.4-lts-x86_64.vmware.box

In addition, the VMware Fusion virtual machine can be deleted.

Create a Vagrant Project and Configure Vagrantfile

You can have as many vagrant projects as you want. Each will contain different Vagrantfiles and different virtual machines. So, create a directory somewhere to house your Vagrantfile and associative virtual machines:

mkdir -p ~/Development/vagrant-test

cd ~/Development/vagrant-test

Create the Vagrantfile:

vagrant init ubuntu-server-12.04.4-lts-x86_64

You now have a Vagrantfile that points to the ubuntu-server-12.04.4-lts-x86_64 Base Box you just created.

If you are using your own SSH private/public key, and not the SSH private/public key provided by Vagrant, you need to tell Vagrantfile where to find your SSH private key, so add the following to your Vagrantfile:

config.ssh.private_key_path = "~/.ssh/id_rsa"

Lastly, if you do not want Shared Folders setup between your workstation and virtual machine, disable it by adding the following to your Vagrantfile:

config.vm.synced_folder ".", "/vagrant", id: "vagrant-root", disabled: true

Start Using Vagrant

Spin up your first virtual machine:

vagrant up --provider vmware_fusion

If everything spins up properly you can see the status of your virtual machine using vagrant status, you can SSH into your virtual machine using vagrant ssh, or you can destroy your virtual machine using vagrant destroy.

References

Creating a CentOS 6.2 Base Box for Vagrant 1.1

Base Boxes



comments powered by Disqus