live blogging: lets create a vm for a vagrant box

photoLive blogging:

Be aware reader: this is live blogging which means that this article has been written during the experiment and not after it. For this reason this is just a note, what is contained here can be used to produce a precise checklist of what to do and what NOT to do to get to the achievement.


How to create a vagrant box

Hi all, today i want o build my own box to be used with Vagrant starting from a VirtualBox export.

What we know since now?

The .box file we need to produce has to be a tar containing at least:

  • the VirtualBox settings file,
  • the VirtualBox virtual disk,
  • a settings file called Vangrantfile but different than the Vagrantfile we want to use in order to setup one project (actually they could have been a little less confusing with filenames…).

About this last one, no need for the moment to get a deep study of it, because it is produced by the command “vagrant package”.

This said, the quickest way to know how normally it works is seeing what a actual working .box file is made of; for this reason, i will download and inspect one of these machines. What i want to achieve is not just a vm , but I want a Vm which is able to accept connections made using the command

vagrant ssh

without asking for username nor password. I saw this already in a machine from puppetlabs, and it’s very useful! This is what I’m looking for: the perfect Vm settings (ssh and networking) in order to be very comfortable and quick once i start a new machine.

Yesterday i already gave a try to the command

vagrant package

which is supposed to create a box file starting from an existent VirtualBox Vm.
I already had a basic Wheezy install called “wheezy1” in my VirtualBox Gui, so all i’ve done was to type the above command with the -h switch to obtain a clear help:

giuseppe@macbian:~$ vagrant package -h
Usage: vagrant package [vm-name] [--base name] [--output name.box]
                       [--include one,two,three] [--vagrantfile file]

        --base NAME                  Name of a VM in virtualbox to package as a base box
        --output NAME                Name of the file to output
        --include x,y,z              Additional files to package with the box.
        --vagrantfile file           Vagrantfile to package with the box.
    -h, --help                       Print this help

What wasn’t clear enough to me was the usage of the –base, but it turned out to be a way to identify VirtualBox as provider, but i want to get back on this later.

Right now I’m downloading a box from the vagrant website, the one mentioned in the example present in the “getting started” part of the vagrant documentation. I’m downloading the .box file instead of running the command suggested in the guide, because i want to inspect this package so i can do the same but with any other distribution i want, and more a .box file can be used with vagrant from the file system as well, so no need to download it again if i want to fire up a lucid environment.

This file looks like a 282 MB large. The first thing to do is to check which kind of file it is (even we already know that it is a tar, optionaly gz with the virtual disk image and some config files), with the next command:

giuseppe@macbian:~/Downloads$ file precise32.box 
precise32.box: POSIX tar archive

This means that we can extract this file with the tar utility (I use Debian on my macboook, so i will suppose all are using Debian too).

giuseppe@macbian:~/Downloads$ mkdir box_test
giuseppe@macbian:~/Downloads$ mv precise32.box box_test/
giuseppe@macbian:~/Downloads$ cd box_test/
giuseppe@macbian:~/Downloads/box_test$ ls -al
total 288348
drwxr-xr-x 2 giuseppe giuseppe      4096 Jun 16 14:15 .
drwx------ 8 giuseppe giuseppe      4096 Jun 16 14:15 ..
-rw-r--r-- 1 giuseppe giuseppe 295255040 Jun 16 14:13 precise32.box
giuseppe@macbian:~/Downloads/box_test$ tar -xf precise32.box
giuseppe@macbian:~/Downloads/box_test$ ls -alh
total 564M
drwxr-xr-x 2 giuseppe giuseppe 4.0K Jun 16 14:17 .
drwx------ 8 giuseppe giuseppe 4.0K Jun 16 14:15 ..
-rw------- 1 giuseppe giuseppe 282M Sep 14  2012 box-disk1.vmdk
-rw------- 1 giuseppe giuseppe  14K Sep 14  2012 box.ovf
-rw-r--r-- 1 giuseppe giuseppe 282M Jun 16 14:13 precise32.box
-rw-r--r-- 1 giuseppe giuseppe  505 Sep 14  2012 Vagrantfile

Well the only unnecessary file is the original .box downloaded one. We can store it back in the ~/Download directory.

Well I highlighted in red the file we are interested in, and they are:

  • box-disk1.vmdk
  • box.ovf
  • Vagrantfile

 

The first file, the biggest, is the virtual hard disk image of the virtual machine;

The second, box.ovf, contains the setting of the Vm and it is generated either during a Vm export from within the VirtualBox interface or while runnng the command vagrant package.

The third file, instead is produced from vagrant during the execution of the command vagrant package and contains information which are different from the information contained in the Vagrantfile with which you usually configure your environment.

This one is like follows:

giuseppe@macbian:~/Downloads/box_test$ cat Vagrantfile 
Vagrant::Config.run do |config|
  # This Vagrantfile is auto-generated by `vagrant package` to contain
  # the MAC address of the box. Custom configuration should be placed in
  # the actual `Vagrantfile` in this box.
  config.vm.base_mac = "080027129698"
end

# Load include vagrant file if it exists after the auto-generated
# so it can override any of the settings
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)

Which doesn’t say that much, apart from taking care of the known problem of the mac address in cloned machines. But we’ll get back on this in a not far future.

Now let’s use this machine, first thing to do is to create a usable box in vagrant adding this one with a name:

giuseppe@macbian:~/Downloads$ vagrant box add precise32 ~/Downloads/precise32.box
Downloading or copying the box...
Extracting box...te: 54.8M/s, Estimated time remaining: 0:00:01)
Successfully added box 'precise32' with provider 'virtualbox'!

Now let’s create a folder and start a new precise project, to see if this machine will let me use the command vagrant ssh without bothering with password requests.

giuseppe@macbian:~/Downloads$ mkdir ~/vagrant-precise32
giuseppe@macbian:~/Downloads$ cd ~/vagrant-precise32/
giuseppe@macbian:~/vagrant-precise32$ vagrant init
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 I have to edit the Vagrantfile, in order to specify the box I want to use in this project:
Change only this:

 # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "base"

Into this

 # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "lucid32"

And now let’s start the vm with the command:

giuseppe@macbian:~/vagrant-precise32$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'precise32'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.

Guest Additions Version: 4.2.0
VirtualBox Version: 4.1
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant

Now, this machine makes me login using the command vangrant ssh

giuseppe@macbian:~/vagrant-precise32$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic-pae i686)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:22:31 2012 from 10.0.2.2
vagrant@precise32:~$

At this point i just wonder if this is the resut of VirtualBox guest stuff or if it is a particular configuration of the virtual machine (users, passwords ecc).

But, doing this made me check if the password of the user “vagran” is “vagrant”, and it is!

vagrant@precise32:~$ sudo su nobody
$ whoami
nobody
$ su vagrant -
Password: 
vagrant@precise32:~$

Then another small thing i noticed is that this user is able to use sudo commands without being asked for the password, which means that in the sudoers configuration file there is a specification to not to as for passwords… and it should be: “username ALL = NOPASSWD : ALL” in /etc/sudoers or the file opened by the command visudo:

vagrant ALL=(ALL) NOPASSWD:ALL

So what i will do now is to prepare a vm to be packaged with vagrant both installing Vbox Guest Additions and preparing the user vagrant with password vagrant to be able to do “sudo” without being asked for a password.

First of all: start the virtual machine i want to use from the VirtualBox GUI:

Then add the vagrant user with vagrant password, add the user to sudoers with adduser vagrant sudo, and enabling it to sudo without password with the line above to be added to visudo.

log out and login back as vagrant user, sudo ls -al is executed without password request.

Now i have to install Virtual Box Guest Addition to let vagrant do all its stuff with the post install and provision.

So i login into my vm and start doing some maintenance:

apt-get update
apt-get upgrade

For the installation of the Virtual Box Guest Addition, we need to get the linux kernel headers so some module can be built. But now i remember that i already tried to install the VBoxLinuxAddition.run and i had trouble with the missing linux headers which, by the way, were installed with apt-get upgrade.
So now if i type

mount /media/cdrom
cd /media/cdrom
sh VBocLinuxAdditions.sh

I have a warning about the VirtualBox version which is different than the one for this additions package. But I’m pretty sure that i downloaded this iso with the VirtualBox GUI… anyway, as soon as the installation of the Additions finishes, it complains about th emissing X.org or XFree86 system, and for this reason it will not be capable of using VNC, at leat i presume.

Now, this done, we can go further with our box production.
We now need to cleanup as much as possible this box so it will take as small space as possible.

So After removing unneeded stuff, it’s time to shutdown the machine.

Then go back to the console and start building the new VM.box file for infinite reuses.

giuseppe@macbian:~$ vagrant package --base wheezy1 --output /home/giuseppe/wheezy.box
[wheezy1] Clearing any previously set forwarded ports...
[wheezy1] Creating temporary directory for export...
[wheezy1] Exporting VM...
[wheezy1] Compressing package to: /home/giuseppe/wheezy.box

At this point all i have to do is to add this machine to my base, and then try to start a project, with

giuseppe@macbian:~/Desktop/vagrant-w$ vagrant box add wheezy1 /home/giuseppe/wheezy.box 
Downloading or copying the box...
Extracting box...te: 21.9M/s, Estimated time remaining: --:--:--)
Successfully added box 'wheezy1' with provider 'virtualbox'!

And then i have to create a new project using this new machine with

giuseppe@macbian:~/Desktop$ mkdir vagrant-w
giuseppe@macbian:~/Desktop$ cd vagrant-w
giuseppe@macbian:~/Desktop/vagrant-w$ vagrant init wheezy1 wheezy1
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.
giuseppe@macbian:~/Desktop/vagrant-w$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'wheezy1'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.

But this seems to take forever and at the end it ended up with an error:
So i decided to stop it

giuseppe@macbian:~/Desktop/vagrant-w$ VBoxManage list runningvms
"wheezy1" {7837d2c9-d561-437f-84ed-71cfb6a2ca7b}
"vagrant-w_1371395307" {8338f81d-4738-443c-a54d-86f25a35c4c2}

and on the initial terminal, i have:

The VM failed to remain in the "running" state while attempting to boot.
This is normally caused by a misconfiguration or host system incompatibilities.
Please open the VirtualBox GUI and attempt to boot the virtual machine
manually to get a more informative error message.

So i go back on my steps and on the “mother” wheezy i install some package to see if after installing these package I’m able to install LinuxGuestAdditions properly.

apt-get install xserver-xorg-video-dummy vnc4server x11-xserver-utils xterm wm2

This did not work. so i decided to install xorg as well (no more that 20 megs) I want to keep this image as small as possible.

apt-get -y install xorg

Then i run again the command

cd /media/cdrom
sh VBoxLinuxAdditions.sh

And at the end, after havin complained about missing OpenGL it worked!
Now i have even the GuestAdditions so it’s time to give it a try and repack.

Also i make myself sure that the user vagrant will be able to access with a private key to ssh.
To do this i type in the right directory:

giuseppe@macbian:~/Desktop/vagrant-w$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile "/home/giuseppe/.vagrant.d/insecure_private_key"
  IdentitiesOnly yes
  LogLevel FATAL

With this information, i start the “Mother” vm and i scp the public key (taken from this url: https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub) as it is the same for all the vagrant installations.
Then i checked that i was able to login with that key from another terminal on my machine and it seemed to work.

Now i have to repack back again the box file and check if that one works.

giuseppe@macbian:~$ vagrant package --base wheezy1 --output /home/giuseppe/wheezy.box
[wheezy1] Clearing any previously set forwarded ports...
[wheezy1] Creating temporary directory for export...
[wheezy1] Exporting VM...
[wheezy1] Compressing package to: /home/giuseppe/wheezy.box
giuseppe@macbian:~$ vagrant box add wheezy /home/giuseppe/wheezy.box 
Downloading or copying the box...
Extracting box...te: 25.9M/s, Estimated time remaining: 0:00:01)
Successfully added box 'wheezy' with provider 'virtualbox'!
giuseppe@macbian:~/Desktop$ mkdir vagrant-oracle
giuseppe@macbian:~/Desktop$ cd vagrant-oracle/
giuseppe@macbian:~/Desktop/vagrant-oracle$ vagrant init wheezy 
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.
giuseppe@macbian:~/Desktop/vagrant-oracle$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'wheezy'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
giuseppe@macbian:~/Desktop/vagrant-oracle$ vagrant ssh
Linux vmdebian 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jun 16 17:07:08 2013 from macbian.arnhem.chello.nl
vagrant@vmdebian:~$

It works!

In the next episode, we’ll see together how to configure the VM using a single file to be exchanged with your colleagues.

That’s all

Giuseppe 

(Visited 679 times, 1 visits today)

Author: Giuseppe Urso

Giuseppe lives in Haarlem now with his shiny dog, Filippa
In 1982 received his first home computer, a Commodore 64, followed by Datasette and a 1541 Floppy Disk Drive.
In 1999 he installed his first Linux distro (LRH6).
In 2006 he switched to Debian as favourite OS. Giuseppe Urso actively sustains the Free Software Fundation and his founder Richard Mattew Stallman, he speaks to people trying to convince them to join the fight now, and about how important is to use Free Software only.
He has a job as Infra Specialist at Hippo Enterprise Java Cms an Open Source Enterprise class Content Management System, one of the coolest company ever, in Amsterdam. He’s always ready to install Debian on other people computers for free.

Leave a Reply

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