1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
extern crate serde_json;

use std::io::{Error, ErrorKind, Result};
use std::path::Path;
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(&out.stderr).into_owned(),
        ));
    }
    let stdout = String::from_utf8_lossy(&out.stdout);
    let deserialized: String = serde_json::from_str(&stdout).map_err(|e| {
        Error::new(ErrorKind::Other, e)
    })?;
    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(&out.stderr).into_owned(),
        ));
    }
    let stdout = String::from_utf8_lossy(&out.stdout);
    let deserialized: String = serde_json::from_str(&stdout).map_err(|e| {
        Error::new(ErrorKind::Other, e)
    })?;
    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(&out.stderr).into_owned(),
        ));
    }
    let stdout = String::from_utf8_lossy(&out.stdout);
    let deserialized: String = serde_json::from_str(&stdout).map_err(|e| {
        Error::new(ErrorKind::Other, e)
    })?;
    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(&out.stderr).into_owned(),
        ));
    }
    Ok(())
}

pub fn list_nvme_namespaces(dev: &Path) -> Result<Vec<String>> {
    let out = Command::new("nvme")
        .args(&["list-ns", &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(&out.stderr).into_owned(),
        ));
    }
    let stdout = String::from_utf8_lossy(&out.stdout);
    let deserialized: Vec<String> = serde_json::from_str(&stdout).map_err(|e| {
        Error::new(ErrorKind::Other, e)
    })?;
    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(&out.stderr).into_owned(),
        ));
    }
    let stdout = String::from_utf8_lossy(&out.stdout);
    let deserialized: Vec<String> = serde_json::from_str(&stdout).map_err(|e| {
        Error::new(ErrorKind::Other, e)
    })?;
    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(&out.stderr).into_owned(),
        ));
    }
    let stdout = String::from_utf8_lossy(&out.stdout);
    let deserialized: Vec<String> = serde_json::from_str(&stdout).map_err(|e| {
        Error::new(ErrorKind::Other, e)
    })?;
    Ok(deserialized)
}