3

This question already has an answer here:

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?

marked as duplicate by G-Man, Gilles, roaima, Community Sep 18 '15 at 22:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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, 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
10

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

  • Very helpful indeed! If you are trying to mount an lvm partition, you should follow these directions, 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 4 '17 at 0: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).

  • 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
  • 1
    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

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