配置同步文件夹
在启动虚拟机的时候,我们可以看到这样的提示:
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/c/Users/Davy/demo/ => /vagrant
先注意最后一行的提示:Rsyncing folder: /cygdrive/c/Users/Davy/demo/ => /vagrant
- /cygdrive/c/Users/Davy/demo/ 这是宿主机的本地目录,也就是 Vagrantfile 所在的目录。
- /vagrant 是虚拟机内部的路径
- Rsyncing 表示同步的方式是 Rsync
在 demo 目录下创建一些文件,例如 hello.py
执行 vagrant reload,重启虚拟机
在虚拟机启动完成后登录到虚拟机内,操作如下:
$ vagrant ssh
Last login: Sat May 2 16:25:00 2020 from 10.0.2.2
[vagrant@localhost ~]$ cd /vagrant/
[vagrant@localhost vagrant]$ ls
hello.py Vagrantfile
[vagrant@localhost vagrant]$ python hello.py
helloworld
这种同步方式在大多数情况下都能提供便利,不过也有不足之处:
- 同步是一次性的,即只有启动虚拟机的时候执行,也就是说改了代码必须要重启一次虚机
- 单向的,即只能从宿主机同步到虚拟机,也就是说在虚机内的改动不会同步到外面(也就是虚拟机中的这个文件夹在下次重新启动之后会被覆盖掉)
- 需要拷贝文件,如果要同步的文件数量较多,会占用更多的磁盘空间
添加额外的虚拟机共享文件夹
vagrant的共享目录类型有:
-
NFS (适用于Mac OS宿主机), 配置语法:
config.vm.synced_folder “/hostPath”, “/guestPath”, type: “nfs”
官网配置文档地址:https://www.vagrantup.com/docs/synced-folders/nfs.html -
RSync , 配置语法:
config.vm.synced_folder “/hostPath”, “/guestPath”, type: “rsync”
官网配置文档地址:https://www.vagrantup.com/docs/synced-folders/rsync.html -
SMB (适用于Windows宿主机), 配置语法:
config.vm.synced_folder “/hostPath”, “/guestPath”, type: “smb”
官网配置文档地址:https://www.vagrantup.com/docs/synced-folders/smb.html -
VirtualBox
如果你的vagrant使用virtualbox的provider,这是默认的共享目录的类型。这些同步文件夹使用ValualBox共享文件夹系统将文件更改从客户机同步到主机,反之亦然。
官网配置文档地址:https://www.vagrantup.com/docs/synced-folders/virtualbox.html
操作方法
-
打开虚拟机目录的Vagrantfile文件
-
用记事本打开
-
修改这行代码
在Windows下不能实时同步的解决方案,smb协议
修改Vagrantfile文件
Vagrant.configure("2") do |config|
config.vm.synced_folder "C:/Work", "/home/Code",
type: "smb",
smb_host: "192.168.33.1",
smb_username: "kehao",
smb_password: "yourpassword",
mount_options: ["username=kehao","password=yourpassowrd"]
end
参数解释:
1. smb_host
表示需要与哪台主机共享文件,这里填的是 host 端的 IP 地址,需要以下配置启动后,才可以使用这个 IP。
config.vm.network "private_network", ip: "192.168.33.11"
这个配置的意思是,guest 主机与 host 主机通信的私有网络。host 端的 IP 为 192.168.33.1,可以在 cmd 中输入 ipconfig 查看:
这样以来,就可以使用 smb 实现 guest 的文件夹和 host 端的文件夹之间的数据同步了。
2.smb_username,smb_password
如果不输入这个参数的话,在你运行 vagrant up 的时候,vagrant 会提示你输入 windows 的用户名和密码,来实现对 windows 中某文件夹的连接。目前我是在 windows 10 系统上实现的,还请根据实际情况填写这两个参数。总之配置了这两个参数不用每次启动的时候都输入 windows 的用户名和密码。
3.mount_options
填写这个参数是为了解决 “wrong fs type” 错误,见:https://www.vagrantup.com/docs/synced-folders/smb.html 的 COMMON ISSUES 章节。这是官方提出的错误解决方案,反正我这样子用了,就没有出现问题了。
错误提示:
一些常用技巧:
1.为了防止smb共享剔除不活动的连接需要执行以下命令让系统不要自动踢掉不活动的连接
net config server /autodisconnect:-1
2.vagrant不会自动删除共享,要删除共享使用命令
net share c30268623ba3dedeaa9f098b570dca21 /delete
3. 用 net share 查看文件共享状态