2016-09-06 76 views
0

我使用jhcook/osx-elcapitan-10.11作爲基準盒。我想共享/vagrant目錄,但它「無法正常工作」,因爲guest虛擬機添加內容不安裝在OSX中,因此/vagrant目錄無法安​​裝。我被告知有一個workaround,那就是使用NFS。但是,它需要在主機上配置,並且可以在Vagrantfile中放入一個條目。如何在OSX Vagrant盒子上設置共享/流浪目錄?

按照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,只需添加類型: 「NFS」 標誌到您的同步文件夾:

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

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

但它不工作。以下是我的Vagrantfile。另請注意,我也遇到了有關USB無法正常工作的錯誤,解決方法是按照this tutorial禁用USB。

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

回答

0

根據一issue的解決方案是使用靜態IP。根據documentation

您也可以爲機器指定一個靜態IP地址。這使您可以使用靜態的已知IP訪問Vagrant管理的機器。該Vagrantfile靜態IP是這樣的:

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