Vagrant 是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境。它使用 Oracle 的开源 VirtualBox 虚拟化系统,使用 Chef 创建自动化虚拟环境。
安装
安装 VirtualBox
安装 Parallels
Vagrant Parallels Plugin
安装 Vagrant
命令
基本操作
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
| // 添加镜像到 Vagrant
vagrant box add centos ~/.vagrant.d/boxes/vagrant-centos-7.1.box
// 创建开发目录, 切换目录
mkdir -pv ~/Vagrant/centos
cd ~/Vagrant/centos
// 初始化
vagrant init centos
// 启动环境
vagrant up
// 登录虚拟机
vagrant ssh
// 将虚拟机挂起, 虚拟机内存都保存到硬盘上, 下次可以快速恢复
vagrant suspend
// 将虚拟机关闭, 虚拟机内存释放, 下次启动要慢一点
vagrant halt
// 将虚拟机删除, 所有变更都丢失, 下次启动要重新克隆一个 VagrantBox
vagrant destroy
// 移除虚拟机
vagrant box remove centos
// 虚拟机打包之前删除下面的文件
sudo rm -rf /etc/udev/rules.d/70-persistent-ipoib(net).rules
// 虚拟机打包
vagrant package --base centos --output centos.box
|
其它命令
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
| // 添加别名为 centos 的镜像
vagrant box add centos ~/.vagrant.d/boxes/vagrant-centos-7.1.box
// 查看所有添加的镜像
vagrant box list
// 移除别名为 centos 的镜像
vagrant box remove centos
// 移除名称为 bento/centos-7.2,版本为 2.3.0 的镜像
vagrant box remove bento/centos-7.2 --box-version 2.3.0
// 重载虚拟机
vagrant reload
// 重启虚拟机并应用对 Vagrantfile 的更改
vagrant reload --provision
// 恢复虚拟机状态
vagrant resume
// 登录虚拟机
vagrant ssh
// 查看虚拟机状态
vagrant status
// 暂停虚拟机, 保存状态
vagrant suspend
// 查看全局状态
vagrant global-status
// 销毁指定的虚拟机(慎用,一旦执行,创建的虚拟机就灰飞烟灭了)
vagrant destroy 1a2b3c4d
// 升级所有 Box
vagrant box update
|
VirtualBox
VBoxManage User Manual
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
92
93
94
95
96
97
| // 列出当前系统中所有可用的虚拟机
VBoxManage list vms
"<inaccessible>" {b457c29d-3eaf-4d19-8f38-7991c85aa43a}
"ubuntu_default_1588584023862_25557" {eee17f07-35dc-4c48-bff9-8006bf3df55c}
"centos_default_1588597614517_34912" {3c640794-da30-40b2-a155-c76b42c5cac7}
// 克隆虚拟机
VBoxManage clonevm --register ubuntu_default_1588584023862_25557 --name Ubuntu
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Machine has been successfully cloned as "Ubuntu"
// 将本地虚拟机导出为一个使用开放虚拟化格式(OVF - Open Virtualization Format)文件
VBoxManage export ubuntu_default_1588584023862_25557 -o Ubuntu.ova
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Successfully exported 1 machine(s).
// 删除克隆的虚拟机
VBoxManage unregistervm --delete "Ubuntu"
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
// 导入虚拟机
VBoxManage import Ubuntu.ova --vsys 0 --vmname Ubuntu18
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Interpreting /Users/majinyun/Downloads/qiniu/Ubuntu.ova...
OK.
Disks:
vmdisk1 10737418240 -1 http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized Ubuntu-disk001.vmdk -1 -1
vmdisk2 10485760 -1 http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized Ubuntu-disk002.vmdk -1 -1
Virtual system 0:
0: Suggested OS type: "Ubuntu_64"
(change with "--vsys 0 --ostype <type>"; use "list ostypes" to list all possible values)
1: VM name specified with --vmname: "Ubuntu18"
2: Suggested VM group "/"
(change with "--vsys 0 --group <group>")
3: Suggested VM settings file name "/Users/majinyun/VirtualBoxVMs/ubuntu_default_1588584023862_25557 2/ubuntu_default_1588584023862_25557 2.vbox"
(change with "--vsys 0 --settingsfile <filename>")
4: Suggested VM base folder "/Users/majinyun/VirtualBoxVMs"
(change with "--vsys 0 --basefolder <path>")
5: Number of CPUs: 2
(change with "--vsys 0 --cpus <n>")
6: Guest memory: 1024 MB
(change with "--vsys 0 --memory <MB>")
7: Sound card (appliance expects "", can change on import)
(disable with "--vsys 0 --unit 7 --ignore")
8: Network adapter: orig NAT, config 3, extra slot=0;type=NAT
9: Network adapter: orig HostOnly, config 3, extra slot=1;type=HostOnly
10: IDE controller, type PIIX4
(disable with "--vsys 0 --unit 10 --ignore")
11: IDE controller, type PIIX4
(disable with "--vsys 0 --unit 11 --ignore")
12: SCSI controller, type LsiLogic
(change with "--vsys 0 --unit 12 --scsitype {BusLogic|LsiLogic}";
disable with "--vsys 0 --unit 12 --ignore")
13: Hard disk image: source image=Ubuntu-disk001.vmdk, target path=Ubuntu-disk001.vmdk, controller=12;channel=0
(change target path with "--vsys 0 --unit 13 --disk path";
disable with "--vsys 0 --unit 13 --ignore")
14: Hard disk image: source image=Ubuntu-disk002.vmdk, target path=Ubuntu-disk002.vmdk, controller=12;channel=1
(change target path with "--vsys 0 --unit 14 --disk path";
disable with "--vsys 0 --unit 14 --ignore")
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Successfully imported the appliance.
// 显示指定虚拟机的详细信息
VBoxManage showvminfo "Ubuntu18"
Name: Ubuntu18
Groups: /
Guest OS: Ubuntu (64-bit)
UUID: 03d737bc-2e47-4130-aa0b-b6a80767b474
Config file: /Users/majinyun/VirtualBoxVMs/Ubuntu18/Ubuntu18.vbox
Snapshot folder: /Users/majinyun/VirtualBoxVMs/Ubuntu18/Snapshots
Log folder: /Users/majinyun/VirtualBoxVMs/Ubuntu18/Logs
Hardware UUID: 03d737bc-2e47-4130-aa0b-b6a80767b474
Memory size 1024MB
Page Fusion: disabled
VRAM size: 16MB
CPU exec cap: 100%
HPET: disabled
CPUProfile: host
Chipset: piix3
Firmware: BIOS
.
.
.
// 启动虚拟机
VBoxManage startvm Ubuntu18
Waiting for VM "Ubuntu18" to power on...
VM "Ubuntu18" has been successfully started.
// 关闭虚拟机
VBoxManage controlvm Ubuntu18 poweroff
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
// 删除导入的虚拟机
VBoxManage unregistervm --delete "Ubuntu18"
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
|
问题
解决系统更新后不能挂载共享目录
1
2
| // 解决 yum update 更新后 vagrant reload 不能挂载共享目录
sudo /etc/init.d/vboxadd setup
|
解决 VBoxManage: error: Failed to create the host-only adapter
1
2
| // 解决 CleanMyMacX 智能清理后不能启动 vagrant 虚拟机
sudo /Library/Application\ Support/VirtualBox/LaunchDaemons/VirtualBoxStartup.sh restart
|
解决 /sbin/mount.vboxsf: mounting failed with the error: No such device
VirtualBox Guest Additions in Vagrant image
1
2
3
4
5
6
7
8
| vagrant plugin install vagrant-vbguest
vagrant vbguest
vagrant ssh
cd /opt/VBoxGuestAdditions-5.2.4/init/
sudo ./vboxadd setup
sudo yum -y install kernel kernel-devel
exit
vagrant reload
|
增加 Vagrant 磁盘空间
1
2
3
| VBoxManage clonehd centos-vm-disk1.vmdk centos-vm-disk1.vdi --format VDI
VBoxManage modifyhd centos-vm-disk1.vdi --resize 20480
VBoxManage clonehd centos-vm-disk1.vdi centos-vm-disk.vmdk --format VMDK
|
更新 VirtualBox 和 Vagrant 后无法启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| vagrant up
Vagrant failed to initialize at a very early stage:
The plugins failed to initialize correctly. This may be due to manual
modifications made within the Vagrant home directory. Vagrant can
attempt to automatically correct this issue by running:
vagrant plugin repair
If Vagrant was recently updated, this error may be due to incompatible
versions of dependencies. To fix this problem please remove and re-install
all plugins. Vagrant can attempt to do this automatically by running:
vagrant plugin expunge --reinstall
Or you may want to try updating the installed plugins to their latest
versions:
vagrant plugin update
Error message given during initialization: Unable to resolve dependency: user requested 'vagrant-parallels (> 0)'
|
更新 VirtualBox 后 Vagrant 提示需要更新版本
I patched Vagrant on macOS with this script
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 up
The provider 'virtualbox' that was requested to back the machine
'default' is reporting that it isn't usable on this system. The
reason is shown below:
Vagrant has detected that you have a version of VirtualBox installed
that is not supported by this version of Vagrant. Please install one of
the supported versions listed below to use Vagrant:
4.0, 4.1, 4.2, 4.3, 5.0, 5.1, 5.2, 6.0
A Vagrant update may also be available that adds support for the version
you specified. Please check www.vagrantup.com/downloads.html to download
the latest version.
curl https://github.com/briancain/vagrant/commit/fb4e6985e142da56bad143d70600cd3695c91757.patch > /tmp/vagrant_226_virtualbox_610.patch
cd /opt/vagrant/embedded/gems/2.2.6/gems/vagrant-2.2.6
sudo git apply --verbose --exclude='website/*' /tmp/vagrant_226_virtualbox_610.patch
Password:
Skipped patch 'website/source/docs/virtualbox/index.html.md'.
Checking patch plugins/providers/virtualbox/driver/meta.rb...
Checking patch plugins/providers/virtualbox/driver/version_6_1.rb...
Checking patch plugins/providers/virtualbox/plugin.rb...
Applied patch plugins/providers/virtualbox/driver/meta.rb cleanly.
Applied patch plugins/providers/virtualbox/driver/version_6_1.rb cleanly.
Applied patch plugins/providers/virtualbox/plugin.rb cleanly.
|
参考