Skip to content

Implement /dev/disk/by-parttype - #28894

Closed
AdrianVovk wants to merge 4 commits into
systemd:mainfrom
AdrianVovk:udev-disk-byparttype
Closed

AdrianVovk wants to merge 4 commits into
systemd:mainfrom
AdrianVovk:udev-disk-byparttype

Conversation

@AdrianVovk

Copy link
Copy Markdown
Contributor

Also implements a /dev/boot-disk device node, so we can implement root=ddi

@github-actions github-actions Bot added the udev label Aug 18, 2023
@daandemeyer

Copy link
Copy Markdown
Collaborator

Maybe go for /dev/gpt-auto for the whole disk for consistency? I don't think we use /dev/gpt-auto for anything already right?

@AdrianVovk

Copy link
Copy Markdown
Contributor Author

That might imply a connection to the gpt-auto generator that isn't there... /dev/boot-disk is a bit more descriptive. I can change it anyway if you prefer

Comment thread rules.d/60-persistent-storage.rules.in Resolved
} LIBSYSTEMD_253;

LIBSYSTEMD_255 {
sd_gpt_uuid_from_name;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, why export this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that apps looking for the "root-x86-64" partition can just get the UUID instead of maintaining their own lookup table of name->uuid for the DPS

ENV{.PART_NAME}=="", ENV{ID_FS_LABEL_ENC}=="?*", ENV{.PART_NAME}="$env{ID_FS_LABEL_ENC}"
ENV{.PART_NAME}=="", ENV{ID_PART_ENTRY_UUID}=="?*", ENV{.PART_NAME}="$env{ID_PART_ENTRY_UUID}"
ENV{.PART_NAME}=="", GOTO="persistent_storage_by_parttype_end"
ENV{ID_PART_ENTRY_TYPE}=="?*", SYMLINK+="disk/by-parttype/$env{ID_PART_ENTRY_TYPE}/$env{.PART_NAME}"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this symlink should probably carry "boot" in the name somewhere.

And I think it would make sense to patch block_id, to look up the id we maintain for partition type uuids and simply append it as yet another property onto the device. Then use that property here to create a named symlink.

That said, for compat I think we should always create the uuid-based symlink, and the id-based symlink if we have one. That way, people can use the uuid always without having to rely us to register the part type they need one day.

also, this should initialize link_priority from the partno somehow, so that we get the guarantee that the first partitoin always gets the symlink if multiple partitions have the same type on the same disk.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this symlink should probably carry "boot" in the name somewhere.

👍

And I think it would make sense to patch block_id, to look up the id we maintain for partition type uuids and simply append it as yet another property onto the device. Then use that property here to create a named symlink.

So in bkid we export ID_BOOT_DEV_SYMLINKS="disk/by-parttype/{uuid}/{label} disk/by-parttype/{name1}/{label} ...", then in the rule add SYMLINK+="$env{ID_BOOT_DEV_SYMLINKS}"?

That said, for compat I think we should always create the uuid-based symlink, and the id-based symlink if we have one. That way, people can use the uuid always without having to rely us to register the part type they need one day.

The problem w/ the id-based symlinks we came across is that there are many IDs for one partition type... and it's not a fixed number either.

also, this should initialize link_priority from the partno somehow, so that we get the guarantee that the first partitoin always gets the symlink if multiple partitions have the same type on the same disk.

Same type and label, you mean?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, so i was thinking the symlink wouldn't also contain the label, but just the parttype. i.e. that you have a stable link to the first partition of a specific type, and the label doesn't matter. e.g. a stable reference to ESP, XBOOTLDR, swap and other partitions could be maintained that way, that just always works, and where multiple partitions of the same type on the same disk doesn't make sense.

but your usecase seems different if you want to include the label too. can you elaborate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically the idea is that for some partitions, we do want multiple of them (i.e. we'll have two /usr trees). Including the label lets you access all the partitions on the disk

I agree that it doesn't make sense for many partition types (i.e. swap, /var, etc). However, the idea is that this mechanism is also generic, not just for partition types defined by systemd/uapi-group. So if Vanilla OS wants to define 08e5611e-7996-4a0b-b793-0cb63b2a3e14 = grub installation, and they have two grub installations, they'd be able to find both installations of grub in /dev/disk/by-parttype/08e5611e-7996-4a0b-b793-0cb63b2a3e14/* (this is not a contrived example. I'm pretty sure that Vanilla does have grub installed 2x)

However, perhaps picking the first partition we find on the disk might be a good tradeoff. Anyone who needs anything more than that should dissect the GPT table and find the partition themselves?

Something else to consider: sometimes we don't want the first partition we find. Sometimes we want the latest partition by version number

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, we could simply add both kinds of symlinks i guess. i.e. with the label, and without it. i think that might be quite nice actually. key is that we format them into a strong that has a clear delimiter where the label begins so that there could never be confusion. and we should suppress the additional with-label symlinks for cases where the label is empty anyway.

Comment thread rules.d/60-persistent-storage.rules.in Resolved
This lets shell scripts do: `ENVVAR=$(systemd-id128 -m show root)`
Comment thread man/systemd-id128.xml

<listitem><para>When paired with the <command>show</command> command, prints the ID in a machine-readable
format. This is useful for shell scripts, where you might want to put the UUID of a partition type into
an environment variable.</para></listitem>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this already exists:

systemd-id128 -P machine-id

if (found_esp_or_xbootldr && !sd_id128_is_null(root_id))
udev_builtin_add_property(dev, test, "ID_PART_GPT_AUTO_ROOT_UUID", SD_ID128_TO_UUID_STRING(root_id));
if (is_boot_disk) {
udev_builtin_add_property(dev, test, "ID_BOOT_DISK", "1");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is lovely btw. We should have done this from the beginning.


# add symlink to GPT root disk
# Add a stable symlink for the disk that we're booting from
SUBSYSTEM=="block", ENV{ID_BOOT_DISK}=="1", SYMLINK+="boot-disk"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should add ENV{DEVTYPE}=="disk" here, as safety check. We currently don't set the property on per-partition devices, but we might eventually import the property from the whole block device onto the partition devices (or we might not, but somebody else might in a udev rule), and that shouldn't fuck up our symlink here.

I think this symlink should probably be called /dev/disk/boot, i.e. be placed where the other /dev/disk/by-*/ symlinks are, but as a singleton right inside /dev/disk/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, this one symlink is so generic, it should really be in rules.d/60-persistent-storage.rules.in I think.

@poettering

Copy link
Copy Markdown
Member

I think the ID_BOOT_DISK thing and symlink we can do in a separate PR and already merge. i.e. the first commit. Seems almost good to go.

@poettering

Copy link
Copy Markdown
Member

regarding the translation of gpt type uuids to names, I'd propose the following:

in builtin_blkid() whenever we encounter a partition table entry which has ID_PART_ENTRY_TYPE= set to a parsable uuid, we'll synthesize optionally two additional fields ID_PART_ENTRY_TYPE_NAME= and ID_PART_ENTRY_TYPE_NAME_NOARCH=. These names are generated from the existing gpt_partition_type_uuid_to_string() and from a new function gpt_partition_type_uuid_to_noarch_string(). The latter would be very similar to gpt_partition_type_uuid_to_string() but only return a string if the arch field of the GptPartitionType structure is < 0 for it (i.e. if the entry is arch-independent). We'd also suppress if the regular name and the noarch name are the same.

Net result, we could then in udev rules generated three symlinks for each partition: one based on ID_PART_ENTRY_TYPE= directly, one pased on ID_PART_ENTRY_TYPE_NAME= and one on ID_PART_ENTRY_TYPE_NAME_NOARCH=. e.g. /dev/disk/by-parttype/4f68bce3-e8cd-4db1-96e7-fbcaf984b709, /dev/disk/by-parttype/root, /dev/disk/by-parttype/root-x86_64 would all point to the same partition device, and programs can rely on them

@poettering poettering added reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks needs-rebase labels Sep 8, 2023
@poettering

Copy link
Copy Markdown
Member

also, please add a commit that drops this entry from TODO:

* New udev block device symlink names:
  /dev/disk/by-parttypelabel/<pttype>-<ptlabel>. Use case: if pt label is used
  as partition image version string, this is a safe way to reference a specific
  version of a specific partition type, in particular where related partitions
  are processed (e.g. verity + rootfs both named "LennartOS_0.7").

as well as this one:

* introduce /dev/disk/root/* symlinks that allow referencing partitions on the
  disk the rootfs is on in a reasonably secure way. (or maybe: add
  /dev/gpt-auto-{home,srv,boot,…} similar in style to /dev/gpt-auto-root as we
  already have it.

which are kinda the same

@AdrianVovk

Copy link
Copy Markdown
Contributor Author

This has been replaced, first by #36209 and now by #36631

I'm probably never circling back to this, so closing

@AdrianVovk AdrianVovk closed this Mar 7, 2025
@github-actions github-actions Bot removed reviewed/needs-rework 🔨 PR has been reviewed and needs another round of reworks needs-rebase labels Mar 7, 2025
poettering added a commit that referenced this pull request Apr 3, 2025
…ks based on partition designators (#36631)

This replaces #36209 and implements this via a new builtin, rather than
by shelling out to systemd-dissect.

Replaces: #36209 
Replaces: #28894
Fixes: #34319 
Fixes: #33453 
Fixes: #27897
Fixes: #18035
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants