Skip to content

Commit

Permalink
Add nvme support
Browse files Browse the repository at this point in the history
  • Loading branch information
cholcombe973 committed Sep 27, 2017
1 parent 1761eaf commit 5772575
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 12 deletions.
23 changes: 12 additions & 11 deletions api/protos/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ message Osd {
required uint64 total_space = 6;
}

message RepairResponse {
required bool corrupted = 1;
required bool repaired = 2;
required bool in_progress = 3;
required string tracking_ticket_id = 4;
required Disk disk = 5;
optional string mount_path = 6;
}
// This will be added in a future version
//message RepairResponse {
//required bool corrupted = 1;
//required bool repaired = 2;
//required bool in_progress = 3;
//required string tracking_ticket_id = 4;
//required Disk disk = 5;
//optional string mount_path = 6;
//}

message Disk {
required DiskType type = 1;
Expand All @@ -31,11 +32,11 @@ message Disk {
}

enum DiskType {
/// AKA SSD
// AKA SSD
SOLID_STATE = 0;
/// Regular rotational device
// Regular rotational device
ROTATIONAL = 1;
/// Special loopback device
// Special loopback device
LOOPBACK = 2;
// Logical volume device
LVM = 3;
Expand Down
95 changes: 95 additions & 0 deletions src/nvme.rs
Original file line number Diff line number Diff line change
@@ -1 +1,96 @@
extern crate serde_json;

use std::io::Result;
use std::process::Command;



/// Retrieve the error logs from the nvme device
pub fn get_error_log(dev: &Path) -> Result<String>{
let out = Command::new("nvme").args(&["error-log", &dev.to_string_lossy(), "-o", "json"]).output()
if !out.status.success() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::NotFound,
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
let deserialized: Vec<String> = serde_json::from_str(&s)?;
Ok(deserialized)
}

/// Retrieve the firmware logs from the nvme device
pub fn get_firmware_log(dev: &Path) -> Result<String>{
let out = Command::new("nvme").args(&["fw-log", &dev.to_string_lossy(), "-o", "json"]).output()
if !out.status.success() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::NotFound,
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
let deserialized: Vec<String> = serde_json::from_str(&s)?;
Ok(deserialized)
}

/// Retrieve the smart logs from the nvme device
pub fn get_smart_log(dev: &Path) -> Result<String>{
let out = Command::new("nvme").args(&["smart-log", &dev.to_string_lossy(), "-o", "json"]).output()
if !out.status.success() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::NotFound,
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
let deserialized: Vec<String> = serde_json::from_str(&s)?;
Ok(deserialized)
}

// Format an nvme block device
pub fn format(dev: &Path) -> Result<()>{
let out = Command::new("nvme").args(&["format", &dev.to_string_lossy()]).output()
if !out.status.success() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::NotFound,
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
Ok(())
}

pub fn list_nvme_namespaces(dev: &Path) -> Result<Vec<String>>{
let out = Command::new("nvme").args(&["list-ns", &device.to_string_lossy(), "-o", "json"]).output()
if !out.status.success() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::NotFound,
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
let deserialized: Vec<String> = serde_json::from_str(&s)?;
Ok(deserialized)

}

/// List the nvme controllers on the host
pub fn list_nvme_controllers() -> Result<Vec<String>>{
let out = Command::new("nvme-list").args(&["-o", "json"]).output()
if !out.status.success() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::NotFound,
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
let deserialized: Vec<String> = serde_json::from_str(&s)?;
Ok(deserialized)
}

/// List the nvme devices on the host
pub fn list_nvme_devices()->Result<Vec<String>>{
let out = Command::new("nvme-list").args(&["-o", "json"]).output()
if !out.status.success() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::NotFound,
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
let deserialized: Vec<String> = serde_json::from_str(&s)?;
Ok(deserialized)
}
20 changes: 19 additions & 1 deletion src/test_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub struct Status {
pub device: Device,
/// Osd that was operated on
pub mount_path: PathBuf,
/// If smart is supported this filed will be filled in
pub smart_passed: Option<bool>,
}

pub fn check_all_disks() -> Result<Vec<Result<Status>>> {
Expand All @@ -48,6 +50,11 @@ pub fn check_all_disks() -> Result<Vec<Result<Status>>> {
Error::new(ErrorKind::Other, e)
})?;

//Check all devices we've discovered
for device in devices {
results.push(run_checks(&device));
}

// Gather info on all devices and skip Loopback devices
let device_info: Vec<Device> = block_utils::get_all_device_info(devices.as_slice())
.map_err(|e| Error::new(ErrorKind::Other, e))?
Expand Down Expand Up @@ -76,6 +83,8 @@ pub fn check_all_disks() -> Result<Vec<Result<Status>>> {
results.push(run_checks(&mtab_device));
}

//TODO: Add nvme devices to block-utils

Ok(results)
}

Expand All @@ -85,8 +94,18 @@ fn run_checks(device_info: &Device) -> Result<Status> {
repaired: false,
device: device_info.clone(),
mount_path: PathBuf::from(""),
smart_passed: None,
};
let dev_path = format!("/dev/{}", device_info.name);

// Run a smart check on the base device without partition
match run_smart_checks(dev_path.file_name()){
Ok(result) => {disk_status.smart_passed = Ok(result);}
Err(e) => {
error!("Smart test failed: {:?}", e);
};
};

let device = Path::new(&dev_path);
match get_mountpoint(&device) {
Ok(mount_info) => {
Expand All @@ -109,7 +128,6 @@ fn run_checks(device_info: &Device) -> Result<Status> {
}
};
if corrupted {
// If udev doesn't know about the disk we can't repair it right?
if let Ok(udev_info) = info {
let check_result =
check_filesystem(&udev_info.fs_type, &device);
Expand Down

0 comments on commit 5772575

Please sign in to comment.