gpart如何恢复Linux磁盘分区表(特别声明-lvm卷还需研究下)

副标题:分区表丢失如何恢复

工具下载及原理

    1、Gpart是一个小型工具,可以尝试猜测PC类型(MBR分区的硬盘)上的分区是什么,以防主分区表损坏。
    2、Gpart通过按扇区扫描命令行上提供的设备(或文件)来工作。询问每个猜测模块是否认为它知道的文件系统可以从给定的扇区开始。内置了几个文件系统猜测的函数模块。
    3、gpart本身无法重建损坏的逻辑分区链。
    4、官方说 gpart 说是支持lvm分区,但是我还不知道如何使用,本教程不适用于lvm卷
    5、这里是官方的github:https://github.com/baruch/gpart
    6、这里提供了各大发行版本的 二进制包,可以根据需求下载 :https://pkgs.org/download/gpart

注意了解点:
    1、ubuntu18.04   fdisk 分区,默认起始扇区是2048
    2、centos7   fdisk 分区,默认起始扇区是2048
    3、gpart 可以恢复 mbr以及gpt磁盘分区类型的 ext4,xfs文件系统

该方式适用于什么情况

    1、原本的分区表被意外删除或丢失,但是没有新数据的写入
    2、或者当你分区表丢失后,新创建了分区表,但是没有格式化,或者没有删除磁盘上的leable信息)

工具安装方式介绍

    1、方式一:根据各自的系统版本 去 https://pkgs.org/download/gpart 下载对应的二进制版本进行安装
    2、方式二:若是ubuntu及debian系列可以直接 apt install gpart 进行安装
    3、特别注意:centos7目前好像不支持这个软件,也没看到那儿有二进制的版本供centos7使用

第一:验证测试前数据分区正常:

将vdb磁盘写创建一个分区,并创建一个ext4的文件系统,然后将里面写入一行数据,并验证是否写入成功

#A、数据磁盘vdb 分区介绍 mbr分区格式的ext4文件系统,注意分区的起始和接收扇区
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xb2513bf9
Device     Boot Start      End  Sectors  Size Id Type
/dev/vdb1        2201 10971519 10969319  5.2G 83 Linux

#B、查看挂载情况,证明是ext4文件系统
root@VM-0-7-ubuntu:~# df -Th
Filesystem         Type      Size  Used Avail Use% Mounted on
udev               devtmpfs  461M     0  461M   0% /dev
tmpfs              tmpfs      99M  5.8M   93M   6% /run
/dev/vda1          ext4       50G  2.3G   45G   5% /
tmpfs              tmpfs     493M   24K  493M   1% /dev/shm
tmpfs              tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs              tmpfs     493M     0  493M   0% /sys/fs/cgroup
tmpfs              tmpfs      99M     0   99M   0% /run/user/0
/dev/vdb1          ext4      5.1G   21M  4.8G   1% /home/mbr-ext4

#C、挂载好了,往里面写一些文件
root@VM-0-7-ubuntu:~# echo "this is mbr-ext4" > /home/mbr-ext4/1.txt

#D、证明已经写进去了
root@VM-0-7-ubuntu:~# cat /home/mbr-ext4/1.txt 
this is mbr-ext4

第二:搞破坏:卸载文件系统,或者不卸载直接删除vdb的分区表,直接使用fdisk 删除分区表

第三:恢复操作并验证是否恢复成功 gpart -W /dev/vdb /dev/vdb

#A、扫描vdb 并将扫描到的分区表写入vdb ,对于这个扫描过程是比较漫长的,我扫描一个10G的ext4磁盘花费6分钟
root@VM-0-7-ubuntu:~# gpart -W /dev/vdb /dev/vdb
Begin scan...
Possible partition(Linux ext2), size(5356mb), offset(1mb)

End scan.
Checking partitions...
Partition(Linux ext2 filesystem): primary 
Ok.

Guessed primary partition table:
Primary partition(1)
   type: 131(0x83)(Linux ext2 filesystem)
   size: 5356mb #s(10969312) s(2201-10971512)
   chs:  (2/2/60)-(1023/15/63)d (2/2/60)-(10884/6/63)r

Primary partition(2)
   type: 000(0x00)(unused)
   size: 0mb #s(0) s(0-0)
   chs:  (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)r

Primary partition(3)
   type: 000(0x00)(unused)
   size: 0mb #s(0) s(0-0)
   chs:  (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)r

Primary partition(4)
   type: 000(0x00)(unused)
   size: 0mb #s(0) s(0-0)
   chs:  (0/0/0)-(0/0/0)d (0/0/0)-(0/0/0)r

Edit this table (y,n) : y                           #这里输入y
Activate which partition (1..4, q to quit) : 1      #指定一个分区序号
Write this partition table (y,n) : y                #写入这个分区表

* Warning: partition table written, you should reboot now.

#B、再次查看分区表(看上去正常,是恢复的,起始都正确)
root@VM-0-7-ubuntu:~# fdisk -l /dev/vdb
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xb2513bf9

Device     Boot Start      End  Sectors  Size Id Type
/dev/vdb1  *     2201 10971512 10969312  5.2G 83 Linux

#C、挂载看看数据(OK恢复成功,mbr-ext4没问题)
root@VM-0-7-ubuntu:~# mount /dev/vdb1 /home/mbr-ext4/
root@VM-0-7-ubuntu:~# cat /home/mbr-ext4/1.txt 
this is mbr-ext4
声明:本文为原创,作者为 辣条①号,转载时请保留本声明及附带文章链接:https://boke.wsfnk.com/archives/392.html
谢谢你请我吃辣条谢谢你请我吃辣条

如果文章对你有帮助,欢迎点击上方按钮打赏作者

最后编辑于:2022/12/3作者: 辣条①号

现在在做什么? 接下来打算做什么? 你的目标什么? 期限还有多少? 进度如何? 不负遇见,不谈亏欠!

暂无评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

arrow grin ! ? cool roll eek evil razz mrgreen smile oops lol mad twisted wink idea cry shock neutral sad ???

文章目录