Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored. Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
190 lines
5.3 KiB
Rust
190 lines
5.3 KiB
Rust
//! Disk model and protection classification.
|
|
//!
|
|
//! Protection is a pure function of observed facts so it can be unit tested
|
|
//! without touching a real system. The reference behavior protects the live
|
|
//! medium (`/dev/sr0` in the proven VM), mounted disks, read-only devices,
|
|
//! optical/loop/device-mapper nodes, and non-disk classes.
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum DiskKind {
|
|
Physical,
|
|
Optical,
|
|
Loop,
|
|
DeviceMapper,
|
|
Other,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum ProtectionReason {
|
|
ReadOnly,
|
|
NotAWholeDisk(DiskKind),
|
|
MountedFilesystems,
|
|
LiveMedium,
|
|
NonBlockDevice,
|
|
}
|
|
|
|
impl ProtectionReason {
|
|
pub fn describe(&self) -> String {
|
|
match self {
|
|
ProtectionReason::ReadOnly => "read-only device".into(),
|
|
ProtectionReason::NotAWholeDisk(kind) => format!("not a whole disk ({kind:?})"),
|
|
ProtectionReason::MountedFilesystems => "contains mounted filesystems".into(),
|
|
ProtectionReason::LiveMedium => "live installation medium".into(),
|
|
ProtectionReason::NonBlockDevice => "not a block device".into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct Disk {
|
|
pub path: String,
|
|
pub model: Option<String>,
|
|
pub size_bytes: u64,
|
|
pub transport: Option<String>,
|
|
pub removable: bool,
|
|
pub read_only: bool,
|
|
pub mounted: bool,
|
|
pub is_block_device: bool,
|
|
pub kind: DiskKind,
|
|
pub live_media: bool,
|
|
}
|
|
|
|
impl Disk {
|
|
/// Protection reason, in the same order as the proven Bash implementation.
|
|
pub fn protection(&self) -> Option<ProtectionReason> {
|
|
if !self.is_block_device {
|
|
return Some(ProtectionReason::NonBlockDevice);
|
|
}
|
|
if self.read_only {
|
|
return Some(ProtectionReason::ReadOnly);
|
|
}
|
|
match self.kind {
|
|
DiskKind::Optical | DiskKind::Loop | DiskKind::DeviceMapper => {
|
|
return Some(ProtectionReason::NotAWholeDisk(self.kind));
|
|
}
|
|
_ => {}
|
|
}
|
|
if self.mounted {
|
|
return Some(ProtectionReason::MountedFilesystems);
|
|
}
|
|
if self.live_media {
|
|
return Some(ProtectionReason::LiveMedium);
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn is_protected(&self) -> bool {
|
|
self.protection().is_some()
|
|
}
|
|
|
|
pub fn size_human(&self) -> String {
|
|
human_bytes(self.size_bytes)
|
|
}
|
|
|
|
pub fn display_line(&self) -> String {
|
|
let flag = match self.protection() {
|
|
Some(reason) => format!(" [PROTECTED: {}]", reason.describe()),
|
|
None => String::new(),
|
|
};
|
|
format!(
|
|
"{:<16} {:>8} {:<8} {}{}",
|
|
self.path,
|
|
self.size_human(),
|
|
self.transport.clone().unwrap_or_else(|| "?".into()),
|
|
self.model.clone().unwrap_or_else(|| "disk".into()),
|
|
flag
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Format bytes using binary (IEC) units, e.g. `30.0 GiB`.
|
|
pub fn human_bytes(bytes: u64) -> String {
|
|
const UNITS: [&str; 6] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
|
|
if bytes == 0 {
|
|
return "0 B".into();
|
|
}
|
|
let mut value = bytes as f64;
|
|
let mut unit = 0;
|
|
while value >= 1024.0 && unit + 1 < UNITS.len() {
|
|
value /= 1024.0;
|
|
unit += 1;
|
|
}
|
|
if unit == 0 {
|
|
format!("{bytes} B")
|
|
} else {
|
|
format!("{value:.1} {}", UNITS[unit])
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn physical() -> Disk {
|
|
Disk {
|
|
path: "/dev/vda".into(),
|
|
model: Some("QEMU HARDDISK".into()),
|
|
size_bytes: 30 * 1024 * 1024 * 1024,
|
|
transport: Some("virtio".into()),
|
|
removable: false,
|
|
read_only: false,
|
|
mounted: false,
|
|
is_block_device: true,
|
|
kind: DiskKind::Physical,
|
|
live_media: false,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn clean_target_is_unprotected() {
|
|
assert!(physical().protection().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn optical_live_media_is_protected() {
|
|
let mut d = physical();
|
|
d.path = "/dev/sr0".into();
|
|
d.kind = DiskKind::Optical;
|
|
d.live_media = true;
|
|
assert!(d.is_protected());
|
|
assert!(matches!(
|
|
d.protection(),
|
|
Some(ProtectionReason::NotAWholeDisk(DiskKind::Optical))
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn mounted_is_protected() {
|
|
let mut d = physical();
|
|
d.mounted = true;
|
|
assert_eq!(d.protection(), Some(ProtectionReason::MountedFilesystems));
|
|
}
|
|
|
|
#[test]
|
|
fn read_only_wins() {
|
|
let mut d = physical();
|
|
d.read_only = true;
|
|
d.mounted = true;
|
|
assert_eq!(d.protection(), Some(ProtectionReason::ReadOnly));
|
|
}
|
|
|
|
#[test]
|
|
fn loop_and_dm_are_protected() {
|
|
for kind in [DiskKind::Loop, DiskKind::DeviceMapper] {
|
|
let mut d = physical();
|
|
d.kind = kind;
|
|
assert!(d.is_protected());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn human_bytes_formats() {
|
|
assert_eq!(human_bytes(0), "0 B");
|
|
assert_eq!(human_bytes(1024), "1.0 KiB");
|
|
assert_eq!(human_bytes(30 * 1024 * 1024 * 1024), "30.0 GiB");
|
|
}
|
|
}
|