Skip to content

Commit

Permalink
topology: Support PartUUID again
Browse files Browse the repository at this point in the history
Signed-off-by: Ikey Doherty <ikey@serpentos.com>
ikeycode committed May 23, 2024
1 parent 38db06c commit 8c794cf
Showing 4 changed files with 48 additions and 65 deletions.
63 changes: 1 addition & 62 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion blsforme/Cargo.toml
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
gpt = "3.1.0"
log.workspace = true
nix.workspace = true
thiserror.workspace = true
4 changes: 2 additions & 2 deletions crates/topology/Cargo.toml
Original file line number Diff line number Diff line change
@@ -8,6 +8,6 @@ edition = "2021"
[dependencies]
thiserror.workspace = true
superblock = { path = "../superblock" }
indextree = { version = "4.6.1", features = ["par_iter"] }
nix.workspace = true
log.workspace = true
log.workspace = true
gpt = "3.1.0"
45 changes: 45 additions & 0 deletions crates/topology/src/disk/probe.rs
Original file line number Diff line number Diff line change
@@ -137,6 +137,16 @@ impl Probe {
pub fn get_rootfs_device(&self, path: impl AsRef<Path>) -> Result<BlockDevice, super::Error> {
let path = path.as_ref();
let device = self.get_device_from_mountpoint(path)?;

// Scan GPT for PartUUID
let guid = if let Some(parent) = self.get_device_parent(&device) {
log::warn!("guid {}", parent.display());
self.get_device_guid(parent, &device)
} else {
log::warn!("no parent");
None
};

let chain = self.get_device_chain(&device)?;
let mut custodials = vec![device];
custodials.extend(chain);
@@ -149,6 +159,41 @@ impl Probe {
.iter()
.flat_map(|c| BlockDevice::new(self, c, None, true))
.collect::<Vec<_>>();
block.guid = guid;

Ok(block)
}

/// For GPT disks return the PartUUID (GUID)
pub fn get_device_guid(
&self,
parent: impl AsRef<Path>,
path: impl AsRef<Path>,
) -> Option<String> {
let device = fs::canonicalize(path.as_ref()).ok()?;
let sysfs_path = fs::canonicalize(
device
.file_name()
.map(|f| self.sysfs.join("class").join("block").join(f))
.ok_or_else(|| super::Error::InvalidDevice(device.clone()))
.ok()?,
)
.ok()?;
let partition = str::parse::<u32>(
fs::read_to_string(sysfs_path.join("partition"))
.ok()?
.trim(),
)
.ok()?;
let fi = fs::File::open(parent).ok()?;
let gpt_header = gpt::GptConfig::new()
.writable(false)
.initialized(true)
.open_from_device(Box::new(fi))
.ok()?;
gpt_header
.partitions()
.get(&partition)
.map(|partition| partition.part_guid.hyphenated().to_string())
}
}

0 comments on commit 8c794cf

Please sign in to comment.