Vagrant is a tool for building and managing virtual machine environments in a single workflow.
VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product.
The CentOS Linux distribution is a stable, predictable, manageable and reproducible platform derived from the sources of Red Hat Enterprise Linux (RHEL).
We will be using macOS and homebrew.
Open Terminal and Install VirtualBox and Vagrant.
brew cask install virtualbox vagrant
Download CentOS Minimal ISO.
Creating the Virtual Machine
Open VirtualBox and click New.
Under Name and operating system set Name to centos, Type to Linux and Version to Red Hat (64-bit) and click Continue.
Under Memory Size set size to 1024 MB and click Continue.
This is the recommended minimal RAM for CentOS7 and you can change this on-the-fly later with Vagrant.
Under Hard disk, select Create a virtual hard disk now and click Create.
Under Hard disk file type, select VDI (VirtualBox Disk Image) and click Continue.
Some guides suggest selecting VMDK, but that will pose problems if in future you want to change your HDD size. ALWAYS CHOOSE VDI.
Under Storage on physical hard disk, select Dynamically allocated and click Continue.
Under File location and size, leave the name field as is and set size to 64GB and click Create.
You should see the new Virtual Machine in your list like this.
Configuring the Virtual Machine
Click Settings and go to Storage tab.
In the Storage Tree click the Empty just under Controller: IDE and on the right side of the window click the CD icon.
Select Choose Virtual Optical Disk File…
Navigate to where the CentOS ISO was downloaded, select it, and click Open.
Go to Audio tab in Settings window and uncheck Enable Audio.
Go to Network tab in Settings window and ensure Adapter 1 is set to NAT.
Go to Ports tab in Settings window, select USB sub-tab and uncheck Enable USB Controller.
You’re done, click OK.
Installing the CentOS
Click Start and wait until you get to CentOS installation initial menu. Select Install CentOS Linux 7 and hit enter.
Wait until you are asked to choose the installation language and select English from the left list, and select English (United States) from the right list. Then click Continue.
Click INSTALLATION DESTINATION.
Click NETWORK & HOST NAME.
Turn Ethernet (enp0s3) ON and click Done.
Click Begin Installation and in the next screen click ROOT PASSWORD.
In both input fields type vagrant and click Done twice.
Click USER CREATION.
In all 4 input fields type vagrant and click Done twice.
Wait until you see the Reboot button at the bottom right corner and click Reboot.
Wait until you see the terminal login prompt.
Login as root with password vagrant.
Creating cleanup script
To have the smallest installation possible we are going to create and run cleanup script after each next step.
touch clean.sh && chmod +x clean.sh
Lets edit clean.sh with vim.
Press i key to enable INSERT mode and type the following into the file.
yum -y install yum-utils
package-cleanup -y --oldkernels --count=1
yum -y autoremove
yum -y remove yum-utils
yum clean all
rm -rf /tmp/*
rm -f /var/log/wtmp /var/log/btmp
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
cat /dev/null > ~/.bash_history && history -c
Press esc to exit INSTER mode, then type :wq and hit Enter.
Run your clean.sh script.
After couple of moments your screen should look like this.
Clear the screen.
In order to enable Vagrant work with the box the OS needs to be configured. It can be done by typing the following in the terminal.
echo "vagrant ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoerssed -i 's/^\(Defaults.*requiretty\)/#\1/' /etc/sudoersmkdir -m 0700 /home/vagrant/.sshcurl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub >> /home/vagrant/.ssh/authorized_keyschmod 600 /home/vagrant/.ssh/authorized_keyschown -R vagrant:vagrant /home/vagrantsed -i -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/configsed -i -e 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
Fresh CentOS installation needs some default settings and configuration to speed us the system bootup and get rid of some simple warnings.
echo "LANG=en_US.utf-8" >> /etc/environmentecho "LC_ALL=en_US.utf-8" >> /etc/environmentecho "blacklist i2c_piix4" >> /etc/modprobe.d/modprobe.confecho "blacklist intel_rapl" >> /etc/modprobe.d/modprobe.confsed -i -e 's/installonly_limit=5/installonly_limit=2/' /etc/yum.confsed -i -e 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT=0/' /etc/default/grubsed -i -e 's/GRUB_DEFAULT=saved/GRUB_DEFAULT=0/' /etc/default/grubgrub2-mkconfig -o /boot/grub2/grub.cfg
Removing unnecessary components
CentOS comes with some preinstalled software which is unnecessary for a vagrant box and we can remove it, making the box even lighter.
systemctl stop postfix && systemctl disable postfix && yum -y remove postfixsystemctl stop chronyd && systemctl disable chronyd && yum -y remove chronysystemctl stop avahi-daemon.socket avahi-daemon.servicesystemctl disable avahi-daemon.socket avahi-daemon.serviceyum -y remove avahi-autoipd avahi-libs avahiservice network restartchkconfig network onsystemctl restart network
Cleaning up the CentOS
At this step we are going to clean up the OS with the clean.sh script then remove it, clear history and shutdown.
. clean.shrm -f clean.shhistory -c && shutdown -h now
Congratulations! You have successfully created your vagrant box. There is only one step left.
Packing the Vagrant Box
Open macOS Terminal and type the following command.
vagrant package --base centos
Vagrant will export and compress your box, after a minute or two you should see the following screen.
Installing VirtualBox Guest Additions
The VirtualBox Guest Additions are not preinstalled. The recommended solution is vagrant-vbguest plugin.
vagrant plugin install vagrant-vbguest
BONUS: Updating CentOS kernel
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
yum -y install yum-utils
yum-config-manager --enable elrepo-kernel
yum -y install kernel-ml
grub2-mkconfig -o /boot/grub2/grub.cfg
If you don’t want to get in all this “trouble”, you can simply use this box already from hashicorp, setting the vagrant box equal to cyberglob/centos7 in your Vagrantfile.