How to Create Your Own CentOS Vagrant Box

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值