nfsサーバになってストレージを他の(Linux)マシンと共有させるには下記のようにする

/dev/sda は /(system disk)
/dev/sdb は /apps(アプリ共有部分) をもって、
/dev/sdc は /home(ホームディレクトリ共有部分) として用意したいと思います。
*ローカルのデバイスへのACL適用はデフォで有効なのですが、nfs先へのACLはnfs向けのACL(nfs4-acl-tools)と uid/gid情報が必要になります.
nfsサーバをクラスターのログインノードとして扱ってもいい. 多分幸せ. でも容量が増える/増やす際は再起動が必要になる場合もある.
freeipaでクラスターの管理なら libvrt でvmを作った方がいいかも. nis?前へ進もう.
system disk(/dev/sda)にはOSが入っている. 追加に /apps と /home を加える
[root@nfs ~]# cat /etc/redhat-release
Rocky Linux release 9.7 (Blue Onyx)
[root@nfs ~]# parted -s /dev/sdb mklabel gpt mkpart primary 0% 100%
[root@nfs ~]# mkfs.xfs -L apps /dev/sdb1
[root@nfs ~]# parted -s /dev/sdc mklabel gpt mkpart primary 0% 100%
[root@nfs ~]# mkfs.xfs -L home /dev/sdc1
[root@nfs ~]# echo "LABEL=apps /apps xfs defaults 0 0" >> /etc/fstab
[root@nfs ~]# echo "LABEL=home /home xfs defaults 0 0" >> /etc/fstab
[root@nfs ~]# mkdir /apps
[root@nfs ~]# systemctl daemon-reload
[root@nfs ~]# mount -a
[root@nfs ~]# df -lTh
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs tmpfs 1.6G 8.7M 1.6G 1% /run
/dev/sda5 xfs 31G 1.7G 30G 6% /
/dev/sda2 xfs 960M 206M 755M 22% /boot
tmpfs tmpfs 794M 0 794M 0% /run/user/0
/dev/sdb1 xfs 32G 261M 32G 1% /apps
/dev/sdc1 xfs 128G 946M 128G 1% /home
[root@nfs ~]#*「LABEL」を使う利点. 「/dev/disk/by-label」にLABELの情報が見えるのでmount先がどこかが分かる. UUIDだけでは分からん.
nfsサーバの下準備
[root@nfs ~]# dnf install nfs-utils
[root@nfs ~]# systemctl enable nfs-server --now次に「/etc/exports」を作る
従来のその場所をそのまま提供する方法
/apps 192.168.0.0/255.255.255.0(rw,no_root_squash)
/home 192.168.0.0/255.255.255.0(rw,no_root_squash)疑似ファイルシステムとして提供する場合. /nfsとか/exportにexportしたい場所をbindさせてexportする
疑似ファイルシステムの利点はexportする場所を分からなくさせる効果がある. 例えば /lustre1/group1/apps を隠して /appsのみ出したいとか
[root@nfs ~]# mkdir /nfs/{apps,home} -p
[root@nfs ~]# mount --bind /apps /nfs/apps
[root@nfs ~]# mount --bind /home /nfs/home
(恒久的対応のために)
[root@nfs ~]# vi /etc/fstab
:
/apps /nfs/apps none bind 0 0
/home /nfs/home none bind 0 0
[root@nfs ~]#そして疑似ファイルシステム経由での「/etc/exports」は下記のようにする
/nfs 192.168.0.0/255.255.255.0(ro,fsid=0)
/nfs/apps 192.168.0.0/255.255.255.0(rw,no_root_squash)
/nfs/home 192.168.0.0/255.255.255.0(rw,no_root_squash)fsid=0の場所はroがいいみたい
「/etc/exports」の作成が完了したらexportfsコマンドで反映させる
[root@nfs ~]# exportfs -avr
exporting 192.168.0.0/255.255.255.0:/nfs/home
exporting 192.168.0.0/255.255.255.0:/nfs/apps
exporting 192.168.0.0/255.255.255.0:/nfs
[root@nfs ~]#nfsサービスを提供しているので穴をあける.
NFSv4のみなら
[root@nfs ~]# firewall-cmd --add-service=nfs --zone=public --permanent
[root@nfs ~]# firewall-cmd --reloadもしNFSv3でmountする可能性があるなら
[root@nfs ~]# firewall-cmd --add-service=nfs --add-service=rpc-bind --add-service=mountd --zone=public --permanent
[root@nfs ~]# firewall-cmd --reloadとします。
ここもいろいろmount方法があります.
* mount コマンド
* /etc/fstab に直接記載
* autofs を経由する方法
|
+--- direct map 方法
|
+--- indirect map 方法
mountコマンドは言わずもがな.
「mount -t nfs -o vers=4 nfs:/aspp /apps」とかで. ほとんどデバック用途でしょうか. あるいは一時的用途に.
次に /etc/fstab に直接記載する方法
[root@rockylinux9 ~]# vi /etc/fstab
:
nfs:/home /home nfs _netdev,x-systemd.automount,nofail,noatime 0 0
nfs:/apps /apps nfs _netdev,x-systemd.automount,noatime 0 0
[root@rockylinux9 ~]# mkdir /apps
[root@rockylinux9 ~]# systemctl daemon-reload
[root@rockylinux9 ~]# mount -aこれでmountができる.
| オプション | 意味 |
| _netdev | ネットワークが上がってからmount |
| x-systemd.automount | アクセス時に自動mount(systemdを利用). systemd unit が生成されます |
| noatime | NFSアクセスでatime更新しない(性能改善) |
| nofail | NFS server unreachableでもbootは継続します |
| soft/hard | デフォのhardでいいみたい |
/etc/fstabにmount情報を記載せずautofsを使う場合です
autofsとしてもそのmount情報はどこから取得するかでまた違いがって、それは「/etc/nsswitch.conf」で定義されています。
[root@rockylinux9 ~]# cat /etc/nsswitch.conf
:
automount: sss files
:
[root@rockylinux9 ~]#「files」なら/etc/auto.master(.d/)を参照しますが、「sss」ならsss由来で定義されますが、sssdの設定次第. FreeIPAだったりActiveDirectoryだったりしますね.
「files」とした場合でのauto.masterとauto.homeは下記のようにします
[root@rockylinux9 ~]# cat /etc/auto.master
/- /etc/auto.home --timeout=60
[root@rockylinux9 ~]# cat /etc/auto.home
/home nfs:/home
/apps nfs:/apps
(あるいは)
/home/saber nfs:/home/saber
/home/illya nfs:/home/illya
/apps nfs:/apps
[root@rockylinux9 ~]#
[root@rockylinux9 ~]# systemctl restart autofs「direct map」で/appsや/homeのみとすると、クライアントに /home/saber があったとしてもnfsサーバ由来の /home に差し変わります。見えなくなるだけですが、注意点の一つ。
「/apps」とかならいいけど、「/home」は避けた方がいいかもね
必要なフォルダのみをmountする方式です
[root@rockylinux9 ~]# cat /etc/auto.master
/apps /etc/auto.apps --timeout=60
/home /etc/auto.home --timeout=60
[root@rockylinux9 ~]# cat /etc/auto.apps
* nfs:/apps/&
[root@rockylinux9 ~]# cat /etc/auto.home
* nfs:/home/&
(あるいは)
saber nfs:/home/saber
illya nfs:/home/illya
:
[root@rockylinux9 ~]#
[root@rockylinux9 ~]# systemctl restart autofs「indirect map」の場合、その場所に行かないとmountされない. クライアントで「ls /home」としても/home/saberは表示されません。なのである程度セキュア?かもね。
/appsはアプリケーションなので「direct map」、/homeはそのクライアントのみのユーザもあり得るので「indirect map 」がいいかなと思う
そうなると
[root@rockylinux9 ~]# cat /etc/auto.master
/apps auto.apps --timeout=60
/home auto.home --timeout=60
[root@rockylinux9 ~]# cat /etc/auto.apps
/apps nfs:/apps
[root@rockylinux9 ~]# cat /etc/auto.home
* nfs:/home/&
[root@rockylinux9 ~]#でいいかなと
[root@nfs ~]# semanage fcontext -a -t home_root_t /nfs/home
[root@nfs ~]# restorecon -R /nfs/homenfsで共有された場所をsmbで供出することは可能.
ただ場合によっては遅くなるとかの症状があるとのこと.
一応出来る. ただ問題が発生する可能性もあるよとのこと.
[root@nfs ~]# cat /etc/samba/smb.conf
[global]
workgroup = SAMBA
security = user
passdb backend = tdbsam
printing = cups
printcap name = cups
load printers = yes
cups options = raw
[home]
path = /home/
browseable = yes
read only = no
inherit acls = Yes
[root@nfs ~]# systemctl enable smb && systemctl start smb
[root@nfs ~]# smbpasswd -a userA
[root@nfs ~]# firewall-cmd --add-service=samba --permanent
[root@nfs ~]# firewall-cmd --reload
[root@nfs ~]# setsebool -P samba_enable_home_dirs 1
[root@nfs ~]# setsebool -P samba_share_nfs on問題点としてあるのがファイルロック機構がNFS由来とsmb由来が被ること.
片方を使うようにできれば大変宜しいみたい.
nfsで統一なら、windows10にはnfsクライアント機能があるようでこれを活用する. macOSはそもそも持っているから大丈夫
ただuid/gidのマッピングが面倒でしょうか
smbで統一なら、計算ノードのmountをcifs mountにすれば解決可能みたい.
https://wiki.centos.org/TipsAndTricks/WindowsShares
windowsクライアントマシンが nfs プロトコルを喋ればいい. そうしてsmb/cifsを排除できる.
昔HummingbirdからNFS Maestroとかでwindows向けNFSクライアントアプリが有償販売されていたが、OS側でもnfs機能を提供されているみたい.
[コントロール パネル]->[すべてのコントロール パネル項目]->[プログラムと機能]を開き、nfsパッケージをインストールします

これで「mount」「rpcinfo」コマンドが使えます
使い方は同じで「コマンドプロンプト」を立ち上げて下記のように実行します.
mount nfs:/home S:
(管理者権限は不要. 一般ユーザで実行可能)マウントポイント先がドライブ名になることに留意.
*nfsサーバにおける/etc/exportsフォーマットは従来型なら大丈夫. 疑似ファイルシステムはどうもダメみたい.
一応これでnfs mountは出来ましたが、大事なのはユーザマッピング. windowsアカウントの userX は unixアカウントの誰に符合するのか. その定義が大事.
windows側のアカウントとnfs側のアカウントが下記のような関係だった場合
| windows | posixAccount(/etc/passwd) | ||
| ユーザ名 | グループ名 | UID | GID |
| Administrator | BUILTIN\Administrators | 0 | 0 |
| illya | Users | 1000 | 1000 |
| saber | Users | 1001 | 1000 |
ならwindows10の「%SystemRoot%\system32\drivers\etc\group」に下記を作ります
BUILTIN\Administrators:x:0:root
Users:x:1000:illya,saberそして「%SystemRoot%\system32\drivers\etc\passwd」も作成します
Administrator:x:0:0:root:/root:/bin/bash
illya:x:1000:1001:illya:/home/illya:/bin/bash
saber:x:1001:1001:saber:/home/saber:/bin/bash「メモ帳」アプリを「管理者として実行」にて起動させ、該当の場所に保存すればいいみたい.
作成後一旦再起動して、面倒だが再度mount操作を行えばアクセスOK
groupとpasswdファイルをwindows側に用意できれば便利かもしれないが、エクスプローラーのアドレスで「\\nfs\home」と入力すれば即座に使えるのに比べるとやや面倒かな.
あとmacOSにはこのようなマッピング機能がない模様. なのでヘテロ環境ではこの方法は難しいかも.
windwos/macOSクライアントに手を加えず、smb/cifsプロトコルで全てをカバーするなら計算ノードとかのmountにsmb/cifsを宛がう必要がある.
ローカルなストレージデバイスならACLはsetfaclで行えますが、nfs mount先では機能しないです.
[root@nfs-client ~]# mount |grep apps
nfs-rockylinux:/apps on /apps type nfs4 (rw,relatime,vers=4.2,rsize=262144,wsize=262144,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.0.23,local_lock=none,addr=192.168.0.11)
[root@nfs-client ~]# id saber
uid=1000(saber) gid=1000(saber) groups=1000(saber)
[root@nfs-client ~]# setfacl -m user:saber:rwx /apps/cryo
setfacl: /apps/cryo: Operation not supported
[root@nfs-client ~]#っでnfs mountしたボリュームに ACL を掛けるには「nfs4-acl-tools」を使います
ただ要件としては
[root@nfs-client ~]# dnf install nfs4-acl-toolsRHEL7系,RHEL8系,REHL9系にもあるみたい. ubuntu20,22もあるみたい
っで使い方.
nfsマウント先の「/apps/cryo」にユーザsaberのみ書き込み権限を与えてみる.
[root@nfs-client ~]# ls -ld /apps/cryo/
drwxr-xr-x. 2 root root 6 Feb 13 03:22 /apps/cryo/
[root@nfs-client ~]# nfs4_getfacl /apps/cryo/
# file: /apps/cryo/
A::OWNER@:rwaDxtTcCy
A::GROUP@:rxtcy
A::EVERYONE@:rxtcy
[root@nfs-client ~]# id saber
uid=1000(saber) gid=1000(saber) groups=1000(saber)
[root@nfs-client ~]# nfs4_setfacl -a A::1000:RWX /apps/cryo/ <-- 1000はuid. これをidで行うには/etc/idmapd.confにdomain(sybyl.local)を定義して「A::saber@sybyl.local:RWX」とする
[root@nfs-client ~]# ls -ld /apps/cryo/
drwxr-xr-x+ 2 root root 6 Feb 13 03:22 /apps/cryo/
[root@nfs-client ~]# nfs4_getfacl /apps/cryo/
# file: /apps/cryo/
A::OWNER@:rwaDxtTcCy
A::1000:rwaDxtcy <-- 値1000はuid. これは/etc/idmapd.confを有効にしアカウント名での定義であってもuidになるみたい
A::GROUP@:rxtcy
A::EVERYONE@:rxtcy
[root@nfs-client ~]#確認として
[saber@nfs-client ~]$ touch /apps/cryo/sample
[saber@nfs-client ~]$ mkdir /apps/cryo/data-sample
[saber@nfs-client ~]$ ls -l /apps/cryo/
total 0
drwxr-xr-x. 2 saber saber 6 Feb 13 04:53 data-sample
-rw-r--r--. 1 saber saber 0 Feb 13 04:53 sample
[saber@nfs-client ~]$
(他のユーザでは)
[illya@nfs-client ~]$ id illya
uid=1001(illya) gid=1001(illya) groups=1001(illya)
[illya@nfs-client ~]$ touch /apps/cryo/sample2
touch: cannot touch '/apps/cryo/sample2': Permission denied
[illya@nfs-client ~]$ mkdir /apps/cryo/data-sample2
mkdir: cannot create directory ‘/apps/cryo/data-sample2’: Permission denied
[illya@nfs-client ~]$なんか/etc/idmapd.confのdomain定義なしで、uidベースで設定するのがいいのかも.
外し方. 「-x」を使います. 引数はnfs4_getfacl の値を参照した方がいいのかも.
[root@nfs-client ~]# nfs4_getfacl /apps/cryo/
# file: /apps/cryo/
A::OWNER@:rwaDxtTcCy
A::1000:rwaDxtcy
A::GROUP@:rxtcy
A::EVERYONE@:rxtcy
[root@nfs-client ~]# nfs4_setfacl -x A::1000:rwaDxtcy /apps/cryo/
[root@nfs-client ~]# nfs4_getfacl /apps/cryo/
# file: /apps/cryo/
A::OWNER@:rwaDxtTcCy
A::GROUP@:rxtcy
A::EVERYONE@:rxtcy
[root@nfs-client ~]# ls -l /apps/cryo/
total 0
drwxr-xr-x. 2 saber saber 6 Feb 13 04:53 data-sample
-rw-r--r--. 1 saber saber 0 Feb 13 04:53 sample
[root@nfs-client ~]#当然ながら権限を外しても既に作ったファイルの所有者情報は残ります.