如何在 OSX Vagrant box 上设置共享 /vagrant 目录?

How to set up a shared /vagrant directory on an OSX Vagrant box?

我正在使用jhcook/osx-elcapitan-10.11 as a base box. I'd like to have a shared /vagrant directory but it's "not working", because the guest additions do not install in OSX and therefore the /vagrant directory does not mount. I've been told there is a workaround,也就是使用NFS。但是,它需要在主机上进行配置,您可以在 Vagrantfile 中添加一个条目。

根据 documentation:

If you are using the VirtualBox provider, you will also need to make sure you have a private network set up. This is due to a limitation of VirtualBox's built-in networking. With VMware, you do not need this.

我正在使用 VirtualBox。

再次引用 documentation:

The easiest way to use a private network is to allow the IP to be assigned via DHCP.

Vagrant.configure("2") do |config|
  config.vm.network "private_network", type: "dhcp"
end

然后返回设置 "nfs"

To enable NFS, just add the type: "nfs" flag onto your synced folder:

Vagrant.configure("2") do |config|
  # ...

  config.vm.synced_folder ".", "/vagrant", type: "nfs"
end

但它不起作用。下面是我的Vagrantfile。另请注意,我还收到有关 USB 无法正常工作的错误,解决方法是在 this tutorial.

之后禁用 USB

流浪文件:

# -*- mode: ruby -*-
# vi: set ft=ruby :
.
.
.
Vagrant.configure("2") do |config|

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "jhcook/osx-elcapitan-10.11"

  # private network setup
  config.vm.network "private_network", type: "dhcp"
  # enable NFS
  config.vm.synced_folder ".", "/vagrant", type: "nfs"

  # disable usb
  config.vm.provider "virtualbox" do |vb|
    # VM Customizations go here
    vb.customize ["modifyvm", :id, "--usb", "off"]
    vb.customize ["modifyvm", :id, "--usbehci", "off"]
  end
  .
  .
  .
end

根据issue the solution is to use a static ip. According to the documentation

You can also specify a static IP address for the machine. This lets you access the Vagrant managed machine using a static, known IP. The Vagrantfile for a static IP looks like this:

Vagrant.configure("2") do |config|
  config.vm.network "private_network", ip: "192.168.50.4"
end

所以现在我的工作 Vagrantfile 看起来像:

# -*- mode: ruby -*-
# vi: set ft=ruby :
.
.
.
Vagrant.configure("2") do |config|

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "jhcook/osx-elcapitan-10.11"

  # private network setup
  # config.vm.network "private_network", type: "dhcp"
  config.vm.network :private_network, ip: "192.168.10.2"

  # enable NFS
  config.vm.synced_folder ".", "/vagrant", type: "nfs"

  # disable usb
  config.vm.provider "virtualbox" do |vb|
    # VM Customizations go here
    vb.customize ["modifyvm", :id, "--usb", "off"]
    vb.customize ["modifyvm", :id, "--usbehci", "off"]
  end
  .
  .
  .
end

呜呜呜:)

osx-el-capitan ❯ ls
Vagrantfile
osx-el-capitan ❯ echo "Hello world?" > hello-world
osx-el-capitan ❯ vagrant ssh
Last login: Tue Sep  6 09:49:21 2016 from 10.0.2.2
This-MacBook-Pro:~ vagrant$ cat /vagrant/hello-world
Hello world?
This-MacBook-Pro:~ vagrant$ echo ":)"
:)
This-MacBook-Pro:~ vagrant$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
        options=3<RXCSUM,TXCSUM>
        inet6 ::1 prefixlen 128
        inet 127.0.0.1 netmask 0xff000000
        inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
        nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        options=2b<RXCSUM,TXCSUM,VLAN_HWTAGGING,TSO4>
        ether 08:00:27:d2:a9:5f
        inet6 fe80::a00:27ff:fed2:a95f%en0 prefixlen 64 scopeid 0x4
        inet 10.0.2.15 netmask 0xffffff00 broadcast 10.0.2.255
        nd6 options=1<PERFORMNUD>
        media: autoselect (1000baseT <full-duplex>)
        status: active
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        options=2b<RXCSUM,TXCSUM,VLAN_HWTAGGING,TSO4>
        ether 08:00:27:76:d5:29
        inet6 fe80::a00:27ff:fe76:d529%en1 prefixlen 64 scopeid 0x5
        inet 192.168.10.2 netmask 0xffffff00 broadcast 192.168.10.255
        nd6 options=1<PERFORMNUD>
        media: autoselect (1000baseT <full-duplex>)
        status: active