跳转至

parted 恢复误删除的分区


2013-07-01 by dongnan

问题描述

parted 环境,原本想删除第4个分区,结果误操作删除第3个分区,杯具了:

(parted)
rm

Partition number? 3 # 这里
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your
changes until after reboot.
(parted) print
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
1      1049kB  211MB   210MB   primary   ext4            boot
2      211MB   2358MB  2147MB  primary   linux-swap(v1)

(parted) quit
Information: You may need to update /etc/fstab.

检查发现分区表已经更改了:

fdisk /dev/sda

#...省略
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           2         201      204800   83  Linux
/dev/sda2             202        2249     2097152   82  Linux swap / Solaris

尝试恢复

不过重启之前,分区表仍然可用,再次进入parted, 发现 rescue 参数,尝试回复分区:

parted /dev/sda

GNU Parted 2.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
#...省略
(parted) help
rescue START END                 rescue a lost partition near START and END
resize NUMBER START END          resize partition NUMBER and its file system
#...省略

查看分区表,注意 free 参数:

(parted) print free
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system     Flags
32.3kB  1049kB  1016kB            Free Space
1      1049kB  211MB   210MB   primary   ext4            boot
2      211MB   2358MB  2147MB  primary   linux-swap(v1)
2358MB  21.5GB  19.1GB            Free Space

尝试恢复,提示发现一个ext4 分区,但是我删除的是 lvm 卷:

(parted) rescue 2358MB 21.5GB
Information: A ext4 primary partition was found at 2359MB -> 21.5GB.  Do you want to add it to the partition table?
Yes/No/Cancel? no

为什么找到的是ext4分区? 猜测是lvm 卷格式化ext4分区有关,如果恢复的话可能无法引导,所以采用保守的方法。

分区表中删除了某个删除了某个分区信息,但是数据都还在没有丢,重建删除的分区信息,这样分区信息将重新添加到分区表中。

建立分区:

更改分区类型:

回到页面顶部