Mounting dd Images of DisksR

October 5, 2006 on 10:27 pm | In Uncategorized |

The tool dd is one of the most useful disk manipulation tools available. It’s standard on almost every *nix distribution out there, and is widely considered one of the essential disk manipulation tools. It can easily read from a disk, partition, or other source and write it out to any of the above, and more.

In this brief article I’ll cover a few steps I used to make a backup of a compact flash disk with a proprietary OS and mount the partitions on the disk image.

Most of the information on this post comes from the OsFaqWiki Article on Disk Images. The important distinction, however, is that most of the articles on the subject start with an empty disk. In this case, I want to make a backup of an existing disk.

In this case, the disk is a SanDisk ultra II 1.0 GB Compact Flash. I have a nice little USB-to-CF adapter which allows me to access the card directly, in this case through VMWare and my Ubuntu 6.06 installation:

22:00:49 root:disk# fdisk -u /dev/sdb
Command (m for help): p
Disk /dev/sdb: 1024 MB, 1024966656 bytes
32 heads, 62 sectors/track, 1009 cylinders, total 2001888 sectors
Units = sectors of 1 * 512 = 512 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *          62     2001855     1000897   83  Linux

To start things off, I need to make an image of this disk as a local file on my harddrive. I’m doing this for versioning purposes, but of course this could just as easily be used to make disaster recovery, disk duplication or backup, or any other of a variety of reasons. Using the ‘dd’ tool, I read directly from the disk device:

22:05:21 root:disk# dd if=/dev/sdb of=mycf.img

The result is the ‘mycf.img’ file, one gigabyte big, that contains a direct copy of everything on the compact flash disk. But since this is an image of the disk, and not an image of the partition, we can’t directly mount it with ‘mount’. Instead, we need to calculate the offset of the partition and mount that, instead. Take a look above at the output from fdisk, and at the command line for fdisk. On the command line, you’ll note the ‘-u’ parameter. This tells fdisk to display sizes in sectors, instead of cylinders. This is important: we need to take the number of sectors the first partition starts at, in this case 62, and multiply it by the number of bytes in a sector, 512, to get the number of bytes the offset is. The result is 31744 bytes.

Armed with this value, we now want to mount the partition itself. Assuming the existence of the ‘losetup’ binary:

losetup -o31744 /dev/loop0 mycf.img
mount -r -t ext2 /dev/loop0 /media/loop

We start by using the losetup tool to map the first loopback device, /dev/loop0, to the mycf.img file starting at offset 31744. Then, using a very typical mount command, we mount the partition to /media/loop.

It’s actually possible to combine all this into a single command line:

mount mycnf.img /media/loop -r -o loop=/dev/loop3,offset=31744

The options specified by -o get passed verbatim to the losetup binary, and achieve the same result.

Enjoy!

No Comments yet

Sorry, the comment form is closed at this time.

Entries and comments feeds. ^Top^
17 queries. 0.196 seconds.