You are not logged in.

#1 2022-12-04 18:10:08

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 257

[SOLVED] Force udev to detect nested partitions on luks device

I have created following partitions layout:

/dev/nvme0n1: GPT
  /dev/nvme0n1p1: EFI FAT32
  /dev/nvme0n1p2: crypto_LUKS
    /dev/mapper/system -> /dev/dm-0: GPT
      /dev/dm-0p1: Root partition
      /dev/dm-0p2: Swap partition

nvme0n1p1 and nvme0n1p2 are regular GPT partitions and visible by initramfs by default.
/dev/mapper/system is successfully created by encrypt initramfs hook.

But nested partitions dm-0p1 and dm-0p2 aren't created automatically.

I can create them manually by running

# partx -av /dev/mapper/system

(I added partx into initramfs).
After they are created, no /dev/disk/by-... symlinks created.

Is there an easy way to force udev to rescan partitions and create corresponding symlinks in /dev/disk/ automatically after block device in /dev/mapper/ appeared? As i can see, symlinks for regular partitions are created by /usr/lib/udev/rules.d/*-dm-*.rules. Of course they are absent on initramfs by default, but running

# cryptsetup open /dev/nvme0n1p2 system

from Arch Live CD also doesn't trigger to create /dev/dm-0p* and corresponding symlinks.
Probably i need to modify udev rules from /usr/lib/udev/rules.d/, but can't figure out how does it distinguish regular and nested partitions and skips applying commands for latter.

Thanks in advance for any hints.

Last edited by dimich (2022-12-06 13:56:05)

Offline

#2 2022-12-04 19:10:16

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,144

Re: [SOLVED] Force udev to detect nested partitions on luks device

What are you trying to achieve exactly with layering GPT over LUKS? I didn't even know you could do that, but I'm puzzled why you'd want to. Why not use LVM, say, instead?


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#3 2022-12-04 19:28:07

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 257

Re: [SOLVED] Force udev to detect nested partitions on luks device

cfr wrote:

What are you trying to achieve exactly with layering GPT over LUKS? I didn't even know you could do that, but I'm puzzled why you'd want to. Why not use LVM, say, instead?

I want to have both root and swap partitions on single LUKS volume. Of course, LVM is first thing i thought about (and it is a backup plan). But LVM is additional layer, i'd like to avoid it.
I can boot the system using manual triggering partitions re-read with partx. I could write udev rules and initcpio hook to do it automatically with explicit disks identifiers. But i'm curious if there is some standard solution. For example, udev already has the rules which create symlinks in /dev/disk/.. but they aren't applied to nested partitions. Maybe some tag in single additional rule will be enough to force udev to create symlinks.

Offline

#4 2022-12-04 19:39:44

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,144

Re: [SOLVED] Force udev to detect nested partitions on luks device

LVM is an additional layer, but it is a standard and widely tested layer. In contrast, you are adding an additional layer which is far from standard or widely tested.

Normally, you put the details in fstab and pass details of root as kernel parameters. I'm not sure udev is the right tool here as it is more aimed at rules for non-persistent or more peripheral devices rather than those core ones which are always present. What's in fstab?


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#5 2022-12-04 19:51:13

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED] Force udev to detect nested partitions on luks device

dimich wrote:

I want to have both root and swap partitions on single LUKS volume.

Then we can wonder again why you want this wink
How about a swap file on the root partition? Or maybe unlocking two LUKS volumes with the same password (at least sd-encrypt hook is said to support that)?

Offline

#6 2022-12-05 00:42:14

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 257

Re: [SOLVED] Force udev to detect nested partitions on luks device

cfr wrote:

LVM is an additional layer, but it is a standard and widely tested layer. In contrast, you are adding an additional layer which is far from standard or widely tested.

LVM is designed for logical volumes management. I'm not going to manage volumes after they are created once, not split, concatenate, add new devices etc. I understand that performance loss is insignificant, especially comparing to cryptoLUKS, but nevertheless. I don't mind to discuss pros and cons of LVM but it's not my question.

cfr wrote:

Normally, you put the details in fstab and pass details of root as kernel parameters. I'm not sure udev is the right tool here as it is more aimed at rules for non-persistent or more peripheral devices rather than those core ones which are always present. What's in fstab?

fstab contains records for /boot and swap. However, swap is not enabled for now. I'm experimenting in virtualbox and going to apply this layout to laptop, if it works.
The goal is to pass

cryptdevice=UUID=<luks_uuid>:system root=UUID=<root_uuid> rw resume=UUID=<swap_uuid>

to the kernel and get it working as usual. Rootfs should be mounted in initramfs after LUKS opened, so nothing to do with fstab.

Udev somehow desides whether to run kpartx on newly appeared block device or not. Also it desides whether to create symlinks in /dev/disk/ or not.
Let me rephrase the question: Is it possible to change it's behavior without rewriting default rules, i.e. treat /dev/mapper/system (/dev/dm-0) as a disk with partitions after it appeared?
Indeed, we already have the functionality to detect partitions, why can't we apply it to any block device?

Raynman wrote:

Then we can wonder again why you want this wink
How about a swap file on the root partition? Or maybe unlocking two LUKS volumes with the same password (at least sd-encrypt hook is said to support that)?

Yep, i want to enter password only once at boot. I don't want to mess with hardcoded offset of swap file on the root partition for resume, also with systemd in initramfs for sd-encrypt.

Last edited by dimich (2022-12-05 00:44:51)

Offline

#7 2022-12-05 03:28:46

cfr
Member
From: Cymru
Registered: 2011-11-27
Posts: 7,144

Re: [SOLVED] Force udev to detect nested partitions on luks device

dimich wrote:

Indeed, we already have the functionality to detect partitions, why can't we apply it to any block device?

Because it is not a block device?

Edit:
https://lists.archlinux.org/archives/li … RNXJ3T2242
https://bbs.archlinux.org/viewtopic.php?id=256720

Last edited by cfr (2022-12-05 05:19:23)


CLI Paste | How To Ask Questions

Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L

Offline

#8 2022-12-06 13:55:46

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 257

Re: [SOLVED] Force udev to detect nested partitions on luks device

cfr wrote:

Because it is not a block device?

Device mapper works with block devices. /dev/dm-0 is definitely a block device.

Thank you!

partprobe provided by mkinitcpio-busybox doesn't work for some reason.

partprobe from parted package does all the job!

/etc/initcpio/hooks/partprobe-encrypt:

#!/usr/bin/ash

run_hook() {
    if [ -n "${cryptdevice}" ]; then
        IFS=: read cryptdev cryptname cryptoptions <<EOF
$cryptdevice
EOF
        if [ -e "/dev/mapper/${cryptname}" ]; then
            partprobe "/dev/mapper/${cryptname}"
        fi
    fi
}

/etc/initcpio/install/partprobe-encrypt:

#!/bin/bash

build() {
    add_binary 'partprobe'
    add_runscript
}

In /etc/mkinitcpio.conf:

HOOKS=(… encrypt partprobe-encrypt …)

Offline

#9 2022-12-06 14:23:00

frostschutz
Member
Registered: 2013-11-15
Posts: 1,457

Re: [SOLVED] Force udev to detect nested partitions on luks device

parted (gparted, partprobe, anything libparted-based) uses the linear device mapper to emulate partitions. So technically, this is equivalent to what LVM does, just a lot less flexible and not nearly as well supported...

These "partitions" also show up in 'dmsetup table' accordingly.

# dmsetup table
foobar: 0 2064384 crypt aes-xts-plain64 :64:logon:cryptsetup:61d795b6-7e1f-4cef-88ff-cfd6d00f039e-d0 0 7:0 32768 1 sector_size:4096
foobar1: 0 202752 linear 253:54 2048
foobar2: 0 204800 linear 253:54 204800
foobar3: 0 204800 linear 253:54 409600

partx on the other hand only informs the kernel about partitions - and the kernel will create block devices for them, if supported. no additional device mapper targets created. ends up looking like this:

brw-rw---- 1 root disk 253, 54 Dec  6 15:04 /dev/dm-54
brw-rw---- 1 root disk 259, 13 Dec  6 15:05 /dev/dm-54p1
brw-rw---- 1 root disk 259, 14 Dec  6 15:05 /dev/dm-54p2
brw-rw---- 1 root disk 259, 15 Dec  6 15:05 /dev/dm-54p3

now, the problem is - if you use both partx and parted, you end up having two devices for each partition instead of one.

/dev/dm-54p2: Linux rev 1.0 ext4 filesystem data, UUID=e5d1a071-4fa2-4349-b6de-9e31f0cae481, volume name "partition" (extents) (64bit) (large files) (huge files)
/dev/dm-56:   Linux rev 1.0 ext4 filesystem data, UUID=e5d1a071-4fa2-4349-b6de-9e31f0cae481, volume name "partition" (extents) (64bit) (large files) (huge files)

dm-54p2 being the kernel partition and dm-56 the linear device mapper created by parted. but both represent the same data.

There are also some differences in behavior. with parted partitions, you can't cryptsetup close foobar as long as foobar123 exist (even if nothing is mounted). with kernel partitions you can close it as long as the partitions are not in use.

All in all this setup is a bit prone to conflicts. You never want to see two block devices for one thing. It results in duplicate UUIDs, mounting same thing twice or getting weird device busy error or other unexpected behavior.

Use LVM or two separate partitions / two LUKS headers.

It's possible that the kernel didn't even support partitions for dm- devices in the past and that's why parted creates the partitions in its own way. That may also be the reason why those partition devices aren't well supported by udev yet either. You could try reporting those issues upstream, after all if the kernel supports something, udev/parted should probably follow along...

But barely anyone uses this setup, or rather not activating partitions by default is a feature - allows VMs to use such block devices directly without the host interfering. To the host machine a partition table only makes sense on raw disks after all, not on any nested level.

Offline

#10 2022-12-06 16:17:03

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 257

Re: [SOLVED] Force udev to detect nested partitions on luks device

frostschutz wrote:

now, the problem is - if you use both partx and parted, you end up having two devices for each partition instead of one.

Thank you for the explanation. I see these tools work in different way. But i don't use partx and partprobe together. I used partx for manual boot just because builtin busybox's partprobe didn't work. Partprobe from parted package creates /dev/dm-1 and /dev/dm-2 and udev automatically creates correct symlinks to them in /dev/disk/, so i don't need to pass additional parameters to initramfs (the hook can use encrypt's "cryptdevice=" parameter) nor hardcode device pathes.

Offline

Board footer

Powered by FluxBB