【问题标题】:How to set up a shared /vagrant directory on an OSX Vagrant box?如何在 OSX Vagrant 盒子上设置共享 /vagrant 目录?
【发布时间】:2016-09-06 16:56:59
【问题描述】:

我使用jhcook/osx-elcapitan-10.11 作为基础框。我想要一个共享的/vagrant 目录,但它“不起作用”,因为来宾添加不会安装在 OSX 中,因此/vagrant 目录不会挂载。有人告诉我有一个workaround,那就是使用 NFS。但是,它需要在主机上进行配置,您可以在 Vagrantfile 中添加一个条目。

根据documentation

如果您使用的是 VirtualBox 提供程序,您还需要确保您有一个private network set up。这是由于 VirtualBox 内置网络的限制。有了 VMware,您就不需要这个了。

我正在使用 VirtualBox。

再次引用documentation

使用专用网络最简单的方法是允许通过 DHCP 分配 IP。

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

然后回到设置“nfs”

要启用 NFS,只需将 type: "nfs" 标志添加到您的同步文件夹:

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

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

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

流浪文件:

# -*- 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

【问题讨论】:

    标签: vagrant virtualbox


    【解决方案1】:

    根据issue,解决方案是使用静态IP。根据documentation

    您还可以为机器指定静态 IP 地址。这使您可以使用静态的已知 IP 访问 Vagrant 托管机器。静态 IP 的 Vagrantfile 如下所示:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 2015-04-14
      • 1970-01-01
      相关资源
      最近更新 更多