#!/bin/bash
# scripts: rmdisk.sh
# function
#The CH function is auto umount on the disk partition
CH () {
read -p "Enter your choice,if continue,umount the disk.continue:(y/Y)" CH
if [ "$CH" = "Y" -o "$CH" = "y" ];then
MOUNT=`mount |grep $1 |awk '{print $1}'`
for i in $MOUNT;do
fuser $i
umount $i
done
else
exit
fi
}
#The function FDISK is auto create 3 partition and format.
FDISK () {
dd if=/dev/zero of=$1 bs=512 count=1
partprobe $1
echo '
n
p
1
+100M
n
p
2
+512M
n
p
3
+128M
t
3
82
w ' | fdisk $1 &> /dev/null
[ $? -ne 0 ] && return 68
sync
partprobe $1
sleep 1
mke2fs -j ${1}1 &> /dev/null
PART1=$?
mke2fs -j ${1}2 &> /dev/null
PART2=$?
mkswap ${1}3 &> /dev/null
PART3=$?
[ $PART1 -ne 0 -o $PART2 -ne 0 -o $PART3 -ne 0 ] && return 69
}
#check the partition has benn mounted
read -p "Look at the partition: " DISK
fdisk -l $DISK |grep ^/dev
MOUNT=`fdisk -l $DISK |grep ^/dev |awk '{print $1}'`
for i in $MOUNT;do
mount |grep "$i"
REVTL=$?
if [ $REVTL -eq 0 ];then
echo "The $i is mounted."
else
echo "The $i is not mounted."
fi
done
read -p "umount the $DISK of partition,and create 3 pattitions: continue(y/n)" CHOICE
if [ "$CHOICE" = "Y" -o "$CHOICE" = "y" ];then
CH $DISK
FDISK $DISK
echo "The action is successful!"
else
exit
fi