19

I used dd to backup a 80GB drive

dd if=/dev/sdb of=~/sdb.img

Now I need to access some files on that drive, but I don't want to copy the ".img" back over the drive.

mount ~/sdb.img /mnt/sdb doesn't work either. It returns :

mount: you must specify the filesystem type

I tried to find the filesystem type with file -s

fox@shoebox $ file -s sdb.img
sdb.img: x86 boot sector; partition 1: ID=0x12, starthead 1, startsector 63, 10233342 sectors; partition 2: ID=0xc, active, starthead 0, startsector 10233405, 72517410 sectors; partition 3: ID=0xc, starthead 0, startsector 82750815, 73545570 sectors, code offset 0xc0

Is it possible to mount sdb.img, or must I use dd to restore the drive?

27

When you use dd on /dev/sdb instead of /dev/sdb1 or /dev/sdb2, you copy all the partitions from the said drive into one file.

You must mount each partition separately.


To mount a partition from a file, you must first find out where in the file that partition resides.

Using your output from file -s sdb.img we find the startsectors for each partition:

sdb.img: x86 boot sector; partition 1: ID=0x12, starthead 1, startsector 63, 10233342 sectors; partition 2: ID=0xc, active, starthead 0, startsector 10233405, 72517410 sectors; partition 3: ID=0xc, starthead 0, startsector 82750815, 73545570 sectors, code offset 0xc0

Partition     Startsector
1                   63
2                   10233405
3                   82750815

To mount a single partition, where X is the startsector of that partition, run:

mount ~/sdb.img /mnt/sdb -o offset=$((X*512))

So to mount the second partition, you will have to run:

mount ~/sdb.img /mnt/sdb2 -o offset=$((10233405*512))

sidenote: make sure that /mnt/sdb2 exists before you run this.

Have fun!


update: In the answer, I assumed that the sector size for you image was 512, please see this question on how to calculate that.

  • You can also get the startsectors in a tabular format using file -l sdb.img as posted in the answer of your question: unix.stackexchange.com/questions/2668/… – student Aug 19 '12 at 10:33
  • @student: Only a correction for future readers: That would be fdisk -l not file -l. – Runium Apr 27 '13 at 22:29
  • Further clarification - you can get the number of 512 byte sectors by adding the -u option to that command - so fdisk -u -l sdb.img – Aaron Mason Apr 10 '14 at 6:54
  • If your version of file is less talkative by default, try adding the -k switch to get the kind of output shown in the answer – like this: file -k FILENAME.img. – Christian Pietsch Jul 22 '14 at 11:20
7

On Linux mount commands within the accepted answer will implicitly create a loop device. Assuming the the full disk image has a correct partition table (which your kernel is able to understand), you may prefer to use losetup(8) directly to save you the hassle of calculating offsets. Also there is no need for extra tools like kpartx as suggest in an other answer. It will do basically the same thing as the following:

losetup -Prf sdb.img will associate the first free loop device (-f) as readonly (-r) with the image file sdb.img. The -P option forces the kernel to scan the partition table of this loop device and will create loop devices for each partition.

So you may get something like:

$ lsblk
NAME      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0       7:0    0 167.7G  1 loop
├─loop0p1 259:0    0   500M  1 loop
└─loop0p2 259:1    0 167.2G  1 loop
...

You can now mount each partition, readonly of course, at a desired mountpoint.

$ mount -r /dev/loop0p1 /tmp/backup_sdb1

Keeping everything readonly is optional, but may be a good thing to do for a backup image.

update: -P option was added with util-linux-2.21 in 2012. So it was not available at the time the accepted answer was written. Also note this and the other answers are specific to linux. OpenBSD and NetBSD have vnode disks managed by vnconfig(8) and FreeBSD has memory disks managed by mdconfig(8).

2

Alternatively, you can use a nice tool called kpartx. From the man page:

kpartx - Create device maps from partition tables

This means that you can make a "pseudo device", with device partitions, right from the img file:

$ kpartx -av sdb.img
add map loop0p1 (254:2): 0 2048 linear /dev/loop0 0
add map loop0p2 (254:3): 0 31162 linear /dev/loop0 2048
$ lsblk
.
.
.
loop0                     7:0    0  16.2M  0 loop
├─loop0p1               254:2    0     1M  0 part
└─loop0p2               254:3    0  15.2M  0 part

Then you can mount /dev/mapper/loop0p2 for example, if you want the second partition on the image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.