Skip to main content

aprender_zram_cli/commands/
create.rs

1//! Create zram device command.
2//!
3//! This is a pure shim that delegates to `trueno_zram_core::zram`.
4
5use clap::Args;
6use trueno_zram_core::zram::{format_size, parse_size, SysfsOps, ZramConfig, ZramOps};
7
8/// Arguments for creating a zram device.
9#[derive(Debug, Args)]
10pub struct CreateArgs {
11    /// Device number (0-16).
12    #[arg(short, long, default_value = "0", value_parser = clap::value_parser!(u32).range(0..=16))]
13    pub device: u32,
14
15    /// Device size (e.g., "4G", "512M", "ram/2").
16    #[arg(short, long)]
17    pub size: String,
18
19    /// Compression algorithm (lz4, zstd).
20    #[arg(short, long, default_value = "lz4")]
21    pub algorithm: String,
22
23    /// Number of compression streams (0 = auto).
24    #[arg(long, default_value = "0")]
25    pub streams: u32,
26}
27
28/// Create and configure a zram device.
29///
30/// # Errors
31/// Returns an error if the zram device cannot be created or configured.
32pub fn create(args: &CreateArgs) -> Result<(), Box<dyn std::error::Error>> {
33    let size_bytes = parse_size(&args.size)?;
34
35    let config = ZramConfig {
36        device: args.device,
37        size: size_bytes,
38        algorithm: args.algorithm.clone(),
39        streams: args.streams,
40    };
41
42    let ops = SysfsOps::new();
43    ops.create(&config)?;
44
45    println!(
46        "Created zram{} with size {} using {}",
47        args.device,
48        format_size(size_bytes),
49        args.algorithm
50    );
51
52    Ok(())
53}