在学习给硬盘分区的时候,小编发现一个问题,当使用fdisk给新的硬盘分区,第一个扇区是从2048扇区开始的。并且系统盘的第一个分区,也是从第2048个扇区开始的!
[root@CentOS7 ~]# fdisk -l
Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000c5079
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1953791 975872 83 Linux
/dev/sda2 1953792 99608575 48827392 83 Linux
/dev/sda3 99608576 197263359 48827392 83 Linux
/dev/sda4 197263360 419430399 111083520 5 Extended
/dev/sda5 197265408 203556863 3145728 83 Linux
/dev/sda6 203558912 209850367 3145728 83 Linux
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
fdisk: cannot open /dev/sdc: Input/output error
[root@CentOS7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x8a0fba5a.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
我们都知道,硬盘按照分区类型来划分,有MBR分区和GPT分区。
对于MBR分区类型的硬盘,他们的第1个扇区存的是mbr,(512字节=主引导程序Bootloader(446字节)Grub+磁盘分区表DPT(64字节)+标识位(2字节)55 aa),这样充其量也只是占了1个扇区的空间!而对于GPT分区类型的硬盘,第1个扇区为:Proctive MBR;第2个扇区Primary GUID Partition Header;第3~34个扇区记录着分区表;这样他只是占用了34个扇区!
因此,小编分别对两种类型的硬盘做了一个测试。
首先,我使用fdisk命令对一块新的硬盘(sdb)分区,我发现,分区的第一个扇区就是只能是2048以后的扇区。为了确定一下,该硬盘的分区方式,分完区后,我使用gdisk查看,该硬盘的分区类型:MBR分区(MBR only)。
[root@CentOS7 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe23f07bf.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe23f07bf
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@CentOS7 ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: not present
***************************************************************
Found invalid GPT and valid MBR; converting MBR to GPT format.
THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if
you don't want to convert your MBR partitions to GPT format!
***************************************************************
Command (? for help):
然后,当我用gdisk命令,对一块新的硬盘(sdc)分区,很奇怪,他标明的第一个扇区可以是:34-209715166扇区。但是,当我输入:34扇区的时候,系统输出“Information: Moved requested sector from 34 to 2048 in order to align on 2048-sector boundaries.(大致含义是:为了保证与2048扇区对齐,将34扇区移至2048扇区)”!但是,当我继续对sdc硬盘分区的时候,系统标明第一个扇区可以是:34-209715166扇区;我输入:34,系统标明,结束扇区最大为:2047。(并且,小编将/dev/sdc格式化后,挂载到/mnt/sdc2目录下后,存入了两个文件,可以使用hexdump命令查看。这就说明,34~2047扇区是可以记录数据的。)
问题来了,为什么第一个分区最小只能是2048扇区呢?
[root@CentOS7 ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries.
Command (? for help): n
Partition number (1-128, default 1): 1
First sector (34-209715166, default = 2048) or {+-}size{KMGTP}: 34
Information: Moved requested sector from 34 to 2048 in
order to align on 2048-sector boundaries.
Use 'l' on the experts' menu to adjust alignment
Last sector (2048-209715166, default = 209715166) or {+-}size{KMGTP}: +10G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/sdc: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 791DA65D-2F00-4BD2-99B3-F06F32102DB8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 188743613 sectors (90.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 20973567 10.0 GiB 8300 Linux filesystem
Command (? for help): n
Partition number (2-128, default 2):
First sector (34-209715166, default = 20973568) or {+-}size{KMGTP}: 34
Last sector (34-2047, default = 2047) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'
Command (? for help): p
Disk /dev/sdc: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 791DA65D-2F00-4BD2-99B3-F06F32102DB8
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 188741599 sectors (90.0 GiB)
Number Start (sector) End (sector) Size Code Name
1 2048 20973567 10.0 GiB 8300 Linux filesystem
2 34 2047 1007.0 KiB 8300 Linux filesystem
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
2048个扇区并不大,区区1M的空间,或许只是一个并不引人关注的小问题。可是,存在即合理。平白无故,计算机为什么要空出1M的空间呢?
对于GPT分区的硬盘,34~2047扇区可以存储数据,MBR分区的硬盘,1~2047扇区,可以存储数据吗,还是已经有数据存在了?
为此,小编使用hexdump命令,在命令行中执行命令:hexdump -C -n 1048576 /dev/sdb |less
对于该命令的输出结果,小编发现,里面的数据还是挺有规律的,不过,猜想,里面的数据并不太有价值,为此,我又看了第2048扇区之后的数据,发现,前后并没有太大的不同。
另外,当执行命令:hexdump -C -n 1048576 /dev/sdc |less
发现sdb(MBR分区)盘与sdc(GPT分区)输出结果,差不多!
000f4000 f4 80 00 00 f4 80 01 00 f4 80 02 00 f4 80 03 00 |................|
000f4010 f4 80 04 00 f4 80 0c 00 f4 80 0d 00 f4 80 18 00 |................|
000f4020 f4 80 28 00 f4 80 3e 00 f4 80 79 00 f4 80 ab 00 |..(...>...y.....|
000f4030 f4 80 38 01 f4 80 6c 01 00 00 00 00 00 00 00 00 |..8...l.........|
000f4040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000f5000 f5 80 00 00 f5 80 01 00 f5 80 02 00 f5 80 03 00 |................|
000f5010 f5 80 04 00 f5 80 0c 00 f5 80 0d 00 f5 80 18 00 |................|
000f5020 f5 80 28 00 f5 80 3e 00 f5 80 79 00 f5 80 ab 00 |..(...>...y.....|
000f5030 f5 80 38 01 f5 80 6c 01 00 00 00 00 00 00 00 00 |..8...l.........|
000f5040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
“Information: Moved requested sector from 34 to 2048 in order to align on 2048-sector boundaries.(为了保证与2048扇区对齐,将34扇区移至2048扇区)”想了好长时间,一直想不明白这句话什么意思!要保证与2048扇区对齐!为什么要与2048扇区对齐?与谁的2048扇区对齐?是系统盘吗?
为此,小编对系统盘(sda)执行命令:hexdump -C -n 1048576 /dev/sda |less
发现输出结果为:
00000130 88 c5 30 c0 c1 e8 02 08 c1 88 d0 5a 88 c6 bb 00 |..0........Z....|
00000140 70 8e c3 31 db b8 01 02 cd 13 72 1e 8c c3 60 1e |p..1......r...`.|
00000150 b9 00 01 8e db 31 f6 bf 00 80 8e c6 fc f3 a5 1f |.....1..........|
00000160 61 ff 26 5a 7c be 80 7d eb 03 be 8f 7d e8 34 00 |a.&Z|..}....}.4.|
00000170 be 94 7d e8 2e 00 cd 18 eb fe 47 52 55 42 20 00 |..}.......GRUB .|
00000180 47 65 6f 6d 00 48 61 72 64 20 44 69 73 6b 00 52 |Geom.Hard Disk.R|
00000190 65 61 64 00 20 45 72 72 6f 72 0d 0a 00 bb 01 00 |ead. Error......|
000001a0 b4 0e cd 10 ac 3c 00 75 f4 c3 00 00 00 00 00 00 |.....<.u........|
000001b0 00 00 00 00 00 00 00 00 79 50 0c 00 00 00 80 20 |........yP..... |
000001c0 21 00 83 9d 24 79 00 08 00 00 00 c8 1d 00 00 9d |!...$y..........|
000001d0 25 79 83 fe ff ff 00 d0 1d 00 00 18 d2 05 00 fe |%y..............|
******************************************************************************
******************************************************************************
0000d540 96 53 a5 e0 6c dc 9f 66 69 3d 5b b1 94 ef a2 04 |.S..l..fi=[.....|
0000d550 9b f5 d5 9d 2b 21 12 66 03 4c 7d 1b 86 45 6f ea |....+!.f.L}..Eo.|
0000d560 47 10 36 65 53 5a 7a be 4a b5 22 06 af 0b a5 f6 |G.6eSZz.J.".....|
0000d570 9d 2e 02 27 9c 7c 0b d2 fb 99 43 a7 2f bd d6 37 |...'.|....C./..7|
0000d580 fe 44 f1 84 66 75 0e 24 17 f7 51 f0 4c a3 25 b1 |.D..fu.$..Q.L.%.|
0000d590 bc 6d 5c 7e 4d e9 0a b7 26 ba 05 4c 18 62 6e 31 |.m\~M...&..L.bn1|
0000d5a0 ce 58 43 80 5a 3b 43 27 60 32 be 88 46 4b ec 37 |.XC.Z;C'`2..FK.7|
0000d5b0 93 89 72 6e 2f ce f1 27 6c 21 d4 01 23 65 95 52 |..rn/..'l!..#e.R|
0000d5c0 0e 2f 16 c9 46 42 b3 91 6f 20 32 17 95 aa 2d 60 |./..FB..o 2...-`|
0000d5d0 25 e5 eb 69 5a 12 7a 66 5b aa e0 dd 98 10 bb e5 |%..iZ.zf[.......|
0000d5e0 2a a7 a8 ea ac e2 08 3d dc 7c 26 72 3b 17 c2 52 |*......=.|&r;..R|
0000d5f0 66 4c 51 d3 f1 f8 af 83 51 e6 ea 8c 6a 76 1e 98 |fLQ.....Q...jv..|
0000d600 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00100000
通过查看上面的数据的行数,小编发现,到了d5f(3243)行,通过计算,得出到了第226(3423*16/512)个扇区,后面的数据就变成了“0”。
为了得到真相,小编产生了一个“邪恶”的想法,把第2~226扇区中的全部或部分内容给抹掉,看一下计算机的反应。
命令行执行:dd if=/dev/zero of=/dev/sda bs=512 count=10 seek=5
为了保证硬盘与内存同步,执行:sync
。小编只dd掉一部分内容,并且为了保证不破坏mbr,指定选项seek=5。重启之后,发现,机器完了,开不了机,blos也进不了;也没法重装系统(只能卸掉硬盘后,重装)。“快照”真是一个好东西。(dd不同的区域,会出现不同的内容,结果都是开不了机。)
如果是只dd掉sda硬盘的第一个扇区(mbr),他会提示你重装系统!
到此,大致可以做一个猜想:对于MBR分区的硬盘,第2~2047个扇区,里面记录了与开机有关的数据,该片数据并不大,不过特别重要。操作系统为了保证空间足够,加上保证硬盘的性能,直接给了1M的空间(也就是,每个盘面的第一个块block)。以后新加的MBR硬盘,为了保证分区的边界一致(亦或是约定俗成),分区直接从第2048扇区开始。
可是为什么要使用第2~2048的扇区呢?
小编猜想:一个扇区512字节,一个块(block)有8个扇区,一块硬盘有256个磁头(盘面),所以,每个盘面上的第一个block,加一块儿正好是2048个扇区。这样可以保证硬盘的性能。
(以上实验,是在Centos7中做的!Centos6与7不太一样,6中的第2~2047个扇区中,似乎并未写入数据!)