Create A Base Box From Scratch

Create A Base Box From Scratch_第1张图片

What's in a Base Box?

A base box typically consists of only a bare minimum set of software for Vagrant to function. As an example, a Linux box may contain only the following:

  • Package manager
  • SSH
  • SSH user so Vagrant can connect
  • Perhaps Chef, Puppet, etc. but not strictly required.

In addition to this, each provider may require additional software. For example, if you are making a base box for VirtualBox, you will want to include the VirtualBox guest additions so that shared folders work properly. But if you are making an AWS base box, this is not required.

Creating a Base Box

Creating a base box is actually provider-specific. This means that depending on if you are using VirtualBox, VMware, AWS, etc. the process for creating a base box is different.

Provider-specific guides for creating base boxes are linked below:

本文以VirtualBox为例

Prepare a Virtual Machine

本文用VirtualBox安装CentOS Minimal7,所以选择Linux 64位类型

In terms of system resources, keep the machine minimal — number of CPUs and RAM size can later easily be changed in each user's Vagrantfile. I will use one CPU and 1024MB of RAM (less RAM will not allow you to use the graphical installer).

Disable any non-necessary hardware in a base box such as audio and USB controllers. These are generally unnecessary for Vagrant usage and, again, can be easily added via the Vagrantfile in most cases.

With the new machine selected, click Settings in the toolbar and make the following adjustments:

  • In the System-tab, uncheck the floppy from boot order.
  • Uncheck Enable Audio in the Audio-tab.
  • In the Network-tab, make sure that Adapter 1 is enabled and Attached to: is set to NAT (Vagrant needs this to work).
  • In the Ports-tab, select USB and uncheck Enable USB Controller.

Install the operating system

Do not forget to enable the network interface and configure it to use DHCP to obtain an IP address.

# vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

ONBOOT=yes
BOOTPROTO=dhcp

Default User Settings

"vagrant" User

By default, Vagrant expects a "vagrant" user to SSH into the machine as. This user should be setup with theinsecure keypairthat Vagrant uses as a default to attempt to SSH. 

添加vagrant用户

useradd vagrant

设置vagrant密码为vagrant

passwd vagrant

配置不安全的keypair

mkdir -p /home/vagrant/.ssh
curl https://raw.githubusercontent.com/hashicorp/vagrant/master/keys/vagrant.pub --output /home/vagrant/.ssh/authorized_keys
chmod 0700 /home/vagrant/.ssh
chmod 0600/home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant/.ssh

When Vagrant boots a box and detects the insecure keypair, it will automatically replace it with a randomly generated keypair for additional security while the box is running.

这对秘钥是Vagrant默认提供的,你可以换为自己的。你可以通过如下命令,查看私钥所在位置:

vagrant ssh-config

Root Password: "vagrant"

Vagrant does not actually use or expect any root password. However, having a generally well known root password makes it easier for the general public to modify the machine if needed.

Publicly available base boxes usually use a root password of "vagrant" to keep things easy.

Password-less Sudo

This is important!. Many aspects of Vagrant expect the default SSH user to have passwordless sudo configured. This lets Vagrant configure networks, mount synced folders, install software, and more.

Using the visudo command, add the line following line at the and of the sudoers file:

vagrant ALL=(ALL) NOPASSWD: ALL

Additionally, Vagrant does not use a pty or tty by default when connected via SSH. You will need to make sure there is no line that has Defaults requiretty in it. Remove that if it exists. 

SSH Tweaks

In order to keep SSH speedy even when your machine or the Vagrant machine is not connected to the internet, set the UseDNS configuration to no in the SSH server configuration.

To accomplish this, login as root, open /etc/ssh/sshd_config, find the line which reads #UseDNS yes and change it to UseDNS no.

Restart the SSH daemon with systemctl restart sshd.service after that. 

VirtualBox Guest Additions

VirtualBox Guest Additions must be installed so that things such as shared folders can function. Installing guest additions also usually improves performance since the guest OS can make some optimizations by knowing it is running within VirtualBox.

Install the packages needed for compiling the VirtualBox guest additions which Vagrant needs to configure shared folders:

yum install bzip2 gcc kernel-devel-`uname-r` perl

Insert Guest Additions CD image from VirtualBox' menu which enables you to mount the filesystem and install the guest additions:
通过【Devices】【Insert Guest Additions CD image...】

mount /dev/cdrom /media
/media/VBoxLinuxAdditions.run
umount /media

还可通过Vagrant插件vagrant-vbguest

Package the Vagrant box

You can now reduce the VMs file size by running the following commands. They fix fragmentation issues and allow the machine to be compressed more efficiently.

dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY

Congratulations — your virtual machine is now ready to be packaged by Vagrant after powering it off with the poweroff command.

然后通过如下命令导出为box

vagrant package --base centos7-minimal

centos7-minimal is the machine's name as you have set it in VirtualBox' GUI.

Packaging needs some time, but soon you will see that a file called package.box has been created in this directory.

Use this box

You can now import this base box into Vagrant with the command 

agrant box add centos7-minimal package.box

After that, you can init a Vagrant box with vagrant init centos7-minimal in any directory and use it like any other Vagrant box. 

References

你可能感兴趣的:(devops,vagrant)