Skip to content

Instantly share code, notes, and snippets.

@djoreilly
Last active August 29, 2015 14:03
Show Gist options
  • Save djoreilly/01f2ddbf55c7cbfb59ad to your computer and use it in GitHub Desktop.
Save djoreilly/01f2ddbf55c7cbfb59ad to your computer and use it in GitHub Desktop.
Using Vagrant

#Using Vagrant Vagrant does the grunt work of creating VirtualBox VMs.

Install Vagrant

sudo apt-get install vagrant

Create a Vagrant Box

mkdir devstack1
cd devstack1

Create a file named Vagrantfile as follows (configure the number CPUs, RAM and networks to what you need).

Vagrant.configure("2") do |config|

  config.vm.box = "trusty"
  # download happens first time only
  config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"

  config.vm.hostname = "devstack"

  # forward ssh keys from main host - handy for gerrit and github
  config.ssh.forward_agent = true

  # eth1 will be the primary interface for access to the vagrant box.
  # It's plugged into a virtualbox bridge, so its IP can be accessed
  # directly from the main host without nat port-forwarding hassle.
  # You can configure devstack so OpenStack services listen on this
  # address by putting HOST_IP=192.168.2.2 in localrc
  config.vm.network :private_network, ip: "192.168.2.2"

  # Add eth2 and eth3 - these can be used by Neutron or nova-network
  # It's necessary to specify IPs here - but they won't be configured.
  config.vm.network :private_network, ip: "192.168.3.2", auto_config: false
  config.vm.network :private_network, ip: "192.168.4.2", auto_config: false

  config.vm.provider "virtualbox" do |v|
    v.customize ["modifyvm", :id, "--cpus", "4"]
    v.customize ["modifyvm", :id, "--memory", "6000"]
    # enable serial port just in case vagrant image does not
    v.customize ["modifyvm", :id, "--uart1", "0x3F8", 4]
  end

end

Note: Vagrant configures and uses the guest's first NIC (eth0 for an Ubuntu VM) for NAT. Don't try to fight this. The VM's default route will use it and access to the Internet should work.

More Ubuntu images for Vagrant can be found here.

See this example if you need the VM to have additional hard disks.

Start it

vagrant up

Connect to it

vagrant ssh

Stop it

Power down to save CPU cycles and memory on the host.

vagrant halt

To start it again do vagrant up

Delete it

Do it this way instead of deleting the VM directly in VirtualBox:

vagrant destroy

Help

$ vagrant 
Usage: vagrant [options] <command> [<args>]

    -v, --version                    Print the version and exit.
    -h, --help                       Print this help.

Common commands:
     box             manages boxes: installation, removal, etc.
     connect         connect to a remotely shared Vagrant environment
     destroy         stops and deletes all traces of the vagrant machine
     global-status   outputs status Vagrant environments for this user
     halt            stops the vagrant machine
     help            shows the help for a subcommand
     init            initializes a new Vagrant environment by creating a Vagrantfile
     login           log in to Vagrant Cloud
     package         packages a running vagrant environment into a box
     plugin          manages plugins: install, uninstall, update, etc.
     provision       provisions the vagrant machine
     rdp             connects to machine via RDP
     reload          restarts vagrant machine, loads new Vagrantfile configuration
     resume          resume a suspended vagrant machine
     share           share your Vagrant environment with anyone in the world
     ssh             connects to machine via SSH
     ssh-config      outputs OpenSSH valid configuration to connect to the machine
     status          outputs status of the vagrant machine
     suspend         suspends the machine
     up              starts and provisions the vagrant environment
     version         prints current and latest Vagrant version

For help on any individual command run `vagrant COMMAND -h`

Additional subcommands are available, but are either more advanced
or not commonly used. To see all subcommands, run the command
`vagrant list-commands`.

Sample output

~/vags/devstack1$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'trusty' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Adding box 'trusty' (v0) for provider: virtualbox
    default: Downloading: https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box
==> default: Successfully added box 'trusty' (v0) for 'virtualbox'!
==> default: Importing base box 'trusty'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: devstack1_default_1404486000137_95945
==> 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: Adapter 1: nat
    default: Adapter 2: hostonly
    default: Adapter 3: hostonly
    default: Adapter 4: hostonly
==> 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: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /home/oreillyd/vags/devstack1


~/vags/devstack1$ vagrant ssh
Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.13.0-30-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Jul  4 15:00:25 UTC 2014

  System load:  0.08              Processes:           115
  Usage of /:   2.7% of 39.34GB   Users logged in:     0
  Memory usage: 2%                IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud

0 packages can be updated.
0 updates are security updates.


vagrant@devstack:~$ free
             total       used       free     shared    buffers     cached
Mem:       5967020     376804    5590216        680      12376     205008
-/+ buffers/cache:     159420    5807600
Swap:            0          0          0


vagrant@devstack:~$ grep processor /proc/cpuinfo 
processor	: 0
processor	: 1
processor	: 2
processor	: 3


vagrant@devstack:~$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:e6:fa:fc brd ff:ff:ff:ff:ff:ff
    inet 10.0.2.15/24 brd 10.0.2.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fee6:fafc/64 scope link 
       valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:69:27:f3 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.2/24 brd 192.168.2.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fe69:27f3/64 scope link 
       valid_lft forever preferred_lft forever
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 08:00:27:68:1b:d3 brd ff:ff:ff:ff:ff:ff
5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 08:00:27:3b:93:d3 brd ff:ff:ff:ff:ff:ff


vagrant@devstack:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        40G  1.1G   37G   3% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            2.9G   12K  2.9G   1% /dev
tmpfs           583M  360K  583M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            2.9G     0  2.9G   0% /run/shm
none            100M     0  100M   0% /run/user
vagrant         219G   25G  195G  12% /vagrant


vagrant@devstack:~$ ls /vagrant/
Vagrantfile


Accessing the guest's filesystem from the host

sshfs is a handy way to edit files in the guest using a GUI editor on the host. On the host add this to ~/.ssh/config

Host 192.168.2.*
    User vagrant
    IdentityFile /home/oreillyd/.vagrant.d/insecure_private_key

Create a mountpoint and mount the guest's root filesystem:

mkdir -p guest-fs
sshfs 192.168.2.2:/ guest-fs

Umount with:

fusermount -u guest-fs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment