Skip to content

Commit

Permalink
Check the mkfs.xfs version
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Nov 28, 2023
1 parent effbb63 commit 7bc08d1
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions src/engine/strat_engine/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,65 @@ fn get_clevis_executable(name: &str) -> StratisResult<PathBuf> {
}
}

// Return the mkfs.xfs version reported by the "-V" option as a string.
fn get_mkfs_xfs_version() -> StratisResult<String> {
let mut command = Command::new(get_executable(MKFS_XFS).as_os_str())
.arg("-V")
.stdout(Stdio::piped())
.spawn()?;
command.wait()?;

let mut output = String::new();
let is_ok = command.wait()?.code() == Some(0);
command
.stdout
.ok_or_else(|| {
StratisError::Msg("Could not read string value from xfs.mkfs output".to_string())
})?
.read_to_string(&mut output)?;

if is_ok {
output
.trim()
.strip_prefix("mkfs.xfs version ")
.ok_or_else(|| {
StratisError::Msg("Could not parse version string from mkfs.xfs output".to_string())
})
.map(|v| v.to_string())
} else {
Err(StratisError::Msg(
"\"mkfs.xfs -V\" returned an error".to_string(),
))
}
}

/// Create a filesystem on devnode. If uuid specified, set the UUID of the
/// filesystem on creation. If 'noalign', set the noalign option.
pub fn create_fs(devnode: &Path, uuid: Option<StratisUuid>) -> StratisResult<()> {
// If the version can not be obtained, which is unlikely, assume that the
// version of mkfs.xfs is new enough to use the nrext64 option. This will
// become more and more true with time. If the version is not high enough,
// this will be obvious when the command-line invocation fails.
// The check only verifies that the major number is greater than 5, because
// before that time, the larger extent counter size was not used by default,
// so, for earlier versions, it is unnecessary to turn it off explicitly.
let use_nrext64_option = match get_mkfs_xfs_version().and_then(|v| {
v.split('.')
.next()
.ok_or_else(|| StratisError::Msg("".to_string()))
.and_then(|n| {
n.parse::<u64>()
.map_err(|_| StratisError::Msg("".to_string()))
.map(|r| r > 5u64)
})
}) {
Ok(val) => val,
Err(err) => {
warn!("Unable to read version of mkfs.xfs: {err}; guessing that mkfs.xfs version is high enough to support nrext64 option");
true
}
};

let mut command = Command::new(get_executable(MKFS_XFS).as_os_str());
command.arg("-f");
command.arg("-q");
Expand All @@ -250,8 +306,10 @@ pub fn create_fs(devnode: &Path, uuid: Option<StratisUuid>) -> StratisResult<()>
// be a costly operation. If a filesystem is created with the larger size
// extent counters, which is the default for mkfs.xfs >= 6.5.0, then it
// will be unmountable on kernels which do not support the larger size.
command.arg("-i");
command.arg("nrext64=0");
if use_nrext64_option {
command.arg("-i");
command.arg("nrext64=0");
}

execute_cmd(&mut command)
}
Expand Down

0 comments on commit 7bc08d1

Please sign in to comment.