#!/bin/bash #说明:本脚本只适用于数据盘未做分区,执行一次脚本只挂载一块磁盘,多块磁盘需要多次执行
#查看fstab文件是否存在注释,存在注释就删除注释,不存在注释继续执行下一步 if test -z "$(cat /etc/fstab |grep data |grep ?\#)"; then ? ? echo "No annotations exist" else ? ? ANNOTUUID=`cat /etc/fstab |grep data |grep \# |awk '{print $1}'` ?? ?NOANNOTUUID=`cat /etc/fstab |grep data |grep \# |awk '{print $1}' |awk -F# '{print $2}'` ?? ?sed -i "s/$ANNOTUUID/$NOANNOTUUID/g" ?/etc/fstab?? ? fi
#输出将要进行格式化提示并显示所有磁盘 echo "Initial and mount a disk..." echo "all disk display" fdisk -l 2> /dev/null | grep -o "^Disk /dev/[sh]d[a-z]" |sort
#输出系统盘,磁盘总数和数据盘总数 SYSDISK=`df -h |grep boot |awk '{print $1}'`? echo ${SYSDISK%?} |awk -F/ '{print $3}' echo "is the SystemDisk" DISKNUM=`fdisk -l 2> /dev/null | grep -o "^Disk /dev/[sh]d[a-z]" |wc -l` ? echo "total DiskNumber is $DISKNUM"? DATANUM=`expr $DISKNUM - 1` echo DataDisk number is $DATANUM
#交互输入:如果输入quit或者q就退出程序 read -p "Your choice: "??DISK if [ $DISK == 'quit' -o $DISK == 'q' ]; then ??echo "quit" ??exit 7 fi
#判断原数据盘文件系统类型,如果是ext4文件系统类型就执行格式化并挂载?? ? if test -z "$(cat /etc/fstab |grep data |grep ext4)"; then ? ? echo "data disk not ext4 type" else ? ? echo "y"| mkfs.ext4 /dev/$DISK? ?? ?tune2fs -i 1 -c 1 /dev/$DISK ?? ?NEWUUID=`blkid /dev/$DISK|awk -F: '{print $2}'|awk -F\" '{print $2}'` ?? ? ?? ?blkid |sort |awk -F: '{print $2}'|awk -F\" '{print $2}' >blkid.txt ? ? cat /etc/fstab |grep -v \# |awk '{print $1}' |awk -F= '{print $2}' |grep -v '^$' >fstab.txt ? ? while read line ? ? do ? ? ? ? txt=`cat blkid.txt | grep -w $line` ? ? ? ? if [ $? -eq 0 ] ? ? ? ? then ? ? ? ? ? ? ? ? echo err &>/dev/null ? ? ? ? else ? ? ? ? ? ? ? ? echo $line > OLDUUID.txt ?? ??? ??? ??? ?break ? ? ? ? fi ? ? ? ? done <fstab.txt ?? ??? ? ? ? OLDUUID=`cat OLDUUID.txt` ? ? sed -i "s/$OLDUUID/$NEWUUID/g" ?/etc/fstab ? ? ? if [ "cat /etc/redhat-release |grep -o 7" == "7" ]; ?then ?? ? ? ?systemctl daemon-reload?? ? ? ??? ? ? ? ? ? fi ? ? mount -a?? ? ? fi
#判断原数据盘文件系统类型,如果是xfs文件系统类型就执行格式化并挂载?? ? if test -z "$(cat /etc/fstab |grep data |grep xfs)"; then ? ? echo "data disk not xfs type" else ? ? mkfs.xfs /dev/$DISK ?? ? ?? ?NEWUUID=`blkid /dev/$DISK|awk -F: '{print $2}'|awk -F\" '{print $2}'` ?? ? ?? ?blkid |sort |awk -F: '{print $2}'|awk -F\" '{print $2}' >blkid.txt ? ? cat /etc/fstab |grep -v \# |awk '{print $1}' |awk -F= '{print $2}' |grep -v '^$' >fstab.txt ? ? while read line ? ? do ? ? ? ? txt=`cat blkid.txt | grep -w $line` ? ? ? ? if [ $? -eq 0 ] ? ? ? ? then ? ? ? ? ? ? ? ? echo err &>/dev/null ? ? ? ? else ? ? ? ? ? ? ? ? echo $line > OLDUUID.txt ?? ??? ??? ??? ?break ? ? ? ? fi ? ? ? ? done <fstab.txt ?? ??? ? ? ? OLDUUID=`cat OLDUUID.txt` ? ? sed -i "s/$OLDUUID/$NEWUUID/g" ?/etc/fstab ? ? ? if [ "cat /etc/redhat-release |grep -o 7" == "7" ]; ?then ?? ? ? ?systemctl daemon-reload?? ? ? ??? ? ? ? ? ? fi ? ? mount -a?? ? ? fi
#删除脚本执行过程临时文件并显示挂载情况 rm -r blkid.txt fstab.txt OLDUUID.txt df -h |grep data |sort
|