nfsFormatDisk Best Practices: Safe Formatting for NFS Volumes
Overview
nfsFormatDisk is a utility for preparing disks to be used with NFS (Network File System) storage backends. Proper use reduces data loss risk, ensures compatibility, and improves performance. This guide lists safe, practical steps and recommendations for formatting NFS volumes with nfsFormatDisk.
1. Pre-format checklist
- Backup: Ensure all important data is backed up to an independent location.
- Identify target device: Confirm device path (e.g., /dev/sdb) and verify it is the intended disk.
- Unmount and stop services: Unmount any mounted filesystems on the device and stop services that might access it.
- Check for LVM/RAID: Verify whether the device is part of LVM or RAID arrays and handle accordingly.
- Verify permissions: Run commands as a privileged user (root) or via sudo.
2. Verify device and current state
- List disks: Use lsblk, fdisk -l, or blkid to confirm device layout.
- Zero superblocks (if reusing): If reformatting, clear old filesystem signatures with:
Code
sudo wipefs -a /dev/sdX
- SMART check: For hardware reliability, run:
Code
sudo smartctl -a /dev/sdX
3. Choose filesystem and options
- Recommended filesystems: XFS or ext4 are common for NFS exports; XFS often preferred for large-scale NFS due to performance.
- Mount options for NFS servers: Plan export and client mount options (e.g., noatime, nodiratime) to reduce write overhead.
- Filesystem features: Disable features not needed (e.g., journaling options) only if you understand trade-offs.
4. Formatting commands
- Ext4 example:
Code
sudo mkfs.ext4 -F -m 1 -E lazy_itable_init=0,lazy_journalinit=0 /dev/sdX
- XFS example:
Code
sudo mkfs.xfs -f -m crc=1 /dev/sdX
- Alignment: Ensure partition alignment for SSDs or advanced format disks when creating partitions (use parted with optimal alignment).
5. Mounting and export configuration
- Create mountpoint:
Code
sudo mkdir -p /srv/nfs/vol1
- Mount:
Code
sudo mount /dev/sdX /srv/nfs/vol1 -o noatime,nodiratime
- Persist in /etc/fstab: Use UUIDs for stability:
Code
UUID=xxxx-xxxx/srv/nfs/vol1 xfs defaults,noatime,nodiratime 0 2
- Export via /etc/exports: Configure appropriate client access and options:
Code
/srv/nfs/vol1 10.0.0.0/24(rw,sync,no_subtreecheck)
- Apply exports:
Code
sudo exportfs -ra
6. Permissions and ownership
- Set root squash as needed: Decide whether to enable rootsquash to map remote root to anonymous user.
- Directory ownership: Set uid/gid and ACLs consistent with client expectations:
Code
sudo chown -R nfsuser:nfsgroup /srv/nfs/vol1 sudo chmod -R 2775 /srv/nfs/vol1
7. Testing and validation
- Mount from client: Test mounting with representative client configuration:
Code
sudo mount -t nfs server:/srv/nfs/vol1 /mnt/test
- Read/write test: Use dd, fio, or rsync for functional and performance testing:
Code
dd if=/dev/zero of=/mnt/test/testfile bs=1M count=100 oflag=sync
- Check exports:
Code
sudo exportfs -v
8. Recovery and safety practices
- Snapshot or backup schedule: Implement regular backups or snapshots before major changes.
- Rollback plan: Keep a documented rollback procedure and spare capacity.
- Monitoring: Monitor disk health, space, and NFS server metrics (iostat, nfsstat, smartctl).
- Permissions audits: Regularly audit exports and filesystem permissions.
9. Automation and scripting tips
- Idempotent scripts: Make scripts safe to re-run (check device existence, confirm mounts).
- Logging and dry-run: Include verbose logging and a dry-run mode.
- Use UUIDs and labels: Refer to volumes by UUID or label in scripts and fstab.
10. Common pitfalls
- Exporting a mounted filesystem without correct permissions or wrong path.
- Forgetting to clear old signatures leading to mount failures.
- Using the wrong filesystem or mount options that degrade NFS client compatibility.
- Relying on device names (/dev/sdX) instead of UUIDs.
Quick checklist (summary)
- Backup data
- Confirm target device and stop services
- wipefs and smartctl checks if reusing disk
- Choose XFS/ext4 and run mkfs with safe options
- Mount with noatime/nodiratime and export correctly
- Test from a client and run read/write checks
- Automate carefully and monitor continuously
If you want, I can generate a ready-to-run, idempotent bash script that performs these steps for a specified device and export path.