How to Create Your Own CentOS Vagrant Box

原文鏈接:https://medium.com/@gevorggalstyan/creating-own-custom-vagrant-box-ae7e94043a4e

Introduction

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.

Getting Prepared

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 Done.
在這裏插入圖片描述
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.

vi clean.sh

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.

. clean.sh

After couple of moments your screen should look like this.在這裏插入圖片描述
Clear the screen.

clear

Configuring CentOS

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/sudoers
sed -i 's/^\(Defaults.*requiretty\)/#\1/' /etc/sudoers
mkdir -m 0700 /home/vagrant/.ssh
curl https://raw.githubusercontent.com/mitchellh/vagrant/master/keys/vagrant.pub >> /home/vagrant/.ssh/authorized_keys
chmod 600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant
sed -i -e 's/^SELINUX=.*/SELINUX=permissive/' /etc/selinux/config
sed -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/environment
echo "LC_ALL=en_US.utf-8" >> /etc/environment
echo "blacklist i2c_piix4" >> /etc/modprobe.d/modprobe.conf
echo "blacklist intel_rapl" >> /etc/modprobe.d/modprobe.conf
sed -i -e 's/installonly_limit=5/installonly_limit=2/' /etc/yum.conf
sed -i -e 's/GRUB_TIMEOUT=5/GRUB_TIMEOUT=0/' /etc/default/grub
sed -i -e 's/GRUB_DEFAULT=saved/GRUB_DEFAULT=0/' /etc/default/grub
grub2-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 postfix
systemctl stop chronyd && systemctl disable chronyd && yum -y remove chrony
systemctl stop avahi-daemon.socket avahi-daemon.service
systemctl disable avahi-daemon.socket avahi-daemon.service
yum -y remove avahi-autoipd avahi-libs avahi
service network restart
chkconfig network on
systemctl 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.sh
rm -f clean.sh
history -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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章