磁盤管理之磁盤分區,主引導分區表修復

磁盤管理:


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個邏輯分區)


wKiom1e9gyjCeuY3AAMdOYNANMo928.jpg


分區表備份及修復

[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

wKioL1e9hN7TVMffAAJeN_1T8nE393.jpg


管理分區:

列出塊設備

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                                         #列出所有設備的分區信息


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章