-
Notifications
You must be signed in to change notification settings - Fork 92
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
Use blkid instead of lsblk to get disk info when udev is not present #1511
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, how about instead, we keep the interface the same (no new command-line arguments), but in get_partitions
, we check if udev is present (e.g. check for /run/udev/control
; we have code that does something similar already), and if so then use lsblk()
, otherwise use blkid()
.
Both return a Result<Vec<HashMap<String, String>>>
we can iterate over. But of course in the blkid case, some of the key names to pull from will be different.
Though I think this is glossing over an important detail. fn get_partitions(&self) -> Result<Vec<Partition>> {
// walk each device in the output
let mut result: Vec<Partition> = Vec::new();
for devinfo in lsblk(Path::new(&self.path), true)? {
if let Some(name) = devinfo.get("NAME") {
// Only return partitions. Skip the whole-disk device, as well
// as holders like LVM or RAID devices using one of the partitions.
if devinfo.get("TYPE").map(|s| s.as_str()) != Some("part") {
continue;
}
// only trust lsblk output for the following fields if we have udev
if have_udev() {
let (mountpoint, swap) = match devinfo.get("MOUNTPOINT") {
Some(mp) if mp == "[SWAP]" => (None, true),
Some(mp) => (Some(mp.to_string()), false),
None => (None, false),
};
result.push(Partition {
path: name.to_owned(),
label: devinfo.get("LABEL").map(<_>::to_string),
fstype: devinfo.get("FSTYPE").map(<_>::to_string),
parent: self.path.to_owned(),
mountpoint,
swap,
});
} else {
let devinfo = blkid_single(name)?;
// note TYPE here: blkid uses TYPE instead of FSTYPE
let fstype = devinfo.get("TYPE").map(<_>::to_string);
result.push(Partition {
path: name.to_owned(),
label: devinfo.get("LABEL").map(<_>::to_string),
fstype,
parent: self.path.to_owned(),
None,
fstype.is_some_and(|s| s == "swap"),
});
}
}
}
Ok(result)
} |
Great, in the current code I used bklid for a single device as well adding the dev path:
Fixing the rest ;) |
9ecd906
to
f4aa1bc
Compare
src/blockdev.rs
Outdated
|
||
cmd_dev.extend(devices | ||
.lines() | ||
.filter_map(|line|Some(Path::new(line.trim()))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can just use map
here if we don't need filtering functionality.
@@ -1141,10 +1175,13 @@ fn disk_has_mbr(file: &mut (impl Read + Seek)) -> Result<bool> { | |||
Ok(sig == [0x55, 0xaa]) | |||
} | |||
|
|||
pub fn have_udev() -> bool { | |||
Path::new("/run/udev/control").exists() | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: spacing
src/blockdev.rs
Outdated
let mut cmd_dev: Vec<&Path> = Vec::new(); | ||
let devices; | ||
if dev.is_none() { | ||
devices = { | ||
let mut cmd = Command::new("blkid"); | ||
cmd.arg("--cache-file"); | ||
cmd.arg("/dev/null"); | ||
cmd.arg("-o"); | ||
cmd.arg("device"); | ||
cmd_output(&mut cmd)? | ||
}; | ||
|
||
cmd_dev.extend(devices | ||
.lines() | ||
.filter_map(|line|Some(Path::new(line.trim()))) | ||
); | ||
} else { | ||
if let Some(path) = dev { | ||
cmd_dev.extend(path); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more idiomatic way to restructure all this is:
let cmd_dev = if let Some(paths) = dev {
paths
} else {
let devices = {
...
};
devices.lines().map(|line| Path::new(line.trim())).collect()
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the free rust class @jlebon ;)
d73ae86
to
0c30375
Compare
- OSBuild does not allow udev rules in loop devices, for this reason can not rely on labels on the partitions to create the osmet image, due it use blkid instead of lsblk that relys on udev. Signed-off-by: Renata Ravanelli <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just added spacing and tweaked some of the variable names.
LGTM!
need to skip this part and pass only the fstype
for the it to be mounted.