Vagrant from scratch to create your own local boxes – VirtualBox based – and use them with Chef part 1.

I spin up a lot of virtualboxes, why?  I’m a combo Linux Admin & Oracle DBA & Developer with skills that I try to keep current across many technologies LAMP etc. too.

I got tired of using the VirtualBox GUI to repetitively create VMs.  So I thought about using “vboxmanage” the command line interface into VirtualBox to automate a lot of the VM creation… but that has already been done and more with a couple of tools.  Namely “Vagrant” and “Chef”.

Part 1 of this series addresses how to install Vagrant and use it with VirtualBox to help automate the creation and provisioning of VirtualBox based VMs.  BTW, Vagrant can be used for the same purpose in other VM environments like VMWare.

The environment that I used to do this is as follows:

Host OS environment: Oracle Linux 7.4 with gnome – I am performing all work from root account (or prefix all commands with sudo).

The Vagrant packaged boxes (VirtualBox guests) I’m creating all use gnome in the base VM more explanation later.  Basically, I don’t just want command line VMs.

If you are using Windows for your host OS… almost all of this post will be applicable with a few minor exceptions.

This post will benefit people not familiar with Vagrant and that really want to do it from Linux.  But, I’m going to talk about concepts and the vagrant commands themselves will pretty much be the same whether you are using Windows or Linux as a host OS.

Please be aware that I am relatively new to vagrant but pretty seasoned with both Linux especially RHEL 6 & 7 and VirtualBox… so bear with me as this post goes thru some iterations… it will be one of my longer ones and split into parts.

VirtualBox Installation On The Host OS

I am not going to walk thru the Download and installation of Virtual Box… that is easy enough goto virtualbox.org and google the rest… a standard install of the most current version (no betas) will suffice as vagrant keeps current with VirtualBox pretty closely. I googled that subject to check – 5.2.X of VirtualBox works with 2.0.2 of Vagrant.

Vagrant Installation On The Host OS

https://www.vagrantup.com/downloads.html

Download the correct version (in my case an RPM for RHEL 7)- I just saved the rpm to a local dir and did (again all this is done as root):

yum localinstall vagrant_2.0.2_x86_64.rpm

So I’ll digress for a minute to provide some background – many people use vagrant to download pre-made vagrant “boxes”.  I did that once or twice and knew I had to create my own for a few reasons – security, components never matched what I wanted, and some times the downloads were terribly slow.  No matter, vagrant still solves a lot of issues for me and chef later… so on-wards.

Creating My Own Vagrant Packaged Box

First some rules about how to create your “base VM”.  We need to have a VirtualBox VM that can be used as a “base” for your vagrant package.  My VirtualBox VM that will be used as a base is named OL74.

That base VM should use NAT networking (vagrant requires it for base boxes), and if you want to use vagrant ssh – should have a vagrant user setup in the VM BEFORE you create the vagrant package as follows here.

Additionally, on the base VM, I installed the Chef client as I intend to use it in all subsequent VMs “based” on it.  That went as follows on the base VM OL74.

yum clean all
yum update
curl https://omnitruck.chef.io/install.sh | sudo bash -s -- -P chefdk -c stable -v 2.3.4

Now assuming you have a nice clean VM as a base ( OL74 in my case ) shut it down.

shutdown -h now

As a reminder, I am using Oracle Linux 7 for both my host OS and all my guest OSes during this post.

As preparation for storing my local vagrant boxes I created the following directory and cd’d into it on the host OS:

mkdir -p /root/vvm/basebox/ol74

The ol74 sub-dir is named so to match the fact that the base VM is Oracle Linux 7.4.  The command below puts “package.box” in this directory.  Again cd to it first.

cd /root/vvm/basebox/ol74 

# to create the package.box the command is of the form:
# vagrant package –base my-virtual-machine
# in my case – note base name is my virtual machine name which in this case is
# is upper case.

 vagrant package --base OL74

# what happens now is the VM is exported and compress and placed in
# the current direct .e.g again: /root/vvm/basebox/ol74

[root@mega ol74]# pwd
/root/vvm/basebox/ol74
[root@mega ol74]# ls -altr
total 1881292
drwxr-xr-x. 3 root root 18 Feb 11 09:34 ..
-rw-r--r--. 1 root root 1926441933 Feb 11 10:55 package.box

# The local vagrant box just generated (package.box) is a good sized image even compressed – I believe due to the large # of GUI / gnome libraries etc.
# Next we are going to add the new package so that vagrant can use it when we spin up new VMs
# As follows:

vagrant box add OL74 /root/vvm/basebox/ol74/package.box
vagrant box list
[root@mega ol74]# vagrant box list
OL74 (virtualbox, 0)
hashicorp/precise64 (virtualbox, 1.1.0)

# Notice my custom box and one that I downloaded and will be deleting from
# The downloaded box comes from here: https://app.vagrantup.com/boxes/search

# Now that you have added the package you can use it – first cd into a directory where
# you want to store the Vagrantfile – that will point to a specific VM that you are about to create
# so I did this.

mkdir -p /root/vvm/ol743
cd /root/vvm/ol743
# the init command below creates the Vagrantfile in the current dir
vagrant init OL74

[root@mega ol743]# vagrant init OL74
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

# now spin the VirtualBox VM up - with vagrant
[root@mega ol743]# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'OL74'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ol743_default_1518383438372_79680
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
 default: Adapter 1: nat
==> default: Forwarding ports...
 default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
 default: SSH address: 127.0.0.1:2200
 default: SSH username: vagrant
 default: SSH auth method: private key
 default: 
 default: Vagrant insecure key detected. Vagrant will automatically replace
 default: this with a newly generated keypair for better security.
 default: 
 default: Inserting generated public key within guest...
 default: Removing insecure key from the guest if it's present...
 default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
 default: /vagrant => /root/vvm/ol743
[root@mega ol743]# 

# so if we want to connect to it – we can do a:

vagrant ssh

# Again, the vagrant ssh works because in the base image I created a vagrant user with .ssh keys

# Check for the VM in the VirtualBox GUI – it will be there and running if you saw no errors in the “vagrant up” command

# One thing I noticed is the VM was created and started with the “vagrant up” command but GUI / gnome did not come up – until I “shutdown -h now” and or rebooted/restared

The Resulting Running VM

# Here’s a screenshot using gnome-screenshot showing  virtualbox gui console.  Notice the VM name has a “default_1518…” appended to the name.

# I had to shutdown and restart the VM – to get the GUI to start – I am researching why that is – but vagrant created this VM not me manually – hope this helps.  More to come on Chef.

 

Leave a Reply

Your email address will not be published. Required fields are marked *