如何为 vagrant 中尚不存在的用户挂载目录

How to mount a directory for a user that doesn't exist yet in vagrant

我正在尝试使用共享文件夹机制在 vagrant 中挂载数据磁盘,除了该目录默认由 vagrant 拥有且 tomcat6 无法写入外,该机制运行良好它。所以我想我可以使用以下方法在 vagrantfile 中设置磁盘的所有者:

config.vm.synced_folder "data/", "/data", owner: "tomcat6", group: "tomcat6"

但是 tomcat6 用户还不存在,因为 tomcat 配方还没有 运行 所以 vagrant 失败了:

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 tomcat6`,gid=`getent group tomcat6 | cut -d: -f3` data /data
mount -t vboxsf -o uid=`id -u tomcat6`,gid=`id -g tomcat6` data /data

The error output from the last command was:

stdin: is not a tty
id: tomcat6: no such user
id: tomcat6: no such user
uid= requires an argument (i.e. uid==<arg>)

现在我可以通过使用世界写权限挂载 /data 来解决这个问题,但这对我来说似乎 "wrong"(即使在测试机器上)。

那么有什么方法可以让 vagrant 在我挂载磁盘之前为我创建一个新用户,或者让 Chef 稍后为我提供一个共享磁盘?

您可以切换到 nfs 同步提供程序,因为它支持具有未知 UID 的远程文件。有关详细信息,请参阅 https://docs.vagrantup.com/v2/synced-folders/nfs.html

如果您知道未来用户的 UID/GID 是什么,您可以简单地提供这些值而不是 user/group 名称。例如如果 'tomcat6' 的 GID/GID 为 11111,您可以在 Vagrantfile 中声明:

config.vm.synced_folder "data/", "/data", owner: "11111", group: "11111"

我在自己的 VM 上使用的解决方法是强制 Vagrant 在 运行 配置后重新运行 初始设置:

vagrant up  # this fails at synced_folder; put that at the end of Vagrantfile
vagrant provision  # this creates the user
vagrant halt  # shut down
vagrant up  # re-runs the synced_folder command, this time the user exists

它不优雅,但将它放在 bash 脚本中,它会支持一个工作的 VM。