Tuesday, November 29, 2011

Creating base box from scratch for Vagrant


At vagrantbox.es, you can find boxes for many flavours like CentOS, Ubuntu, Debian etc.

How ever, you might require a flavour of OS that is not available packaged for you already.
In such a case, you might want to package it for use with Vagrant.
I needed Oracle Enterprise Linux Box.

Following is a step by step approach to create a base box for Oracle Enterprise Linux 5.7 64 bit version.


Creating a VM on VirtualBox

Step 1: Get the ISO file from which we will install the Oracle Enterprise Linux.

Step 2: Create your virtual machine on VirtualBox.

  Create a new Virtual Machine 
      Type: VMDK
      Name : oel57
      Base memory size: 512 MB, Memory Space Maximum 40 GB
      Enable Host I/O cache



Step 3: Attach the installer ISO to cdrom: Settings -> Storage -> CD ROM

Step 4: Start the virtual machine and follow the OS installer instructions.

    Section1: Initialize drive and erase data: yes
    Section2: Disable IPv6, leave IPv4 enabled with DHCP
              Configure network: yes
              Set hostname to vagrantoel5 & Domain to vagrant.com
    Section3: Root password: vagrant
    Section4: Set the apprpriate time zone
    Section5: Customize the package installation
              Deselect everything and Dial-up networking, Text-based internet
              Select Base, development tools & libraries
              Remove GUI - GTK, KDE, X11
    Section6: After reboot
              Disable firewall
              Run Services: Disable cups

This will install a lot of unwanted packages. Try to reduce it to minimum. Reboot the VM after installation.


Configuring the new VM

Step 5: Before, you move on, check if eth0 has been assigned an IP
      ifconfig
      inet address: 10.0.2.15 ...
      ....

Step 6: Installing VirtualBox Guest Additions
      From VirtualBox/VM Menu -> Devices -> Install Guest Additions
      On the running VM:
        mkdir /media/VirtualBoxGuestAdditions
        mount -r /dev/cdrom /media/VirtualBoxGuestAdditions
        cd /media/VirtualBoxGuestAdditions
        sh ./VBoxLinuxAdditions.run
        reboot

Step 7: Restart VM


Easing working on VM

Step 11: Create vagrant account
      groupadd admin
      useradd -G admin vagrant
      passwd vagrant
      # choose "vagrant" as the password

Step 12: Changes to visudo for vagrant usage as root
    visudo changes:
    
       %admin          ALL=(ALL)       NOPASSWD: ALL
    
       Defaults    env_keep = .. SSH_AUTH_SOCK PATH <-- add in last line
    
       # Defaults   !visiblepw   <--- Comment it
       # Defaults    requiretty   <--- Comment it

Step 13: Basic steps for vagrant usage as root
      su - vagrant
      echo ‘export PATH=$PATH:/usr/sbin:/sbin’ >> .bashrc
      exit
    
      /etc/init.d/sudo restart


Package required for vagrant usage

Login as vagrant user and do the following

Step 13: Ruby
           
             Install RVM
        curl -s https://rvm.beginrescueend.com/install/rvm -o rvm-installer
        chmod +x rvm-installer
        sudo ./rvm-installer --version latest
        exit the shell and relogin as vagrant user
           
            Additional installs required
        yum install -y gcc-c++ patch readline readline-devel 
             zlib zlib-devel libyaml-devel
             libffi-devel openssl-devel make bzip2 autoconf 
             automake libtool bison iconv-devel
           
             Install Ruby
        sudo rvm install 1.9.2


Package required for vagrant usage

Login as vagrant user and do the following

Step 14: Setup yum repository
      Install Oracle repository
        cd /etc/yum.repos.d
        wget http://public-yum.oracle.com/public-yum-el5.repo
        Change enabled=0 to enabled=1
    
      Install EPEL repo
        sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/5Server/x86_64/epel-release-5-4.noarch.rpm
 

Step 15: Puppet, Chef & SSH
     Install Puppet: sudo yum install puppet
     Install Chef :sudo gem install chef --no-rdoc --no-ri
     Install SSH

Allow automatic ssh for vagrant

Step 16: Add vagrant insecure public key
     mkdir .ssh 
     chmod 755 .ssh 
     wget --no-check-certificate 
     https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub
     cat vagrant.pub > .ssh/authorized_keys 
     chmod 644 .ssh/authorized_keys

Step 17: Cleanup and shutdown virtual machine
     sudo /sbin/ifconfig
     write down MAC address of eth0 (eg. 08:00:27:60:AE:41)

     sudo yum clean headers packages dbcache expire-cache
     shutdown now


Finally!!: Packaging Base Box

Step 18: Package vagrant box
     # Use the same name as there in VirtualBox
     vagrant package --base oel57
    
     # It creates a file called package.box in the same directory
     # Rename it for distribution & download
     mv package.box to oel57.box
 
Step 19: Try using it
     vagrant box add oel57 oel57.box
     mkdir test_environment
     cd test_environment
     vagrant init oel57
     vagrant up
     vagrant ssh

Taste your base box !!

Step 20: Add the base box to vagrant repository
     vagrant box add oel57 oel57.box
    
     vagrant box list  <--- outputs all the boxes available for use
    
        lucid64
        oel57
        puppet-rhel-6-64

Step 21: Try using it
     mkdir test_environment
     cd test_environment
     vagrant init oel57
     vagrant up
     vagrant ssh

12 comments:

  1. Thanks for writing up your experiences with creating a vagrant base box from scratch. It nicely bridges the gap between the vagrant documentation and my own experiences.

    I would like to point out one minor improvement. In step 16 I was missing an closing angle bracket. The cat command should read more like 'cat vagrant.pub > .ssh/authorized_keys'

    ReplyDelete
  2. Thank you very much for this article, I'll try to create Fedora 16 vagrant image now (it is missing at vagrantbox.es).

    Also my question is - is it required to add shared folder "/vagrant" manually, or it will be created automatically by vagrant? http://vagrantup.com/docs/getting-started/ssh.html bottom of the page.

    If folder should be created manually, could you add step(s) for it?

    ReplyDelete
    Replies
    1. Hello Dionio,

      You don't need to create that in Base box image that you are creating. The Shared folder should be defined in the Vagrant configuration. While loading up the VM, Vagrant will do the needful mapping for you.

      Delete
  3. Hi,

    Thank you very very much for this article. I desperately and definitely needed this documentation for creating a Fedora Base Box which I need to configure it and send it to one of my client. This helped me a lot. Thanks again.

    Srinivas G

    ReplyDelete
  4. Hi,

    Thank you Ashish. I was able to build a Vagrant Base Box of Fedora 17. I am herewith providing a link for anyone who wants the Fedora 17 vagrant base box to be downloaded and use it right away.

    The link is: http://www.vertizone.com/vagrant

    Thanks,
    Srinivas G

    ReplyDelete
    Replies
    1. Great!. Thanks for providing the link to Fedora 17 box. It will be quite useful.

      Delete
  5. You might want to look at veewee to create baseboxes . it acts as a plugin

    https://github.com/jedi4ever/veewee

    ReplyDelete
  6. Hey there! Thanks for this, but I am running into a problem when I run the final packaging of the box file. I get "The box Ubuntu could not be found. That's what I named it and that's what I see in Virtualbox. Google is no help from what I've seen so far. Any ideas?

    ReplyDelete
    Replies
    1. Hi Travis, Sorry for late reply. What you see in virtualbox is the name of the VM. Look into the properties for the actual name of vmdk / vbox file.

      Delete
  7. Hi Ashish,

    Thanks for the wonderful article. I was able to create the box. But now when i try to init the machine via vagrant it does not log into the machine and timeouts. But if i do vagrant ssh in next terminal i am able to log into the machine. Any idea what can be the issue?

    ReplyDelete
  8. Very nice post, thanks for sharing the information. Keep up the good work.
    cheap box printing

    ReplyDelete