主要分爲三步
1. 刪除原有分區
2. 使用fdisk進行分區
3. 創建文件系統
#!/bin/bash
#
disks=$(fdisk -l 2>/dev/null |grep '^Disk /dev/[sh]d[a-z]' |awk -F: '{print $1}' |cut -d' ' -f2 |awk -F '/' '{print $3}' |tr '\n' ' ')
repartition(){
echo "Disk: $disks"
read -p "Input which disk you want re-partition: " disk
echo -e "Step 1: delete all partion of $disk "
read -p "[y/n]" x
if [ $x = 'y' ]; then
dd if=/dev/zero of=/dev/$disk bs=512 count=1
echo "partition of $disk delete complete."
else
exit 0
fi
echo "Step 2: re-partition $disk"
echo 'n
p
1
+20M
n
p
2
+512M
n
p
3
+128M
t
3
82
w' | fdisk /dev/$disk &>/dev/null
sleep 3
sync
# create filesystem
echo "Step 3: create filesystem"
mkfs.ext4 /dev/${disk}1 &>/dev/null
mkfs.ext4 /dev/${disk}2 &>/dev/null
mkswap /dev/${disk}3 &>/dev/null
sleep 3
}
while true; do
read -p "Input your choice: [q]-->quit, [n]-->re-partition: " choice
if [ $choice = 'q' ]; then
exit 0
elif [ $choice = 'n' ]; then
read -p "Warning: the operation may destroy your data
Are you sure continue [y/n]" confirm
if [ $confirm = 'y' ]; then
repartition
else
exit 0
fi
else
echo "Your input is invalid, plz input again!"
fi
done