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 CentOS 6.5 Vagrant Base Box from scratch using VMware Fusion 5 or 6.
If you prefer VirtualBox to VMware Fusion, read how to create a CentOS 6.5 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 CentOS 6.5 Virtual Machine
The following steps were written for VMware Fusion 6. They may differ if you are using VMware Fusion 5.
- Download CentOS 6.5 x86_64.
- Open VMware Fusion, go to File > New….
- Click Install from disc or image and click Continue.
- Click Use another disc or disc image….
- Choose the downloaded CentOS-6.5-x86_64-bin-DVD1.iso, click Open, and click Continue.
- Uncheck Easy Install and click Continue.
- Click Customize Settings.
- Save your virtual machine with what name you like, I chose centos-6.5-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 CentOS 6.5
You can install the operating system manually or using a Kickstart Profile. I will be providing steps to install the operating system manually and using a Kickstart Profile.
Take note, 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.
Open the virtual machine console and login as the root user (password is vagrant).
By default, eth0 is not brought up, so bring it up:
ifup eth0
Install additional repository packages:
yum install -y openssh-clients man git vim wget curl ntp
Enable the ntpd service to start on boot:
chkconfig ntpd on
Set the time:
service ntpd stop
ntpdate time.nist.gov
service ntpd start
Enable the ssh service to start on boot:
chkconfig sshd on
Disable the iptables and ip6tables services from starting on boot:
chkconfig iptables off
chkconfig ip6tables off
Set SELinux to permissive:
sed -i -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
Add vagrant user:
useradd vagrant
Create vagrant user’s .ssh 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
Change permissions on authorized_keys files to be more restrictive:
chmod 600 /home/vagrant/.ssh/authorized_keys
Make sure vagrant user and group owns the .ssh folder and its contents:
chown -R vagrant:vagrant /home/vagrant/.ssh
Comment out requiretty in /etc/sudoers. This change is important because it allows ssh to send remote commands using sudo. Without this change vagrant will be unable to apply changes (such as configuring additional NICs) at startup:
sed -i 's/^\(Defaults.*requiretty\)/#\1/' /etc/sudoers
Allow user vagrant to use sudo without entering a password:
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
Open /etc/sysconfig/network-scripts/ifcfg-eth0 and make it look exactly like the following:
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=dhcp
Remove the udev persistent net rules file:
rm -f /etc/udev/rules.d/70-persistent-net.rules
Clean up yum:
yum clean all
Clean up the tmp directory:
rm -rf /tmp/*
Clean up the last logged in users logs:
rm -f /var/log/wtmp /var/log/btmp
Clean up history:
history -c
Shutdown the virtual machine:
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.
When the CentOS boot menu appears, highlight Install or upgrade an existing system, hit the Tab key to bring up the anaconda boot line, and append the following:
noverifyssl ks=https://raw.githubusercontent.com/jameswthorne/kickstart-profiles/master/centos-6-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 open Settings again 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/centos-6.5-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
In the current directory 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 centos-6.5-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 centos-6.5-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 centos-6.5-x86_64 ~/Desktop/centos-6.5-x86_64.vmware.box
Once the Vagrant Box has been copied, the original Vagrant Box file can be deleted.
rm -f ~/Desktop/centos-6.5-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 centos-6.5-x86_64
You now have a Vagrantfile that points to the centos-6.5-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
.