Linux command: Automatic format of hard disk

Source: Internet
Author: User

HDD Auto Format/dev/sdb:

1, list all the disks on the current system, let the user choose, if you choose Quit to quit the script, if the user chooses the error, let the user choose again;

2, when the user selects, remind the user to confirm that the next operation may damage the data, and ask the user to confirm; if the user chooses Y to continue, n exits; otherwise, let the user re-select;

3. Erase all partitions on the hard drive (prompt, erase all partitions after the Sync command, and let the script sleep for 3 seconds before partitioning), and create three primary partitions for it, the first one is 20M, the second is 512M, the third is 128M, and the third is the swap partition type; Hint: The partition command is delivered to Fdisk via Echo)

#!/bin/bash
#
echo "Initial a disk ..."
Echo-e "\033[31mwarning:\033[0m"
Fdisk-l 2>/dev/null | Grep-o "^disk/dev/[sh]d[a-z]"

Read-p "Your choice:" Partdisk

if [$PARTDISK = = ' quit '-o $PARTDISK = = ' Q ']; Then
echo "Quit"
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, Your choice again:" Partdisk
Done

Read-p "would destroy all Data,continue:" CHOICE
until [$CHOICE = = ' y '-o $CHOICE = = ' n '];d o
Read-p "would destroy all Data,continue:" CHOICE
Done

if [$CHOICE = = ' n ']; Then
echo "Cancel partition, quiting ..."
Exit 9
Else
# # Auto Umount
For I in ' Mount | grep "/dev/sdb" | awk ' {print '} '; Do
fuser-km $I
Umount $I
echo "$I umount OK."
Done

# # Delete Disk partition
DD If=/dev/zero of= $PARTDISK bs=512 count=1 &>/tmp/log.txt
echo "Deleted partition, formating new partition ..."
Sync
Sleep 3
# # Format Disk
Echo ' n
P
1

+20m
N
P
2

+512m
N
P
3

+128m
T
3
82
W ' | Fdisk $PARTDISK >>/tmp/log.txt
Partprobe $PARTDISK
Sync
Sleep 3

# # Format Partition
Mke2fs-j ${partdisk}1 >>/tmp/log.txt |echo "format ${partdisk}1 finished."
Mke2fs-j ${partdisk}2 >>/tmp/log.txt |echo "format ${partdisk}2 finished."
Mkswap ${partdisk}3 >>/tmp/log.txt |echo "format swap ${partdisk}3 finished."
Fi


---end---

Linux command: Automatic format of hard disk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.