磁盘管理:


I/O Ports: I/O设备地址

一切皆文件:

open(), read(), write(), close()

设备类型:

块设备:block,存取单位“块”,磁盘

字符设备:char,存取单位“字符”,键盘

设备文件:关联至一个设备驱动程序,进而能够跟与之对应硬件设备进行通信

设备号码:

主设备号:major number, 标识设备类型

次设备号:minor number, 标识同一类型下的不同设备


磁盘设备的设备文件命名:/dev/DEV_FILE

IDE: /dev/hd

SCSI, SATA, SAS, USB: /dev/sd

不同设备:a-z

/dev/sda, /dev/sdb, ...

同一设备上的不同分区:1,2, ...

/dev/sda1, /dev/sda5


硬盘存储术语:


head:磁头

track:磁道

cylinder: 柱面

secotr: 扇区,512bytes


分区:

两种分区方式:MBR,GPT

MBR: Master Boot Record,1982年,使用32位表示扇区数,分区不超过2T

如何分区:按柱面

0磁道0扇区:512bytes

446bytes: boot loader

64bytes:分区表

16bytes: 标识一个分区

2bytes: 55AA

4个主分区;3主分区+1扩展(N个逻辑分区)


磁盘管理之磁盘分区,主引导分区表修复_第1张图片


分区表备份及修复

[root@localhost ~]# hexdump -C -n 512 /dev/sda       #查看主分区表的前512字节

00000000  eb 48 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.H..............|

00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|

00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|

00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 03 02  |.........|...t..|

00000040  80 00 00 80 fc 82 00 00  00 08 fa 90 90 f6 c2 80  |................|

00000050  75 02 b2 80 ea 59 7c 00  00 31 c0 8e d8 8e d0 bc  |u....Y|..1......|

00000060  00 20 fb a0 40 7c 3c ff  74 02 88 c2 52 f6 c2 80  |. ..@|<.t...R...|

00000070  74 54 b4 41 bb aa 55 cd  13 5a 52 72 49 81 fb 55  |tT.A..U..ZRrI..U|

00000080  aa 75 43 a0 41 7c 84 c0  75 05 83 e1 01 74 37 66  |.uC.A|..u....t7f|

00000090  8b 4c 10 be 05 7c c6 44  ff 01 66 8b 1e 44 7c c7  |.L...|.D..f..D|.|

000000a0  04 10 00 c7 44 02 01 00  66 89 5c 08 c7 44 06 00  |....D...f.\..D..|

000000b0  70 66 31 c0 89 44 04 66  89 44 0c b4 42 cd 13 72  |pf1..D.f.D..B..r|

000000c0  05 bb 00 70 eb 7d b4 08  cd 13 73 0a f6 c2 80 0f  |...p.}....s.....|

000000d0  84 f0 00 e9 8d 00 be 05  7c c6 44 ff 00 66 31 c0  |........|.D..f1.|

000000e0  88 f0 40 66 89 44 04 31  d2 88 ca c1 e2 02 88 e8  |[email protected]........|

000000f0  88 f4 40 89 44 08 31 c0  88 d0 c0 e8 02 66 89 04  |[email protected]..|

00000100  66 a1 44 7c 66 31 d2 66  f7 34 88 54 0a 66 31 d2  |f.D|f1.f.4.T.f1.|

00000110  66 f7 74 04 88 54 0b 89  44 0c 3b 44 08 7d 3c 8a  |f.t..T..D.;D.}<.|

00000120  54 0d c0 e2 06 8a 4c 0a  fe c1 08 d1 8a 6c 0c 5a  |T.....L......l.Z|

00000130  8a 74 0b bb 00 70 8e c3  31 db b8 01 02 cd 13 72  |.t...p..1......r|

00000140  2a 8c c3 8e 06 48 7c 60  1e b9 00 01 8e db 31 f6  |*....H|`......1.|

00000150  31 ff fc f3 a5 1f 61 ff  26 42 7c be 7f 7d e8 40  |1.....a.&B|..}.@|

00000160  00 eb 0e be 84 7d e8 38  00 eb 06 be 8e 7d e8 30  |.....}.8.....}.0|

00000170  00 be 93 7d e8 2a 00 eb  fe 47 52 55 42 20 00 47  |...}.*...GRUB .G|

00000180  65 6f 6d 00 48 61 72 64  20 44 69 73 6b 00 52 65  |eom.Hard Disk.Re|

00000190  61 64 00 20 45 72 72 6f  72 00 bb 01 00 b4 0e cd  |ad. Error.......|

000001a0  10 ac 3c 00 75 f4 c3 00  00 00 00 00 00 00 00 00  |..<.u...........|

000001b0  00 00 00 00 00 00 00 00  4a 63 05 00 00 00 80 20  |........Jc..... |

000001c0  21 00 83 9f 06 19 00 08  00 00 00 40 06 00 00 9f  |!..........@....|

000001d0  07 19 8e fe ff ff 00 48  06 00 00 00 80 07 00 00  |.......H........|

000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|

00000200


备份分区表:

方法一:

[root@localhost ~]# dd if=/dev/sda of=/root/mbr bs=1 count=512


备份好的分区表可以放到u盘等地方


方法二:

sfdisk -d /dev/sda > /tmp/partition.bak


恢复分区表:

[root@localhost ~]# dd if=mbr of=/dev/sda

1+0 records in

1+0 records out

512 bytes (512 B) copied, 0.000361271 s, 1.4 MB/s





GPT分区:

GPT:GUID patition table 支持128个分区,使用64位,支持8Z(512Byte/block )64Z (4096Byte/block)

使用128位UUID 表示磁盘和分区GPT分区表自动备份在头和尾两份,并有CRC校验位

UEFI (统一扩展固件接口)硬件支持GPT

磁盘管理之磁盘分区,主引导分区表修复_第2张图片


管理分区:

列出块设备

blkid

例:

[root@localhost ~]# blkid

/dev/sdb1: UUID="8d1f5a4f-afc1-4056-bfb6-508acc88abe1" TYPE="ext4" 

/dev/sdb2: UUID="EN4Pe9-Pt0z-Xg8b-Th5l-CEd1-8LH5-1IR7EJ" TYPE="LVM2_member" 

/dev/mapper/vg0-root: UUID="6155dbd1-9d2a-42ce-9ec4-1a2bdc13d585" TYPE="ext4" 

/dev/mapper/vg0-swap: UUID="72877876-9119-45a2-bf82-195ed41ad51f" TYPE="swap" 

/dev/mapper/vg0-usr: UUID="c2741397-ff83-41b8-9cc5-caaa5727f486" TYPE="ext4" 

/dev/mapper/vg0-var: UUID="6a187221-b4fe-442d-ac15-1fa5b060f6a4" TYPE="ext4" 


添加分区后不用重启系统,添加硬盘

[root@localhost ~]# echo "- - -" > /sys/class/scsi_host/host2/scan


[root@localhost ~]# fdisk -l

#sdd和sde为新添加的硬盘

Disk /dev/sda: 1073 MB, 1073741824 bytes

255 heads, 63 sectors/track, 130 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



Disk /dev/sdb: 214.7 GB, 214748364800 bytes

255 heads, 63 sectors/track, 26108 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: 0x0005634a


   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1   *           1          26      204800   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sdb2              26        7859    62914560   8e  Linux LVM


Disk /dev/sdc: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 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


Disk /dev/sdd: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 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



Disk /dev/sde: 21.5 GB, 21474836480 bytes

255 heads, 63 sectors/track, 2610 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




创建分区使用:

fdisk创建MBR分区,也支持GPT,对于一块硬盘,最多只能管理15分区

gdisk创建GPT分区

GNU parted高级分区操作(创建、复制、调整大小等等)

partprobe-重新设置内存中的内核分区表版本

fdisk /dev/sdb

gfisk /dev/sdb

# fdisk -l [-u] [device...]

子命令:

p 分区列表

t 更改分区类型

n 创建新分区

d 删除分区

w 保存并退出

q 不保存并退出



查看内核是否已经识别新的分区:

# cat /proc/partations

通知内核重新读取硬盘分区表

新增分区用

partx-a -n M:N /dev/DEVICE

kpartx-a /dev/DEVICE -f: force

删除分区用

partx-d -n M:N /dev/DEVICE

centos6: –nrN-M

CentOS 5,7: 使用partprobe

partprobe[/dev/DEVICE]



例:

给磁盘sdd分区

[root@localhost ~]# fdisk /dev/sdd

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel with disk identifier 0x55380c6d.

Changes will remain in memory only, until you decide to write them.

After that, of course, the previous content won't be recoverable.


Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

         switch off the mode (command 'c') and change display units to

         sectors (command 'u').


Command (m for help): n

Command action

   e   extended                                  #扩展分区

   p   primary partition (1-4)                   #主分区

p

Partition number (1-4): 1                        #分区号

First cylinder (1-2610, default 1):              #默认为1

Using default value 1                     

Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1000   #默认以柱面为单位,一个柱面大小为8M,添加1000个柱面,比8G小一点。


 Device Boot      Start         End      Blocks   Id  System

/dev/sdd1               1        1001     8040501   83  Linux

/dev/sdd2            1002        1133     1060290   83  Linux

/dev/sdd3            1134        1265     1060290   83  Linux

/dev/sdd4            1266        2610    10803712+   5  Extended

/dev/sdd5            1266        1919     5253223+  83  Linux

/dev/sdd6            1920        2610     5550426   83  Linux


Command (m for help): w

The partition table has been altered!


fdisk -l 查看分区是否写入分区表,如果没有写入分区表,用以下命令进行写入:


查看内核是否已经识别新的分区:

# cat /proc/partitions



通知内核重新读取硬盘分区表

新增分区用

partx-a -n /dev/DEVICE

kpartx-a /dev/DEVICE -f: force



删除分区用

partx-d -n M:N /dev/DEVICE

centos6: –nr N-M

CentOS 5,7: 使用partprobe

partprobe[/dev/DEVICE]




parted的操作都是实时生效的,小心使用

用法:parted [选项]... [设备[命令[参数]...]...]

parted /dev/sdb mklabel gpt|msdos                 #创建gpt或者mbr分区

parted /dev/sdb print                             #查看分区

parted /dev/sdb mkpart primary 1 200 (默认M)    #创建主分区从1到200M

parted /dev/sdb rm 1                              #删除分区编号1

parted -l                                         #列出所有设备的分区信息