virtualbox下载

地址:https://www.virtualbox.org/wiki/Downloads

vagrant下载

地址:https://developer.hashicorp.com/vagrant/downloads

virtualbox安装

添加box

1
vagrant box add --name centos/7 D:\HashiCorp\Vagrant\boxes\CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box

安装插件/配置环境变量(修改disksize需要)

1
2
3
4
5
6
7
# 解决centos7下使用synced_folder报错,https://stackoverflow.com/a/65545379/14936119
vagrant plugin install vagrant-vbguest --plugin-version 0.21
# 配合config.disksize.size使用(非必要,目前vagrant已支持实验参数node.vm.disk)
# vagrant plugin install vagrant-disksize

# 配置环境变量
VAGRANT_EXPERIMENTAL=disks

初始化vm file(管理员执行)

1
vagrant init
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
48
49
50
51
52
53
Vagrant.configure("2") do |config|
(1..1).each do |i|
config.vm.define "development#{i}" do |node|
# 设置虚拟机的Box
node.vm.box = "centos/7"

# 设置虚拟机的主机名
node.vm.hostname="development#{i}"

# 设置虚拟机的IP
node.vm.network "private_network", ip: "192.168.56.#{99+i}", netmask: "255.255.255.0"

# 禁用自动框更新检查。如果你禁用这个,那么只有当用户运行' vagrant box expired '时,方框才会被检查更新。不建议这样做。
# node.vm.box_check_update = false

# 创建转发端口映射,允许从主机上的端口访问机器内的特定端口。在下面的示例中,访问“localhost:8080”将访问来宾计算机上的端口80。注意:这将允许公众访问开放的端口
# node.vm.network "forwarded_port", guest: 80, host: 8080

# 创建一个转发端口映射,允许从主机上的端口访问机器内的特定端口,并且只允许通过127.0.0.1访问以禁用公共访问
# node.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

# 创建公网,一般与桥接网络匹配。桥接网络使机器看起来像网络上的另一个物理设备。
# node.vm.network "public_network"

# 将另一个文件夹共享给客户VM。第一个参数是实际文件夹在主机上的路径。第二个参数是虚拟机上挂载文件夹的路径。可选的第三个参数是一组非必需的选项。
node.vm.synced_folder "D:/", "/mnt/d"
node.vm.synced_folder "C:/", "/mnt/c"
# node.vm.synced_folder "D:/", "/mnt/d", owner: "vagrant", groupe: "vagrant", mount_options: ["dmode=777", "fmode=777"]

# 磁盘容量,实验参数,需要先设置环境变量VAGRANT_EXPERIMENTAL=disks
node.vm.disk :disk, size: "100GB", primary: true

# VirtaulBox相关配置
node.vm.provider "virtualbox" do |v|
# 设置虚拟机的名称
v.name = "development#{i}"
# 设置虚拟机的内存大小
v.memory = 8192
# 设置虚拟机的CPU个数
v.cpus = 4
end

# 使用shell脚本启用供应。此外,还提供了Ansible、Chef、Docker、Puppet和Salt等其他配置程序。有关它们的特定语法和用法的更多信息,请参阅文档。
# node.vm.provision "shell", inline: <<-SHELL
# yum update -y
# yum install -y vim
# SHELL
end

# 设置虚拟机的默认磁盘空间(vagrant plugin install vagrant-disksize)
# config.disksize.size = '100GB'
end
end

启动虚拟机

1
vagrant up

磁盘分区

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
1. 查看磁盘大小,此时如果是更新操作而非初始化操作,查看磁盘大小可能未改变,需执行第二步
# fdisk -l
2. 配置系统重新扫描存储设备的scsi总线,找到scsi磁盘编号,进行rescan
# ls /sys/class/scsi_disk/
0:0:0:0
# echo 1 >/sys/class/scsi_disk/0\:0\:0\:0/device/rescan #此处用\转意
3. 再次查看磁盘大小
# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0009ef1a

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 83886079 41942016 83 Linux
4. 划分磁盘空间
# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (83886080-209715199, default 83886080):
Using default value 83886080
Last sector, +sectors or +size{K,M,G} (83886080-209715199, default 209715199):
Using default value 209715199
Partition 2 of type Linux and of size 60 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
5. 挂载虚拟文件系统
# kpartx -a /dev/sda
device-mapper: reload ioctl on sda1 failed: Device or resource busy
create/reload failed on sda1
device-mapper: reload ioctl on sda2 failed: Device or resource busy
create/reload failed on sda2
# partprobe
# mkfs -t ext3 /dev/sda2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3932160 inodes, 15728640 blocks
786432 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
480 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
6. 创建新目录并挂载
# mkdir /mnt/sda2
# mount /dev/sda2 /mnt/sda2/
7. 永久挂载,在文件最后写入下面这一行
# vi /etc/fstab
/dev/sda2 /mnt/sda2/ ext3 defaults 0 0
8. 检查
# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.6M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 40G 4.2G 36G 11% /
tmpfs 783M 0 783M 0% /run/user/1000
/dev/sda2 59G 52M 56G 1% /mnt/sda2

vagrant指令

vagrant help

vagrant help

1
vagrant help

vagrant [command] -h

1
vagrant box -h

vagrant box

添加box

1
vagrant box add --name centos-7.4-base centos-7.4-base.box

查看box

1
vagrant box list

移除box

1
vagrant box remove centos-7.4-base

vagrant vm

初始化

如果自己编写Vagrantfile,不需要这一步

这一步会把config.vm.box写入

1
vagrant init centos-7.4-base

启动虚拟机

1
vagrant up

查看状态

1
vagrant status

ssh

1
vagrant ssh

暂停虚拟机

1
vagrant suspend

恢复虚拟机

1
vagrant resume

关闭虚拟机

1
vagrant halt

销毁虚拟机

1
vagrant destroy

更新Vagrantfile,刷新容器

1
vagrant reload

vagrant snapshot

使用虚拟机快照命令需要先启动虚拟机

保存虚拟机快照

1
vagrant snapshot save snap1

list虚拟机快照

1
vagrant snapshot list

恢复虚拟机快照

1
vagrant snapshot restore snap1

删除虚拟机快照

1
vagrant snapshot delete snap1