undo rm in parted - recover parition? cancel operations?

by dan   Last Updated August 10, 2018 02:00 AM

I accidentally removed a partition on my primary partition. I thought I was on a removable drive. I clearly should have been paying more attention... I didn't "save" or "exit" parted, but it looks like the partition is gone! Is there any way to undo this? Does parted ask me to save when I exit and I can just say no?

[[email protected] ~]# parted 


^C


GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)                                                                  
(parted)                                                                  
(parted)
(parted) parted /dev/sdf                                                  
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program               
  rescue START END                         rescue a lost partition near START and END
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program               
  rescue START END                         rescue a lost partition near START and END
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted)         
(parted) print                                                            
Model: ATA KINGSTON SMS200S (scsi)         
Disk /dev/sda: 60.0GB
Sector size (logical/physical): 512B/512B  
Partition Table: msdos                     
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  525MB   524MB   primary  xfs          boot
 2      525MB   60.0GB  59.5GB  primary    

(parted) rm 2                                                             
Error: Partition(s) 2 on /dev/sda have been written, but we have been unable to inform the kernel of the change, probably because it/they are in use.  As a result, the old partition(s) will remain in use.  You should reboot now before     
making further changes.                    
Ignore/Cancel? cancel                                                     
(parted) print              
Model: ATA KINGSTON SMS200S (scsi)
Disk /dev/sda: 60.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  xfs          boot

(parted) testdisk
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) rescue 525MB 60.0GB
(parted) print
Model: ATA KINGSTON SMS200S (scsi)
Disk /dev/sda: 60.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  xfs          boot

(parted) rescue
Start? 525MB
End? 60GB
(parted) print
Model: ATA KINGSTON SMS200S (scsi)
Disk /dev/sda: 60.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  xfs          boot

Per the suggestion below, I ran testdisk. However, due to it being an encrypted partition, testdisk didn't find the end of the partition properly. I now see this:

[[email protected] ~]# testdisk 
TestDisk 7.0, Data Recovery Utility, April 2015
Christophe GRENIER <[email protected]>
http://www.cgsecurity.org
You have to reboot for the change to take effect.
You have new mail in /var/spool/mail/root
[[email protected] ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: ATA KINGSTON SMS200S (scsi)
Disk /dev/sda: 60.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End    Size    Type     File system  Flags
 1      1049kB  525MB  524MB   primary  xfs          boot
 2      525MB   527MB  2097kB  primary

(parted) quit


Answers 1


From the manual:

It is only after Parted exits that the Linux kernel knows about the changes Parted has made to the disks. However, the changes caused by typing your commands will probably be made to the disk immediately after typing a command. However, the operating system’s cache and the disk’s hardware cache may delay this.

You can exit and then attempt to recover the partition; given the information you have above, this should be very straightforward.

If parted is unable to recover the partition, I would suggest using testdisk: http://www.cgsecurity.org/wiki/TestDisk

There is a fairly comprehensive tutorial for testdisk here: http://www.dedoimedo.com/computers/linux-data-recovery.html

Joshua Griffiths
Joshua Griffiths
July 06, 2016 10:44 AM

Related Questions


raspi-config like disk expansion script

Updated July 11, 2017 10:00 AM

Partitions on Linux and their CHS dependance

Updated January 25, 2017 14:00 PM


Is there any way to fix a corrupted LDM database?

Updated April 03, 2015 00:00 AM

Resizing a LVM Partition Using Parted in a Script

Updated April 17, 2017 23:00 PM