Skip to main content

Creating and Mounting Drive Images with `dd` in Linux

In Linux, one of the most powerful tools for creating an exact replica of a drive or partition is dd. When it comes to backup or migration scenarios, having an image of your drive can be invaluable. In this article, we'll guide you through creating an image of a USB drive using dd, and then mounting that image to access its contents, ensuring it's a true clone.

1. Creating an Image with dd

First, ensure the USB drive is properly recognized. We'll assume the drive is /dev/sdb based on our previous interactions.

Step 1: Navigate to the directory where you want to save the image.

cd /path/to/storage/location/

Step 2: Use dd to create an image of the USB drive.

sudo dd if=/dev/sdb of=usb_drive.img bs=4M status=progress

Explanation:

  • if=/dev/sdb: This specifies the input file, which in this case is our USB drive.
  • of=usb_drive.img: This is our output file or the name of the image.
  • bs=4M: Sets the block size to 4 Megabytes to speed up the process.
  • status=progress: Displays a progress bar while the image is being created.

2. Mounting the Drive Image

Once you have an image, you can mount it directly to access its contents.

Step 1: Create a mount point.

sudo mkdir /mnt/usb_image

Step 2: Attach the image to a loop device. This makes the system treat the file like a block device.

sudo losetup -fP usb_drive.img

Find out which loop device got associated with our image:

losetup -a | grep usb_drive.img

Assuming the image is associated with /dev/loop0, we proceed.

Step 3: Now, mount the first partition from the image (for example, the ext4 one).

sudo mount /dev/loop0p1 /mnt/usb_image

You can navigate to /mnt/usb_image and verify that the content matches the original USB drive's content.

Note: If you wish to mount the second partition (NTFS), you'd use /dev/loop0p2.

3. Cleanup:

After you're done inspecting the contents, it's essential to clean up.

Step 1: Unmount the image.

sudo umount /mnt/usb_image

Step 2: Detach the loop device.

sudo losetup -d /dev/loop0

Conclusion

The dd utility provides a straightforward way to create exact replicas of drives in the form of image files. These images can then be mounted and accessed like regular file systems, making it incredibly useful for backup, recovery, and migration scenarios. Always handle dd with care, as it's a powerful tool that can overwrite data if misused.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle