那天下午,阳光透过办公室的窗户洒在桌面上,我刚刚处理完一个棘手的bug,正享受着一杯茶的宁静时光。然而,这份宁静很快被一阵急促的电话铃声打破。
“我们在做虚拟机磁盘扩容,遇到一个奇怪的问题,你能帮忙看看吗?”电话那头的声音透着焦急。
我调整了一下状态,心中略显兴奋。回复道:“别担心,我马上来。”
一、故障描述
在使用不同的磁盘分区工具(如`fdisk`和`parted`)时,发现它们显示的分区类型不一致。这种情况看似微小,但实际上可能引发一系列严重问题,影响数据安全和系统稳定性。
# fdisk -l /dev/sda
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 104857599 52428799+ ee GPT
# parted -s /dev/sda unit s print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 125829120s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 2048s 411647s 409600s fat16 EFI System Partition boot
2 411648s 2508799s 2097152s xfs
3 2508800s 104855551s 102346752s
二、初步排查
# which fdisk /usr/sbin/fdisk # rpm -qf /usr/sbin/fdisk util-linux-2.23.2-59.el7.x86_64
2.查看版本
# fdisk -v fdisk from util-linux 2.23.2
3.查看分区表
# fdisk -lu /dev/sda Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x00000000 Device Boot Start End Blocks Id System /dev/sda1 1 104857599 52428799+ ee GPT # fdisk -lu /dev/sda Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: dosDisk identifier: 0x00000000 Device Boot Start End Blocks Id System /dev/sda1 1 104857599 52428799+ ee GPT # parted -s /dev/sda unit s print Model: VMware Virtual disk (scsi) Disk /dev/sda: 125829120s Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 2048s 411647s 409600s fat16 EFI System Partition boot 2 411648s 2508799s 2097152s xfs 3 2508800s 104855551s 102346752s lvm
三、试错与分析
在初步排查中,发现`fdisk`的版本较旧(`util-linux 2.23.2`)。通常情况下,较新的工具版本会修复旧版本中的一些已知问题。因此,首先想到的是通过升级`util-linux`来解决问题,期望新版本能够正确处理分区显示问题。
# rpm -Uvh util-linux-2.23.2-65.el7_9.1.x86_64.rpm --force --nodeps warning: util-linux-2.23.2-65.el7_9.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ################################# [100%] Updating / installing... 1:util-linux-2.23.2-65.el7_9.1 ################################# [ 50%] Cleaning up / removing... 2:util-linux-2.23.2-59.el7 ################################# [100%]
但是很遗憾,升级后`fdisk`的显示仍然异常。
# fdisk -l Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: dosDisk identifier: 0x00000000 Device Boot Start End Blocks Id System /dev/sda1 1 104857599 52428799+ ee GPT
1.查看扇区
2.备份mbr
dd if=/dev/sda of=sda_mbr.data bs=512 count=1
3.清空mbr
dd if=/dev/zero of=/dev/sda bs=512 count=1
# dd if=/dev/zero of=/dev/sda bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000450755 s, 1.1 MB/s
然而,不幸的是,再次分区表丢失。
# fdisk -l /dev/sda Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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 # parted /dev/sda print Error: /dev/sda: unrecognised disk label Model: VMware Virtual disk (scsi) Disk /dev/sda: 64.4GB Sector size (logical/physical): 512B/512B Partition Table: unknown Disk Flags:
四、故障分析
如果你已经有了答案,不妨一起来看看我的分析。
五、故障模拟
为了验证这一假设,我模拟了在GPT环境下扩展分区的情况,果然复现了相同的故障。
##扩容sda之后##
六、故障原因
至此,故障原因也就很清晰了。在创建GUID分区表(GPT)时,系统会同时创建一个MBR(msdos)分区表,其中包含一个覆盖整个磁盘或高达2TB的0xEE(GPT)分区,称为MBR保护分区。这个分区用于提醒旧命令和实用程序磁盘确实已分区。
正常gpt分区 通过hexdump查看,200是主分区开始,033ffe00 是secondary (backup) 分区。