Vagrant 共享和同步文件夹 [英] Vagrant shared and synced folders
问题描述
我创建了一个 Vagrantfile,内容如下:
I created a Vagrantfile with the following content:
Vagrant::Config.run do |config|
config.vm.define :foo do |cfg|
cfg.vm.box = 'foo'
cfg.vm.host_name = "foo.localdomain.local"
cfg.vm.network :hostonly, "192.168.123.10"
end
Vagrant.configure("2") do |cfg|
cfg.vm.customize [ "modifyvm", :id , "--name", "foo" , "--memory", "2048", "--cpus", "1"]
cfg.vm.synced_folder "/tmp/", "/tmp/src/"
end
end
在 vagrant up
或 vagrant reload
之后,我得到:
After vagrant up
or vagrant reload
I get:
[foo] Attempting graceful shutdown of VM...
[foo] Setting the name of the VM...
[foo] Clearing any previously set forwarded ports...
[foo] Fixed port collision for 22 => 2222. Now on port 2200.
[foo] Creating shared folders metadata...
[foo] Clearing any previously set network interfaces...
[foo] Preparing network interfaces based on configuration...
[foo] Forwarding ports...
[foo] -- 22 => 2200 (adapter 1)
[foo] Booting VM...
[foo] Waiting for VM to boot. This can take a few minutes.
[foo] VM booted and ready for use!
[foo] Setting hostname...
[foo] Configuring and enabling network interfaces...
[foo] Mounting shared folders...
[foo] -- /vagrant
我的问题是:
- 为什么 Vagrant 挂载
/vagrant
共享文件夹?我读到共享文件夹已被弃用,而支持同步文件夹,而且我从未在 Vagrantfile 中定义任何共享文件夹. - 为什么没有设置同步文件夹?
- Why is Vagrant mounting the
/vagrant
shared folder? I read shared folders are deprecated in favour of synced folders, and I never defined any shared folder in my Vagrantfile. - Why is the synced folder not set up?
我在 MacOX 10.8.4 上使用 Vagrant 1.2.7 版.
I'm using Vagrant version 1.2.7 on MacOX 10.8.4.
推荐答案
共享文件夹 VS 同步文件夹
基本上共享文件夹被重命名为同步文件夹从 v1 到 v2 (docs),在引擎盖下它仍然在主机和来宾之间使用 vboxsf
(如果有大量文件/目录).
shared folders VS synced folders
Basically shared folders are renamed to synced folder from v1 to v2 (docs), under the bonnet it is still using vboxsf
between host and guest (there is known performance issues if there are large numbers of files/directories).
Vagrant 将当前工作目录(Vagrantfile
所在的位置)挂载为来宾中的 /vagrant
,这是默认行为.
Vagrant is mounting the current working directory (where Vagrantfile
resides) as /vagrant
in the guest, this is the default behaviour.
参见文档
注意:默认情况下,Vagrant 会将您的项目目录(包含 Vagrantfile 的目录)共享到/vagrant.
NOTE: By default, Vagrant will share your project directory (the directory with the Vagrantfile) to /vagrant.
您可以通过在 Vagrantfile
中添加 cfg.vm.synced_folder ".", "/vagrant", disabled: true
来禁用此行为.
You can disable this behaviour by adding cfg.vm.synced_folder ".", "/vagrant", disabled: true
in your Vagrantfile
.
基于输出 /tmp
主机在正常运行期间未挂载.
Based on the output /tmp
on host was NOT mounted during up time.
使用 VAGRANT_INFO=debug vagrant up
或 VAGRANT_INFO=debug vagrant reload
来启动 VM,以获取有关未安装同步文件夹原因的更多输出.可能是权限问题(主机上 /tmp
的模式位应该是 drwxrwxrwt
).
Use VAGRANT_INFO=debug vagrant up
or VAGRANT_INFO=debug vagrant reload
to start the VM for more output regarding why the synced folder is not mounted. Could be a permission issue (mode bits of /tmp
on host should be drwxrwxrwt
).
我使用以下内容进行了快速测试,并且成功了(我使用了 opscode bento raring vagrant base box)
I did a test quick test using the following and it worked (I used opscode bento raring vagrant base box)
config.vm.synced_folder "/tmp", "/tmp/src"
输出
$ vagrant reload
[default] Attempting graceful shutdown of VM...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Available bridged network interfaces:
1) eth0
2) vmnet8
3) lxcbr0
4) vmnet1
What interface should the network bridge to? 1
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Running 'pre-boot' VM customizations...
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /tmp/src
在虚拟机中,你可以看到挂载信息/tmp/src on/tmp/src type vboxsf (uid=900,gid=900,rw)
.
Within the VM, you can see the mount info /tmp/src on /tmp/src type vboxsf (uid=900,gid=900,rw)
.
这篇关于Vagrant 共享和同步文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!