手记

fdisk磁盘分区脚本[仅供参考]

一、在虚拟机中添加两块IDE磁盘,大小分别为8G、10G。
二、用fdisk -l查看当前磁盘分区情况。
三、用df-T查看当前磁盘的挂载情况。
四、在/home/test下建立shell脚本fdisk.sh文件【另附】
五、改变fdisk.sh脚本的权限,运行程序对sdb磁盘进行分区,查看分区状况。
chmod 777 fdisk.sh
六、在/home下建立kenan和xinyi两个目录,分别对/dev/sdb1和/dev/sdb2进行挂载。
mount /dev/sdb1 /home/kenan
mount /dev/sdb2 /home/xinyi
df -T
七、运行fdisk.sh再次对磁盘进行分区。

!/bin/bash

echo "The system of creating for district!"
fdisk -l 2>/dev/null | grep -o "Disk /dev/[sh]d[a-z]"
read -p "Enter yours choice:" PARTDISK
if [ $PARTDISK == 'quit' ];then
echo "exit!"
exit 7
fi

until fdisk -l 2>/dev/null | grep -o "Disk /dev/[sh]d[a-z]" | grep "Disk $PARTDISK" &> /dev/null;do
read -p "wrong option!Enter the choice again:" PARTDISK
done

read -p "Will destory the data,continue?(y/n):" CHOICE
until [ $CHOICE == 'y' -o $CHOICE == 'n' ];do
read -p "Will destory the data,continue?(y/n):" CHOICE
done

if [ $CHOICE == 'n' ];then
echo "exit!"
exit 9
else
echo "Creating!"

for i in df -T | grep "$PARTDISK" | awk '{ print $1 }';do
fuser -km $i
umount $i
echo "$i umount"

sleep 2

done

dd if=/dev/zero of=$PARTDISK bs=512 count=1
sync
echo 'n
p
1

+20M
n
p
2

+512M
n
p
3

+128M
t
3
82
w' | fdisk $PARTDISK &> /dev/null
partprobe $PARTDISK
sync
mkfs.ext4 ${PARTDISK}1 &> /dev/null
mkfs.ext4 ${PARTDISK}2 &> /dev/null
mkswap ${PARTDISK}3
sync
fi

2人推荐
随时随地看视频
慕课网APP