磁盘逻辑结构浅谈-逻辑分区和LVM-阿里云开发者社区

开发者社区> 宁希波若> 正文

磁盘逻辑结构浅谈-逻辑分区和LVM

简介: 本文档主要的目的是要揭下磁盘存储逻辑结构的神秘面纱,与此同时,我们也介绍一些常见工具和数据结构。 我们讨论里涉及的操作,如果你有Linux虚拟机可用,则尽可以大胆尝试。对于透彻理解我们讨论的主题,亲自动手尝试是必须的。
+关注继续查看

磁盘逻辑结构浅谈-MBR和分区,我们接着说说有关的扩展结构-逻辑分区。因为LVM也偶尔制造一些麻烦,所以我们也捎带说说LVM

EBR:扩展分区和逻辑分区

扩展分区和逻辑分区之关系

由于MBR里磁盘分区结构只有64字节的空间可用,而每个分区项需要16个字节,因此,MBR里最多只能存储4个分区。这就是为什么会有扩展分区和逻辑分区的原因:在一块磁盘上不受限于只能有4个分区,可以按需制作多个分区。那么,扩展分区是怎么实现的呢?

扩展分区和逻辑分区的关系,可以用下图表示,其中绿色块代表的是2048块扇区。很明显,每个逻辑分区前面都有2048块没有使用的扇区

D5088B488539013144CB5389B71915B1

逻辑分区的分区数据,就存储在这些没有使用的扇区里,准确的说,是储存在其中的第一个扇区里(这个扇区叫做扩展引导扇区,简称EBR)。虽然EBR使用和MBR一样的分区表结构。但是,不同于MBR分区表,每个EBR分区表都只是使用前两项记录。一项记录当前的逻辑分区,一项记录下一个逻辑分区。这样,EBR数据之间就构成了一个树结构,可以任意扩展

36BCEDA0D27647DF697D57C082111E10

当然,这也意味着每个逻辑分区,除了第一个逻辑分区外,都是有两条分区数据的。有趣的是,这两条记录并不完全一样。

让我们综合一下。从MBR分区数据我们能够确定扩展分区,然后根据扩展分区,我们可以确定逻辑分区

6F642A320AB9E8F8A465E7206B612A01

验证

我们使用如下的数据来验证扩展分区和逻辑分区以及其间之关系

06058DEFF0576FAB83415DB25929F505

我们首先验证每个逻辑分区前都有2048块空闲扇区

D83FFCBB7789F025F4199E92A6D2E8DA

接下来我们验证这些空闲的2048块扇区的第一块扇区里都有两个分区数据。当然,最后一块逻辑分区例外

73DEE307BB151039632714DDC029EF86

一个逻辑分区的两条记录并不一致

6EA91A90ADCDBDD92469D48E47598419

我们验证一下fdisk的输出和磁盘MBR和EBR记录之间的关系

03517F313FE7BD999D976F5A9FBB4F0F

用到的命令

dd if=/dev/sdb bs=1 count=64 skip=446 2>/dev/null | od -v -tx1

for n in 14336 4210688 8407040;do \
fisrt_sector_number=$(expr $n - 2048);\
dd if=/dev/sdb bs=1 count=64 skip=$(expr ${fisrt_sector_number} \* 512  + 446) 2>/dev/null | \
od -v -tx1;\
done

{ 
dd if=/dev/sdb bs=1 count=64 skip=446 2>/dev/null | od -v -tx1;\
for n in 14336 4210688 8407040;do \
fisrt_sector_number=$(expr $n - 2048);\
dd if=/dev/sdb bs=1 count=64 skip=$(expr ${fisrt_sector_number} \* 512  + 446) 2>/dev/null | \
od -v -tx1;\
done
} | \
perl -nE '
chomp;print qq[$_ ];
if (/\s[0-9a-f]+\s*/) {
  @fields = split;
  print hex(qq{$fields[11]$fields[10]$fields[9]$fileds[8]});
}
print qq[\n];'

LVM:又一层包装

我们直接给出LVM的实际例子,供大家自行分析如下信息

  1. 物理卷
  2. 卷组
  3. 逻辑卷

很明显,LVM有关的数据结构和关系都是记录在磁盘上的。

## LVM分区信息

[root@localhost ~]# cat lvm.info
 --- Physical volume ---
 PV Name               /dev/vda5
 VG Name               ubuntu
 PV Size               9.76 GiB / not usable 2.00 MiB
 Allocatable           yes (but full)
 PE Size               4.00 MiB
 Total PE              2498
 Free PE               0
 Allocated PE          2498
 PV UUID               AIie9r-pdTS-09Za-cATW-UC6j-n3lO-4rraGc

 --- Volume group ---
 VG Name               ubuntu
 System ID
 Format                lvm2
 Metadata Areas        1
 Metadata Sequence No  3
 VG Access             read/write
 VG Status             resizable
 MAX LV                0
 Cur LV                2
 Open LV               2
 Max PV                0
 Cur PV                1
 Act PV                1
 VG Size               9.76 GiB
 PE Size               4.00 MiB
 Total PE              2498
 Alloc PE / Size       2498 / 9.76 GiB
 Free  PE / Size       0 / 0
 VG UUID               padOl1-EPWN-At1M-3GJk-MgFR-mPic-GATrFp

 --- Logical volume ---
 LV Name                /dev/ubuntu/root
 VG Name                ubuntu
 LV UUID                qjonzd-yqJh-cLi1-KZvh-0x6H-W9zB-Fy26Ic
 LV Write Access        read/write
 LV Status              available
 # open                 1
 LV Size                9.29 GiB
 Current LE             2379
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           251:0

 --- Logical volume ---
 LV Name                /dev/ubuntu/swap_1
 VG Name                ubuntu
 LV UUID                HiLDjt-0TVl-zNj2-QxIM-Vl7m-Upsd-Nyfcjw
 LV Write Access        read/write
 LV Status              available
 # open                 1
 LV Size                476.00 MiB
 Current LE             119
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           251:1

[root@iZ2ze3shz9b6z00qqhjoyzZ ~]#

## 二进制编辑器里的LVM信息

00000000: 4c41 4245 4c4f 4e45 0100 0000 0000 0000  LABELONE........
00000010: 9ebe 7a39 2000 0000 4c56 4d32 2030 3031  ..z9 ...LVM2 001
00000020: 4149 6965 3972 7064 5453 3039 5a61 6341  AIie9rpdTS09ZacA
00000030: 5457 5543 366a 6e33 6c4f 3472 7261 4763  TWUC6jn3lO4rraGc
00000040: 0000 a070 0200 0000 0000 0300 0000 0000  ...p............
...
00000e00: 1a37 8d62 204c 564d 3220 785b 3541 2572  .7.b LVM2 x[5A%r
00000e10: 304e 2a3e 0100 0000 0010 0000 0000 0000  0N*>............
00000e20: 00f0 0200 0000 0000 000a 0000 0000 0000  ................
00000e30: 8e04 0000 0000 0000 c279 ed59 0000 0000  .........y.Y....
00000e40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000e50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000e60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000e70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000e80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000e90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000ea0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000eb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000ec0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000ed0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000ee0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000ef0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000f90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000fa0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000fb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000fc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000fd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000fe0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000ff0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001000: 7562 756e 7475 207b 0a69 6420 3d20 2270  ubuntu {.id = "p
00001010: 6164 4f6c 312d 4550 574e 2d41 7431 4d2d  adOl1-EPWN-At1M-
00001020: 3347 4a6b 2d4d 6746 522d 6d50 6963 2d47  3GJk-MgFR-mPic-G
00001030: 4154 7246 7022 0a73 6571 6e6f 203d 2031  ATrFp".seqno = 1
00001040: 0a73 7461 7475 7320 3d20 5b22 5245 5349  .status = ["RESI
00001050: 5a45 4142 4c45 222c 2022 5245 4144 222c  ZEABLE", "READ",
00001060: 2022 5752 4954 4522 5d0a 666c 6167 7320   "WRITE"].flags
00001070: 3d20 5b5d 0a65 7874 656e 745f 7369 7a65  = [].extent_size
00001080: 203d 2038 3139 320a 6d61 785f 6c76 203d   = 8192.max_lv =
00001090: 2030 0a6d 6178 5f70 7620 3d20 300a 0a70   0.max_pv = 0..p
000010a0: 6879 7369 6361 6c5f 766f 6c75 6d65 7320  hysical_volumes
000010b0: 7b0a 0a70 7630 207b 0a69 6420 3d20 2241  {..pv0 {.id = "A
000010c0: 4969 6539 722d 7064 5453 2d30 395a 612d  Iie9r-pdTS-09Za-
000010d0: 6341 5457 2d55 4336 6a2d 6e33 6c4f 2d34  cATW-UC6j-n3lO-4
000010e0: 7272 6147 6322 0a64 6576 6963 6520 3d20  rraGc".device =
000010f0: 222f 6465 762f 7364 6135 220a 0a73 7461  "/dev/sda5"..sta
00001100: 7475 7320 3d20 5b22 414c 4c4f 4341 5441  tus = ["ALLOCATA
00001110: 424c 4522 5d0a 666c 6167 7320 3d20 5b5d  BLE"].flags = []
00001120: 0a64 6576 5f73 697a 6520 3d20 3230 3436  .dev_size = 2046
00001130: 3737 3132 0a70 655f 7374 6172 7420 3d20  7712.pe_start =
00001140: 3338 340a 7065 5f63 6f75 6e74 203d 2032  384.pe_count = 2
00001150: 3439 380a 7d0a 7d0a 0a7d 0a23 2047 656e  498.}.}..}.# Gen
00001160: 6572 6174 6564 2062 7920 4c56 4d32 2076  erated by LVM2 v
00001170: 6572 7369 6f6e 2032 2e30 322e 3534 2831  ersion 2.02.54(1
00001180: 2920 2832 3030 392d 3130 2d32 3629 3a20  ) (2009-10-26):
00001190: 4d6f 6e20 4170 7220 3234 2030 383a 3534  Mon Apr 24 08:54
000011a0: 3a31 3920 3230 3137 0a0a 636f 6e74 656e  :19 2017..conten
000011b0: 7473 203d 2022 5465 7874 2046 6f72 6d61  ts = "Text Forma
000011c0: 7420 566f 6c75 6d65 2047 726f 7570 220a  t Volume Group".
000011d0: 7665 7273 696f 6e20 3d20 310a 0a64 6573  version = 1..des
000011e0: 6372 6970 7469 6f6e 203d 2022 220a 0a63  cription = ""..c
000011f0: 7265 6174 696f 6e5f 686f 7374 203d 2022  reation_host = "
00001200: 7562 756e 7475 2209 2320 4c69 6e75 7820  ubuntu".# Linux
00001210: 7562 756e 7475 2032 2e36 2e33 322d 3231  ubuntu 2.6.32-21
00001220: 2d67 656e 6572 6963 2023 3332 2d55 6275  -generic #32-Ubu
00001230: 6e74 7520 534d 5020 4672 6920 4170 7220  ntu SMP Fri Apr
00001240: 3136 2030 383a 3130 3a30 3220 5554 4320  16 08:10:02 UTC
00001250: 3230 3130 2069 3638 360a 6372 6561 7469  2010 i686.creati
00001260: 6f6e 5f74 696d 6520 3d20 3134 3933 3032  on_time = 149302
00001270: 3430 3539 0923 204d 6f6e 2041 7072 2032  4059.# Mon Apr 2
00001280: 3420 3038 3a35 343a 3139 2032 3031 370a  4 08:54:19 2017.
00001290: 0a00 0000 0000 0000 0000 0000 0000 0000  ................
...
00001400: 7562 756e 7475 207b 0a69 6420 3d20 2270  ubuntu {.id = "p
00001410: 6164 4f6c 312d 4550 574e 2d41 7431 4d2d  adOl1-EPWN-At1M-
00001420: 3347 4a6b 2d4d 6746 522d 6d50 6963 2d47  3GJk-MgFR-mPic-G
00001430: 4154 7246 7022 0a73 6571 6e6f 203d 2032  ATrFp".seqno = 2
00001440: 0a73 7461 7475 7320 3d20 5b22 5245 5349  .status = ["RESI
00001450: 5a45 4142 4c45 222c 2022 5245 4144 222c  ZEABLE", "READ",
00001460: 2022 5752 4954 4522 5d0a 666c 6167 7320   "WRITE"].flags
00001470: 3d20 5b5d 0a65 7874 656e 745f 7369 7a65  = [].extent_size
00001480: 203d 2038 3139 320a 6d61 785f 6c76 203d   = 8192.max_lv =
00001490: 2030 0a6d 6178 5f70 7620 3d20 300a 0a70   0.max_pv = 0..p
000014a0: 6879 7369 6361 6c5f 766f 6c75 6d65 7320  hysical_volumes
000014b0: 7b0a 0a70 7630 207b 0a69 6420 3d20 2241  {..pv0 {.id = "A
000014c0: 4969 6539 722d 7064 5453 2d30 395a 612d  Iie9r-pdTS-09Za-
000014d0: 6341 5457 2d55 4336 6a2d 6e33 6c4f 2d34  cATW-UC6j-n3lO-4
000014e0: 7272 6147 6322 0a64 6576 6963 6520 3d20  rraGc".device =
000014f0: 222f 6465 762f 7364 6135 220a 0a73 7461  "/dev/sda5"..sta
00001500: 7475 7320 3d20 5b22 414c 4c4f 4341 5441  tus = ["ALLOCATA
00001510: 424c 4522 5d0a 666c 6167 7320 3d20 5b5d  BLE"].flags = []
00001520: 0a64 6576 5f73 697a 6520 3d20 3230 3436  .dev_size = 2046
00001530: 3737 3132 0a70 655f 7374 6172 7420 3d20  7712.pe_start =
00001540: 3338 340a 7065 5f63 6f75 6e74 203d 2032  384.pe_count = 2
00001550: 3439 380a 7d0a 7d0a 0a6c 6f67 6963 616c  498.}.}..logical
00001560: 5f76 6f6c 756d 6573 207b 0a0a 726f 6f74  _volumes {..root
00001570: 207b 0a69 6420 3d20 2271 6a6f 6e7a 642d   {.id = "qjonzd-
00001580: 7971 4a68 2d63 4c69 312d 4b5a 7668 2d30  yqJh-cLi1-KZvh-0
00001590: 7836 482d 5739 7a42 2d46 7932 3649 6322  x6H-W9zB-Fy26Ic"
000015a0: 0a73 7461 7475 7320 3d20 5b22 5245 4144  .status = ["READ
000015b0: 222c 2022 5752 4954 4522 2c20 2256 4953  ", "WRITE", "VIS
000015c0: 4942 4c45 225d 0a66 6c61 6773 203d 205b  IBLE"].flags = [
000015d0: 5d0a 7365 676d 656e 745f 636f 756e 7420  ].segment_count
000015e0: 3d20 310a 0a73 6567 6d65 6e74 3120 7b0a  = 1..segment1 {.
000015f0: 7374 6172 745f 6578 7465 6e74 203d 2030  start_extent = 0
00001600: 0a65 7874 656e 745f 636f 756e 7420 3d20  .extent_count =
00001610: 3233 3739 0a0a 7479 7065 203d 2022 7374  2379..type = "st
00001620: 7269 7065 6422 0a73 7472 6970 655f 636f  riped".stripe_co
00001630: 756e 7420 3d20 3109 2320 6c69 6e65 6172  unt = 1.# linear
00001640: 0a0a 7374 7269 7065 7320 3d20 5b0a 2270  ..stripes = [."p
00001650: 7630 222c 2030 0a5d 0a7d 0a7d 0a7d 0a7d  v0", 0.].}.}.}.}
00001660: 0a23 2047 656e 6572 6174 6564 2062 7920  .# Generated by
00001670: 4c56 4d32 2076 6572 7369 6f6e 2032 2e30  LVM2 version 2.0
00001680: 322e 3534 2831 2920 2832 3030 392d 3130  2.54(1) (2009-10
00001690: 2d32 3629 3a20 4d6f 6e20 4170 7220 3234  -26): Mon Apr 24
000016a0: 2030 383a 3534 3a32 3120 3230 3137 0a0a   08:54:21 2017..
000016b0: 636f 6e74 656e 7473 203d 2022 5465 7874  contents = "Text
000016c0: 2046 6f72 6d61 7420 566f 6c75 6d65 2047   Format Volume G
000016d0: 726f 7570 220a 7665 7273 696f 6e20 3d20  roup".version =
000016e0: 310a 0a64 6573 6372 6970 7469 6f6e 203d  1..description =
000016f0: 2022 220a 0a63 7265 6174 696f 6e5f 686f   ""..creation_ho
00001700: 7374 203d 2022 7562 756e 7475 2209 2320  st = "ubuntu".#
00001710: 4c69 6e75 7820 7562 756e 7475 2032 2e36  Linux ubuntu 2.6
00001720: 2e33 322d 3231 2d67 656e 6572 6963 2023  .32-21-generic #
00001730: 3332 2d55 6275 6e74 7520 534d 5020 4672  32-Ubuntu SMP Fr
00001740: 6920 4170 7220 3136 2030 383a 3130 3a30  i Apr 16 08:10:0
00001750: 3220 5554 4320 3230 3130 2069 3638 360a  2 UTC 2010 i686.
00001760: 6372 6561 7469 6f6e 5f74 696d 6520 3d20  creation_time =
00001770: 3134 3933 3032 3430 3631 0923 204d 6f6e  1493024061.# Mon
00001780: 2041 7072 2032 3420 3038 3a35 343a 3231   Apr 24 08:54:21
00001790: 2032 3031 370a 0a00 0000 0000 0000 0000   2017...........
000017a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000017b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000017c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000017d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000017e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000017f0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001800: 7562 756e 7475 207b 0a69 6420 3d20 2270  ubuntu {.id = "p
00001810: 6164 4f6c 312d 4550 574e 2d41 7431 4d2d  adOl1-EPWN-At1M-
00001820: 3347 4a6b 2d4d 6746 522d 6d50 6963 2d47  3GJk-MgFR-mPic-G
00001830: 4154 7246 7022 0a73 6571 6e6f 203d 2033  ATrFp".seqno = 3
00001840: 0a73 7461 7475 7320 3d20 5b22 5245 5349  .status = ["RESI
00001850: 5a45 4142 4c45 222c 2022 5245 4144 222c  ZEABLE", "READ",
00001860: 2022 5752 4954 4522 5d0a 666c 6167 7320   "WRITE"].flags
00001870: 3d20 5b5d 0a65 7874 656e 745f 7369 7a65  = [].extent_size
00001880: 203d 2038 3139 320a 6d61 785f 6c76 203d   = 8192.max_lv =
00001890: 2030 0a6d 6178 5f70 7620 3d20 300a 0a70   0.max_pv = 0..p
000018a0: 6879 7369 6361 6c5f 766f 6c75 6d65 7320  hysical_volumes
000018b0: 7b0a 0a70 7630 207b 0a69 6420 3d20 2241  {..pv0 {.id = "A
000018c0: 4969 6539 722d 7064 5453 2d30 395a 612d  Iie9r-pdTS-09Za-
000018d0: 6341 5457 2d55 4336 6a2d 6e33 6c4f 2d34  cATW-UC6j-n3lO-4
000018e0: 7272 6147 6322 0a64 6576 6963 6520 3d20  rraGc".device =
000018f0: 222f 6465 762f 7364 6135 220a 0a73 7461  "/dev/sda5"..sta
00001900: 7475 7320 3d20 5b22 414c 4c4f 4341 5441  tus = ["ALLOCATA
00001910: 424c 4522 5d0a 666c 6167 7320 3d20 5b5d  BLE"].flags = []
00001920: 0a64 6576 5f73 697a 6520 3d20 3230 3436  .dev_size = 2046
00001930: 3737 3132 0a70 655f 7374 6172 7420 3d20  7712.pe_start =
00001940: 3338 340a 7065 5f63 6f75 6e74 203d 2032  384.pe_count = 2
00001950: 3439 380a 7d0a 7d0a 0a6c 6f67 6963 616c  498.}.}..logical
00001960: 5f76 6f6c 756d 6573 207b 0a0a 726f 6f74  _volumes {..root
00001970: 207b 0a69 6420 3d20 2271 6a6f 6e7a 642d   {.id = "qjonzd-
00001980: 7971 4a68 2d63 4c69 312d 4b5a 7668 2d30  yqJh-cLi1-KZvh-0
00001990: 7836 482d 5739 7a42 2d46 7932 3649 6322  x6H-W9zB-Fy26Ic"
000019a0: 0a73 7461 7475 7320 3d20 5b22 5245 4144  .status = ["READ
000019b0: 222c 2022 5752 4954 4522 2c20 2256 4953  ", "WRITE", "VIS
000019c0: 4942 4c45 225d 0a66 6c61 6773 203d 205b  IBLE"].flags = [
000019d0: 5d0a 7365 676d 656e 745f 636f 756e 7420  ].segment_count
000019e0: 3d20 310a 0a73 6567 6d65 6e74 3120 7b0a  = 1..segment1 {.
000019f0: 7374 6172 745f 6578 7465 6e74 203d 2030  start_extent = 0
00001a00: 0a65 7874 656e 745f 636f 756e 7420 3d20  .extent_count =
00001a10: 3233 3739 0a0a 7479 7065 203d 2022 7374  2379..type = "st
00001a20: 7269 7065 6422 0a73 7472 6970 655f 636f  riped".stripe_co
00001a30: 756e 7420 3d20 3109 2320 6c69 6e65 6172  unt = 1.# linear
00001a40: 0a0a 7374 7269 7065 7320 3d20 5b0a 2270  ..stripes = [."p
00001a50: 7630 222c 2030 0a5d 0a7d 0a7d 0a0a 7377  v0", 0.].}.}..sw
00001a60: 6170 5f31 207b 0a69 6420 3d20 2248 694c  ap_1 {.id = "HiL
00001a70: 446a 742d 3054 566c 2d7a 4e6a 322d 5178  Djt-0TVl-zNj2-Qx
00001a80: 494d 2d56 6c37 6d2d 5570 7364 2d4e 7966  IM-Vl7m-Upsd-Nyf
00001a90: 636a 7722 0a73 7461 7475 7320 3d20 5b22  cjw".status = ["
00001aa0: 5245 4144 222c 2022 5752 4954 4522 2c20  READ", "WRITE",
00001ab0: 2256 4953 4942 4c45 225d 0a66 6c61 6773  "VISIBLE"].flags
00001ac0: 203d 205b 5d0a 7365 676d 656e 745f 636f   = [].segment_co
00001ad0: 756e 7420 3d20 310a 0a73 6567 6d65 6e74  unt = 1..segment
00001ae0: 3120 7b0a 7374 6172 745f 6578 7465 6e74  1 {.start_extent
00001af0: 203d 2030 0a65 7874 656e 745f 636f 756e   = 0.extent_coun
00001b00: 7420 3d20 3131 390a 0a74 7970 6520 3d20  t = 119..type =
00001b10: 2273 7472 6970 6564 220a 7374 7269 7065  "striped".stripe
00001b20: 5f63 6f75 6e74 203d 2031 0923 206c 696e  _count = 1.# lin
00001b30: 6561 720a 0a73 7472 6970 6573 203d 205b  ear..stripes = [
00001b40: 0a22 7076 3022 2c20 3233 3739 0a5d 0a7d  ."pv0", 2379.].}
00001b50: 0a7d 0a7d 0a7d 0a23 2047 656e 6572 6174  .}.}.}.# Generat
00001b60: 6564 2062 7920 4c56 4d32 2076 6572 7369  ed by LVM2 versi
00001b70: 6f6e 2032 2e30 322e 3534 2831 2920 2832  on 2.02.54(1) (2
00001b80: 3030 392d 3130 2d32 3629 3a20 4d6f 6e20  009-10-26): Mon
00001b90: 4170 7220 3234 2030 383a 3534 3a32 3220  Apr 24 08:54:22
00001ba0: 3230 3137 0a0a 636f 6e74 656e 7473 203d  2017..contents =
00001bb0: 2022 5465 7874 2046 6f72 6d61 7420 566f   "Text Format Vo
00001bc0: 6c75 6d65 2047 726f 7570 220a 7665 7273  lume Group".vers
00001bd0: 696f 6e20 3d20 310a 0a64 6573 6372 6970  ion = 1..descrip
00001be0: 7469 6f6e 203d 2022 220a 0a63 7265 6174  tion = ""..creat
00001bf0: 696f 6e5f 686f 7374 203d 2022 7562 756e  ion_host = "ubun
00001c00: 7475 2209 2320 4c69 6e75 7820 7562 756e  tu".# Linux ubun
00001c10: 7475 2032 2e36 2e33 322d 3231 2d67 656e  tu 2.6.32-21-gen
00001c20: 6572 6963 2023 3332 2d55 6275 6e74 7520  eric #32-Ubuntu
00001c30: 534d 5020 4672 6920 4170 7220 3136 2030  SMP Fri Apr 16 0
00001c40: 383a 3130 3a30 3220 5554 4320 3230 3130  8:10:02 UTC 2010
00001c50: 2069 3638 360a 6372 6561 7469 6f6e 5f74   i686.creation_t
00001c60: 696d 6520 3d20 3134 3933 3032 3430 3632  ime = 1493024062
00001c70: 0923 204d 6f6e 2041 7072 2032 3420 3038  .# Mon Apr 24 08
00001c80: 3a35 343a 3232 2032 3031 370a 0a00 0000  :54:22 2017.....
00001c90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001ca0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001cb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001cc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001cd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001ce0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001cf0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d00: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d10: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d20: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d30: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d40: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d50: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d60: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d70: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d80: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001d90: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001da0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001db0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001dc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001dd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001de0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00001df0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
...

MBR的引导代码

提取引导代码

因为引导代码也是MBR的一部分,因此,调整提取MBR的命令即可提取之到文件,而后反编译之

dd if=path_to_your_disc bs=1 count=446 > path_to_mbr_code.bin
objdump -D -b binary -m i386 -Maddr16,data16 --adjust-vma=0x7c00 path_to_mbr_code.bin
# if you like the intel style
objdump -D -b binary -m i386 -Maddr16,data16,intel --adjust-vma=0x7c00 path_to_mbr_code.bin

看看效果

913FBFA37BEA039E3B0194F7785EB3E4

一睹真实引导代码风采

现在的引导代码并非只是简单的一段实模式下的二进制程序,因此反编译的结果就差强人意了。如果你想一睹真实代码风貌,请看grub-core/boot/i386/pc/boot.S文件内容

/* -*-Asm-*- */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 1999,2000,2001,2002,2005,2006,2007,2008,2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/symbol.h>
#include <grub/machine/boot.h>

/*
 *  defines for the code go here
 */

    /* Print message string */
#define MSG(x)    movw $x, %si; call LOCAL(message)
#define ERR(x)    movw $x, %si; jmp LOCAL(error_message)

    .macro floppy
part_start:

LOCAL(probe_values):
    .byte    36, 18, 15, 9, 0

LOCAL(floppy_probe):
    pushw    %dx
/*
 *  Perform floppy probe.
 */
#ifdef __APPLE__
    LOCAL(probe_values_minus_one) = LOCAL(probe_values) - 1
    movw    MACRO_DOLLAR(LOCAL(probe_values_minus_one)), %si
#else
    movw    MACRO_DOLLAR(LOCAL(probe_values)) - 1, %si
#endif

LOCAL(probe_loop):
    /* reset floppy controller INT 13h AH=0 */
    xorw    %ax, %ax
    int    MACRO_DOLLAR(0x13)

    incw    %si
    movb    (%si), %cl

    /* if number of sectors is 0, display error and die */
    testb    %cl, %cl
    jnz    1f

/*
 * Floppy disk probe failure.
 */
    MSG(fd_probe_error_string)
    jmp    LOCAL(general_error)

/* "Floppy" */
fd_probe_error_string:    .asciz "Floppy"

1:
    /* perform read */
    movw    MACRO_DOLLAR(GRUB_BOOT_MACHINE_BUFFER_SEG), %bx
    movw    %bx, %es
    xorw    %bx, %bx
    movw    MACRO_DOLLAR(0x201), %ax
    movb    MACRO_DOLLAR(0), %ch
    movb    MACRO_DOLLAR(0), %dh
    int    MACRO_DOLLAR(0x13)

    /* if error, jump to "LOCAL(probe_loop)" */
    jc    LOCAL(probe_loop)

    /* %cl is already the correct value! */
    movb    MACRO_DOLLAR(1), %dh
    movb    MACRO_DOLLAR(79), %ch

    jmp    LOCAL(final_init)
    .endm

    .macro scratch

    /* scratch space */
mode:
    .byte    0
disk_address_packet:
sectors:
    .long    0
heads:
    .long    0
cylinders:
    .word    0
sector_start:
    .byte    0
head_start:
    .byte    0
cylinder_start:
    .word    0
    /* more space... */
    .endm

    .file    "boot.S"

    .text

    /* Tell GAS to generate 16-bit instructions so that this code works
       in real mode. */
    .code16

.globl _start, start;
_start:
start:
    /*
     * _start is loaded at 0x7c00 and is jumped to with CS:IP 0:0x7c00
     */

    /*
     * Beginning of the sector is compatible with the FAT/HPFS BIOS
     * parameter block.
     */

    jmp    LOCAL(after_BPB)
    nop    /* do I care about this ??? */

#ifdef HYBRID_BOOT
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    nop
    nop
    jmp    LOCAL(after_BPB)
#else
    /*
     * This space is for the BIOS parameter block!!!!  Don't change
     * the first jump, nor start the code anywhere but right after
     * this area.
     */

    .org GRUB_BOOT_MACHINE_BPB_START
    .org 4
#endif
#ifdef HYBRID_BOOT
    floppy
#else
    scratch
#endif

    .org GRUB_BOOT_MACHINE_BPB_END
    /*
     * End of BIOS parameter block.
     */

LOCAL(kernel_address):
    .word    GRUB_BOOT_MACHINE_KERNEL_ADDR

#ifndef HYBRID_BOOT
    .org GRUB_BOOT_MACHINE_KERNEL_SECTOR
LOCAL(kernel_sector):
    .long    1
LOCAL(kernel_sector_high):
    .long    0
#endif

    .org GRUB_BOOT_MACHINE_BOOT_DRIVE
boot_drive:
    .byte 0xff    /* the disk to load kernel from */
            /* 0xff means use the boot drive */

LOCAL(after_BPB):

/* general setup */
    cli        /* we're not safe here! */

        /*
         * This is a workaround for buggy BIOSes which don't pass boot
         * drive correctly. If GRUB is installed into a HDD, check if
         * DL is masked correctly. If not, assume that the BIOS passed
         * a bogus value and set DL to 0x80, since this is the only
         * possible boot drive. If GRUB is installed into a floppy,
         * this does nothing (only jump).
         */
    .org GRUB_BOOT_MACHINE_DRIVE_CHECK
boot_drive_check:
        jmp     3f    /* grub-setup may overwrite this jump */
        testb   $0x80, %dl
        jz      2f
3:
    /* Ignore %dl different from 0-0x0f and 0x80-0x8f.  */
    testb   $0x70, %dl
    jz      1f
2:    
        movb    $0x80, %dl
1:
    /*
     * ljmp to the next instruction because some bogus BIOSes
     * jump to 07C0:0000 instead of 0000:7C00.
     */
    ljmp    $0, $real_start

real_start:

    /* set up %ds and %ss as offset from 0 */
    xorw    %ax, %ax
    movw    %ax, %ds
    movw    %ax, %ss

    /* set up the REAL stack */
    movw    $GRUB_BOOT_MACHINE_STACK_SEG, %sp

    sti        /* we're safe again */

    /*
     *  Check if we have a forced disk reference here
     */
    movb   boot_drive, %al
    cmpb    $0xff, %al
    je    1f
    movb    %al, %dl
1:
    /* save drive reference first thing! */
    pushw    %dx

    /* print a notification message on the screen */
    MSG(notification_string)

    /* set %si to the disk address packet */
    movw    $disk_address_packet, %si

    /* check if LBA is supported */
    movb    $0x41, %ah
    movw    $0x55aa, %bx
    int    $0x13

    /*
     *  %dl may have been clobbered by INT 13, AH=41H.
     *  This happens, for example, with AST BIOS 1.04.
     */
    popw    %dx
    pushw    %dx

    /* use CHS if fails */
    jc    LOCAL(chs_mode)
    cmpw    $0xaa55, %bx
    jne    LOCAL(chs_mode)

    andw    $1, %cx
    jz    LOCAL(chs_mode)

LOCAL(lba_mode):
    xorw    %ax, %ax
    movw    %ax, 4(%si)

    incw    %ax
    /* set the mode to non-zero */
    movb    %al, -1(%si)

    /* the blocks */
    movw    %ax, 2(%si)

    /* the size and the reserved byte */
    movw    $0x0010, (%si)

    /* the absolute address */
    movl    LOCAL(kernel_sector), %ebx
    movl    %ebx, 8(%si)
    movl    LOCAL(kernel_sector_high), %ebx
    movl    %ebx, 12(%si)

    /* the segment of buffer address */
    movw    $GRUB_BOOT_MACHINE_BUFFER_SEG, 6(%si)

/*
 * BIOS call "INT 0x13 Function 0x42" to read sectors from disk into memory
 *    Call with    %ah = 0x42
 *            %dl = drive number
 *            %ds:%si = segment:offset of disk address packet
 *    Return:
 *            %al = 0x0 on success; err code on failure
 */

    movb    $0x42, %ah
    int    $0x13

    /* LBA read is not supported, so fallback to CHS.  */
    jc    LOCAL(chs_mode)

    movw    $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
    jmp    LOCAL(copy_buffer)

LOCAL(chs_mode):
    /*
     *  Determine the hard disk geometry from the BIOS!
     *  We do this first, so that LS-120 IDE floppies work correctly.
     */
    movb    $8, %ah
    int    $0x13
    jnc    LOCAL(final_init)

    popw    %dx
    /*
     *  The call failed, so maybe use the floppy probe instead.
     */
    testb    %dl, %dl
    jnb    LOCAL(floppy_probe)

    /* Nope, we definitely have a hard disk, and we're screwed. */
    ERR(hd_probe_error_string)

LOCAL(final_init):
    /* set the mode to zero */
    movzbl    %dh, %eax
    movb    %ah, -1(%si)

    /* save number of heads */
    incw    %ax
    movl    %eax, 4(%si)

    movzbw    %cl, %dx
    shlw    $2, %dx
    movb    %ch, %al
    movb    %dh, %ah

    /* save number of cylinders */
    incw    %ax
    movw    %ax, 8(%si)

    movzbw    %dl, %ax
    shrb    $2, %al

    /* save number of sectors */
    movl    %eax, (%si)

setup_sectors:
    /* load logical sector start (top half) */
    movl    LOCAL(kernel_sector_high), %eax

    orl    %eax, %eax
    jnz    LOCAL(geometry_error)

    /* load logical sector start (bottom half) */
    movl    LOCAL(kernel_sector), %eax

    /* zero %edx */
    xorl    %edx, %edx

    /* divide by number of sectors */
    divl    (%si)

    /* save sector start */
    movb    %dl, %cl

    xorw    %dx, %dx    /* zero %edx */
    divl    4(%si)        /* divide by number of heads */

    /* do we need too many cylinders? */
    cmpw    8(%si), %ax
    jge    LOCAL(geometry_error)

    /* normalize sector start (1-based) */
    incb    %cl

    /* low bits of cylinder start */
    movb    %al, %ch

    /* high bits of cylinder start */
    xorb    %al, %al
    shrw    $2, %ax
    orb    %al, %cl

    /* save head start */
    movb    %dl, %al

    /* restore %dl */
    popw    %dx

    /* head start */
    movb    %al, %dh

/*
 * BIOS call "INT 0x13 Function 0x2" to read sectors from disk into memory
 *    Call with    %ah = 0x2
 *            %al = number of sectors
 *            %ch = cylinder
 *            %cl = sector (bits 6-7 are high bits of "cylinder")
 *            %dh = head
 *            %dl = drive (0x80 for hard disk, 0x0 for floppy disk)
 *            %es:%bx = segment:offset of buffer
 *    Return:
 *            %al = 0x0 on success; err code on failure
 */

    movw    $GRUB_BOOT_MACHINE_BUFFER_SEG, %bx
    movw    %bx, %es    /* load %es segment with disk buffer */

    xorw    %bx, %bx    /* %bx = 0, put it at 0 in the segment */
    movw    $0x0201, %ax    /* function 2 */
    int    $0x13

    jc    LOCAL(read_error)

    movw    %es, %bx

LOCAL(copy_buffer):
    /*
     * We need to save %cx and %si because the startup code in
     * kernel uses them without initializing them.
     */
    pusha
    pushw    %ds

    movw    $0x100, %cx
    movw    %bx, %ds
    xorw    %si, %si
    movw    $GRUB_BOOT_MACHINE_KERNEL_ADDR, %di
    movw    %si, %es

    cld

    rep
    movsw

    popw    %ds
    popa

    /* boot kernel */
    jmp    *(LOCAL(kernel_address))

/* END OF MAIN LOOP */

/*
 * BIOS Geometry translation error (past the end of the disk geometry!).
 */
LOCAL(geometry_error):
    ERR(geometry_error_string)

/*
 * Read error on the disk.
 */
LOCAL(read_error):
    movw    $read_error_string, %si
LOCAL(error_message):
    call    LOCAL(message)
LOCAL(general_error):
    MSG(general_error_string)

/* go here when you need to stop the machine hard after an error condition */
        /* tell the BIOS a boot failure, which may result in no effect */
        int    $0x18
LOCAL(stop):
    jmp    LOCAL(stop)

notification_string:    .asciz "GRUB "
geometry_error_string:    .asciz "Geom"
hd_probe_error_string:    .asciz "Hard Disk"
read_error_string:    .asciz "Read"
general_error_string:    .asciz " Error\r\n"

/*
 * message: write the string pointed to by %si
 *
 *   WARNING: trashes %si, %ax, and %bx
 */

    /*
     * Use BIOS "int 10H Function 0Eh" to write character in teletype mode
     *    %ah = 0xe    %al = character
     *    %bh = page    %bl = foreground color (graphics modes)
     */
1:
    movw    $0x0001, %bx
    movb    $0xe, %ah
    int    $0x10        /* display a byte */
LOCAL(message):
    lodsb
    cmpb    $0, %al
    jne    1b    /* if not end of string, jmp to display */
    ret

    /*
     *  Windows NT breaks compatibility by embedding a magic
     *  number here.
     */

#ifdef HYBRID_BOOT
    .org 0x1b0
LOCAL(kernel_sector):
    .long    1
LOCAL(kernel_sector_high):
    .long    0
#endif
    .org GRUB_BOOT_MACHINE_WINDOWS_NT_MAGIC
nt_magic:
    .long 0
    .word 0

    /*
     *  This is where an MBR would go if on a hard disk.  The code
     *  here isn't even referenced unless we're on a floppy.  Kinda
     *  sneaky, huh?
    */

    .org GRUB_BOOT_MACHINE_PART_START

#ifndef HYBRID_BOOT
    floppy
#else
    scratch
#endif

    .org GRUB_BOOT_MACHINE_PART_END
    
/* the last 2 bytes in the sector 0 contain the signature */
    .word    GRUB_BOOT_MACHINE_SIGNATURE

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
linux LVM 一键分区脚本自动扩容
lv 分区,vg 卷,pv 硬盘;加s 列出,remove 删除 ;如:lvs和 lvremove 需要fdisk -l 查看LVM硬盘目录,如删除分区:lvremove /dev/mapper/eisc-b
771 0
《数字逻辑设计与计算机组成》一 2.5 逻辑化简算法
本节书摘来自华章出版社《数字逻辑设计与计算机组成》一 书中的第2章,第2.5节,作者:[美]尼克罗斯·法拉菲,更多章节内容可以访问云栖社区“华章计算机”公众号查看。
1286 0
+关注
宁希波若
Linux系统黑客,传统意义上的。多时候栖身C库和内核交界处。
23
文章
0
问答
来源圈子
更多
作为全球云计算的领先者,阿里云为全球230万企业提供着云计算服务,服务范围覆盖200多个国家和地区。我们致力于为企业、政府等组织机构提供安全可靠的云计算服务,给用户带来极速愉悦的服务体验。
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载