4

Note: While I agree that this question basically is a duplicate of the above question, I feel @alienth's answer (below) is more concise, so I suggest you take a look at it before going to the other question.

I periodically backup/image/clone my entire ubuntu system drive to another drive with:

dd if=/dev/sda of=/media/disk1/backup.iso

It works great when I need to restore the drive after an experiment, drive failure, etc.

However I'd now like to mount a partition from within that .iso (i.e. what would have been /dev/sda1 when I was dd'ing the drive). If I'd backed up with:

dd if=/dev/sda1 of=/media/disk1/backup.iso

then the .iso would be easily mountable using ubuntu's mount volume utility. But the utility doesn't work for an iso of the entire drive. Is there a way to just mount sda1 from the original iso?

Fred Hamilton
  • 445
  • 2
  • 5
  • 8
  • https://www.google.com/webhp?q=mount%20partition%20of%20disk%20image => http://madduck.net/blog/2006.10.20:loop-mounting-partitions-from-a-disk-image/ – Mingye Wang Sep 18 '15 at 21:14
  • One notable issue with that blog post is that it uses `fdisk`, which may not work correctly with a GPT partition table (which is the standard on many recent distro releases). – alienth Sep 18 '15 at 21:39
  • You're aware that although you've used the suffix `.iso`, your image isn't actually an [ISO image](https://en.wikipedia.org/wiki/ISO_9660), it's a raw disk image? – roaima Sep 18 '15 at 22:26
  • I knew that I was creating a raw disk image, and now that I think about it I do remember someone explaining that an iso is some sort of concatenation of files, so I guess I *should* have been aware. I am now, anyway - thanks. – Fred Hamilton Sep 18 '15 at 22:42
  • See the answers to this question: https://askubuntu.com/questions/69363/mount-single-partition-from-image-of-entire-disk-device – erik May 05 '20 at 22:56

2 Answers2

11

You'll need to determine where in the disk image your partition starts. To do so, run the following:

sudo parted /media/disk1/backup.iso unit s print

The output will look like the following:

Model:  (file)
Disk /tmp/file: 200000s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start  End      Size     Type     File system  Flags
 1      2048s  199999s  197952s  primary

You need to take the logical sector size and multiply that by the Start of the partition you'd like to mount. In this case, if I want to mount the first partition, the position would be 2048 * 512, or 1048576.

You can then mount the partition using a loopback setup, plugging the value determined from above into the offset parameter.

mount -o loop,offset=1048576 /media/disk1/backup.iso /mnt/mydisk

alienth
  • 2,187
  • 12
  • 20
  • Very helpful indeed! If you are trying to mount an lvm partition, you should follow [these directions](http://www.utilities-online.info/articles/Mount-an-LVM-partition-image-cloned-with-dd/#.Wf0MzXBrxGo), but you will need to use the offset as shown in this answer. So instead of `losetup /dev/loop0 backup-cloud.img`, you need `losetup -o XXX` where `XXX` is the offset computed as shown above (start * 512, assuming logical/physical is 512). – svenevs Nov 04 '17 at 00:49
1

There is a way to see a partition table within a full-disk image. Two, in fact.

The first way:

# losetup /dev/loop0 /media/disk1/backup.iso
# fdisk /dev/loop0 # Be very careful with this!
Command: w

This should, ideally, create loop devices /dev/loop0p1, /dev/loop0p2 and so forth for the partitions in backup.iso. You can then mount these normally as loop devices; it's probably prudent to do it read-only, so as not to corrupt your backup. There are likely safer ways to trigger the partition read than using fdisk directly, such as partprobe or kpartx; however, I've had reliability issues with those, and fdisk is the best way I've found.

The second way is more difficult. It involves reading the partition start manually (fdisk -l /dev/loop0), then redoing losetup with an appropriate --offset option to make the loop device begin at the partition start. Precisely what number you pass depends on the output from fdisk, but it's an offset in bytes, equal to the starting block times the block size. At that point, you can mount /dev/loop0 directly (again, read-only is prudent).

Tom Hunt
  • 9,808
  • 4
  • 25
  • 43
  • This assumes that his disk image isn't in GPT format. `parted` would handle it either way, so is likely preferable. – alienth Sep 18 '15 at 21:18
  • Recent `fdisk` can handle GPT, I thought? Don't know if it's part of the default `-l` output, though. If you're doing the manual-offset method, you're likely correct. – Tom Hunt Sep 18 '15 at 21:19
  • I'm on a recent Mint release and `fdisk` bombs with a warning about GPT and spits out an incorrect partition layout. Using version 2.20.1. – alienth Sep 18 '15 at 21:22
  • Also, curiously I can't get `loop0p1` to show up in a test case by simply writing out the partition table with `fdisk` (with a standard DOS-type table), despite it being listed when printed out with `fdisk`. I do know that used to work. `partprobe` doesn't fetch it, either. – alienth Sep 18 '15 at 21:24
  • Huh. Historically I've had intermittent success with `partprobe` and reliable success with `fdisk`. `kpartx`, maybe? – Tom Hunt Sep 18 '15 at 21:29
  • Same result. Very weird! `kpartx -av` does spit out the `loop0p1` partition, but a device mapping isn't created. This is on Mint 17.1 (aka Ubuntu 14.04). I wonder what changed to make this not work? – alienth Sep 18 '15 at 21:32
  • I've tested mostly on eccentric custom systems, but they've certainly had modern kernels and userlands. Ubuntu being odd, maybe? – Tom Hunt Sep 18 '15 at 21:33
  • @Tom Hunt: `gdisk` is for GPT things; `fdisk` only got GPT support in Util-linux 2.24, which may not have tricked down yet (it's not in centos7). – thrig Sep 18 '15 at 21:42
  • 2
    You can use the `-P` option to requst a partition scan of the created loop device - this is what sets up the partition device nodes. I often use it like this: `dev=$(sudo losetup -P -f --show disk.img)` and then I can do `sudo mount ${dev}p2 /mnt`, cleaning up afterwards with `sudo losetup -d $dev`. – starfry Dec 20 '16 at 12:32