vagrant(5)基于centos7镜像优化

目的

开启密码、key认证、添加国内yum源、增加磁盘大小、关闭selinux、安装常用软件包、关闭防火墙

1.Vagrantfile

Vagrant.configure("2") do |config|
    config.vm.box = "centos7"
    config.vm.hostname = "vgt-c7-101"
    config.disksize.size = '50GB'
    #config.vm.disk :disk, size: "50GB", primary: true
    config.vm.network "private_network", ip: "172.24.20.101", hostname: true
    config.ssh.insert_key = true
    config.vm.provider "virtualbox" do |vb|
      vb.memory = 2048
      vb.cpus   = 2
      vb.name   = "vgt-c7-101"
    end

    config.vm.provision "shell", path: "init.sh"
end

注意:依赖 vagrant-disksize
vagrant plugin install vagrant-disksize

2.init.sh

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/sh
# support centos7 init

# 1.init
sudo su - <<-'EOG'

# 1.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

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

# 3.disable firwalld
systemctl mask firewalld

# 4.china repo
REPS_BASE="https://mirrors.aliyun.com/repo/Centos-7.repo"
REPS_EPEL="https://mirrors.aliyun.com/repo/epel-7.repo"
rm -rf /etc/yum.repos.d/* \
&& curl -s -o /etc/yum.repos.d/base.repo $REPS_BASE \
&& curl -s -o /etc/yum.repos.d/epel.repo $REPS_EPEL \
&& yum -q clean all && yum -q makecache 2>/dev/null

# 5.install requirement
yum install -q -y vim lrzsz wget tree unzip
EOG

# resize default 40g -> 50g
echo -e "d\nn\np\n\n\n\nw" | sudo fdisk /dev/sda

# sudo su - <<EOG
# fdisk /dev/sda
# d
# n
# p



# w
# EOG

# 2.resize xfs
sudo su - <<EOG
partprobe /dev/sda
xfs_growfs /
EOG

⚠️:partprobe 提醒内核重新读取分区表信息

3.up

vagrant up
vagrant ssh