用parted工具实现磁盘分区的一个小脚本

       由于fdisk工具只能划分小于2T的磁盘,而在服务器环境下动不动就是2T或者3T以上的磁盘,因此考虑用GNU的另一个分区工具parted来对磁盘进行分区。但是,当一个大磁盘要分区的区很多时,一步步操作还是太费时间了。如果每个分区大小都是相同的话,还是利用parted的-s、--script参数来写一个不需要用户干预的脚本为妙。

[root@localhost ~]# /sbin/fdisk -l /dev/sdi

WARNING: GPT (GUID Partition Table) detected on '/dev/sdi'! The util fdisk doesn't support GPT. Use GNU Parted.

Disk /dev/sdi: 3000.6 GB, 3000592982016 bytes
255 heads, 63 sectors/track, 364801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

【脚本实例】

[root@localhost ~]# vim repaire_disk.sh 
  1 #! /bin/bash
  2 
  3 DiskPath=$1
  4 PartSize=204800         #MB
  5 PartSizeG=$((PartSize/1024))
  6 
  7 PartNumCur=$((`parted -s ${DiskPath} print | grep -v ^$ | tail -n +6 | wc -l`))
  8 
  9 for i in `seq 1 ${PartNumCur}`
 10 do
 11         parted -s ${DiskPath} rm ${i}
 12 done
 13 
 14 PartNum=$((`fdisk -l $DiskPath | grep ^Disk | grep dev | cut -d" " -f3 | cut -d"." -f1`/${PartSizeG}+1))
 15 
 16 for i in `seq 1 ${PartNum}`
 17 do
 18         if [ $i -ne $PartNum ] && [ $i -lt $(( $PartNum-1 )) ]; then
 19                 parted -s ${DiskPath} mkpart primary $(( (${i}-1)*${PartSize} )) $(( ${i}*${PartSize} ))
 20         else
 21                 parted -s ${DiskPath} mkpart primary $(( (${i}-1)*${PartSize} )) -1
 22         fi
 23 done

【运行结果】

[root@localhost ~]# /sbin/parted /dev/sdi
GNU Parted 2.4
Using /dev/sdi
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
p
Model: ATA WDC WD3000FYYZ-0 (scsi)
Disk /dev/sdi: 3001GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size   File system  Name     Flags
 1      17.4kB  205GB   205GB               primary
 2      205GB   410GB   205GB               primary
 3      410GB   614GB   205GB               primary
 4      614GB   819GB   205GB               primary
 5      819GB   1024GB  205GB               primary
 6      1024GB  1229GB  205GB               primary
 7      1229GB  1434GB  205GB               primary
 8      1434GB  1638GB  205GB               primary
 9      1638GB  1843GB  205GB               primary
10      1843GB  2048GB  205GB               primary
11      2048GB  2253GB  205GB               primary
12      2253GB  2458GB  205GB               primary
13      2458GB  2662GB  205GB               primary
14      2662GB  2867GB  205GB               primary
15      2867GB  3001GB  133GB               primary

你可能感兴趣的:(系统运维)