我能否在Vagrant中挂载共享文件夹之前强制进行配置?

3
我有以下的 Vagrantfile 文件:
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos/6"
  config.vm.provision :shell, :path => "bootstrap.sh"
  config.vm.network :forwarded_port, host: 8080, guest: 80
  config.vm.network :forwarded_port, host: 3306, guest: 3306
  config.vm.synced_folder "../../applications", "/var/www/html", :owner=>"apache", :group=>"apache"

  config.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", 2048]
      vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
      vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]
      vb.name = "appserver"
   end
end

每次我运行vagrant up(比如第一次,假设我先运行了vagrant destroy -f),都会出现以下错误:
==> default: Mounting shared folders...
    default: /vagrant => E:/Development/vagrant/centos6
    default: /var/www/html => E:/Development/applications/arx
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:

id -u apache    
The error output from the command was:    
id: apache: No such user

错误非常明显:"apache用户不存在"。据我所见,有两种或三种方法可以解决这个问题:
  • 通过从synced_folder中删除:owner => "apache",:group => "apache"部分来解决问题。对于我来说,这是一个大问题,因为如果我将它们移除,那么文件夹将被vagrant拥有,当apache尝试从/var/www/html读取某些内容时,可能会引起问题。
  • 通过注释掉config.vm.synced_folder,启动盒子让所有东西都安装和设置好,然后关闭盒子并再次启动未注释的行:它可以工作,但仍然是一个丑陋的解决方案。
  • 通过强制在任何东西被安装之前进行配置:这是理想的解决方案。

是否有人知道是否可能实现第三个解决方案,如果是,该怎么做?我找不到任何关于第三个解决方案的信息:(如果您有更好的解决方案,欢迎在此发布,它可能会帮助我和其他人。

我已经找到了 这个这个,但它们并没有提供帮助。

2个回答

2

一个可能的解决方案是将Apache作为Vagrant用户运行。

在您的/etc/httpd/conf文件中,您可以替换User和Group的值为:

User vagrant
Group vagrant

这样你就可以继续与vagrant用户共享文件夹,而httpd将以vagrant用户身份运行。


0

你可以使用数字 ID,然后创建一个用户

config.vm.synced_folder '${HOSTDIR}', '${EXPORTDIR}', type: "virtualbox", owner: '123', group: '456'
config.vm.provision :shell, inline: "useradd --system -g '456' -u '123' -d '${EXPORTDIR}' --shell /bin/false apache"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接