Live data from Hacker News

Mounting partitions from a dd image

assafmo.github.io

21–30 of 37 posts

Re: Mounting partitions from a dd image

#21
I think ZFS has the easiest way to do this sort of thing:

    # fallocate -l 256M disk.bin
    # zpool create filepool /root/disk.bin
    # zfs set mountpoint=/root/filepool filepool
    # df -h /root/filepool
    Filesystem      Size  Used Avail Use% Mounted on
    filepool        208M     0  208M   0% /root/filepool

Re: Mounting partitions from a dd image

#22

This is doing it the hard way. Make the image a loop device: losetup /dev/loop0 /path/to/your/image.img Now you can use /dev/loop0 like it's a normal disk. If you're not seeing your partitions (/dev/loop0p1, etc.): partprobe /dev/loop0 When you're done, detach the loop device: losetup -d /dev/loop0 I use this trick to transparently compress ddrescue images with brtfs: https://brashear.me/blog/2017/11/14/how-to-create…

Thanks, not OP but that looks useful to me. For context, I've used the strategy in OP to debug a small linux system running on an SD card, if I have one version that has a bug and one that doesn't, and I don't know the difference, I can image them both, mount them and diff the whole system (i.e. with meld.) And it can be done with the same card over time. I'd like to figure out how to do this with VM disks as well bu…

I commonly use loopdevs to create and partition bootable disk images for clonezilla. I like that I can create them sparse so that they take up less space.

Re: Mounting partitions from a dd image

#23
post #21

I think ZFS has the easiest way to do this sort of thing: # fallocate -l 256M disk.bin # zpool create filepool /root/disk.bin # zfs set mountpoint=/root/filepool filepool # df -h /root/filepool Filesystem Size Used Avail Use% Mounted on filepool 208M 0 208M 0% /root/filepool

Sure, ZFS has lots of awesome features, but the point of loopback device lets you to attach and mount basically any disk image that has kernel or fuse drivers.

Re: Mounting partitions from a dd image

#24

This is doing it the hard way. Make the image a loop device: losetup /dev/loop0 /path/to/your/image.img Now you can use /dev/loop0 like it's a normal disk. If you're not seeing your partitions (/dev/loop0p1, etc.): partprobe /dev/loop0 When you're done, detach the loop device: losetup -d /dev/loop0 I use this trick to transparently compress ddrescue images with brtfs: https://brashear.me/blog/2017/11/14/how-to-create…

[deleted]

Re: Mounting partitions from a dd image

#25

This is doing it the hard way. Make the image a loop device: losetup /dev/loop0 /path/to/your/image.img Now you can use /dev/loop0 like it's a normal disk. If you're not seeing your partitions (/dev/loop0p1, etc.): partprobe /dev/loop0 When you're done, detach the loop device: losetup -d /dev/loop0 I use this trick to transparently compress ddrescue images with brtfs: https://brashear.me/blog/2017/11/14/how-to-create…

There’a actually an even easier way for many use cases. Just use libguestfs. One big benefit it has over the losetup process is that it works in unprivileged containers because nothing needs to be mounted.

Re: Mounting partitions from a dd image

#26
post #2

Is it also possible to repartition the image with conventional tools? How about setting a boot flag and writing it back to e.g. an USB stick?

You can run parted directly on the image file to make modifications.

    fallocate -l 1G disk.img
    parted ./disk.img

Re: Mounting partitions from a dd image

#27

This is doing it the hard way. Make the image a loop device: losetup /dev/loop0 /path/to/your/image.img Now you can use /dev/loop0 like it's a normal disk. If you're not seeing your partitions (/dev/loop0p1, etc.): partprobe /dev/loop0 When you're done, detach the loop device: losetup -d /dev/loop0 I use this trick to transparently compress ddrescue images with brtfs: https://brashear.me/blog/2017/11/14/how-to-create…

There's just one thing I don't get with this approach. How do you make use of the space savings? You've explicitly created an outside disk image which is larger than the ddrescue image, and the ddrescue image is inside that larger file. So while the inside image is taking less space inside the mounted loopback volume, the outside disk image will always take the original amount of space. Do you trim the outside volume after you're done or do you somehow extract the lzo-encoded disk image after?

Re: Mounting partitions from a dd image

#28
You can directly expose the partitions using the tool `kpartx`, which will create a `loop` device for you:

  > sudo kpartx -a test.raw
  > lsblk
  NAME      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
  loop0       7:0    0     4G  0 loop
  └─loop0p1 253:0    0     4G  0 part
  > sudo kpartx -d test.raw

Re: Mounting partitions from a dd image

#30

This is doing it the hard way. Make the image a loop device: losetup /dev/loop0 /path/to/your/image.img Now you can use /dev/loop0 like it's a normal disk. If you're not seeing your partitions (/dev/loop0p1, etc.): partprobe /dev/loop0 When you're done, detach the loop device: losetup -d /dev/loop0 I use this trick to transparently compress ddrescue images with brtfs: https://brashear.me/blog/2017/11/14/how-to-create…

You can make that even easier:

    # losetup -Pf /path/to/disk.img
    # mount /dev/loop0pX /mnt
Post reply on HN