Vagrant chicken-and-egg:与uid = apache用户共享的文件夹

时间:2013-07-31 09:20:01

标签: linux apache vagrant

My Vagrant框是从基础linux(科学linux)构建的,在配置期间(使用shell脚本),安装了Apache。

我最近将Vagrant文​​件(v2)更改为:

config.vm.synced_folder "public", "/var/www/sites.d/example.com",
   :owner => "apache", :group => "apache"

如果该框已经配置并且刚刚重新启动,那么哪个效果很好。

现在,在vagrant destroy && vagrant up我得到错误之后:

mount -t vboxsf -o uid=`id -u apache`,gid=`id -g apache` 
   /var/www/sites.d/example.com /var/www/sites.d/example.com
id: apache: User does not exist

这很清楚 - 在初始运行期间,尚未安装apache。

一个丑陋的解决方法当然是使用synced_folder注释掉的基本配置,对其进行评论然后重新启动。

有什么清洁技巧可以解决这个问题吗?特别是vagrant up始终在没有中断的情况下运行,即使该框是新的。

5 个答案:

答案 0 :(得分:26)

如果您可以修复uid / gid值,可以在mount命令中使用它们 - 它们不必与现有用户/组相关

我使用后来由puppet使用固定(匹配)uid / gid值创建的用户执行此操作

config.vm.synced_folder "foo", "/var/www/foo",
   id: "foo", :mount_options => ["uid=510,gid=510"]

答案 1 :(得分:9)

这就是我所做的:

config.vm.synced_folder "./MyApp", "/opt/MyApp", owner: 10002, group: 1007, create: true

config.vm.provision :shell do |shell|
  shell.inline = "groupadd -g 1007 myapp;
                  useradd -c 'MyApp User' -d /opt/MyApp -g myapp -m -u 10002 myapp;"
end

使用uid和gid代替使用用户名和组(作为文本)。然后使用这些ID创建组和用户。这是因为错误实际上是:

mount -t vboxsf -o uid=`id -u myapp`,gid=`getent group myapp | cut -d: -f3` opt_MyApp /opt/MyApp
...
id: myapp: No such user

id命令无法识别用户。因此,切换到uid和gid命令id不会被vagrant使用。

我用这种方法得到的唯一警告是用户主目录(/ opt / MyApp)已经存在,但我可以使用它,或者您可以更改useradd命令以忽略主目录(如果已存在)。

在此之前,我使用的解决方法是:

vagrant up; vagrant provision; vagrant reload

但是,它既不干净又不干净。

答案 2 :(得分:8)

Ryan Sechrestdealt extensively with this problem

提出的解决方案之一是:

  

将目录的权限设置为777,将文件设置为666

config.vm.synced_folder "/Users/ryansechrest/Projects/Sites", 
  "/var/www/domains", mount_options: ["dmode=777", "fmode=666"]

答案 3 :(得分:2)

我是如何解决这个问题的,我首先将共享配置为Vagrantfile而没有用户或组信息。然后在配置阶段,我卸载共享并使用适当的用户和组信息重新安装它。 e.g:

exec {
'umount /share/location':
  command => 'umount /share/location';
} -> exec {
'mount /share/location':
  command => 'mount -t vboxsf -o uid=`id -u apache`,gid=`id -g apache` /share/name /share/location'

您可以从virtualbox检查共享名称,也可以通过运行带有调试标志和非工作设置的配置来检查共享名称(它会打印出实际的mount命令)。我知道这是一种解决方法,可能不适用于所有情况,但它对我有用。

答案 4 :(得分:0)

就我而言,我不需要在供应阶段挂载synced_folder。因此,如果未配置访客,则我将禁用synced_folder

检查是否在Vagrantfile is a hack but it works中进行了配置。

对我来说,这样做很公平

vagrant up   # To build and provision the first time
vagrant halt # An intermediate step to mount the folder
vagrant up   # To have the folder mounted

所以我的Vagrantfile是这样的:

def provisioned?(vm_name='default', provider='virtualbox')
  File.exist?(".vagrant/machines/#{vm_name}/#{provider}/action_provision")
end

Vagrant.configure(2) do |config|
  [ ... ]
  config.vm.synced_folder "../geoserver_data", "/var/lib/geoserver_data",
    disabled: !provisioned?,
    owner: "tomcat7",
    group: "tomcat7"
  [ ... ]