iStoreOS 开机自动执行命令配置 ⚡

🚀 轻松实现系统启动时自动挂载NFS共享,关机时自动卸载
📖 目录导航
🎯 功能特点
本指南将帮助您在iStoreOS(基于OpenWrt)中配置开机自动挂载NFS共享的功能:
- ✅ 开机自动挂载: 系统启动时自动挂载远程NFS共享
- ✅ 关机自动卸载: 系统关闭时安全卸载NFS共享,避免数据损坏
- ✅ 持久化配置: 重启后配置依然有效,无需手动干预
- ✅ 状态检测: 自动检测NFS服务器可用性,智能处理连接问题
- ✅ 日志记录: 完整的执行日志,方便排查问题
- ✅ 灵活配置: 支持多个NFS共享和自定义挂载选项
📁 创建启动脚本
1. 创建启动脚本文件
使用以下命令创建并设置启动脚本:
1 2 3 4 5 6 7 8
| touch /etc/init.d/mynfsmount
chmod +x /etc/init.d/mynfsmount
ls -la /etc/init.d/mynfsmount
|
2. 查看现有启动脚本
1 2 3 4 5
| ls -la /etc/init.d/
ls -la /etc/init.d/mynfsmount
|
⚙️ 脚本配置详解
编辑启动脚本内容
1 2
| vi /etc/init.d/mynfsmount
|
脚本内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #!/bin/sh /etc/rc.common
START=99 STOP=15
USE_PROCD=1
start_service() { echo "🔄 开始挂载NFS共享..." mkdir -p /mnt/nfs if ping -c 1 -W 2 10.10.10.251 >/dev/null 2>&1; then echo "✅ NFS服务器可达,开始挂载..." mount -t nfs -o vers=3,nolock 10.10.10.251:/mnt/sata1-1 /mnt/nfs if mountpoint -q /mnt/nfs; then echo "✅ NFS挂载成功: /mnt/nfs" echo "📁 共享内容预览:" ls -la /mnt/nfs/ | head -5 else echo "❌ NFS挂载失败,请检查权限和网络连接" logger -t mynfsmount "NFS mount failed" fi else echo "❌ NFS服务器不可达: 10.10.10.251" logger -t mynfsmount "NFS server unreachable" fi }
stop_service() { echo "🔄 开始卸载NFS共享..." if mountpoint -q /mnt/nfs; then if ! umount /mnt/nfs 2>/dev/null; then echo "⚠️ 正常卸载失败,尝试强制卸载..." umount -f /mnt/nfs fi echo "✅ NFS卸载完成" logger -t mynfsmount "NFS unmounted successfully" else echo "ℹ️ NFS未挂载,无需卸载" fi }
restart() { stop_service sleep 2 start_service }
|
🔧 脚本参数说明
参数 |
说明 |
建议值 |
START=99 |
启动优先级 |
99(最后启动) |
STOP=15 |
停止优先级 |
15(较早停止) |
USE_PROCD=1 |
使用procd进程管理 |
1(启用) |
mount -t nfs |
NFS挂载命令 |
根据实际情况修改 |
-o vers=3,nolock |
NFS版本和选项 |
推荐指定版本 |
🔧 启用自启动服务
启用开机自启动
1 2 3 4 5 6 7 8 9
| /etc/init.d/mynfsmount enable
ls -la /etc/rc.d/ | grep mynfsmount
|
服务管理命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| /etc/init.d/mynfsmount start
/etc/init.d/mynfsmount stop
/etc/init.d/mynfsmount restart
/etc/init.d/mynfsmount status
/etc/init.d/mynfsmount disable
|
🧪 测试与验证
立即测试脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| /etc/init.d/mynfsmount stop
/etc/init.d/mynfsmount start
df -h | grep nfs mount | grep nfs
ls -la /mnt/nfs/
logread | tail -20
|
验证自启动功能
1 2 3 4 5 6 7 8 9 10 11 12 13
| reboot
df -h | grep nfs cat /proc/mounts | grep nfs
logread | grep mynfsmount logread | grep nfs
/etc/init.d/mynfsmount status
|
系统日志监控
1 2 3 4 5 6 7 8 9 10 11
| logread -f
logread | grep -i nfs
logread | grep mynfsmount
dmesg | grep nfs
|
🐛 故障排除
常见问题解决
❌ NFS服务器不可达
1 2 3 4 5 6 7 8
| ping -c 3 10.10.10.251
nc -zv 10.10.10.251 2049
iptables -L -n | grep 2049
|
❌ 挂载权限错误
1 2 3 4 5 6 7 8
| showmount -e 10.10.10.251
mount -t nfs -o vers=3,ro 10.10.10.251:/mnt/sata1-1 /mnt/nfs
cat /proc/filesystems | grep nfs
|
❌ 挂载点忙无法卸载
1 2 3 4 5 6
| lsof /mnt/nfs fuser -m /mnt/nfs
umount -l /mnt/nfs
|
调试脚本
1 2 3 4 5 6 7 8
| sh -x /etc/init.d/mynfsmount start
sh -n /etc/init.d/mynfsmount
logread | grep "mynfsmount"
|
NFS连接测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| showmount -e 10.10.10.251
mkdir -p /tmp/test_nfs mount -t nfs -o vers=3 10.10.10.251:/mnt/sata1-1 /tmp/test_nfs
touch /tmp/test_nfs/test_file echo "test" > /tmp/test_nfs/test_file cat /tmp/test_nfs/test_file
umount /tmp/test_nfs rmdir /tmp/test_nfs
|
💡 高级技巧
多个NFS共享挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| start_service() { mkdir -p /mnt/nfs/{data,backup,media} mount -t nfs -o vers=3 10.10.10.251:/data /mnt/nfs/data mount -t nfs -o vers=3 10.10.10.251:/backup /mnt/nfs/backup mount -t nfs -o vers=3 10.10.10.252:/media /mnt/nfs/media }
stop_service() { umount -f /mnt/nfs/data 2>/dev/null umount -f /mnt/nfs/backup 2>/dev/null umount -f /mnt/nfs/media 2>/dev/null }
|
优化挂载选项
1 2 3 4 5 6 7 8 9 10 11
| mount -t nfs -o \ vers=3,\ rw,\ hard,\ timeo=600,\ retrans=2,\ nolock,\ noacl,\ async \ 10.10.10.251:/mnt/sata1-1 /mnt/nfs
|
常用挂载选项说明:
vers=3
: 指定NFSv3协议(兼容性更好)
rw
/ro
: 读写/只读模式
hard
/soft
: 硬挂载/软挂载(硬挂载更可靠)
timeo=n
: 超时时间(十分之一秒)
retrans=n
: 重试次数
nolock
: 禁用文件锁(提高性能)
noacl
: 禁用ACL(访问控制列表)
async
: 异步写入(性能更好但风险略高)
开机延迟挂载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| start_service() { while ! ping -c 1 -W 1 8.8.8.8 >/dev/null 2>&1; do echo "⏳ 等待网络连接..." sleep 2 done local retry_count=0 while ! ping -c 1 -W 1 10.10.10.251 >/dev/null 2>&1; do retry_count=$((retry_count+1)) if [ $retry_count -ge 10 ]; then echo "❌ NFS服务器连接超时" return 1 fi echo "⏳ 等待NFS服务器响应... ($retry_count/10)" sleep 3 done mount -t nfs 10.10.10.251:/mnt/sata1-1 /mnt/nfs }
|
状态检查与监控
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| cat > /usr/bin/check_nfs_status << 'EOF'
if mountpoint -q /mnt/nfs; then echo "✅ NFS挂载正常" if touch /mnt/nfs/.test_write 2>/dev/null; then rm -f /mnt/nfs/.test_write echo "✅ 读写权限正常" else echo "⚠️ 写权限受限" fi else echo "❌ NFS未挂载" /etc/init.d/mynfsmount start fi EOF
chmod +x /usr/bin/check_nfs_status
echo "0 * * * * /usr/bin/check_nfs_status" >> /etc/crontabs/root /etc/init.d/cron restart
|
备份与恢复配置
1 2 3 4 5 6 7 8 9 10 11
| BACKUP_DIR="/root/nfs_mount_backup" mkdir -p $BACKUP_DIR cp /etc/init.d/mynfsmount $BACKUP_DIR/ cp /etc/rc.d/S*mynfsmount $BACKUP_DIR/ 2>/dev/null || true ls -la $BACKUP_DIR/
cp $BACKUP_DIR/mynfsmount /etc/init.d/ chmod +x /etc/init.d/mynfsmount /etc/init.d/mynfsmount enable
|
💡 专业提示:
- 建议在生产环境使用前充分测试脚本功能
- 使用
mount -t nfs -o vers=3
指定NFS版本提高兼容性
- 定期检查系统日志确保挂载正常运作
- 考虑添加监控脚本定期检查NFS连接状态
- 重要数据建议使用硬挂载(hard)模式确保数据完整性
通过本指南,您应该能够在iStoreOS中成功配置NFS共享的自动挂载功能。如有问题,请参考故障排除部分或查看系统日志获取详细信息。🎉