News for hackers. Stuff that matters.
Re:Mounting partitions from a dd image by discreditable on Wednesday May 09, 2018 @10:33AM (#17030114)
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-a-compress...
Re:Mounting partitions from a dd image by jschwartzi on Wednesday May 09, 2018 @01:01PM (#17031492)
If you have a full dd image of the disk, you can just do losetup -Pf /path/to/your/image.img and that will pick a loopback device and perform the partprobe on it automatically.
Re:Mounting partitions from a dd image by strong_silent_t on Wednesday May 09, 2018 @12:55PM (#17031420)
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 but I've never had a pressing need.
Re:Mounting partitions from a dd image by jlgaddis on Thursday May 10, 2018 @07:34AM (#17037963)
> I'd like to figure out how to do this with VM disks as well but I've never had a pressing need.

Everything that you will (probably) need is here: http://libguestfs.org

If you happen to need something that isn't already included, the APIs make it pretty easy to build your own tools as well.

Conveniently, those tools are also already packaged up for your distro (unless you use some esoteric distribution, perhaps).

Re:Mounting partitions from a dd image by discreditable on Wednesday May 09, 2018 @01:54PM (#17032075)
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 by mrrsm on Wednesday May 09, 2018 @10:47AM (#17030232)
I came here to say using the loop device just makes things much easier. No math to get your offsets and fat fingering the numbers this way.
Re:Mounting partitions from a dd image by lathiat on Wednesday May 09, 2018 @12:14PM (#17031017)
Interesting I have used kpartx which uses device mapper instead of Partprobe.

Wonder why kpartx exists; will have to look into that. Maybe not everything can be partprobed.

Re:Mounting partitions from a dd image by discreditable on Wednesday May 09, 2018 @12:44PM (#17031338)
I'm not sure. All I can think is that partprobe is provided by parted and I have noticed people tend to gravitate towards using fdisk or parted and ignoring the other. Notice the author of the article is an fdisk guy.

I might be mis-remebering but I think parted used to be considered lame before fdisk lagged to support GPT.

Re:Mounting partitions from a dd image by Tsiklon on Saturday May 12, 2018 @11:59AM (#17054650)
Personally I’d call myself an fdisk/gdisk person - I like the safety net those programs provide by staging disk changes prior to committing to the MBR/GPT, if I’m doing things by hand this is my preferred tool.

I don’t like how parted will automatically commit your changes as you go, though this is useful for scripting your changes and is my preferred method for disk geometry modifications when preparing maintenances etc

Right tool for the job and all that.

From what I remember partprobe has had a history of occasional unreliable behaviour under RHEL - but I understand it works properly under RHEL 7.3+

Re:Mounting partitions from a dd image by jschwartzi on Wednesday May 09, 2018 @01:02PM (#17031503)
kpartx is an older tool that was used with 2.6.xx-era Linux systems. partprobe/losetup are the newer mechanisms for doing this.

A lot of StackOverflow questions have answers that use kpartx, and those answers are out of date.

Re:Mounting partitions from a dd image by detuur on Wednesday May 09, 2018 @04:16PM (#17033599)
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 by pastage on Thursday May 10, 2018 @04:04PM (#17042472)
It's called sparse files, it's files with holes in them where no data is written to disk. You can have terabytes of files on small disks as long as they contain a high % of bocks of only zeros.
Re:Mounting partitions from a dd image by detuur on Friday May 11, 2018 @07:05PM (#17051350)
Can't believe I've gone so long without knowing about sparse files.
Re:Mounting partitions from a dd image by jlgaddis on Thursday May 10, 2018 @07:35AM (#17037969)
Millions of zeroes in a row tend to compress pretty well.
Re:Mounting partitions from a dd image by nzmsv on Wednesday May 09, 2018 @03:20PM (#17032980)
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 by Skunkleton on Wednesday May 09, 2018 @06:10PM (#17034632)
You can make that even easier:

    # losetup -Pf /path/to/disk.img
    # mount /dev/loop0pX /mnt
Re:Mounting partitions from a dd image by maccam94 on Wednesday May 09, 2018 @06:31PM (#17034825)
Additionally, this method is not susceptible to race conditions when other loop devices are being configured.
Re:Mounting partitions from a dd image by dbolgheroni on Wednesday May 09, 2018 @11:29AM (#17030564)
OpenBSD uses vnconfig (also FreeBSD; on NetBSD it's vndconfig). An example:

  # vnconfig vnd0 install63.fs
Then you can treat vnd0 as if it was a disk:

  # disklabel vnd0
  # /dev/rvnd0c:
  type: vnd
  disk: vnd device
  label: fictitious
  duid: 138b4f2a2e184426
  flags:
  bytes/sector: 512
  sectors/track: 100
  tracks/cylinder: 1
  sectors/cylinder: 100
  cylinders: 7382
  total sectors: 738240
  boundstart: 1024
  boundend: 737280
  drivedata: 0 
  
  16 partitions:
  #                size           offset  fstype [fsize bsize   cpg]
    a:           736256             1024  4.2BSD   2048 16384 16142 
    c:           738240                0  unused                    
    i:              960               64   MSDOS                    
For instance, to mount the 'a' partition:

  # mount /dev/vnd0a /mnt
You can also associate an encryption key with the device. All data written to the diskimage will then be encrypted.
Re:Mounting partitions from a dd image by floatboth on Wednesday May 09, 2018 @12:17PM (#17031048)
FreeBSD does not use vnconfig, FreeBSD uses mdconfig:

    # mdconfig -f smartos-latest-USB.img
    md0
    # gpart list md0
    ...
    scheme: MBR
    Providers:
    1. Name: md0s1
       ...
    # mount -o ro -t msdosfs /dev/md0s1 /mnt
    // (or, use labels)
    # mount -o ro -t msdosfs /dev/msdosfs/SMARTOSBOOT /mnt
Or for ISOs, in one line:

    # mount -t cd9660 /dev/$(mdconfig -f image.iso) /mnt
Re:Mounting partitions from a dd image by jlgaddis on Thursday May 10, 2018 @07:46AM (#17038015)
I was just about to correct your correction, because I can quite clearly recall using vnconfig on FreeBSD.

Apparently that's been a little while, however. According to vnconfig(8):

> The mdconfig utility first appeared in FreeBSD 5.0 as a cleaner replacement for the vn(4) and vnconfig(8) combo.

5.0 was released just over 15 years ago.

Re:Mounting partitions from a dd image by phaedrus on Wednesday May 09, 2018 @11:49AM (#17030755)
One time I tried to create a tool to do in-place conversion between Linux software RAID volumes (e.g. RAID-0 to RAID-1 (assuming data fits), or RAID-1 to RAID-5, etc.) I used disk image files and the loopback device to test it to avoid having to reformat physical partitions.

Despite reading all of the documentation I could find, and even going so far as to read kernel source code, I could never get the system to recognize my striped block data. That is, I tried to generate three files composing a RAID-5 "by hand", but mdadm would refuse to mount them. I never figured out if there was some additional id-block I was missing, or if my striping algorithm was not correct.

Re:Mounting partitions from a dd image by ckastner on Wednesday May 09, 2018 @10:28AM (#17030084)
Under Linux, this can also be achieved with kpartx(8).

https://linux.die.net/man/8/kpartx

Re:Mounting partitions from a dd image by rwmj on Wednesday May 09, 2018 @12:22PM (#17031095)
Or without needing root using guestfish (http://libguestfs.org/guestfish.1.html)

    $ guestfish -a img.dd -m /dev/sda1
    ><fs> ll /
guestmount works if you want to mount it on a local directory (implemented using libguestfs + FUSE).
Re:Mounting partitions from a dd image by dpedu on Wednesday May 09, 2018 @01:42PM (#17031938)
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 by trm42 on Wednesday May 09, 2018 @02:00PM (#17032142)
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 by theonewolf on Wednesday May 09, 2018 @04:45PM (#17033909)
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 by Ace17 on Wednesday May 09, 2018 @11:38AM (#17030630)
> mount -t -o ro,offset=$((51263)) x.dd partition_1

> mount -t -o ro,offset=$((51296390)) x.dd partition_2

Isn't the mount type "-t" value missing?

Re:Mounting partitions from a dd image by assafmo on Wednesday May 09, 2018 @01:20PM (#17031713)
Thanks! I missed this. Fixed. :-)
Re:Mounting partitions from a dd image by pixelbeat__ on Wednesday May 09, 2018 @04:47PM (#17033923)
libguestfs > losetup > fdisk

I.E. libguestfs is safer when dealing with images of unknown origin, and losetup has inbuilt support now for partition scanning.

Here's an old fdisk/parted wrapper I used to use:

http://www.pixelbeat.org/scripts/lomount.sh

Re:Mounting partitions from a dd image by amelius on Wednesday May 09, 2018 @10:20AM (#17030025)
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?

Re:Mounting partitions from a dd image by peterwwillis on Wednesday May 09, 2018 @10:35AM (#17030131)
Yes, you can use losetup.

  ~# dd if=/dev/zero of=image.img bs=1M count=100
  ~# LODEVICE=$(losetup --find --show image.img)
  ~# fdisk $LODEVICE
  ~# losetup -d $LODEVICE
^ The above creates a 100MB disk image, binds it to a loopback device, runs fdisk, and then detaches it. I'm not sure how formatting the partition would work but presumably those tools have an offset option too?

edit: this page explains using offsets for mke2fs and doesn't require losetup: https://superuser.com/questions/737072/userspace-manipulatio...

also, losetup can now scan the device for partition tables and make partition-specific loopbacks! https://stackoverflow.com/a/15200862

Re:Mounting partitions from a dd image by regularfry on Wednesday May 09, 2018 @01:28PM (#17031785)
Extlinux on an ext4 partition doesn't need an offset. Or at least didn't when I wrote this: http://www.blackkettle.org/posts/archive/2013-08-19-bootable...
Re:Mounting partitions from a dd image by jschwartzi on Wednesday May 09, 2018 @01:04PM (#17031531)
I have built block-level bootable system images for the TS-7800 using this mechanism.

https://www.embeddedarm.com/products/TS-7800

That was before I found multistrap.

Re:Mounting partitions from a dd image by bcl on Wednesday May 09, 2018 @03:58PM (#17033425)
You can run parted directly on the image file to make modifications.

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