Linux Shell Script自动挂载数据盘
**1.添加一块数据盘进行验证脚本/dev/sde**
**开机添加数据盘未识别也可以通过启动服务器来识别硬盘**
```bash
开机添加数据盘未识别解决方法
1.优先执行此命令进行扫盘操作,不行就执行方案2
[root@localhost ~]# rescan-scsi-bus.sh
2.通过find查找scan所在位置
[root@localhost ~]# find / -name scan
/sys/devices/pci0000:00/0000:00:10.0/host2/scsi_host/host2/scan
/sys/devices/pci0000:00/0000:00:07.1/ata1/host0/scsi_host/host0/scan
/sys/devices/pci0000:00/0000:00:07.1/ata2/host1/scsi_host/host1/scan
/sys/module/scsi_mod/parameters/scan
/usr/share/libquvi-scripts/0.9.20131130/scan
通过scan命令扫盘
[root@localhost ~]# echo '- - -' > /sys/devices/pci0000:00/0000:00:10.0/host2/scsi_host/host2/scan
[root@localhost ~]# echo '- - -' > /sys/devices/pci0000:00/0000:00:07.1/ata1/host0/scsi_host/host0/scan
[root@localhost ~]# echo '- - -' > /sys/devices/pci0000:00/0000:00:07.1/ata2/host1/scsi_host/host1/scan
发现已经识别sdd数据盘
[root@localhost ~]# lsblk /dev/sde
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sde 8:48 0 10G 0 disk
```
**2.脚本内容如下**
```bash
[root@localhost ~]# cat gz.sh
#!/bin/bash
# 执行脚本需要输入两个位置变量$1,$2
# $0意思回显当前的脚本名
# $#已加载的位置变量的个数
# -b判断文件为块特殊文件
# -d判断文件为目录
# n变量去获取fstab里面是否存在挂载目录0表示无
# uuid1去过滤出UUID
# 格式化成其他格式只需将ext4改成ext3或xfs
echo "Useage $0 盘符 挂载点, 如: $0 /dev/xvdb /data"
if [ $# -ne 2 ];then
exit
fi
if [ ! -b $1 ];then
echo "你提供的盘符不正确,请检查后再操作"
exit 1
fi
mkfs -t ext4 $1
if [ ! -d $2 ];then
mkdir -p $2
fi
n=`egrep " $2 " /etc/fstab|wc -l`
uuid1=`blkid $1 |tr -s " " | awk -F":" '{print $2}' | awk -F" " '{print $1}' `
if [ $n -eq 0 ];then
echo "$uuid1 $2 ext4 defaults 0 0" >> /etc/fstab
mount -a
else
mount $1 $2
echo "配置文件/etc/fstab中已经存在挂载点$2,请检查一下."
fi
```
**3.执行脚本成功**
```bash
[root@localhost ~]# sh gz.sh /dev/sde /u03
Useage gz.sh 盘符 挂载点, 如: gz.sh /dev/xvdb /data
mke2fs 1.45.6 (20-Mar-2020)
创建含有 2621440 个块(每块 4k)和 655360 个inode的文件系统
文件系统UUID:5639bea7-b938-4f78-ab8c-9949958119bd
超级块的备份存储于下列块:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
正在分配组表: 完成
正在写入inode表: 完成
创建日志(16384 个块)完成
写入超级块和文件系统账户统计信息: 已完成
[root@localhost ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Sun Jan 31 21:48:18 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/cs-root / xfs defaults 0 0
UUID=53a56729-2d9e-451f-b794-450c1f0868db /boot xfs defaults 0 0
/dev/mapper/cs-swap none swap defaults 0 0
UUID="5639bea7-b938-4f78-ab8c-9949958119bd" /u03 ext4 defaults 0 0
```