linux-mount-disk
====================磁盘挂载===========================
#!/bin/bash ### BEGIN INIT INFO # Provides: mount.sh # Required-start: $remote_fs $syslog # Required-Stop: $remote_fs $syslog # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: starts mount # Description: starts mount ### END INIT INFO # 开机挂载硬盘 # 扫描可用的磁盘设备
diskcount=$(lsblk -l -o Name |grep -E "sd[a-z]*[a-z]$"|wc -l )
currenttime=$(date "+%Y-%m-%d %H:%M:%S")
#成功挂盘数量
sum=0
for device in $(lsblk -r -o NAME | grep -E "sd[a-zA-Z0-9/]+[0-9]$"); do # 检查设备是否已经挂载 if ! grep -qs "/dev/$device" /proc/mounts; then # 确保挂载点存在,如果不存在就创建 mount_point="/mnt/$device" mkdir -p "$mount_point" # 尝试挂载设备到挂载点 mount "/dev/$device" "$mount_point" # 检查挂载是否成功 if [ $? -eq 0 ]; then echo "成功挂载设备 /dev/$device 到 $mount_point" sum=$((sum+1)) else echo "无法挂载设备 /dev/$device 到 $mount_point,删除挂载点" rmdir "$mount_point" # 删除挂载失败的目录 fi else echo "设备 /dev/$device 已经挂载" fi done
currenttime=$(date "+%Y-%m-%d %H:%M:%S")
echo "成功挂载 $sum --$currenttime" | sudo tee -a /home/tuoluo/mount-info.log
# 所有挂载完成后输出退出码 0(成功) exit 0