Skip to content

Commit

Permalink
Merge pull request #307 from facebookexperimental/fix-path_to_devname
Browse files Browse the repository at this point in the history
rd-util: Use `findmnt` to map path to devname
  • Loading branch information
htejun authored Mar 14, 2024
2 parents d51ea28 + ba9bfc4 commit d345602
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion rd-util/src/storage_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,15 @@ use std::fs;
use std::io::Read;
use std::os::linux::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::process::Command;

lazy_static::lazy_static! {
static ref MOUNT_LIST: Result<MountList> = Ok(MountList::new()?);
static ref FINDMNT_BIN: String = super::find_bin("findmnt", Option::<&str>::None)
.expect("Failed to find findmnt bin")
.to_str()
.unwrap()
.to_owned();
}

/// Given a path, find out the containing mountpoint.
Expand Down Expand Up @@ -98,7 +104,27 @@ pub fn devname_to_devnr<D: AsRef<OsStr>>(name_in: D) -> Result<(u32, u32)> {

/// Given a path, find the underlying device.
pub fn path_to_devname<P: AsRef<Path>>(path: P) -> Result<OsString> {
devnr_to_devname(fs::metadata(&path_to_mountpoint(path.as_ref())?.source)?.st_rdev())
let mtp = path_to_mountpoint(path.as_ref())?;

// Ideally, mtp.source should point to the device node which can be
// canonicalized by reading its devnr and mapping that to devname.
// Unfortunately, determining the device for a filesystem, especially
// root, is not trivial. For example, $mtp.source might point to
// "/dev/root" which doesn't exist. `findmnt` has a bunch of heuristics
// to map mount point to device. Let's use that instead.
let output = Command::new(&*FINDMNT_BIN)
.arg("-no")
.arg("SOURCE")
.arg(&mtp.dest)
.output()?;

if !output.status.success() {
bail!("findmnt on {:?} failed with {:?}", &mtp.dest, &output);
}

let source = String::from_utf8(output.stdout).unwrap();
trace!("path_to_devname path={:?} source={:?}", path.as_ref(), source.trim());
devnr_to_devname(fs::metadata(source.trim())?.st_rdev())
}

fn read_model(dev_path: &Path) -> Result<String> {
Expand Down

0 comments on commit d345602

Please sign in to comment.