大网数据广告活动
服务器运维问答

systemd Vagrant and VirtualBox: wait for synced folder to mount

· 52CE 我爱测

I'm trying to setup a systemd service on a Vagrant box which requires the Vagrant synced folder to be mounted before starting. Vagrantfile Vagrant.configure(2) do |config| config.vm.box = "ubuntu/yakkety64" config.vm.synced_folde…

问题原文

原帖链接 · rich remer · CC BY-SA 3.0

I'm trying to setup a systemd service on a Vagrant box which requires the Vagrant synced folder to be mounted before starting.

Vagrantfile

Vagrant.configure(2) do |config|
    config.vm.box = "ubuntu/yakkety64"
    config.vm.synced_folder ".", "/usr/local/src"
    config.vm.provision "shell", path: "bootstrap/bootstrap"
end

/etc/systemd/system/my-server.service

[Service]
ExecStart=/usr/bin/node /usr/local/src/index.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=my-server
User=ubuntu
Group=ubuntu

[Unit]
Requires=remote-fs.target
After=vboxguest.service
After=virtualbox-guest-utils.service

[Install]
WantedBy=remote-fs.target

If I do not enable the my-server service, the synced folder is properly mounted. But when I attempt to enable the my-server service, the service fails to find the mounted files. If I then login to the Vagrant box, the folder is not mounted. It seems like the service's attempt to load files from that path interferes with Vagrant.

So, how do I wait for the folders to be mounted before my service starts?

Other solutions fall short in various ways, requiring manual intervention to start the service, which is unacceptable.

社区高分解答

原帖链接 · Ivan Krechetov · CC BY-SA 3.0

I've just solved that kind of problem by adding:

[Install]
WantedBy=multi-user.target var-www-example.com.mount'

where var-www-example.com corresponds to:

config.vm.synced_folder ".", "/var/www/example.com"

in the Vagrantfile.

Thus, Vagrant has an XXX.mount systemd unit for each of its synced folder mount points. You can find yours with:

# systemctl list-units | fgrep .mount

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-10-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。