10-5 腳本編程之八 腳本完成磁盤分區格式化

一個腳本(前提:請爲虛擬機新增一塊硬盤,假設它爲/dev/sdb),爲指定的硬盤建立分區bash

一、列出當前系統上全部磁盤,讓用戶選擇,若是選擇quit則退出腳本,若是用戶選擇錯誤,讓用戶從新選擇ide

二、當用戶選擇後,提醒用戶確認接下來的操做可能會損壞數據,並請用戶確認,若是用戶選擇y就繼續,n就退出。不然就讓用戶從新選擇ui

三、刪除那塊硬盤上的全部分區(提示,抹除全部分區後執行sync命令,並讓腳本睡眠3秒鐘後再分區);併爲其建立3個主分區
spa

   且第三個是swap分區類型。(分區命令經過echo傳送給fdisk便可實現)內存


#!/bin/bash同步

echo "Initial a disk.."虛擬機

echo -e "\033[31mWarning: \033[0m"it

fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"io


#!/bin/bashclass

echo "Initial a disk.."

echo -e "\033[31mWarning: \033[0m"

fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"


read -p "Your choice: " PARTDISK

if [ $PARTDISK == 'quit' ]; then

  echo "quit"

  exit 7

fi


until fdisk -l |grep -o "^Disk /dev/[sh]d[a-z]" | grep "^Disk $PARTDISK$" &> /dev/null;do

   read -p "Wrong option,Your choice aging:" PARTDISK

done

echo $PARTDISK


read -p "Will destroy all date,continue: " CHOICE

until [ $CHOICE == 'y' -o $CHOICE == 'n' ];do

    read -p "Wrong option,Your choice aging:" CHOICE

done


if [ $CHOICE == 'n' ];then

    exit 9

echo -e "\033[31mWarning: \033[0m"

fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"


read -p "Your choice: " PARTDISK

if [ $PARTDISK == 'quit' ]; then

  echo "quit"

  exit 7

fi


until fdisk -l |grep -o "^Disk /dev/[sh]d[a-z]" | grep "^Disk $PARTDISK$" &> /dev/null;do

echo -e "\033[31mWarning: \033[0m"

fdisk -l | grep -o "^Disk /dev/[sh]d[a-z]"


read -p "Your choice: " PARTDISK

if [ $PARTDISK == 'quit' ]; then

  echo "quit"

  exit 7

fi


until fdisk -l |grep -o "^Disk /dev/[sh]d[a-z]" | grep "^Disk $PARTDISK$" &> /dev/null;do

   read -p "Wrong option,Your choice aging:" PARTDISK

done

echo $PARTDISK


read -p "Will destroy all date,continue: " CHOICE

until [ $CHOICE == 'y' -o $CHOICE == 'n' ];do

    read -p "Wrong option,Your choice aging:" CHOICE

done


if [ $CHOICE == 'n' ];then

    exit 9

else

dd if=/dev/zero of=$PARTDISK  bs=512 count=1 &> /dev/null

sync

echo '

n

p

1


+20M

n

p

2


+512M

n

p

3


+100M

t

3

82

w' | fdisk $PARTDISK &> /dev/null

partprobe $PARTDISK

sync

sleep 2

mke2fs -j ${PARTDISK}1 &> /dev/null

mke2fs -j ${PARTDISK}2 &> /dev/null

mke2fs -j ${PARTDISK}3 &> /dev/null

fi





 刪除一個磁盤的全部分區,只須要覆蓋它的MBR就能夠了。

dd if=/dev/zero of=/dev/sdb bs=/dev/sdb bs=512 count=1

而後同步到磁盤sync 注:任何操做都是如今內存中完成而後才同步到磁盤上,咱們能夠手動同步到磁盤。

爲了給同步的時間,須要sleep睡3秒。

相關文章
相關標籤/搜索