【问题标题】:Vagrant shared and synced foldersVagrant 共享和同步文件夹
【发布时间】:2013-08-30 08:50:52
【问题描述】:

我创建了一个 Vagrantfile,内容如下:

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 upvagrant reload 之后我得到:

[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

我的问题是:

  1. 为什么 Vagrant 挂载/vagrant 共享文件夹?我读过共享文件夹已被弃用,有利于同步文件夹,而且我从未在我的 Vagrantfile 中定义任何共享文件夹。
  2. 为什么没有设置同步文件夹?

我在 MacOX 10.8.4 上使用 Vagrant 1.2.7 版。

【问题讨论】:

    标签: vagrant


    【解决方案1】:

    共享文件夹 VS 同步文件夹

    基本上共享文件夹被重命名为从 v1 到 v2(docs)的同步文件夹,在主机和来宾之间仍然使用vboxsf(如果有大量文件/目录,则存在已知的性能问题)。

    Vagrantfile 目录挂载为 /vagrant 来宾

    Vagrant 在客户机中将当前工作目录(Vagrantfile 所在的位置)挂载为 /vagrant,这是默认行为。

    docs

    注意:默认情况下,Vagrant 会将您的项目目录(带有 Vagrantfile 的目录)共享到 /vagrant。

    您可以通过在 Vagrantfile 中添加 cfg.vm.synced_folder ".", "/vagrant", disabled: true 来禁用此行为。

    为什么同步文件夹不起作用

    基于主机上的输出/tmp 在正常运行期间未安装。

    使用VAGRANT_INFO=debug vagrant upVAGRANT_INFO=debug vagrant reload 启动VM 以获取有关未安装同步文件夹的原因的更多输出。可能是权限问题(主机上/tmp 的模式位应为drwxrwxrwt)。

    我使用以下内容进行了测试快速测试,它有效(我使用了 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)

    【讨论】:

    • vagrant up --debugvagrant reload --debug 可能有用
    • cfg.vm.synced_folder ".", "/vagrant", disabled: true 禁用默认挂载非常有用,谢谢
    • 我在 vm "public" 文件夹之外有一个主机文件夹,正确设置为同步,但在机器启动时它不会挂载。 VAGRANT_INFO=debug vagrant reload 为我做了诀窍,现在它每次都安装。谢谢!
    • 我必须将config.vm.synced_folder ".", "/vagrant", disabled: false 添加到Vagrantfile 才能使其工作。我猜默认值已经改变了。
    猜你喜欢
    • 2016-03-21
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2017-08-20
    • 2015-10-05
    相关资源
    最近更新 更多