zfskit 0.3.0

Async toolkit for OpenZFS: datasets, snapshots, send/recv streams, holds, bookmarks, pools - via the zfs/zpool CLI with JSON output
Documentation
use std::collections::HashMap;

use serde::Deserialize;

use super::common::{OutputVersion, PropertyMap};

// `zpool list -j` and `zpool get -j` produce structurally identical entries.
// Confirmed against captured fixtures from OpenZFS 2.4.1.

#[derive(Debug, Clone, Deserialize)]
pub struct ZpoolListOutput {
    pub output_version: OutputVersion,
    pub pools: HashMap<String, ZpoolListEntry>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ZpoolListEntry {
    pub name: String,
    #[serde(rename = "type")]
    pub kind: String,
    #[serde(default)]
    pub state: String,
    #[serde(default)]
    pub pool_guid: String,
    #[serde(default)]
    pub txg: String,
    #[serde(default)]
    pub spa_version: String,
    #[serde(default)]
    pub zpl_version: String,
    #[serde(default)]
    pub properties: PropertyMap,
}

pub type ZpoolGetEntry = ZpoolListEntry;
pub type ZpoolGetOutput = ZpoolListOutput;

// `zpool status -j` is a different shape — vdev tree, error counts, scrub
// progress.

#[derive(Debug, Clone, Deserialize)]
pub struct ZpoolStatusOutput {
    pub output_version: OutputVersion,
    pub pools: HashMap<String, ZpoolStatusEntry>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ZpoolStatusEntry {
    pub name: String,
    #[serde(default)]
    pub state: String,
    #[serde(default)]
    pub pool_guid: String,
    #[serde(default)]
    pub txg: String,
    #[serde(default)]
    pub spa_version: String,
    #[serde(default)]
    pub zpl_version: String,
    #[serde(default)]
    pub error_count: String,
    /// The pool's root vdev tree. Keyed by vdev name (which equals the pool
    /// name for the synthetic root vdev).
    #[serde(default)]
    pub vdevs: HashMap<String, VdevStatus>,
    /// Most recent scrub / resilver progress, when one is active or has
    /// completed. JSON key is `scan_stats`; some OpenZFS versions omit
    /// this entirely on a pristine pool.
    #[serde(default, rename = "scan_stats")]
    pub scan: Option<ScanStatus>,
}

/// Recursive vdev tree node. Top-level (root) vdevs have `vdev_type = "root"`;
/// leaves are typically `disk` or `file`. `mirror` / `raidz` are interior
/// nodes whose `vdevs` map contains their member disks.
#[derive(Debug, Clone, Deserialize)]
pub struct VdevStatus {
    pub name: String,
    #[serde(default)]
    pub vdev_type: String,
    #[serde(default)]
    pub guid: String,
    #[serde(default)]
    pub path: Option<String>,
    #[serde(default)]
    pub class: String,
    #[serde(default)]
    pub state: String,
    // Space accounting is absent on leaf disks inside interior vdevs
    // (observed with mirror members on zfs 2.3.2); default to empty
    // rather than failing the whole status parse.
    #[serde(default)]
    pub alloc_space: String,
    #[serde(default)]
    pub total_space: String,
    #[serde(default)]
    pub def_space: String,
    #[serde(default)]
    pub read_errors: String,
    #[serde(default)]
    pub write_errors: String,
    #[serde(default)]
    pub checksum_errors: String,
    #[serde(default)]
    pub rep_dev_size: Option<String>,
    #[serde(default)]
    pub phys_space: Option<String>,
    #[serde(default)]
    pub slow_ios: Option<String>,
    #[serde(default)]
    pub vdevs: HashMap<String, VdevStatus>,
}

/// Status of an in-flight scrub / resilver, when one is active or recently
/// completed. OpenZFS sometimes emits this and sometimes omits it; match by
/// `function` (`SCRUB` / `RESILVER` / `NONE`) and `state` (`SCANNING` /
/// `FINISHED` / `CANCELED`).
///
/// Sizes (`to_examine`, `examined`, `skipped`, `processed`, `issued`) are
/// strings shaped like `"523G"` because that's what zpool emits; consumers
/// that need bytes must parse them. `pass_start` is unix seconds.
#[derive(Debug, Clone, Deserialize)]
pub struct ScanStatus {
    pub function: String,
    pub state: String,
    #[serde(default)]
    pub start_time: Option<String>,
    #[serde(default)]
    pub end_time: Option<String>,
    #[serde(default)]
    pub to_examine: Option<String>,
    #[serde(default)]
    pub examined: Option<String>,
    #[serde(default)]
    pub skipped: Option<String>,
    #[serde(default)]
    pub processed: Option<String>,
    #[serde(default)]
    pub errors: Option<String>,
    #[serde(default)]
    pub pass_start: Option<String>,
    #[serde(default)]
    pub scrub_pause: Option<String>,
    #[serde(default)]
    pub scrub_spent_paused: Option<String>,
    #[serde(default)]
    pub issued: Option<String>,
}