Skip to content

Instantly share code, notes, and snippets.

@solaris9000
Last active January 23, 2019 19:03
Show Gist options
  • Save solaris9000/c7ade28c70dcef8dd1220840d998f721 to your computer and use it in GitHub Desktop.
Save solaris9000/c7ade28c70dcef8dd1220840d998f721 to your computer and use it in GitHub Desktop.
How to create a Vagrant box simply.
How to make and install a vagrant base box from scratch using VirtualBox only for CentOS7.
Some info from: https://www.skoblenick.com/vagrant/creating-a-custom-box-from-scratch/
Pre-Reqs
VirtualBox, VirtualBox Extensions, Vagrant for Windows, ISO of CentOS7
Directory structure to store all VM's and Vagrant boxes. Eg D:\VM
1. Set up a new VM (dont use spaces in the name) in the usual way with these settings via the GUI:
Name: vagrant-centos74 (this will also be the name of the DIR where the VM will live)
Memory: 1024MB
1 x Disk: 40GB
Disable Sound and USB
1 x Adapter 1 as NAT and set the Port Forwarding in the Advance button to:
Name: SSH
Protocol: TCP
Host IP: blank
Host Port: 2222
Guest IP: blank
Guest Port: 22
2. Set up new user as vagrant with pw as vagrant
3. Set root pw as vagrant
POST INSTALL TASKS (as root inside the VM using vagrant ssh)
1. yum update
2. yum install -y gcc make kernel-devel (pre-reqs for Linux Guest Editions)
3. yum install wget
4. reboot
5. Add vagrant to sudoers
echo "vagrant ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/vagrant
6. Install the "insecure" vagrant ssh pub key:
cd /home/vagrant
mkdir .ssh
chmod 700 .ssh
wget --no-check-certificate https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub -O /home/vagrant/.ssh/authorized_keys
chmod 600 .ssh/authorized_keys
chown -R vagrant /home/vagrant/.ssh
7. Configure /etc/ssh/sshd_config as follows:
AuthorizedKeysFile %h/.ssh/authorized_keys
7a. Install dev tools:
yum install kernel-headers kernel-devel
yum groupinstall "Development Tools"
8. Goto console window and select Devices > Insert Guest Additions CD image
9. mount /dev/cdrom /mnt
10. cd /mnt
10a. Run the VBoxLinuxAdditions.run script. If this fails reboot and re-run.
11. Compile and install the Linux Guest Additions CD Image
./VBoxLinuxAdditions.run
12. Reboot
Outside the VM using Powershell on Windows:
1. cd to the VM dir that contains the VM from step 1.
2. Package up the VM using the following command:
vagrant package --base <name of the VM excluding the .vbox ext)
This will produce the final base box called package.box
eg
vagrant package --base centos74
3. Then add the package box to the repo cache and name as follows <distro/version>:
vagrant box add centos/74 package.box
The name "centos/74" will be shown in vagrant box list
4. vagrant init centos74-vagrant
5. vagrant up
6. Cleanup the VirtualBox initial VM by right clicking on the VM and delete all files. This will remove all files related to the original manual VM creation stage using VirtualBox, BUT will leave the vagrant files that were created by the vagrant commands.
7. The boxes dir in the main dir (VM) contains the Vagrant boxes.
8. Cleanup the package.box file by delete this in the VM dir. This is no longer needed as a copy has been cached in the boxes dir.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment