create VMs on Ubuntu 12.04 with Vagrant

Now that I have two separate PCs running Ubuntu 12.04 server, I want to start two VMs on each server through Vagrant.

to install vagrant , first need to install Oracle VirtualBox
below is tested working

wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add –
sudo -s echo “deb http://download.virtualbox.org/virtualbox/debian precise contrib” >> ~/virtualbox.list
sudo mv ~/virtualbox.list /etc/apt/sources.list.d/
sudo apt-get update
sudo apt-get install linux-headers-$(uname -r)
sudo apt-get purge virtualbox virtualbox-dkms virtualbox-ose-qt virtualbox-qt
sudo apt-get install virtualbox virtualbox-dkms virtualbox-ose-qt virtualbox-qt
install vagrant
wget http://files.vagrantup.com/packages/eb590aa3d936ac71cbf9c64cf207f148ddfc000a/vagrant_1.0.3_i686.deb
sudo dpkg -i vagrant_1.0.3_i686.deb

/opt/vagrant/bin/vagrant
Now vagrant is ready on the host box

1, create a basic Ubuntu 12.04 vagrant box
/opt/vagrant/bin/vagrant box add precise32 http://files.vagrantup.com/precise32.box
mkdir cub1
cd cub1
/opt/vagrant/bin/vagrant init
vi Vagrantfile, with below

config.vm.box = “precise32”
config.vm.network :bridged
config.vm.customize [“modifyvm”, :id, “–memory”, 1024]
config.vm.share_folder “hostshared”, “/tmp/share”, “/tmp/share”
config.vm.provision :shell, :inline => “ifconfig”

Now start vm
/opt/vagrant/bin/vagrant up
/opt/vagrant/bin/vagrant ssh
ifconfig to get the assigned IP address

2, add my user
ssh vagrant@IP (password vagrant)
add new user and put into admin group (so it can sudo)
sudo useradd -d /home/new-user -g admin -m new-user
sudo passwd new-user
exit
then ssh to VM with new user
ssh new-user@IP
make the sudo passwordless
sudo visudo
add at bottom
user-name ALL=(ALL)NOPASSWD: ALL
exit

on the host machine, generate key pair if not yet
ssh-keygen -t rsa
ssh new-user@vm-ip mkdir -p .ssh
cat ~/.ssh/id_rsa.pub | ssh new-user@vm-ip ‘cat >> .ssh/authorized_keys’
now you can ssh new-user@vm-ip without entering password.

3, repackage
cp Vagrantfile Vagrantfile.package
vi Vagrantfile.package
add
config.ssh.username = “new-user”
config.ssh.private_key_path = “~/.ssh/id_rsa”
config.package.name = “precise32cub1base”
(did not work)
run command to create the new base box from VM
/opt/vagrant/bin/vagrant package –vagrantfile ./Vagrantfile.package
This will create a base box named “precise32cub1base” in local directory
(in my testing it did not work though, the file still named package.box)
add to local vagrant
/opt/vagrant/bin/vagrant box add precise32cub1base ./package.box
/opt/vagrant/bin/vagrant box list

test the new base box at local
mkdir ~/test-box
cd ~/test-box
/opt/vagrant/bin/vagrant init
vi Vagrantfile
change config.vm.box = “precise32cub1base”

start VM
/opt/vagrant/bin/vagrant up

test with new user
ssh new-user@IP (works without entering password)

to use this new base box on other hosts, make sure to copy the id_rsa key file to the ~/.ssh directory
and mkdir /tmp/share because the packaged Vagrantfile is included whenever you create VM with this base box

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s