shell自動實現磁碟分割槽掛載
利用shell指令碼實現對一個磁碟進行分割槽格式化掛載
#!/bin/bash
#echo "A disk is to init"
fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]" //篩選出來現在Linux中的所有的磁碟其中包括IDE和SATA硬碟
read -p "Your choice: " choice //選在要進行操作的硬碟名稱
until [ $choice != 'q' -a $choice != 'Q' -a $choice != 'quit' -a $choice != 'Quit' ]&& fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"|grep $choice;do //判斷選擇如果不是退出指令的話就繼續
if [ $choice == 'q' -o $choice == 'Q' -o $choice == 'quit' -o $choice == 'Quit' ];then echo "The shell is quiting"
exit 5
else
echo "Wrong options"
read -p "Your choice: " choice
fi
done
echo "Disk $choice is to init,this will destroy all data.Are you sure"
read -p "Continue? Y|n or N|n: " CHOICE
if [ $CHOICE == 'N' -o $CHOICE == 'n' ];then //在進行操作之前等待客戶進行確認,如果輸入的是Y|y或者是N|n的話那麼進行相應的操作
echo "You chose no"
echo "The shell is quiting"
exit 5
elif [ $CHOICE == 'Y' -o $CHOICE == 'y' ];then
echo "You chose yes,init will begin"
echo "initing....please wait"
dd if=/dev/zero of=$choice bs=512 count=1 &>/dev/null //將硬碟原來的分割槽全部刪除掉,並且進行分割槽的重建
echo "n
p
1
+5G
n
p
2
+5G
w" | fdisk $choice &>/dev/null
else
echo "Wrong choice Y|y or N|n"
fi
partprobe $choice
sync
sleep 3
mkfs -t ext4 ${choice}1 &>/dev/null //格式化硬碟並掛載
mkfs -t ext4 ${choice}2 &>/dev/null
mkdir /data1 &>/dev/null
mkdir /data2 &>/dev/null
mount ${choice}1 /data1
mount ${choice}2 /data2
echo "Init finish"