from Hacker News

Mounting partitions from a dd image

by assafmo on 5/9/18, 9:33 AM with 34 comments

  • by discreditable on 5/9/18, 2:33 PM

    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...
  • by dbolgheroni on 5/9/18, 3:29 PM

    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.
  • by phaedrus on 5/9/18, 3:49 PM

    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.

  • by ckastner on 5/9/18, 2:28 PM

    Under Linux, this can also be achieved with kpartx(8).

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

  • by rwmj on 5/9/18, 4:22 PM

    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).
  • by dpedu on 5/9/18, 5:42 PM

    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
  • by theonewolf on 5/9/18, 8:45 PM

    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
  • by Ace17 on 5/9/18, 3:38 PM

    > 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?

  • by pixelbeat__ on 5/9/18, 8:47 PM

    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

  • by amelius on 5/9/18, 2:20 PM

    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?