Thursday, October 6, 2016

add vagrant image manually

vagrant is a tool that we can use to create manage virtual environment easily. it require just a few command to setup setup a new virtual machine and access to it.
Normally it will download the image from its website, it is usually  bigger than 300MB in size, so if you don't want to download from it website, and want to customize the image your self follow on.


*** prepare virtual machine
1- setup your virtual machine, for example use virtualbox to install a machine name yakkety64 and install ubuntu 16.10,

2- after complete install the ubuntu to the virtual machine, and install all your tools you like, then do the following
- create user vagrant with password vagrant
$ sudo useradd -m vagrant
$ sudo passwd vagrant

- add ssh authorized keys, in /home/vagrant/.ssh/authorized_keys with the following content
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key

- add vagrant to sudo group
$ sudo usermod -a -G sudo vagrant

- update sudoer file to allow vagrant to sudo without asking password, add the following like to the file  /etc/sudoers
vagrant ALL=(ALL) NOPASSWD:ALL

-make sure the the ethe0 using dhcp, the /etc/network/interfaces look like below
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp



- install the virtualbox additional tool

3- set the network of the virtual machine to "NAT"

4- shut the the virtual machine and export it, using the command below
$ vboxmanage export ubun -o yakkety.ovf


*** add the vm to vagrant
1- we are going to add ubuntu/yakkety vagrant, the structure of file/directory of it will be like below
~$ tree .vagrant.d/boxes/ubuntu-VAGRANTSLASH-yakkety64/
.vagrant.d/boxes/ubuntu-VAGRANTSLASH-yakkety64/
├── 1.1.0
│   └── virtualbox
│       ├── box.ovf
│       ├── metadata.json
│       ├── Vagrantfile
│       ├── yakkety-disk1.vmdk
│       └── yakkety.ovf
└── metadata_url

2 directories, 6 files



2- so create the directory structure like above, and update the file as below

- ~/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-yakkety64/metadata_url, the url must be match with the url of vagrant url that host the image, if not it will download it again, so the url for the ubuntu/yakkety is below
https://atlas.hashicorp.com/ubuntu/boxes/yakkety64

- ~/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-yakkety64/1.1.0/virtualbox/metadata.json
 {"provider":"virtualbox"}

- ~/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-yakkety64/1.1.0/virtualbox/Vagrantfile
 Vagrant::Config.run do |config|
  config.vm.base_mac = "080027880CA6"
end
include_vagrantfile = File.expand_path("../include/_Vagrantfile", __FILE__)
load include_vagrantfile if File.exist?(include_vagrantfile)


- copy the file yakkety-disk1.vmdk and yakkety.ovf in section "prepare virtual machine", step 4 to directory ~/.vagrant.d/boxes/ubuntu-VAGRANTSLASH-yakkety64/1.1.0/virtualbox

- then copy  yakkety.ovf as box.ovf in the same directory


*** work with vagrant
1- list all virtual machine we already have
$ vagrant box list
hashicorp/precise64 (virtualbox, 1.1.0)
ubuntu/yakkety64    (virtualbox, 1.1.0)


2- init the ubuntu/yakkety64
$ mkdir newtest
$ cd newtest
$ vagrant init ubuntu/yakkety64
$ vagrant up

- then until it finishes its take,  then we can

-ssh to it
$ vagrant ssh

- suspend it
$ vagrant suspend

- resume it
$ vagrant resume

- when you done it, you can delete it
$ vagrant destroy


No comments:

Post a Comment