如何确保在 NFS 服务器启动之前安装本地文件系统?

如何确保在 NFS 服务器启动之前安装本地文件系统?

我有一台简单的 Ubuntu 20.04 家庭服务器,通过 NFS 共享目录。共享的目录包含在本地文件系统中,但不包含在根文件系统中。有时在启动时,NFS 服务器会在挂载准备就绪之前启动,因此 NFS 服务器会出错。我需要登录并重新启动 NFS 服务器(sudo systemctl restart nfs-server.service),或者重新启动并碰碰运气。

有没有办法确保只有在文件系统准备就绪后才启动 NFS 服务器?


来自 NFS 服务器的错误如下:

$ systemctl status nfs-server.service
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2020-05-19 11:16:34 BST; 3h 10min ago
    Process: 927 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=1/FAILURE)
    Process: 931 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
    Process: 932 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)

May 19 11:16:34 box systemd[1]: Starting NFS server and services...
May 19 11:16:34 box exportfs[927]: exportfs: /etc/exports [2]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/storage/Shared".
May 19 11:16:34 box exportfs[927]:   Assuming default behaviour ('no_subtree_check').
May 19 11:16:34 box exportfs[927]:   NOTE: this default has changed since nfs-utils version 1.0.x
May 19 11:16:34 box exportfs[927]: exportfs: Failed to stat /storage/Shared: No such file or directory
May 19 11:16:34 box systemd[1]: nfs-server.service: Control process exited, code=exited, status=1/FAILURE
May 19 11:16:34 box systemd[1]: nfs-server.service: Failed with result 'exit-code'.
May 19 11:16:34 box systemd[1]: Stopped NFS server and services.

/etc/fstab行如下所示:

UUID=<blah>   /storage   btrfs   nofail,subvol=@storage,compress-force=zstd   0       0

类似/etc/exports这样的:

/storage/Shared    *(ro,insecure,all_squash)

答案1

按照@steeldriver 和@Rinzwind 评论中的提示,我设法使其工作正常。

首先,确定为挂载点生成的 systemd 单元的名称(在我的情况下/storage):

$ systemctl list-units | grep '/storage' | grep 'mount'
  storage-.snapshots.mount      loaded active mounted   /storage/.snapshots                                                          
  storage.mount                 loaded active mounted   /storage         

然后,这些文档关于如何修改现有的单元文件,我添加了新的After=调整nfs-server.service

$ cat /etc/systemd/system/nfs-server.service.d/custom.conf 
[Unit]
After=storage.mount

接下来,应用更改并重新启动nfs-server以确认新配置已被采用(请注意已custom.conf列出):

$ sudo systemctl daemon-reload
$ sudo systemctl restart nfs-server.service
$ systemctl status nfs-server.service
● nfs-server.service - NFS server and services
     Loaded: loaded (/lib/systemd/system/nfs-server.service; enabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/nfs-server.service.d
             └─custom.conf
     Active: active (exited) since Sun 2020-05-24 20:52:08 BST; 6s ago
    Process: 20384 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
    Process: 20385 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
   Main PID: 20385 (code=exited, status=0/SUCCESS)

May 24 20:52:07 box systemd[1]: Starting NFS server and services...
May 24 20:52:07 box exportfs[20384]: exportfs: /etc/exports [2]: Neither 'subtree_check' or 'no_subtree_check' specified for export "*:/storage/Shared".
May 24 20:52:07 box exportfs[20384]:   Assuming default behaviour ('no_subtree_check').
May 24 20:52:07 box exportfs[20384]:   NOTE: this default has changed since nfs-utils version 1.0.x
May 24 20:52:08 box systemd[1]: Finished NFS server and services.

最后,重新启动,并享受挂载点可用后启动的 NFS 服务器。

相关内容