Vagrant是一个用于创建和配置虚拟开发环境的工具,它允许开发者在虚拟机中开发软件,而不必担心本地机器的配置问题。Vagrant通过与各种虚拟化技术(如 VirtualBox、VMware、Docker 等)的集成,提供了一种一致的体验

env

  • centos8 box
  • vagrant-v2.4.1

1.centos box拉取

mvpbang@mvpbang c8-51 % vagrant box list
centos7            (virtualbox, 0)
centos8            (virtualbox, 0)
centos9            (virtualbox, 0)
ubuntu-20.04.6-lts (virtualbox, 0)
ubuntu-22.04.4-lts (virtualbox, 0)

2.Vagrantfile及脚本编写

2.1checklist

mvpbang@mvpbang c8-51 % tree .
.
├── Vagrantfile
├── init.sh
└── resizedisk.sh

2.2Vagrantfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Vagrant.configure("2") do |config|
    # 定义虚拟机数量(支持批量)
    vms = Array(1..1)
    vms.each do |i|
        config.vm.define "centos8-5#{i}" do |node|
            # 设置虚拟机的Box
            node.vm.box = "centos8"
            # 设置虚拟机的主机名
            node.vm.hostname="centos8-5#{i}"
            # 磁盘容量
            node.vm.disk :disk, size: "40GB", primary: true
            # 设置虚拟机的IP
            node.vm.network "private_network", ip: "172.24.20.5#{i}", hostname: true
            node.vm.provider "virtualbox" do |v|
                # 设置虚拟机的名称
                v.name = "centos8-20.5#{i}"
                # 设置虚拟机的内存大小  
                v.memory = 4096
                # 设置虚拟机的CPU个数
                v.cpus = 2
            end
        end
        # 自定义执行脚本
        config.vm.provision "shell", path: "resizedisk.sh", run: "once"
        config.vm.provision "shell", path: "init.sh", run: "once"
    end
end

2.3init.sh负责第一次启动优化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/sh

sudo su - <<-'EOG'

# enable passwd auth and reset root passwd
sed -i.bak 's#PasswordAuthentication no#PasswordAuthentication yes#' /etc/ssh/sshd_config
systemctl restart sshd
echo root:321321 |chpasswd

# disable selinux
sed -i.bak 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config

# disable firwalld
systemctl mask firewalld

# china repo
rm -rf /etc/yum.repos.d/*  && curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo &&curl -o /etc/yum.repos.d/epel-archive-8.repo  https://mirrors.aliyun.com/repo/epel-archive-8.repo

dnf clean all && dnf makecache

# install requirement
dnf install -y vim lrzsz wget tree unzip
EOG

2.4resizedisk.sh第一次启动磁盘大小调整

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/sh

# resize default 10g -> 40g
sudo su -  <<EOG
fdisk /dev/sda
d
n
p
1


Y
w
xfs_growfs /
EOG

3.启动及测试过程

3.1启动过程

mvpbang@mvpbang c8-51 % vagrant up      
Bringing machine 'centos8-51' up with 'virtualbox' provider...
==> centos8-51: Importing base box 'centos8'...
==> centos8-51: Matching MAC address for NAT networking...
==> centos8-51: Setting the name of the VM: centos8-20.51
==> centos8-51: Clearing any previously set network interfaces...
==> centos8-51: Preparing network interfaces based on configuration...
    centos8-51: Adapter 1: nat
    centos8-51: Adapter 2: hostonly
==> centos8-51: Forwarding ports...
    centos8-51: 22 (guest) => 2222 (host) (adapter 1)
==> centos8-51: Configuring storage mediums...
    centos8-51: Disk 'vagrant_primary' needs to be resized. Resizing disk...
==> centos8-51: Running 'pre-boot' VM customizations...
==> centos8-51: Booting VM...
==> centos8-51: Waiting for machine to boot. This may take a few minutes...
    centos8-51: SSH address: 127.0.0.1:2222
    centos8-51: SSH username: vagrant
    centos8-51: SSH auth method: private key
    centos8-51: 
    centos8-51: Vagrant insecure key detected. Vagrant will automatically replace
    centos8-51: this with a newly generated keypair for better security.
    centos8-51: 
    centos8-51: Inserting generated public key within guest...
    centos8-51: Removing insecure key from the guest if it's present...
    centos8-51: Key inserted! Disconnecting and reconnecting using new SSH key...
==> centos8-51: Machine booted and ready!
==> centos8-51: Checking for guest additions in VM...
    centos8-51: No guest additions were detected on the base box for this VM! Guest
    centos8-51: additions are required for forwarded ports, shared folders, host only
    centos8-51: networking, and more. If SSH fails on this machine, please install
    centos8-51: the guest additions and repackage the box to continue.
    centos8-51: 
    centos8-51: This is not an error message; everything may continue to work properly,
    centos8-51: in which case you may ignore this message.
==> centos8-51: Setting hostname...
==> centos8-51: Configuring and enabling network interfaces...
==> centos8-51: Rsyncing folder: /Users/mvpbang/vagrants/c8-51/ => /vagrant
==> centos8-51: Running provisioner: shell...
    centos8-51: Running: /var/folders/rr/rgbwqqyn077fxsdfk_y3dq4c0000gn/T/vagrant-shell20240820-9193-l6kevr.sh
    centos8-51: 
    centos8-51: Welcome to fdisk (util-linux 2.32.1).
    centos8-51: Changes will remain in memory only, until you decide to write them.
    centos8-51: Be careful before using the write command.
    centos8-51: 
    centos8-51: 
    centos8-51: Command (m for help): Selected partition 1
    centos8-51: Partition 1 has been deleted.
    centos8-51: 
    centos8-51: Command (m for help): Partition type
    centos8-51:    p   primary (0 primary, 0 extended, 4 free)
    centos8-51:    e   extended (container for logical partitions)
    centos8-51: Select (default p): Partition number (1-4, default 1): First sector (2048-83886079, default 2048): Last sector, +sectors or +size{K,M,G,T,P} (2048-83886079, default 83886079):
    centos8-51: Created a new partition 1 of type 'Linux' and of size 40 GiB.
    centos8-51: 
    centos8-51: Command (m for help):
    centos8-51: Partition #1 contains a xfs signature.
    centos8-51: Y: unknown command
    centos8-51: Command (m for help):
    centos8-51: The partition table has been altered.
    centos8-51: Syncing disks.
    centos8-51: 
    centos8-51: meta-data=/dev/sda1              isize=512    agcount=4, agsize=655296 blks
    centos8-51:          =                       sectsz=512   attr=2, projid32bit=1
    centos8-51:          =                       crc=1        finobt=1, sparse=1, rmapbt=0
    centos8-51:          =                       reflink=1    bigtime=0 inobtcount=0
    centos8-51: data     =                       bsize=4096   blocks=2621184, imaxpct=25
    centos8-51:          =                       sunit=0      swidth=0 blks
    centos8-51: naming   =version 2              bsize=4096   ascii-ci=0, ftype=1
    centos8-51: log      =internal log           bsize=4096   blocks=2560, version=2
    centos8-51:          =                       sectsz=512   sunit=0 blks, lazy-count=1
    centos8-51: realtime =none                   extsz=4096   blocks=0, rtextents=0
    centos8-51: data blocks changed from 2621184 to 10485504
==> centos8-51: Running provisioner: shell...
    centos8-51: Running: /var/folders/rr/rgbwqqyn077fxsdfk_y3dq4c0000gn/T/vagrant-shell20240820-9193-28c7jj.sh
    centos8-51: Last login: Tue Aug 20 09:27:58 UTC 2024
    centos8-51: Created symlink /etc/systemd/system/firewalld.service → /dev/null.
    centos8-51:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    centos8-51:                                  Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0  23317      0 --:--:-- --:--:-- --:--:-- 23317
    centos8-51:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
    centos8-51:                                  Dload  Upload   Total   Spent    Left  Speed
100   521  100   521    0     0   3011      0 --:--:-- --:--:-- --:--:--  2994
    centos8-51: 0 files removed
    centos8-51: CentOS-8.5.2111 - Base - mirrors.aliyun.com     724 kB/s | 4.6 MB     00:06
    centos8-51: CentOS-8.5.2111 - Extras - mirrors.aliyun.com    60 kB/s |  10 kB     00:00
    centos8-51: CentOS-8.5.2111 - AppStream - mirrors.aliyun.co 500 kB/s | 8.4 MB     00:17
    centos8-51: Extra Packages for Enterprise Linux 8 - x86_64  404 kB/s | 6.4 MB     00:16
    centos8-51: Metadata cache created.
    centos8-51: Last metadata expiration check: 0:00:16 ago on Tue 20 Aug 2024 09:28:33 AM UTC.
    centos8-51: Dependencies resolved.
    centos8-51: ================================================================================
    centos8-51:  Package             Arch        Version                   Repository      Size
    centos8-51: ================================================================================
    centos8-51: Installing:
    centos8-51:  lrzsz               x86_64      0.12.20-43.el8            base            84 k
    centos8-51:  tree                x86_64      1.7.0-15.el8              base            59 k
    centos8-51:  unzip               x86_64      6.0-45.el8_4              base           195 k
    centos8-51:  vim-enhanced        x86_64      2:8.0.1763-16.el8         AppStream      1.4 M
    centos8-51:  wget                x86_64      1.19.5-10.el8             AppStream      734 k
    centos8-51: Installing dependencies:
    centos8-51:  gpm-libs            x86_64      1.20.7-17.el8             AppStream       39 k
    centos8-51:  libmetalink         x86_64      0.1.3-7.el8               base            32 k
    centos8-51:  vim-common          x86_64      2:8.0.1763-16.el8         AppStream      6.3 M
    centos8-51:  vim-filesystem      noarch      2:8.0.1763-16.el8         AppStream       49 k
    centos8-51: 
    centos8-51: Transaction Summary
    centos8-51: ================================================================================
    centos8-51: Install  9 Packages
    centos8-51: 
    centos8-51: Total download size: 8.9 M
    centos8-51: Installed size: 34 M
    centos8-51: Downloading Packages:
    centos8-51: (1/9): libmetalink-0.1.3-7.el8.x86_64.rpm        90 kB/s |  32 kB     00:00
    centos8-51: (2/9): lrzsz-0.12.20-43.el8.x86_64.rpm          194 kB/s |  84 kB     00:00
    centos8-51: (3/9): tree-1.7.0-15.el8.x86_64.rpm             129 kB/s |  59 kB     00:00
    centos8-51: (4/9): gpm-libs-1.20.7-17.el8.x86_64.rpm        160 kB/s |  39 kB     00:00
    centos8-51: (5/9): unzip-6.0-45.el8_4.x86_64.rpm            253 kB/s | 195 kB     00:00
    centos8-51: (6/9): vim-filesystem-8.0.1763-16.el8.noarch.rp 263 kB/s |  49 kB     00:00
    centos8-51: (7/9): wget-1.19.5-10.el8.x86_64.rpm            336 kB/s | 734 kB     00:02
    centos8-51: (8/9): vim-enhanced-8.0.1763-16.el8.x86_64.rpm  300 kB/s | 1.4 MB     00:04
    centos8-51: (9/9): vim-common-8.0.1763-16.el8.x86_64.rpm    331 kB/s | 6.3 MB     00:19
    centos8-51: --------------------------------------------------------------------------------
    centos8-51: Total                                           452 kB/s | 8.9 MB     00:20
    centos8-51: Running transaction check
    centos8-51: Transaction check succeeded.
    centos8-51: Running transaction test
    centos8-51: Transaction test succeeded.
    centos8-51: Running transaction
    centos8-51:   Preparing        :                                                        1/1
    centos8-51:   Installing       : vim-filesystem-2:8.0.1763-16.el8.noarch                1/9
    centos8-51:   Installing       : vim-common-2:8.0.1763-16.el8.x86_64                    2/9
    centos8-51:   Installing       : gpm-libs-1.20.7-17.el8.x86_64                          3/9
    centos8-51:   Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64                          3/9
    centos8-51:   Installing       : libmetalink-0.1.3-7.el8.x86_64                         4/9
    centos8-51:   Installing       : wget-1.19.5-10.el8.x86_64                              5/9
    centos8-51:   Running scriptlet: wget-1.19.5-10.el8.x86_64                              5/9
    centos8-51:   Installing       : vim-enhanced-2:8.0.1763-16.el8.x86_64                  6/9
    centos8-51:   Installing       : unzip-6.0-45.el8_4.x86_64                              7/9
    centos8-51:   Installing       : tree-1.7.0-15.el8.x86_64                               8/9
    centos8-51:   Installing       : lrzsz-0.12.20-43.el8.x86_64                            9/9
    centos8-51:   Running scriptlet: lrzsz-0.12.20-43.el8.x86_64                            9/9
    centos8-51:   Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64                    9/9
    centos8-51:   Verifying        : libmetalink-0.1.3-7.el8.x86_64                         1/9
    centos8-51:   Verifying        : lrzsz-0.12.20-43.el8.x86_64                            2/9
    centos8-51:   Verifying        : tree-1.7.0-15.el8.x86_64                               3/9
    centos8-51:   Verifying        : unzip-6.0-45.el8_4.x86_64                              4/9
    centos8-51:   Verifying        : gpm-libs-1.20.7-17.el8.x86_64                          5/9
    centos8-51:   Verifying        : vim-common-2:8.0.1763-16.el8.x86_64                    6/9
    centos8-51:   Verifying        : vim-enhanced-2:8.0.1763-16.el8.x86_64                  7/9
    centos8-51:   Verifying        : vim-filesystem-2:8.0.1763-16.el8.noarch                8/9
    centos8-51:   Verifying        : wget-1.19.5-10.el8.x86_64                              9/9
    centos8-51: 
    centos8-51: Installed:
    centos8-51:   gpm-libs-1.20.7-17.el8.x86_64         libmetalink-0.1.3-7.el8.x86_64
    centos8-51:   lrzsz-0.12.20-43.el8.x86_64           tree-1.7.0-15.el8.x86_64
    centos8-51:   unzip-6.0-45.el8_4.x86_64             vim-common-2:8.0.1763-16.el8.x86_64
    centos8-51:   vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch
    centos8-51:   wget-1.19.5-10.el8.x86_64
    centos8-51: 
    centos8-51: Complete!

3.2测试

mvpbang@mvpbang c8-51 % 
mvpbang@mvpbang c8-51 % vagrant ssh 
[vagrant@centos8-51 ~]$ 
[vagrant@centos8-51 ~]$ df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs          tmpfs     2.0G     0  2.0G   0% /dev/shm
tmpfs          tmpfs     2.0G   17M  1.9G   1% /run
tmpfs          tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1      xfs        40G  3.9G   37G  10% /
tmpfs          tmpfs     393M     0  393M   0% /run/user/1000
[vagrant@centos8-51 ~]$ hostname
centos8-51
[vagrant@centos8-51 ~]$ ping `hostname`
PING centos8-51 (172.24.20.51) 56(84) bytes of data.
64 bytes from centos8-51 (172.24.20.51): icmp_seq=1 ttl=64 time=0.027 ms
64 bytes from centos8-51 (172.24.20.51): icmp_seq=2 ttl=64 time=0.047 ms
^C
--- centos8-51 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1026ms
rtt min/avg/max/mdev = 0.027/0.037/0.047/0.010 ms
[vagrant@centos8-51 ~]$ exit