src/tools.rs: add new run_command helper
This commit is contained in:
parent
b9cf6ee797
commit
144006fade
15
src/tools.rs
15
src/tools.rs
|
@ -480,7 +480,7 @@ pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> {
|
||||||
/// is considered successful.
|
/// is considered successful.
|
||||||
pub fn command_output(
|
pub fn command_output(
|
||||||
output: std::process::Output,
|
output: std::process::Output,
|
||||||
exit_code_check: Option<fn(i32) -> bool>
|
exit_code_check: Option<fn(i32) -> bool>,
|
||||||
) -> Result<String, Error> {
|
) -> Result<String, Error> {
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
|
@ -507,6 +507,19 @@ pub fn command_output(
|
||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn run_command(
|
||||||
|
mut command: std::process::Command,
|
||||||
|
exit_code_check: Option<fn(i32) -> bool>,
|
||||||
|
) -> Result<String, Error> {
|
||||||
|
|
||||||
|
let output = command.output()
|
||||||
|
.map_err(|err| format_err!("failed to execute {:?} - {}", command, err))?;
|
||||||
|
|
||||||
|
let output = crate::tools::command_output(output, exit_code_check)
|
||||||
|
.map_err(|err| format_err!("command {:?} failed - {}", command, err))?;
|
||||||
|
|
||||||
|
Ok(output)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), Error> {
|
pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), Error> {
|
||||||
use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD};
|
use nix::fcntl::{fcntl, FdFlag, F_GETFD, F_SETFD};
|
||||||
|
|
|
@ -563,11 +563,7 @@ pub fn get_partition_type_info() -> Result<HashMap<String, Vec<String>>, Error>
|
||||||
let mut command = std::process::Command::new(LSBLK_BIN_PATH);
|
let mut command = std::process::Command::new(LSBLK_BIN_PATH);
|
||||||
command.args(&["--json", "-o", "path,parttype"]);
|
command.args(&["--json", "-o", "path,parttype"]);
|
||||||
|
|
||||||
let output = command.output()
|
let output = crate::tools::run_command(command, None)?;
|
||||||
.map_err(|err| format_err!("failed to execute '{}' - {}", LSBLK_BIN_PATH, err))?;
|
|
||||||
|
|
||||||
let output = crate::tools::command_output(output, None)
|
|
||||||
.map_err(|err| format_err!("lsblk command failed: {}", err))?;
|
|
||||||
|
|
||||||
let mut res: HashMap<String, Vec<String>> = HashMap::new();
|
let mut res: HashMap<String, Vec<String>> = HashMap::new();
|
||||||
|
|
||||||
|
@ -865,11 +861,7 @@ pub fn reread_partition_table(disk: &Disk) -> Result<(), Error> {
|
||||||
command.arg("--rereadpt");
|
command.arg("--rereadpt");
|
||||||
command.arg(disk_path);
|
command.arg(disk_path);
|
||||||
|
|
||||||
let output = command.output()
|
crate::tools::run_command(command, None)?;
|
||||||
.map_err(|err| format_err!("failed to execute '{}' - {}", BLOCKDEV_BIN_PATH, err))?;
|
|
||||||
|
|
||||||
crate::tools::command_output(output, None)
|
|
||||||
.map_err(|err| format_err!("re-read partition table failed: {}", err))?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -888,11 +880,7 @@ pub fn inititialize_gpt_disk(disk: &Disk, uuid: Option<&str>) -> Result<(), Erro
|
||||||
command.arg(disk_path);
|
command.arg(disk_path);
|
||||||
command.args(&["-U", uuid]);
|
command.args(&["-U", uuid]);
|
||||||
|
|
||||||
let output = command.output()
|
crate::tools::run_command(command, None)?;
|
||||||
.map_err(|err| format_err!("failed to execute '{}' - {}", SGDISK_BIN_PATH, err))?;
|
|
||||||
|
|
||||||
crate::tools::command_output(output, None)
|
|
||||||
.map_err(|err| format_err!("sgdisk command failed: {}", err))?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -909,11 +897,7 @@ pub fn create_single_linux_partition(disk: &Disk) -> Result<Disk, Error> {
|
||||||
command.args(&["-n1", "-t1:8300"]);
|
command.args(&["-n1", "-t1:8300"]);
|
||||||
command.arg(disk_path);
|
command.arg(disk_path);
|
||||||
|
|
||||||
let output = command.output()
|
crate::tools::run_command(command, None)?;
|
||||||
.map_err(|err| format_err!("failed to execute '{}' - {}", SGDISK_BIN_PATH, err))?;
|
|
||||||
|
|
||||||
crate::tools::command_output(output, None)
|
|
||||||
.map_err(|err| format_err!("sgdisk command failed: {}", err))?;
|
|
||||||
|
|
||||||
let mut partitions = disk.partitions()?;
|
let mut partitions = disk.partitions()?;
|
||||||
|
|
||||||
|
@ -924,7 +908,7 @@ pub fn create_single_linux_partition(disk: &Disk) -> Result<Disk, Error> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[api()]
|
#[api()]
|
||||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq)]
|
||||||
#[serde(rename_all="lowercase")]
|
#[serde(rename_all="lowercase")]
|
||||||
pub enum FileSystemType {
|
pub enum FileSystemType {
|
||||||
/// Linux Ext4
|
/// Linux Ext4
|
||||||
|
@ -933,6 +917,16 @@ pub enum FileSystemType {
|
||||||
Xfs,
|
Xfs,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for FileSystemType {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
let text = match self {
|
||||||
|
FileSystemType::Ext4 => "ext4",
|
||||||
|
FileSystemType::Xfs => "xfs",
|
||||||
|
};
|
||||||
|
write!(f, "{}", text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a file system on a disk or disk partition
|
/// Create a file system on a disk or disk partition
|
||||||
pub fn create_file_system(disk: &Disk, fs_type: FileSystemType) -> Result<(), Error> {
|
pub fn create_file_system(disk: &Disk, fs_type: FileSystemType) -> Result<(), Error> {
|
||||||
|
|
||||||
|
@ -941,20 +935,13 @@ pub fn create_file_system(disk: &Disk, fs_type: FileSystemType) -> Result<(), Er
|
||||||
None => bail!("disk {:?} has no node in /dev", disk.syspath()),
|
None => bail!("disk {:?} has no node in /dev", disk.syspath()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let fs_type = match fs_type {
|
let fs_type = fs_type.to_string();
|
||||||
FileSystemType::Ext4 => "ext4",
|
|
||||||
FileSystemType::Xfs => "xfs",
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut command = std::process::Command::new(MKFS_BIN_PATH);
|
let mut command = std::process::Command::new(MKFS_BIN_PATH);
|
||||||
command.args(&["-t", fs_type]);
|
command.args(&["-t", &fs_type]);
|
||||||
command.arg(disk_path);
|
command.arg(disk_path);
|
||||||
|
|
||||||
let output = command.output()
|
crate::tools::run_command(command, None)?;
|
||||||
.map_err(|err| format_err!("failed to execute '{}' - {}", MKFS_BIN_PATH, err))?;
|
|
||||||
|
|
||||||
crate::tools::command_output(output, None)
|
|
||||||
.map_err(|err| format_err!("mkfs {} failed: {}", fs_type, err))?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -992,11 +979,7 @@ pub fn get_fs_uuid(disk: &Disk) -> Result<String, Error> {
|
||||||
command.args(&["-o", "export"]);
|
command.args(&["-o", "export"]);
|
||||||
command.arg(disk_path);
|
command.arg(disk_path);
|
||||||
|
|
||||||
let output = command.output()
|
let output = crate::tools::run_command(command, None)?;
|
||||||
.map_err(|err| format_err!("failed to execute '{}' - {}", BLKID_BIN_PATH, err))?;
|
|
||||||
|
|
||||||
let output = crate::tools::command_output(output, None)
|
|
||||||
.map_err(|err| format_err!("blkid command failed: {}", err))?;
|
|
||||||
|
|
||||||
for line in output.lines() {
|
for line in output.lines() {
|
||||||
if line.starts_with("UUID=") {
|
if line.starts_with("UUID=") {
|
||||||
|
|
Loading…
Reference in New Issue