Quantcast
Channel: User Kamil Maciorowski - Super User
Viewing all articles
Browse latest Browse all 645

Answer by Kamil Maciorowski for How do I provide a disk partition devices for a disk image (not mount)

$
0
0

Modern losetup allows you to do this easily:

-P, --partscan
Force the kernel to scan the partition table on a newly created loop device. Note that the partition table parsing depends on sector sizes. The default is sector size is 512 bytes, otherwise you need to use the option --sector-size together with --partscan.

(source: man 8 losetup)

The command will be like:

sudo losetup --find --show --partscan /the/image

with --sector-size 4096 or without it (equivalent to --sector-size 512). Use --read-only if you want. The tool will print /dev/loopN (where N is a number), this will be the whole image as a block (loop) device. The partitions (if any) will be /dev/loopNpM (where M is also a number).

In case of a real disk, the disk itself could tell us its logical sector size without any doubt. A raw image cannot. To know if you needed to use --sector-size 4096, we may have analyzed the image beforehand and taken an educated guess. There are edge cases though. The easiest way is by trial and error. Use the above command first and then:

  • lsblk
  • sudo file -skr /dev/loopNpM

If there are partitions /dev/loopNpM and their sizes are what you expect, and file for each gives you output you expect, then most likely the default 512 for --sector-size is right and you can work with /dev/loopNpM files.

Otherwise run sudo losetup --detach /dev/loopN (use the right N!) and start anew, this time with --sector-size 4096. If there is no error and this time the output of lsblk and sudo file … looks right, then most likely 4096 is what you needed.

For an image of a HDD, SSD, USB flash drive, SD card or so, 512 and 4096 are the only sane values of --sector-size to try. For comparison: optical media (CDs, DVDs and so) use 2048 bytes as their sector size, but they don't implement partitions and we don't use LUKS on them.

When you're done with whatever you want to do, detach the image:

sudo losetup --detach /dev/loopN

Other possibilities:

  • Alternative tool: kpartx.

  • Even with not-so-modern losetup you can manually attach a fragment of the image (i.e. its partition) to /dev/loopN, by using --offset and --sizelimit. The only problem is to learn the values you need to pass. fdisk -l /the/image or gdisk -l /the/image can tell you sector numbers you would need to multiply by the right logical sector size. Except when the right logical sector size is 4096 and the partition table is GPT; in this case the tools may assume 512 and fail to read the actual partition table in the first place. There are ways to overcome this, but I won't elaborate.

    (E.g. attaching to a virtual machine and learning the right values from there is a cumbersome way, still a way. I'm mentioning it because you did pass the image to a virtual machine, you know how to do this, so in case you need to overcome the mentioned problem, this way will probably be easy for you.)


Viewing all articles
Browse latest Browse all 645

Trending Articles