kanit

Toy init system
Log | Files | Refs | README | LICENSE

swap.rs (1553B)


      1 use async_process::{Command, Stdio};
      2 use async_trait::async_trait;
      3 use log::info;
      4 
      5 use kanit_common::error::{Context, ErrorKind, Result, StaticError};
      6 use kanit_unit::{Dependencies, Unit};
      7 
      8 use crate::oneshot::{Clock, LocalMount, RootFs};
      9 use crate::unit_name;
     10 
     11 pub struct Swap;
     12 
     13 #[async_trait]
     14 impl Unit for Swap {
     15     unit_name!("swap");
     16 
     17     fn dependencies(&self) -> Dependencies {
     18         Dependencies::new()
     19             .need(RootFs.name())
     20             .after(Clock.name())
     21             .before(LocalMount.name())
     22             .clone()
     23     }
     24 
     25     async fn start(&mut self) -> Result<()> {
     26         info!("mounting swap");
     27 
     28         let succ = Command::new("swapon")
     29             .stdout(Stdio::null())
     30             .arg("-a")
     31             .spawn()
     32             .context("failed to spawn swapon")?
     33             .status()
     34             .await
     35             .context("failed to wait on swapon")?
     36             .success();
     37 
     38         if !succ {
     39             Err(StaticError("failed to enable swap")).kind(ErrorKind::Recoverable)?;
     40         }
     41 
     42         Ok(())
     43     }
     44 
     45     async fn stop(&mut self) -> Result<()> {
     46         info!("unmounting swap");
     47 
     48         let succ = Command::new("swapoff")
     49             .stdout(Stdio::null())
     50             .arg("-a")
     51             .spawn()
     52             .context("failed to spawn swapon")?
     53             .status()
     54             .await
     55             .context("failed to wait on swapon")?
     56             .success();
     57 
     58         if !succ {
     59             Err(StaticError("failed to disable swap")).kind(ErrorKind::Recoverable)?;
     60         }
     61 
     62         Ok(())
     63     }
     64 }