LXC

LXC自动挂载威联通NFS4共享文件夹

接上文,LXC直通PVE核显之后,需要解决的问题就是容器开机之后自动挂载多媒体文件夹为下一步安装Jellyfin多媒体管理器做铺垫。

话不多说,直接开整。我上文已经写了我使用的LXC模板是debian11。

一、威联通的准备工作

1、进入QNAP控制台-->网络和文件服务-->Win/Mac/NFS/WebDAV-->Linux NFS服务,选中“激活NFS v4服务”,点击应用。

激活NFS v4服务

备注:这里隐藏着一个坑,后文也会提到:Linux的NFS默认链接的版本是NFS v3,但是我只开启了NFS v4。这就导致了我NFS的配置文件明明看上去没有问题,但是NFS网盘它就是挂载不上。

2、QNAP5.0以后还需要单独设置NFS共享的访问权限,家庭共享的话直接如下图一样开放读写就行了。

二、CT容器下的相关配置

1、安装NFS客户端

apt-get install nfs-common

2、查看并确认可挂载的NFS目录

root@Debian11:~# showmount -e 172.30.100.11
Export list for 172.30.100.11:
/Adult Video 172.30.100.3

3、手动挂载NFS

mount -t nfs4 172.30.100.11:/"Adult Video" /mnt/nfs/"Adult Video"

备注:坑点来了,挂载的时候一定要写上NFS版本号。如果只写NFS。会挂载失败,因为NFS默认协议版本是v3。

4、验证NFS是否挂载成功

ls /mnt/nfs/"Adult Video"

三、配置开机自启

1、将挂载配置写入fstab

root@Debian11:~# nano /etc/fstab
172.30.100.11:/Adult\040Video   /mnt/nfs/Adult\040Video   nfs4      defaults,_netdev,x-systemd.automount    0 0

备注:

① 如果挂载的文件夹名称有空格,请使用转义符“\040”表示;

② 挂载类型一定要有协议版本号,nfs4或者nfs vers=4,;

③ _netdev,防止客户端在网络就绪之前开始挂载文件系统;

④ x-systemd.automount,debian系linux(包括且不限于Ubuntu)自动挂载参数;

⑤ 第一个0,非零值表示文件系统应由dump备份。对于NAS文件系统而言,此值默认为0;

⑥ 第二个0,该值表示fsck在启动时检查文件系统的顺序。对于NAS文件系统而言,此值默认为0,表示fsck不应在启动时运行。

2、重启容器,或者断开之前挂载的NFS,使用如下命令验证NFS是否会自动挂载

root@Debian11:~# mount -a -t nfs4

3、将命令写入/etc/rc.local,并赋予执行权限

[ ! -f /etc/rc.local ] && echo '#!/bin/bash' > /etc/rc.local; echo "mount -a -t nfs4" >> /etc/rc.local; chmod +x /etc/rc.local

备注:由于某些软件并没有增加开机启动的服务,很多时候需要手工添加,一般我们都是推荐使用systemd写个系统服务,但是对于一些简单的脚本或者懒人来说,添加命令到/etc/rc.local文件更方便,但是自从 Debian 9 开始,Debian 默认不带/etc/rc.local文件,而只有rc-local服务,并且默认情况下这个服务还是关闭的状态。为了解决这个问题,我们需要手工添加一个/etc/rc.local文件,并赋予其可执行权限。

4、启动 rc-local 服务:

systemctl enable --now rc-local

此时可能会弹出警告:

The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.
 
Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.

无视警告,因为这个服务没有任何依赖的系统服务,只是开机启动 /etc/rc.local 脚本而已。

5、重启容器以后,查看挂载是否生效了。

root@Debian11:~# systemctl status rc-local
* rc-local.service - /etc/rc.local Compatibility
      Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
     Drop-In: /usr/lib/systemd/system/rc-local.service.d
             `-debian.conf
       Active: active (exited) since Sat 2022-06-18 01:50:41 CST; 10s ago
         Docs: man:systemd-rc-local-generator(8)
    Process: 237 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
          CPU: 5ms

Jun 18 01:50:40 Debian11 systemd[1]: Starting /etc/rc.local Compatibility...
Jun 18 01:50:41 Debian11 systemd[1]: Started /etc/rc.local Compatibility.

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Scroll to top