Vagrant

Vagrant Example Use Case


singhub/ folder has a 
cp Vagranfile.centos7 Vagrantfile 
which download a ready-to-run virtualbox image, eg

	http://cloud.centos.org/centos/7/vagrant/x86_64/images/CentOS-7-x86_64-Vagrant-1805_01.VirtualBox.box
	http://cloud.centos.org/centos/8/vagrant/x86_64/images/CentOS-8-Vagrant-8.0.1905-1.x86_64.vagrant-virtualbox.box

the provision section could run shell commands
OR it can invoke ansible
and execute the vagranfile_playbook.yml

which does some basic setup/cleansing.

Could have this to create VM on Zink for use.
eg Various OS flavor to test out Singularity in them.


PS.
Ansible is not invoking Vagrant to create VM.
VM creation use ansible in two places
- as part of vagrant provision
- IFF full desktop experience inside the VM desired: once machine is a running vm, could run my cueball/ANSIBLE/wsl_locahost_playbook.yml and treat it like any other machine I use.

- historically may have tried osboxes VirtualBox image, but the centos cloud vagrant/box is more scriptable.

Vagrant Annoyances

https://crysol.org/recipe/2015-11-17/vagrant-vdi-virtual-disk-for-virtualbox.html#.X1fWn8jYqUk
confirmed my frustration, vagrant creation of vbox vm are always vmdk, even when vbox default to vdi.
and even if Vagrantfile has ...

 config.vm.disk :disk, name: "root", size: "29GB", disk_ext: "vdi", primary: true

trying to tap into provider specific clause
vb.customize ["modifyhd", :id, "--resize", "44"]
didn't help, either :id resolve to uuid and not full path?  or, less likely, vmdk can't be resized by vbox via cmd vagrant fed to it...

and the annoying part is that the error is not obvious, the documentation page
https://www.vagrantup.com/docs/disks/configuration
implies that vdi was supported (and actually default by vagrant for vbox vm).  LIE LIE LIE !!! :-\

work around:
tried using a two steps to create a vdi file for disk, then create vm with that disk, but that was full of pitfalls as well.
Finally, the clause 
    config.disksize.size = "60GB"
worked and created vdi file, and sized disk correctly!
ie, don't use config.vm.disk nor config.vm.diskside.size !!


other annoying thing is
unable to create a nic in vagrant when ip assigned is same as host.
i wanted it to be bridge.
it works, created vbox vm manually that way.
but vagrant just stubbornly refuses.


would be nice if their nic definition support defining default gateway, netmask, nameserver, maybe even vlan tag.

vagrant is good for providing simple vm for simple use on desktop.
or in cloud env with use case they thought of?
or using VMware is better?  doubt this, since they are supposed to be provider agnostic and so many of the advanced feature are not exposed 

overall, if not cuz don't want to deal with gui and want a cli, may not really want to use vagrant.
Vagrant gateway assignment is another headache. eg see: vagrant github issue 2389 it was closed, without a proper solution. At least, there is still no way to specify a static default gateway to a bridge interface :-\

Vagrant 101


vagrant init 			# will seed a .vagrant/ dir and a new Vagrantfile in current dir
--or--
vi Vagrantfile			# create/edit/cp Vagrantfile.centos8 Vagrantfile

vagrant up


vagrant provision		# (re)run provision section of Vagrantfile 
vagrant provision --debug 	# add debug output to above

	The provision section could run a shell script
	Or a lot of folks also have it run ansible and execute a playbook

vagrant up --provision | tee provision.log	# run vagrant up and forces provision to run

# works on osx sierra, but not on mint rafaela, which needed:
vagrant box add centos/7 https://vagrantcloud.com/centos/boxes/7/versions/1708.01/providers/virtualbox.box   ## this one works!
# see https://github.com/hashicorp/vagrant/issues/5016
# but in mint, still can't do vagrant up, something crashes , but that maybe virtualbox bug... oh, my linux box had been acting up...

vagrant status
vagrant global-status		# multiple Vagrantfile, or global farm 
vagrant suspend			# hibernate vm
vagrant resume			# resume vm after hibernation
vagrant halt			# power off vm
vagrant reload 			# restarts vagrant machine, loads new Vagrantfile configuration
vagrant reload --provision 	# Same as above, but also force provisioning


vagrant box list -i		# -i give extra info.  but container name with /n doesn't show up!  bug??
vagrant box remove -h 		# -h for help on specific subcommand
vagrant box remove foobar/7 virtualbox


#--vagrant destroy		# this will DELETE the box and ...

vagrant push 			# push code with vagrant...
vagrant ssh 			# ssh to vagrant vm.  it has password?  ssh key??

Vagrantfile

Example Vagrantfile.

Multiple instances

There are likely many scenarios where multiple instances are needed, eg multiple machine to support a app/db environment. Those could be in a vagrantfile with multiple instances defined in it.

Here, i cover the case for creating multiple VM that are independent of one another, for various sys admin testings.
eg, a Vagrantfile defining a VM for CentOS 7, another for 8, yet another for FreeBSD 11 or an Ubuntu machine.

Because vagrant expect one directory to host one config file, the fact that one can use VAGRANT_VAGRANTFILE to specify an alternate definition file doesn't really help. This is because there is a .vagrant dir hosting quite a bit of config.
Thus, it is best to create one directory for each test env. This way, VBoxManage list vms will also list machines prefixed with the folder name, thus aiding finding out what is what. eg
"vagrant_centos7_default_1592008049000_63100" {7bfb7f7f-226b-4091-a49a-92d55999f98a}
"vagrant_freeBSD_default_1592249070181_35687" {eea57223-a976-4a5d-a0d7-9deff2d3b748}


Furthermore, one now just need to cd to the right directory to do
vagrant status
vagrant ssh
etc without having to prefix each command with VAGRANT_VAGRANTFILE=Vagrantfile.centos8 or having set it in an export and forgot to change it :)

Dir Structure


$HOME/.vagrant.d/ ...

.vagrantfile/		# store info about VM created by vagrant up

VM images, if using VirtualBox, is in $HOME/VirtualBox VMs/VMNAME ie, a location virtual box always use, not dependent on Vagrantfile...
see vboxmanage list vms --long


Vagrantfile does not allow specifying location of the .vdi file (only for DVD iso)
ie, clause like this doeesn't get heeded for the path:
  config.vm.disk :disk, size: "112GB", primary: true, path: "/clusterfs/gretadev/data/VBoxVM/greyhound"

https://stackoverflow.com/questions/36332996/store-vagrant-vm-in-custom-folder
perhaps:

VBoxManage setproperty machinefolder /clusterfs/gretadev/data/VBoxVM
vagrant up
	

Ref: Using alternate Vagrantfile was dicussed in https://stackoverflow.com/questions/23888381/how-to-run-several-boxes-with-vagrant but i think that point is moot now.

Installation

Mint:    
sudo apt-get install vagrant virtualbox
sudo apt     install virtualbox-qt       # needed by startvm hexcode --type gui

Centos7: 
yum -y install https://releases.hashicorp.com/vagrant/1.9.6/vagrant_1.9.6_x86_64.rpm
# https://www.tecmint.com/how-to-install-vagrant-on-centos-7/ says vagrant can use docker too...
# but not so sure.  vagrant ask for VirtualBox, VMware, hyperV.  

# yum -y install virt-what              # inside VM, tell/guess what env is in.

Mac:
cd ~/Downloads
wget https://releases.hashicorp.com/vagrant/2.0.0/vagrant_2.0.0_x86_64.dmg
sudo hdiutil attach vagrant_2.0.0_x86_64.dmg
sudo installer -package /Volumes/Vagrant/vagrant.pkg -target /
# installed to /opt/vagrant with link in /usr/local
sudo hdiutil detach /Volumes/Vagrant

wget http://download.virtualbox.org/virtualbox/5.1.28/VirtualBox-5.1.28-117968-OSX.dmg
sudo hdiutil attach VirtualBox-5.1.28-117968-OSX.dmg 
sudo installer -package /Volumes/VirtualBox/VirtualBox.pkg -target /
# installed to /Applications/VirtualBox.app
sudo hdiutil detach /Volumes/VirtualBox/

Links

Virtual Box


/bin/VirtualBox # GUI

/usr/bin/vboxmanage  # cli tool
vboxmanage list vms
vboxmanage list vms --long

vboxmanage list runningvms
vboxmanage list vms --long   | egrep Name:\|State  # find out if VM is on or off

vboxmanage showvminfo VMNAME

/usr/bin/VBoxManage  startvm   "springer"           --type headless
/usr/bin/VBoxManage  controlvm "springer"  poweroff --type headless


# eg from https://www.oracle.com/technical-resources/articles/it-infrastructure/admin-manage-vbox-cli.html

VBoxManage modifyvm OracleLinux6Test --cpus 2 --memory 2048 --vram 12 # most can be handled by vagrant
VBoxManage modifyvm OracleLinux6Test --nic1 bridged --bridgeadapter1 eth0
VBoxManage createhd --filename /path/to/hard_drive_image/OracleLinux6Test.vdi --size 5120 --variant Standard
VBoxManage storagectl OracleLinux6Test --name "SATA Controller" --add sata --bootable on
VBoxManage storageattach OracleLinux6Test --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium /path/to/hard_drive_image/OracleLinux6Test.vdi


create VM via cli and osboxes images

Vagrant sucks if you need it to do any sort of things with network. If it is not DHCP (or nat), if you need to manually set gateway or dns, maybe vagrant is not a good choice. I gave up on a machine needing two nics, public vs inside, using bridge connection. vagrant was like a ghost and keep making changes to my network settings. and it refuse to setup some network if the ip range "crash" with the vbox network.

better off setup vm manually. Get a .vdi file from osboxes.org, so that there is no need to install the os via iso. Command example below adapeted from this post

VBoxManage setproperty machinefolder /clusterfs/data/VBoxVM
VBoxManage list ostypes | grep RedHat

VBoxManage createvm --name greyhound-c7 --ostype "RedHat_64" --register --basefolder /clusterfs/data/VBoxVM   # vm2

wget file from osboxes
7za e download.7z

VM_NAME=greyhound-c7
VMBaseFolder=/clusterfs/data/VBoxVM
#eventually move this vdi file into the folder of the VM

VBoxManage modifyvm $VM_NAME --ioapic on
VBoxManage modifyvm $VM_NAME --memory 6100 --vram 128
VBoxManage modifyvm $VM_NAME --nic1  nat # default
VBoxManage modifyvm $VM_NAME --nic1 bridged
VBoxManage modifyvm $VM_NAME --bridgeadapter1 eno2  #  pub , enp0s3
VBoxManage modifyvm $VM_NAME --nic2 bridged
VBoxManage modifyvm $VM_NAME --bridgeadapter2 enp94s0f0 # internal , enp0s8
VBoxManage storagectl $VM_NAME --name "SATA Controller" --add sata --controller IntelAhci

VBoxManage internalcommands sethduuid /clusterfs/data/VBoxVM/grey-c7/grey-c7_Disk.vdi # assign a new (random?)  uuid to the vdi.  does downloading agai from osboxes generate a new uuid?
VBoxManage storageattach $VM_NAME --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium  /clusterfs/data/VBoxVM/grey-c7/grey-c7_Disk.vdi


---- add and resize hdd ----

BoxManage createhd --filename /clusterfs/data/VBoxVM/grey/grey_Disk2.vdi  --size 64000 --format VDI

vboxmanage list hdds
VBoxManage modifymedium disk /clusterfs/gdata/VBoxVM/grey/grey_Disk2.vdi  --resize 538000 # size is in MBytes

VBoxManage storageattach greyhound-c7 --storagectl "SATA Controller" --port 1 --device 0 --type hdd --medium  /clusterfs/data/VBoxVM/grey/grey_Disk2.vdi



vboxmanage modifyvm grey --cpus 7
# memory and ram change need VM to be off.




VBoxHeadless -s ${VM_NAME}



when using VirtualBox gui, console may launch with error.  It has some sort of xauth channel and may not work with X11 forward from another user, even when the main gui works :-\
Safest way is to run vnc as the same user owning the virtual machines.
SSH tunnel to vnc session if not using encrypted vnc like realvnc.
eg ssh -L 5911:localhost:5901 vboxserver.example.com







[Doc URL: http://tin6150.github.io/psg/vagrant.html ]
Last Updated: 2017-10-09
(cc) Tin Ho. See main page for copyright info.


hoti1
sn5050
psg101 sn50 tin6150