Recognizing GPT partition table created with different logical sector size

I found a solution: A program called kpartx, which is a userspace program that uses devmapper to create partitions from loopback devices, which works great:

$ loop_device=`losetup --show -f /dev/sdg`
$ kpartx -a $loop_device
$ ls /dev/mapper
total 0
crw------- 1 root root  10, 236 Mar  2 17:59 control
brw-rw---- 1 root disk 252,   0 Mar  2 18:30 loop0p1
brw-rw---- 1 root disk 252,   1 Mar  2 18:30 loop0p2
$
$ # delete device
$ kpartx -d $loop_device
$ losetup -d $loop_device

This essentially does what I was planning to do in option 1, but much more cleanly.


On Linux, loop devices are partitionable if the max_part parameter of the loop kernel module is set. If the loop is built-in (not a module), you can pass a loop.max_part=31 kernel command line parameter instead.

So after you've configured the loop driver to get partitionable block devices, it should just be a matter of doing:

losetup --show -f /dev/sda

To get some /dev/loopXp1, /dev/loopXp2... devices for each partition.

A few notes as things have evolved on that front in the kernel since you posted your question:

  • since 4.14, it's also possible to specify a logical block size other than 512 for loop devices (losetup -b 4096 for instance). It's also possible to change the block size of a loop device after it has been created.

  • since 4.11, the logical block size of nbd devices is set to the block size passed to nbd-client (-b option). Since the default block size is (and was) 1024, that means nbd devices now get a default logical sector size of 1024 instead of 512 before (pretty bad from a backward compatibility point of view).