Extracting files from a damaged disk image: ddrescue, losetup and a little magic

Tr0jan_Horse

Moderator
Staff member
MODERATOR
ULTIMATE
PREMIUM
MEMBER
Joined
Oct 23, 2024
Messages
304
Reaction score
8,796
Deposit
0$
The problem of damaged hard drives is familiar to many: the drive starts to malfunction, the system freezes, files suddenly disappear. In such situations, conventional recovery methods can only aggravate the problem, causing data to be overwritten.

The best solution is to create a disk image and work with it, not the original.

In this article we will look at how:

  • copy the entire disk to an image file;
  • mount the damaged partition and extract files;
  • Recover deleted files or damaged NTFS file system.
First, let's look at the basic steps, and then at the difficulties that may arise in the process and ways to solve them.

4adbbb2b77dc8e43e9b10a54cde93c57.png

Step 1. Create a disk image with the ddrescue utility​

If a disk is "dying", the first thing to do is create an image of it. Trying to work with the disk directly can only make the situation worse.

The dd utility is usually recommended for creating a disk copy. However, it works on the "all or nothing" principle: if the disk contains damaged sectors, dd may freeze.

The ddrescue utility is more efficient. It:

  • skips broken blocks and continues copying;
  • creates a damage map (mapfile), allowing the process to continue in the event of failures;
  • allows you to combine copies of disks, for example from two different attempts.
We will use ddrescue specifically.

Creating an image​

First, let's define the disk name ( /dev/sdX):

Bash:
lsblk
Now let's create an image:

Bash:
sudo ddrescue -d /dev/sdX image.img mapfile
Command parameters:

  • /dev/sdX- original disk;
  • image.img— the file where the image will be saved;
  • mapfile— file — a control map of bad sectors (allows you to restart the image creation process without starting over).

How do I know how much data I was able to recover?​

While running, ddrescue shows:

  • number of read and skipped sectors;
  • copy speed;
  • how many times did you try to reread the damaged blocks?

Step 2. Analyze the image contents​

Now we have an image image.img, but what's inside?

We define the layout and partition table:

~ $ sudo parted image.img unit B print
The output may look like this:
Model: (file)
Disk image.img: 500107862016B
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 1048576B 2097151B 1048576B fat32
2 2097152B 105906175B 103809024B ext4
3 105906176B 500107861375B 499001955200B ntfs

The following can be seen from the output:

  • Partition Table: gpt (GPT layout).
  • The third partition is sda3 (NTFS), that's what we need.
  • Section starts at 105906176B.

Let's look at the file system type.

  • If we are recovering files from a Windows partition, we are looking for NTFS .
  • If we are working with a Linux partition, we are interested in ext4, ext3, xfs, etc.
  • If we are looking for a boot partition, it is often FAT32 (for example, an EFI partition).

Alternative methods of image analysis:

~ $ fdisk -l image.img
~ $ sfdisk -d image.img

Step 3. Connect and mount the image​

Now we need to access the desired partition. To do this, we will create a disk emulator in Linux that will open access to all partitions.

Using losetup​

Create a loop device to work with the image. This is a virtual device that allows you to work with the disk image as if it were a regular physical disk. To create it, use the command losetup:
~ $ sudo losetup -Pf --show image.img
Conclusion:

/dev/loop0
Now the sections are displayed as /dev/loop0p1, /dev/loop0p2, /dev/loop0p3.

The losetup utility is part of the util-linux package. Key -P, which appeared in version 2.21, allows you to automatically scan and add partitions when a loop device is connected.

To check the version use the command:

~ $ losetup --version
losetup from util-linux 2.37.2
If you have an old losetup, without -P, can be used kpartx:

~ $ sudo kpartx -a image.img
~ $ ls /dev/mapper/

Mounting the desired partition: sda3, NTFS​

Now let's create a mount point:

~ $ sudo mkdir -p /mnt/recovery
Mountable section /dev/loop0p3:
~ $ sudo mount -o ro,uid=$(id -u),gid=$(id -g) /dev/loop0p3 /mnt/recovery
Flags uid And gidallow not only the root user, but also the regular user to work with files.

If the file system has been damaged, mounting may fail. Then it is worth using other methods, which are described further in the article.

Step 4. Extract files​

The files are now available in /mnt/recovery. You can copy them:

~ $ cp -r /mnt/recovery/имя_папки ~/backup/
If you need to copy while preserving the structure and rights:

~ $ rsync -av /mnt/recovery/ ~/backup/
For archiving:
~ $ tar czvf backup.tar.gz -C /mnt/recovery

Step 5. Finishing the job​

After copying the data, you need to disable the loop device:

~ $ sudo umount /mnt/recovery
~ $ sudo losetup -d /dev/loop0

What to do with the error device is busy​

Use fuserto find processes that are holding a mount:

~ $ sudo fuser -vm /mnt/recovery
Then terminate them with the command:

kill -9 PID
That's it for the basic steps. Let's move on to the difficulties you may encounter in the process and options for solving them.

What to do if NTFS partition is not mounted​

instead If the NTFS partition is damaged, the standard driver may refuse to mount. Let's use ntfs-3g .

Force NTFS Mount​

If the standard command mount -t ntfsdidn't work, try disabling the standard driver and explicitly specify ntfs-3g:

~ $ sudo mount -t ntfs-3g /dev/loop0p3 /mnt/recovery

File system recovery with ntfsfix​

If you receive the error "NTFS is inconsistent", "$MFTMirr does not match $MFT" or "The disk contains an unclean file system", proceed to the repair. You can fix a damaged NTFS partition using ntfsfix.

Try running the command:

~ $ sudo ntfsfix /dev/loop0p3
What does ntfsfix do?

  • Clears the dirty bit that may prevent mounting.
  • Fixes major file system errors.
  • Sets the check flag chkdskso that Windows can correctly restore the partition the next time it boots.
ntfsfixdoes not replace a full-fledged one chkdskin Windows, but in 80% of cases it allows you to mount the partition.

If nothing helps, use chkdsk in Windows​

If even ntfsfix does not solve the problem, it is better to boot into Windows and run chkdsk:

~ $ chkdsk /f /r D:
Here D:- this is the letter of the damaged partition, yours may be different.

Recovering Deleted Files with TestDisk and PhotoRec​

If a situation arises where files have been deleted, you should remount the disk in read-only mode (RO). This will prevent data from being overwritten. If this is a system disk, it is recommended to boot from a flash drive and also mount it in RO.

In this case, the disk will not be overwritten with new data, and you can recover deleted data using TestDisk or PhotoRec . Both utilities are free and work with various file systems.

Recovering file structure with TestDisk​

The TestDisk utility is worth using:

  • if the partition has become “unreadable”, but the file structure remains;
  • if a disk or flash drive requires formatting after a failure, but the files still exist.
Let's look at the step-by-step actions for file recovery.

1. Install TestDisk if it is not there:

~ $ sudo apt install testdisk # Для Debian/Ubuntu.
~ $ sudo yum install testdisk # Для CentOS.
2. Run TestDisk:
~ $ sudo testdisk image.img
How can I recover files now?

  1. Select disk ( image.img ) → Proceed .
  2. Select Intel (MBR) or EFI/GPT .
  3. Select Analyze , press p to see the files.
    • If the files are visible, select Write to repair the damaged file table.
    • If they are not visible, use the PhotoRec utility.
  4. Reboot your system and try mounting the partition again.

Recovering Individual Files with PhotoRec​

The PhotoRec utility is worth using:

  • if files are deleted and not visible in TestDisk;
  • if the file system is damaged and files are needed by signatures (file types).
The steps are not much different from those described above.

  1. Install PhotoRec - it comes with TestDisk.
  2. Run the utility:
~ $ sudo photorec image.img
How to recover files?

  1. Select disk.
  2. Specify which partition to search for files from.
  3. Select which file types to recover ( JPG, DOC, PDF ).
  4. Specify the folder to save the data.
  5. Start the search.
The recovered files will appear in the specified directory.

Using ntfs-3g when working with NTFS​

If an NTFS partition is damaged (for example, due to a disk failure or improper shutdown), the standard NTFS driver in the Linux kernel (ntfs) may not mount it at all, or may mount it in read-only mode with errors.

However, ntfs-3g is more flexible and can:

  • force mount of "dirty" NTFS partitions;
  • work with damaged MFT (Master File Table);
  • Fix file system errors with ntfsfix.
Here's how to install this driver and get full NTFS support:

~ $ sudo apt install ntfs-3g # Debian/Ubuntu
~ $ sudo yum install ntfs-3g # CentOS
~ $ sudo pacman -S ntfs-3g # Arch

Alternative emulation methods: qemu-nbd​

If losetup doesn't work, you can emulate a physical disk using qemu-nbd. This is a utility that uses network block devices (NBD) to work with disk images:

~ $ sudo modprobe nbd
~ $ sudo qemu-nbd --connect=/dev/nbd0 image.img
~ $ lsblk
~ $ sudo mount -o ro /dev/nbd0p3 /mnt/recovery
Disabling:

~ $ sudo qemu-nbd --disconnect /dev/nbd0

Conclusion​

Key findings:

  • Do not work directly with a damaged disc.
  • Create an image before attempting to recover data.
  • Use losetup, TestDisk, PhotoRec to recover.
 
Top Bottom