# NFS Server

### Structure

<div class="table-wrapper" id="bkmrk-name-maj%3Amin-rm-size"><table><tbody><tr><th colspan="1" rowspan="1">NAME</th><th colspan="1" rowspan="1">MAJ:MIN</th><th colspan="1" rowspan="1">RM</th><th colspan="1" rowspan="1">SIZE</th><th colspan="1" rowspan="1">RO</th><th colspan="1" rowspan="1">TYPE</th><th colspan="1" rowspan="1">MOUNTPOINTS</th></tr><tr><td colspan="1" rowspan="1">sdb</td><td colspan="1" rowspan="1">8:16</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">3.6T</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">disk</td><td colspan="1" rowspan="1"> </td></tr><tr><td colspan="1" rowspan="1">└─ hdds-datas</td><td colspan="1" rowspan="1">253:0</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">7.3T</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">lvm</td><td colspan="1" rowspan="1">/export/media</td></tr><tr><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1">/srv/dev-disk-by-uuid-a67ae390-f36c-4b7c-98a0-4a3b5601c107</td></tr><tr><td colspan="1" rowspan="1">sdc</td><td colspan="1" rowspan="1">8:32</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">3.6T</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">disk</td><td colspan="1" rowspan="1"> </td></tr><tr><td colspan="1" rowspan="1">└─ hdds-datas</td><td colspan="1" rowspan="1">253:0</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">7.3T</td><td colspan="1" rowspan="1">0</td><td colspan="1" rowspan="1">lvm</td><td colspan="1" rowspan="1">/export/media</td></tr><tr><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1"> </td><td colspan="1" rowspan="1">/srv/dev-disk-by-uuid-a67ae390-f36c-4b7c-98a0-4a3b5601c107</td></tr></tbody></table>

</div>```bash
apt install -y lvm2
```

### Disks

```bash
fdisk -l
```

```
Disk /dev/sdb: 3.64 TiB, 4000787030016 bytes, 7814037168 sectors
Disk model: ST4000VN006-3CW1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/sdc: 3.64 TiB, 4000787030016 bytes, 7814037168 sectors
Disk model: ST4000VN006-3CW1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/hdds-datas: 7.28 TiB, 8001566015488 bytes, 15628058624 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
```

### LVM

#### Physical Volumes (partitions)

```
pvcreate /dev/sdb
pvcreate /dev/sdc
```

#### Volume Group

```
vgcreate hdds /dev/sdb /dev/sdc
```

#### Logical Volume(s)

```
lvcreate --extents 100%FREE -n datas hdds
```

```
pvdisplay 
```

```
--- Physical volume ---
PV Name               /dev/sdb
VG Name               hdds
PV Size               <3.64 TiB / not usable <3.84 MiB
Allocatable           yes (but full)
PE Size               4.00 MiB
Total PE              953861
Free PE               0
Allocated PE          953861
PV UUID               zHiPKx-t2BO-15r3-xPfQ-DPlB-7K40-YxIdM1

--- Physical volume ---
PV Name               /dev/sdc
VG Name               hdds
PV Size               <3.64 TiB / not usable <3.84 MiB
Allocatable           yes (but full)
PE Size               4.00 MiB
Total PE              953861
Free PE               0
Allocated PE          953861
PV UUID               bGAdMs-JsRy-6r3M-0zWt-CYQJ-GPsB-KCnCo6
```

```bash
vgdisplay
```

```
--- Volume group ---
VG Name               hdds
System ID
Format                lvm2
Metadata Areas        2
Metadata Sequence No  6
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               1
Max PV                0
Cur PV                2
Act PV                2
VG Size               <7.28 TiB
PE Size               4.00 MiB
Total PE              1907722
Alloc PE / Size       1907722 / <7.28 TiB
Free  PE / Size       0 / 0
VG UUID               DPZPo2-OwJt-R1wG-wJRB-HD38-JeVl-5ZFfnI
```

```
lvdisplay
```

```
--- Logical volume ---
LV Path                /dev/hdds/datas
LV Name                data
VG Name                hdds
LV UUID                qgdjwr-9Aau-p4mj-rksl-IeBB-SjD1-z6hede
LV Write Access        read/write
LV Creation host, time polito, 2023-08-01 10:10:14 +0200
LV Status              available
# open                 0
LV Size                <7.28 TiB
Current LE             1907722
Segments               2
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           254:0
```

#### Format LV
```bash
sudo mkfs.ext4 /dev/hdds/datas
```

### NFS
```
# mkdir -p /mnt/data/cloud
# mkdir -p /mnt/data/gitea
# mkdir -p /mnt/data/media
# mkdir -p /mnt/data/media/downloads

sudo vim /etc/exports
```

```
# /etc/exports: the access control list for filesystems which may be exported to NFS clients.  See exports(5).
#
# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)
#
# /export 192.168.1.0/24(rw,no_root_squash,no_subtree_check)
/srv/nfs4           192.168.1.0/24(rw,sync,no_subtree_check,crossmnt,fsid=0)
/srv/nfs4/cloud     192.168.1.0/24(rw,sync,wdelay,hide,nocrossmnt,insecure,no_root_squash,no_all_squash,subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,no_root_squash,no_all_squash)
/srv/nfs4/media     192.168.1.0/24(rw,sync,wdelay,hide,nocrossmnt,insecure,no_root_squash,no_all_squash,subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,no_root_squash,no_all_squash)
/srv/nfs4/downloads 192.168.1.0/24(rw,sync,wdelay,hide,nocrossmnt,insecure,no_root_squash,no_all_squash,subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,no_root_squash,no_all_squash)
/srv/nfs4/backups   192.168.1.0/24(ro,sync,no_subtree_check)

```

```bash
sudo systemctl restart nfs-server
```

##### Get LVM's UUID

```bash
sudo blkid -s UUID -o value /dev/mapper/hdds-datas
```

##### Mount LVM

```
sudo vim /etc/fstab
```

```
UUID=0c390345-ca52-45fd-9097-6e931d651a8f       /       ext4    defaults,x-systemd.growfs       0       1
# LVM
/dev/hdds/datas         /mnt/data               ext4    defaults,nofail,user_xattr,usrjquota=aquota.user,grpjquota=aquota.group,jqfmt=vfsv0,acl 0 2
# NFS exports
/mnt/data/cloud                 /srv/nfs4/cloud         none    bind,nofail     0 0
/mnt/data/media                 /srv/nfs4/media         none    bind,nofail     0 0
/mnt/data/media/downloads       /srv/nfs4/downloads     none    bind,nofail     0 0
/opt/backups                    /srv/nfs4/backups       none    bind,nofail     0 0
```

**PS : Check the content of /var/lib/nfs/etab to show NFS shares list and all the applied permission details**

```bash
cat /var/lib/nfs/etab
```

```shell
/srv/nfs4/backups       192.168.1.0/24(ro,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,ro,secure,root_squash,no_all_squash)
/srv/nfs4/media 192.168.1.0/24(rw,sync,wdelay,hide,nocrossmnt,insecure,no_root_squash,no_all_squash,subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,no_root_squash,no_all_squash)
/srv/nfs4/cloud 192.168.1.0/24(rw,sync,wdelay,hide,nocrossmnt,insecure,no_root_squash,no_all_squash,subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,no_root_squash,no_all_squash)
/srv/nfs4/downloads     192.168.1.0/24(rw,sync,wdelay,hide,nocrossmnt,insecure,no_root_squash,no_all_squash,subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,insecure,no_root_squash,no_all_squash)
/srv/nfs4       192.168.1.0/24(rw,sync,wdelay,hide,crossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,fsid=0,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
```

#### Mount needed <folder> on client(s)

```bash
sudo apt install nfs-common -y

sudo mkdir -p /nfs/<folder>

sudo vim /etc/fstab
```

```shell
krabs:/srv/nfs4/<folder>   /nfs/<folder>  nfs  _netdev,x-systemd.automount,noauto,nofail,noatime,nolock,tcp,bg,actimeo=1800   0      0
```

```bash
sudo mount /nfs/<folder>

sudo systemctl daemon-reload
```

### Monitoring NFS Server

#### Netstat
```bash
watch -n 5 "netstat -plaute | grep nfs | sort -r -n -k 3,2"
```

#### NFSWatch
```bash
sudo apt install -y nfswatch
```

Add these aliases to your .bashrc file:

```bash
# NFS watch clients
alias nfsw='nfswatch -allif -clients -usage 2>/dev/null'
# NFS watch users
alias nfsu='nfswatch -allif -auth -usage 2>/dev/null'
```

```bash
source ~/.bashrc
```
