02 January 2014

This post is dedicated to Vagrant

From the Vagrant webpage:

Create and configure lightweight, reproducible, and portable development environments

Using simple commands like:

Let's check a workflow to create VM's using vagrant:

This looks pretty straightforward and very convenient, let's have a deeper look at this.

Vagrant use boxes, that are pre-packer vms and a vagrant file, that have all the configuration for the specific box.

To get Vagrant boxes, have a look at vagrantbox.es

Once the pre-requirements are set, let's import the oracle65.box:

    alvaros-mini:~ alvarom$ vagrant box list
    There are no installed boxes! Use `vagrant box add` to add some.
    alvaros-mini:~ alvarom$ vagrant box add oracle65 ~/Downloads/oracle65.box
    Downloading box from URL: file:~/Downloads/oracle65.box
    Extracting box...te: 104M/s, Estimated time remaining: 0:00:01))
    Successfully added box 'oracle65' with provider 'virtualbox'!
    alvaros-mini:~ alvarom$ vagrant box list
    oracle65 (virtualbox)
    alvaros-mini:~ alvarom$

The command vagrant init will create a basic Vagrantfile to get this started.

We can later modify this Vagrantfile, or use one already created.

Let's create a basic box using this template

    alvaros-mini:~ alvarom$ mkdir box1
    alvaros-mini:~ alvarom$ cd box1/
    alvaros-mini:box1 alvarom$ vagrant init oracle65
    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.
    alvaros-mini:box1 alvarom$ vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    [default] Importing base box 'oracle65'...
    [default] Matching MAC address for NAT networking...
    [default] Setting the name of the VM...
    [default] Clearing any previously set forwarded ports...
    [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] Forwarding ports...
    [default] -- 22 => 2200 (adapter 1)
    [default] Booting VM...
    [default] Waiting for machine to boot. This may take a few minutes...
    [default] Machine booted and ready!
    [default] Mounting shared folders...
    [default] -- /vagrant
    alvaros-mini:box1 alvarom$

That's all.. pretty easy.

Connect to the VM:

    [vagrant@vagrant-oracle65 ~]$ hostname
    vagrant-oracle65
    [vagrant@vagrant-oracle65 ~]$ ifconfig
    eth0      Link encap:Ethernet  HWaddr 08:00:27:1D:AE:B5
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe1d:aeb5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:320 errors:0 dropped:0 overruns:0 frame:0
          TX packets:206 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:35017 (34.1 KiB)  TX bytes:28844 (28.1 KiB)

    lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

    [vagrant@vagrant-oracle65 ~]$ logout
    Connection to 127.0.0.1 closed.
    alvaros-mini:box1 alvarom$

Shutdown the VM:

    alvaros-mini:box1 alvarom$ vagrant halt
    [default] Attempting graceful shutdown of VM...
    alvaros-mini:box1 alvarom$

Let's modify the Vagrantfile to set an specific hostaname, add a second nic and set some other options:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :

    # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
    VAGRANTFILE_API_VERSION = "2"

    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      # Every Vagrant virtual environment requires a box to build off of.
      config.vm.box = "oracle65"

      # The url from where the 'config.vm.box' box will be fetched if it
      # doesn't already exist on the user's system.
      # config.vm.box_url = "http://domain.com/path/to/above.box"

      # Create a private network, which allows host-only access to the machine
      # using a specific IP.
      config.vm.network :private_network, ip: "192.168.56.11"

      config.vm.hostname = "box1"

      # Provider-specific configuration so you can fine-tune various
      # backing providers for Vagrant. These expose provider-specific options.
      # Example for VirtualBox:
      #
      config.vm.provider :virtualbox do |vb|
      #   # Don't boot with headless mode
      #   vb.gui = true
      #
      #   # Use VBoxManage to customize the VM. For example to change memory:
        vb.customize ["modifyvm", :id, "--memory", "6000"]
        vb.customize ["modifyvm", :id, "--cpus", "4"]
      end
      #
      # View the documentation for the provider you're using for more
      # information on available options.

    end

Once the Vagrantfile have been modified, let's start our machine again:

    alvaros-mini:box1 alvarom$ vagrant up
    Bringing machine 'default' up with 'virtualbox' provider...
    [default] Clearing any previously set forwarded ports...
    [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] Forwarding ports...
    [default] -- 22 => 2200 (adapter 1)
    [default] Running 'pre-boot' VM customizations...
    [default] Booting VM...
    [default] Waiting for machine to boot. This may take a few minutes...
    [default] Machine booted and ready!
    [default] Setting hostname...
    [default] Configuring and enabling network interfaces...
    [default] Mounting shared folders...
    [default] -- /vagrant
    [default] VM already provisioned. Run `vagrant provision` or use `--provision` to force it
    alvaros-mini:box1 alvarom$

Connect to the machine and verify:

    alvaros-mini:box1 alvarom$ vagrant ssh
    Last login: Thu Jan  2 14:23:06 2014 from 10.0.2.2
    [vagrant@box1 ~]$ hostname
    box1
    [vagrant@box1 ~]$ ifconfig
    eth0      Link encap:Ethernet  HWaddr 08:00:27:1D:AE:B5
          inet addr:10.0.2.15  Bcast:10.0.2.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe1d:aeb5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:527 errors:0 dropped:0 overruns:0 frame:0
          TX packets:330 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:56982 (55.6 KiB)  TX bytes:47053 (45.9 KiB)

    eth1      Link encap:Ethernet  HWaddr 08:00:27:B5:02:67
          inet addr:192.168.56.11  Bcast:192.168.56.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:feb5:267/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:6 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1662 (1.6 KiB)  TX bytes:588 (588.0 b)

    lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

    [vagrant@box1 ~]$ top
    top - 14:33:34 up 4 min,  1 user,  load average: 0.00, 0.01, 0.01
    Tasks: 131 total,   1 running, 130 sleeping,   0 stopped,   0 zombie
    Cpu0  :  0.0%us,  0.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    Cpu1  :  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    Cpu2  :  0.0%us,100.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    Cpu3  :  0.0%us,  0.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
    Mem:   5967708k total,   144904k used,  5822804k free,     1320k buffers
    Swap:  6143996k total,        0k used,  6143996k free,    57704k cached

Pretty impresive.

Let's do more stuff with our machine, let's say we need a set of specific packages once the machine is created, we will use standard command line arguments to install couple of packages.

Add to the vagrant file, the following line:

    config.vm.provision :shell, :inline => "cd /etc/yum.repos.d ; > public-yum-ol6.repo ; wget http://mini.home.kikitux.net/stage/mini-yum-ol6.repo"
    config.vm.provision :shell, :inline => "yum install -y mc"

The first line, will zero the public-yum-ol6.repo file, and download my private mini-yum-ol6.repo file, so the machine will be using my local yum repository. The second line will install one rpm for us.

As the virtual guest was already created, we need to run the provision manually.

    alvaros-mini:box1 alvarom$ vagrant provision
    [default] Running provisioner: shell...
    [default] Running: inline script
    --2014-01-02 14:46:15--  http://mini.home.kikitux.net/stage/mini-yum-ol6.repo
    Resolving mini.home.kikitux.net... 192.168.1.2
    Connecting to mini.home.kikitux.net|192.168.1.2|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 4354 (4.3K) [application/octet-stream]
    [default] Running provisioner: shell...
    [default] Running: inline script
    Loaded plugins: security
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package mc.x86_64 1:4.7.0.2-3.el6 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    ================================================================================
     Package     Arch            Version                  Repository           Size
    ================================================================================
    Installing:
     mc          x86_64          1:4.7.0.2-3.el6          ol6_latest          1.6 M

    Transaction Summary
    ================================================================================
    Install       1 Package(s)

    Total download size: 1.6 M
    Installed size: 5.4 M
    Downloading Packages:
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Installing : 1:mc-4.7.0.2-3.el6.x86_64                                    1/1
      Verifying  : 1:mc-4.7.0.2-3.el6.x86_64                                    1/1

    Installed:
      mc.x86_64 1:4.7.0.2-3.el6

    Complete!
    alvaros-mini:box1 alvarom$

Pretty cool, let's remove the VM, and start over again:

    alvaros-mini:box1 alvarom$ vagrant destroy
    Are you sure you want to destroy the 'default' VM? [y/N] y
    [default] Forcing shutdown of VM...
    [default] Destroying VM and associated drives...
    alvaros-mini:box1 alvarom$ 

    alvaros-mini:box1 alvarom$ vagrant up --provision
    Bringing machine 'default' up with 'virtualbox' provider...
    [default] Importing base box 'oracle65'...
    [default] Matching MAC address for NAT networking...
    [default] Setting the name of the VM...
    [default] Clearing any previously set forwarded ports...
    [default] Clearing any previously set network interfaces...
    [default] Preparing network interfaces based on configuration...
    [default] Forwarding ports...
    [default] -- 22 => 2222 (adapter 1)
    [default] Running 'pre-boot' VM customizations...
    [default] Booting VM...
    [default] Waiting for machine to boot. This may take a few minutes...
    [default] Machine booted and ready!
    [default] Setting hostname...
    [default] Configuring and enabling network interfaces...
    [default] Mounting shared folders...
    [default] -- /vagrant
    [default] Running provisioner: shell...
    [default] Running: inline script
    --2014-01-02 15:21:18--  http://mini.home.kikitux.net/stage/mini-yum-ol6.repo
    Resolving mini.home.kikitux.net... 192.168.1.2
    Connecting to mini.home.kikitux.net|192.168.1.2|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 4354 (4.3K) [application/octet-stream]
    [default] Running provisioner: shell...
    [default] Running: inline script
    Loaded plugins: security
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package mc.x86_64 1:4.7.0.2-3.el6 will be installed
    --> Processing Dependency: perl(strict) for package: 1:mc-4.7.0.2-3.el6.x86_64
    --> Processing Dependency: perl(bytes) for package: 1:mc-4.7.0.2-3.el6.x86_64
    --> Processing Dependency: perl(POSIX) for package: 1:mc-4.7.0.2-3.el6.x86_64
    --> Processing Dependency: perl(File::Temp) for package: 1:mc-4.7.0.2-3.el6.x86_64
    --> Processing Dependency: perl(File::Basename) for package: 1:mc-4.7.0.2-3.el6.x86_64
    --> Processing Dependency: /usr/bin/perl for package: 1:mc-4.7.0.2-3.el6.x86_64
    --> Processing Dependency: libgpm.so.2()(64bit) for package: 1:mc-4.7.0.2-3.el6.x86_64
    --> Running transaction check
    ---> Package gpm-libs.x86_64 0:1.20.6-12.el6 will be installed
    ---> Package perl.x86_64 4:5.10.1-136.el6 will be installed
    --> Processing Dependency: perl-libs = 4:5.10.1-136.el6 for package: 4:perl-5.10.1-136.el6.x86_64
    --> Processing Dependency: perl-libs for package: 4:perl-5.10.1-136.el6.x86_64
    --> Processing Dependency: perl(version) for package: 4:perl-5.10.1-136.el6.x86_64
    --> Processing Dependency: perl(Pod::Simple) for package: 4:perl-5.10.1-136.el6.x86_64
    --> Processing Dependency: perl(Module::Pluggable) for package: 4:perl-5.10.1-136.el6.x86_64
    --> Processing Dependency: libperl.so()(64bit) for package: 4:perl-5.10.1-136.el6.x86_64
    --> Running transaction check
    ---> Package perl-Module-Pluggable.x86_64 1:3.90-136.el6 will be installed
    ---> Package perl-Pod-Simple.x86_64 1:3.13-136.el6 will be installed
    --> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.13-136.el6.x86_64
    ---> Package perl-libs.x86_64 4:5.10.1-136.el6 will be installed
    ---> Package perl-version.x86_64 3:0.77-136.el6 will be installed
    --> Running transaction check
    ---> Package perl-Pod-Escapes.x86_64 1:1.04-136.el6 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    ================================================================================
     Package                   Arch       Version              Repository      Size
    ================================================================================
    Installing:
     mc                        x86_64     1:4.7.0.2-3.el6      ol6_latest     1.6 M
    Installing for dependencies:
     gpm-libs                  x86_64     1.20.6-12.el6        ol6_latest      28 k
     perl                      x86_64     4:5.10.1-136.el6     ol6_latest      10 M
     perl-Module-Pluggable     x86_64     1:3.90-136.el6       ol6_latest      39 k
     perl-Pod-Escapes          x86_64     1:1.04-136.el6       ol6_latest      32 k
     perl-Pod-Simple           x86_64     1:3.13-136.el6       ol6_latest     211 k
     perl-libs                 x86_64     4:5.10.1-136.el6     ol6_latest     577 k
     perl-version              x86_64     3:0.77-136.el6       ol6_latest      50 k

    Transaction Summary
    ================================================================================
    Install       8 Package(s)

    Total download size: 13 M
    Installed size: 41 M
    Downloading Packages:
    --------------------------------------------------------------------------------
    Total                                            15 MB/s |  13 MB     00:00
    warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
    Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
    Importing GPG key 0xEC551F03:
     Userid : Oracle OSS group (Open Source Software group) <build@oss.oracle.com>
     Package: 6:oraclelinux-release-6Server-5.0.2.x86_64 (@anaconda-OracleLinuxServer-201311261449.x86_64/6.5)
     From   : /etc/pki/rpm-gpg/RPM-GPG-KEY-oracle
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
      Installing : 1:perl-Pod-Escapes-1.04-136.el6.x86_64                       1/8
      Installing : 4:perl-libs-5.10.1-136.el6.x86_64                            2/8
      Installing : 1:perl-Pod-Simple-3.13-136.el6.x86_64                        3/8
      Installing : 3:perl-version-0.77-136.el6.x86_64                           4/8
      Installing : 1:perl-Module-Pluggable-3.90-136.el6.x86_64                  5/8
      Installing : 4:perl-5.10.1-136.el6.x86_64                                 6/8
      Installing : gpm-libs-1.20.6-12.el6.x86_64                                7/8
      Installing : 1:mc-4.7.0.2-3.el6.x86_64                                    8/8
      Verifying  : 1:perl-Module-Pluggable-3.90-136.el6.x86_64                  1/8
      Verifying  : gpm-libs-1.20.6-12.el6.x86_64                                2/8
      Verifying  : 1:perl-Pod-Escapes-1.04-136.el6.x86_64                       3/8
      Verifying  : 4:perl-5.10.1-136.el6.x86_64                                 4/8
      Verifying  : 4:perl-libs-5.10.1-136.el6.x86_64                            5/8
      Verifying  : 1:perl-Pod-Simple-3.13-136.el6.x86_64                        6/8
      Verifying  : 1:mc-4.7.0.2-3.el6.x86_64                                    7/8
      Verifying  : 3:perl-version-0.77-136.el6.x86_64                           8/8

    Installed:
      mc.x86_64 1:4.7.0.2-3.el6

    Dependency Installed:
      gpm-libs.x86_64 0:1.20.6-12.el6
      perl.x86_64 4:5.10.1-136.el6
      perl-Module-Pluggable.x86_64 1:3.90-136.el6
      perl-Pod-Escapes.x86_64 1:1.04-136.el6
      perl-Pod-Simple.x86_64 1:3.13-136.el6
      perl-libs.x86_64 4:5.10.1-136.el6
      perl-version.x86_64 3:0.77-136.el6

    Complete!
    alvaros-mini:box1 alvarom$

Awesome.

  • oracle 6
  • linux 7
  • vagrant 2