Skip to content

Make parent devices global #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

HuijingHei
Copy link
Member

The main of the patch is to initialize parent devices once at the beginning and read them then, also remove some duplicated sysroot as openat::Dir::open("/") and pass as a parameter.

Refer to #855 (comment)

@HuijingHei HuijingHei force-pushed the get-parent-devices branch from 140d3ad to 7b65bfb Compare May 7, 2025 11:07
@HuijingHei
Copy link
Member Author

@dustymabe I think you meant to initialize esp devices, store them and read after, this is the first step make parent devices global, initialize once and read then. Not sure if can make esp devices or bios devices like this, or other way?

@nikita-dubrovskii
Copy link

LGTM

Comment on lines 137 to 139
let devices = crate::blockdev::get_devices(&dest_root)
.with_context(|| "while looking for parent devices")?;
crate::blockdev::init_parent_devices(devices);
Copy link
Member

Choose a reason for hiding this comment

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

Here's where I'd suggest moving the static instead.

However even then in theory the parent devices are scoped to a particular destination root and so it can't actually be static. Which means it's hard to cache in that way.

So if we wanted to avoid querying the devices multiple times, then what we'd need to do is query them up front and then pass them down. That may involve more code churn though.

Comment on lines 133 to 135
let sysroot = openat::Dir::open("/").context("Opening root dir")?;
let dest_fd = format!("/proc/self/fd/{}", sysroot.as_raw_fd());
let dest_root = std::fs::read_link(dest_fd)?;
Copy link
Member

Choose a reason for hiding this comment

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

Hmm so actually we always operated on /, and this code pattern was just a really complicated way to go from / -> file descriptor -> / effectively.

The only tricky thing here is that eventually we actually do want to operate on the non-booted root! See #108

Comment on lines 222 to 224
let sysroot = openat::Dir::open("/").context("Opening root dir")?;
let dest_fd = format!("/proc/self/fd/{}", sysroot.as_raw_fd());
let dest_root = std::fs::read_link(dest_fd)?;
Copy link
Member

Choose a reason for hiding this comment

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

Per above I think we can just for now hardcode something like
let devices = crate::blockdev::get_devices("/") here.

It actually may make sense to move the static to this function as it's the one that's also hardcoding /.

Copy link
Member Author

@HuijingHei HuijingHei May 8, 2025

Choose a reason for hiding this comment

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

Updated, seems will read the static devices later, move the static to crate::blockdev::get_parent_devices() with option, add get_parent_devices(None) to verify it works. WDYT?

HuijingHei added 2 commits May 9, 2025 15:32
As `PARENT_DEVICES` is used multiple times, initialize once at the
begining and read them then, add this in `get_parent_devices()`
according to coreos#923 (comment)
@HuijingHei HuijingHei force-pushed the get-parent-devices branch from b61faad to 60c61c5 Compare May 9, 2025 07:52
Copy link
Member

@dustymabe dustymabe left a comment

Choose a reason for hiding this comment

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

IANA rust expert, but LGTM

@HuijingHei
Copy link
Member Author

HuijingHei commented May 12, 2025

Thank you all for the review! Does it make sense to do the same thing for esp devices and bios devices, make all of them global, initialize at the beginning and reuse them? Like

struct DeviceParts {
    parent_devices: Vec<String>,
    efi_devices: Option<Vec<String>>,
    bios_devices: Option<Vec<String>>,
}

Or just parent_devices is enough?

Remove duplicated sysroot as `openat::Dir::open("/")` and pass
as parameter
@HuijingHei HuijingHei force-pushed the get-parent-devices branch from 60c61c5 to 95af34c Compare May 12, 2025 06:26
@HuijingHei
Copy link
Member Author

HuijingHei commented May 12, 2025

Edit:
If there is no objection, will merge it tomorrow.

@HuijingHei HuijingHei closed this May 12, 2025
@HuijingHei
Copy link
Member Author

Sorry that click the wrong button, reopen it.

@HuijingHei HuijingHei reopened this May 12, 2025
@cgwalters
Copy link
Member

I think Dusty's original comment in #855 (comment) was correct in that we're clearly parsing the devices a lot. However, this is turning into a nontrivial architectural change and it's not clear to me that we have any real performance concerns here. (Really, most of this data is cached in the kernel or filesystem, we're just re-parsing files into JSON and then back into data structures, but computers are fast)

Looking at this more I don't think we want a global cache, we just want to gather the context up front and pass it down. So instead of passing just plain "sysroot" we'd pass e.g.

struct RootContext {
  sysroot: openat::Dir,
  devices: Vec<String>
}

or so? (And actually given all the pain that using openat has caused here, we might as well add path: Utf8PathBuf to that struct, so anything that wants to can just access it as an absolute path instead. I have something similar in bootc's install code. To make that work when we eventually do #108 we'd just need to also mount the target root)

@dustymabe
Copy link
Member

Looking at this more I don't think we want a global cache, we just want to gather the context up front and pass it down. So instead of passing just plain "sysroot" we'd pass e.g...

This SGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants