Partition recovery feature request

How to use TestDisk to recover lost partition
Forum rules
When asking for technical support:
- Search for posts on the same topic before posting a new question.
- Give clear, specific information in the title of your post.
- Include as many details as you can, MOST POSTS WILL GET ONLY ONE OR TWO ANSWERS.
- Post a follow up with a "Thank you" or "This worked!"
- When you learn something, use that knowledge to HELP ANOTHER USER LATER.
Before posting, please read https://www.cgsecurity.org/testdisk.pdf
Locked
Message
Author
Cova
Posts: 3
Joined: 14 Aug 2014, 06:29

Partition recovery feature request

#1 Post by Cova »

After accidentally screwing up my drive using gparted to move and resize a few partitions and LVM2 logical volumes I had to resort to testdisk to find the ext4 filesystem that had been my /home directory to recover some data from it. In this case I did not want to have testdisk re-write the partition table to disk as the partition table was still correct and I believe in the phrase "if its not broke don't fix it" - my problem was that all of my LVM metadata was gone or corrupt and I did not see any options in testdisk to restore or rebuild that. It did find the ext4 superblocks and my missing data though and I was able to browse my files from the testdisk interface just fine.

The next problem came when I tried to use testdisk to copy my files to a network location to prepare to flatten and rebuild my LVM volume group. My /home logical volume was/is a 20GB ext4 filesystem full of regular files, symlinks, and hardlinks. I selected the root folder for my user and asked testdisk to copy it and everything appeared to be working properly. However when I checked on it a short time later I was suprised that it was still running, and that it had copied over 90GB of data so far. I suspect it was not following links correctly and making multiple copies of things.

And so I decided to use the information testdisk had discovered about the location of my filesystem but let the linux kernel and rsync handle copying my data to a safe location. Taking a few snips from the testdisk log file:
Disk /dev/sda - 256 GB / 238 GiB - CHS 31130 255 63
recover_EXT2: s_block_group_nr=0/154, s_mnt_count=59/4294967295, s_blocks_per_group=32768, s_inodes_per_group=8192
recover_EXT2: s_blocksize=4096
recover_EXT2: s_blocks_count 5072896
recover_EXT2: part_size 40583168
Linux 23430 211 14 25957 3 30 40583168
ext4 blocksize=4096 Large file Sparse superblock, 20 GB / 19 GiB
I was able to calculate the LBA of the start sector of my filesystem, and then manually use device mapper to create a block device pointing at it which I could mount normally.
echo "0 40583168 linear /dev/sda 376416256" | dmsetup create test

mount /dev/mapper/test /mnt/test
After which rsync had no problems copying the 12GB of data that were stored there.

Which brings me to my feature request - would it be possible to also include LBA addresses of everything in the testdisk log - having to convert from CHS on a modern system seems unnecessary. An even better bonus would be to include 'dmsetup' commands with all of the parameters supplied to create temporary block devices at the locations of everything found. Perhaps even provide the 'dmsetup' commands to create a snapshot of that device so you can do things like safely run a fsck against the filesystem without modifying any of the blocks on disk.

Edit: It occurred to me later that having the LBA addresses of things would also make it trivially easy to take images of specific parts of the disk using dd or some other tool. To borrow from my example above, I could have taken an image of that filesystem with "dd if=/dev/sda of=/location-with-space/filesystem.img bs=512 skip=376416256 count=40583168".

User avatar
cgrenier
Site Admin
Posts: 5432
Joined: 18 Feb 2012, 15:08
Location: Le Perreux Sur Marne, France
Contact:

Re: Partition recovery feature request

#2 Post by cgrenier »

When I try to use dmsetup to create device, I get

Code: Select all

device-mapper: table: 253:0: linear: dm-linear: Device lookup failed
device-mapper: ioctl: error adding target to table
Any ideas ?

Cova
Posts: 3
Joined: 14 Aug 2014, 06:29

Re: Partition recovery feature request

#3 Post by Cova »

Nothing comes immediately to mind, but I'm happy to help with figuring it out. What was the exact dmsetup command you ran? The syntax is rather odd - in the very simple example I used below the order of the numbers in the echo statement are: Start sector of the new device, size of the new device in sectors, type of target, destination device name, start sector on the destination device. There may also be useful information in the last few lines of dmesg output after trying it.

I also have a suspicion that it will not work against a disk that is already mounted / in use. Every time I've used dmsetup has been from a LiveUSB

User avatar
cgrenier
Site Admin
Posts: 5432
Joined: 18 Feb 2012, 15:08
Location: Le Perreux Sur Marne, France
Contact:

Re: Partition recovery feature request

#4 Post by cgrenier »

The previous message was the tail of dmesg, no very usefull ;-(
It seems to work with disk with no mounted partition. It doesn't work with a dd file.
I am uploading a new 7.0-WIP. If you run it under Linux, you will see in the dmsetup command in the log file.
Can you try it ?

Cova
Posts: 3
Joined: 14 Aug 2014, 06:29

Re: Partition recovery feature request

#5 Post by Cova »

Ok that makes sense. Device mapper can't work directly with a dd file like that as it needs a block device as input. You can however mount an image as a loopback device and then use that with other dmsetup commands.

I should be able to find some time tomorrow to play with the 7.0-WIP version.

Locked