让Vagrant在Windwos下支持使用NFS/SMB共享文件夹从而解决目录共享IO缓慢的问题

自己的本是mbp vagrant使用nfs共享文件夹,所以使用一切ok,但是公司的开发环境是windows10。
windows下是有些问题的,就是在windows下默认的虚拟机共享文件夹的方式,磁盘IO效率实在是太低了。
表现情况,vagrant ssh 在/vagrant/project 下敲命令都卡啊卡,表现的和连接美国vps一样!
执行php程序,从调试来看,花费在文件IO上的时间实在是有点长,能达到1-3秒
vagrant在windows共享文件夹实在是太慢了,亏我忍受那么久。
一直在忍啊忍啊,今天实在是受不了,想要解决掉这个问题,想办法让windows支持NFS或者samb的方式共享文件夹给vagrant虚拟机
如果能支持NFS就当然是最好。
找到vagrant-winnfsd这个vagrant的插件
发现新版也支持Windows下SMB的方式了

NFS方式使用vagrant-winnfsd

安装vagrant 插件 vagrant-winnfsd

$ vagrant plugin install vagrant-winnfsd
但是这样安装会出现以下错误

Installing the 'vagrant-winnfsd' plugin. This can take a few minutes...
Bundler, the underlying system Vagrant uses to install plugins,
reported an error. The error is shown below. These errors are usually
caused by misconfigured plugin installations or transient network
issues. The error from Bundler is:

An error occurred while installing childprocess (0.5.8), and Bundler cannot continue.
Make sure that `gem install childprocess -v '0.5.8'` succeeds before bundling.

Warning: this Gemfile contains multiple primary sources. Using `source` more than once without a block is a security risk, and may result in installing unexpected gems. To resolve this warning, use a block to indicate which gems should come from the secondary source. To upgrade this warning to an error, run `bundle config disable_multisource true`.Gem::RemoteFetcher::FetchError: Errno::ECONNABORTED: An established connection was aborted by the software in your host machine. - SSL_connect (https://rubygems.org/gems/childprocess-0.5.8.gem)

看来是缺少childprocess-0.5.8.gem这个东西
索性把需要的包都下载到本地,然后本地安装
https://rubygems.org/gems/childprocess-0.5.8.gem
https://rubygems.org/gems/vagrant-winnfsd-1.1.0.gem
然后

vagrant plugin install childprocess-0.5.8.gem
vagrant plugin install vagrant-winnfsd-1.1.0.gem

看一看安装好的插件

$ vagrant plugin list
childprocess (0.5.8)
  - Version Constraint: 0.5.8
vagrant-share (1.1.4, system)
vagrant-winnfsd (1.1.0)
  - Version Constraint: 1.1.0

配置

编辑项目下的Vagrantfile文件
网络设置为私有,仅宿主机

config.vm.network "private_network", ip: "192.168.33.10"
# config.vm.network "private_network", type: "dhcp"

在Vagrant.configure(‘2’) do |config| 中加入

Vagrant.configure('2') do |config|
  # other config here
  config.vm.network "private_network", ip: "192.168.33.10"

  #winfsd
  config.winnfsd.logging = "on"
  config.winnfsd.uid = 1
  config.winnfsd.gid = 1
  config.vm.synced_folder "./", "/vagrant", type: "nfs"
end

SMB方式Windows共享方式

配置

Vagrant.configure('2') do |config|
  # other config here
  config.vm.network "private_network", ip: "192.168.33.10"

  #SMB
  config.vm.synced_folder "./", "/vagrant", type: "smb",
      smb_username: "母鸡Windows帐号",
      smb_password: "母鸡Windows密码",
      owner: "www",
      group: "www"
      #mount_options: ["dmode=775,fmode=664"]
end

启动vagrant虚拟机, 注意启动过程当中需要输入windwos系统的帐号和密码

d:\projects>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Preparing SMB shared folders...
    default: You will be asked for the username and password to use for the SMB
    default: folders shortly. Please use the proper username/password of your
    default: Windows account.
    default:
    default: Username: administrator
    default: Password (will be hidden):
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 => 2200 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
    default: Warning: Connection timeout. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.26
    default: VirtualBox Version: 5.0
==> default: Configuring and enabling network interfaces...
==> default: Mounting SMB shared folders...
    default: D:/projects => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.

d:\projects>

发现vagrant开启了一个名称为c30268623ba3dedeaa9f098b570dca21的共享
这个地方有个安全大坑,共享权限居然是Everyone!所以注意母鸡Windows上是否有其他帐号能访问!
我把c30268623ba3dedeaa9f098b570dca21本共享的权限改了,发现vagrant还是会把权限设置为Everyone
如果有开启其他普通帐号,guest什么的这里有安全隐患

C:\Users\Administrator>net share

共享名       资源                            注解

-------------------------------------------------------------------------------
IPC$                                         远程 IPC
c30268623ba3dedeaa9f098b570dca21
             D:\projects
命令成功完成。

为了防止smb共享剔除不活动的连接需要执行以下命令让系统不要自动踢掉不活动的连接
net config server /autodisconnect:-1

vagrant不会自动删除共享,要删除共享使用命令
net share c30268623ba3dedeaa9f098b570dca21 /delete

参考资料

vagrant-winnfsd项目源码
开启 NFS 文件系统提升 Vagrant 共享目录的性能
NFS for Vagrant on Windows
Vagrant SYNCED FOLDERS
Vagrant 使用 samba 共享文件夹

故障

vagrant管理的centos虚拟机在做过 yum -y update 后更新了系统以及内核

然后下一次vagrant up的时候共享目录不能挂载到/vagrant目录( /sbin/mount.vboxsf: mounting failed with the error: No such device)

详细信息

Failed to mount folders in Linux guest. This is usually because
the "vboxsf" file system is not available. Please verify that
the guest additions are properly installed in the guest and
can work properly. The command attempted was:

mount -t vboxsf -o uid=`id -u vagrant`,gid=`getent group vagrant | cut -d: -f3` vagrant /vagrant
mount -t vboxsf -o uid=`id -u vagrant`,gid=`id -g vagrant` vagrant /vagrant

The error output from the last command was:

/sbin/mount.vboxsf: mounting failed with the error: No such device

搜索资料知道原因是yum update更新了内核,但是没有更新VirtualBox的连接的问题。

需要执行

sudo /etc/init.d/vboxadd setup

修复即可

 

资料

‘vagrant up’ fails to mount linked directory /vagrant

Vagrant up遇到mount no such device的问题

下载安装VirtualBox和Vagrant

要使用Vagrant需要先安装依赖支持的VirtualBox。下一步,下一步默认安装完成2个软件。

下载安装 VirtualBox 官网:https://www.virtualbox.org/

下载安装 Vagrant 官网:http://www.vagrantup.com/

 

配置使用Vagrant 安装 kali linux

http://www.vagrantbox.es/ 这里提供了超全的linux系统预制box包。已经给你搞好系统了,任君选取所需,我这里选kali linux

先用迅雷把http://ftp.sliim-projects.eu/boxes/kali-linux-1.0-amd64.box

下载好,放入你的工作目录如D:\Vagrant

增加box

vagrant  box add base kali-linux-1.0-amd64.box

初始化

vagrant init

 

D:\Vagrant>vagrant  box add base kali-linux-1.0-amd64.box
==> box: Adding box ‘base’ (v0) for provider:
    box: Downloading: file://D:/Vagrant/kali-linux-1.0-amd64.box
    box: Progress: 100% (Rate: 65.2M/s, Estimated time remaining: –:–:–)
==> box: Successfully added box ‘base’ (v0) for ‘virtualbox’!

D:\Vagrant>vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Vagrantfile配置

打开目下的Vagrantfile配置文件

网络配置

Vagrant的网络有三种模式

1、端口映射方式,映射虚拟机中端口到宿主机

config.vm.network :forwarded_port, guest: 80, host: 8080

guest: 80 表示虚拟机中的80端口, host: 8080 表示映射到宿主机的8080端口。

2、私有网络

config.vm.network :private_network, ip: "192.168.1.104"

192.168.1.104 表示虚拟机的IP,多台虚拟机的话需要互相访问的话,设置在相同网段即可

3、桥接

config.vm.network :public_network

这样一个广播域的dhcp就可以分配ip了

目录映射

默认情况下,当前的工作目录,会被映射到虚拟机的 /vagrant 目录,当前目录下的文件可以直接在 /vagrant 下进行访问

也可以自己根据需要映射下

config.vm.synced_folder "work1/", "/data1"

前面的参数 “work1/”  表示的是本地的路径,这里使用对于工作目录的相对路径,这里也可以使用绝对路径,比如: “D:\Vagrant\work1”

启动

set VBOX_INSTALL_PATH=%VBOX_MSI_INSTALL_PATH%

vagrant up –provider=virtualbox

常用管理命令

vagrant up (启动虚拟机)
vagrant halt (关闭虚拟机——对应就是关机)
vagrant suspend (暂停虚拟机——只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,可以执行恢复操作后继续使用)
vagrant resume (恢复虚拟机 —— 与前面的暂停相对应)
vagrant destroy (删除虚拟机,删除后在当前虚拟机所做进行的除开Vagrantfile中的配置都不会保留)

参考来源

使用Vagrant在Windows下部署开发环境