feat: bootstrap awg2 harness rust workspace and docs
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
use std::{fs, process::Command};
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
pub fn ensure_swap(cfg: &Config) -> anyhow::Result<()> {
|
||||
if !cfg.swap.enabled {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let path = &cfg.swap.path;
|
||||
if !std::path::Path::new(path).exists() {
|
||||
run("fallocate", &["-l", &format!("{}M", cfg.swap.size_mb), path])?;
|
||||
run("chmod", &["600", path])?;
|
||||
run("mkswap", &[path])?;
|
||||
}
|
||||
|
||||
let swapon = Command::new("swapon").args(["--show"]).output()?;
|
||||
let active = String::from_utf8_lossy(&swapon.stdout);
|
||||
if !active.contains(path) {
|
||||
run("swapon", &["-p", &cfg.swap.priority.to_string(), path])?;
|
||||
}
|
||||
|
||||
ensure_fstab(path, cfg.swap.priority)?;
|
||||
run("sysctl", &["-w", &format!("vm.swappiness={}", cfg.swap.swappiness)])?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn ensure_fstab(path: &str, priority: i32) -> anyhow::Result<()> {
|
||||
let file = "/etc/fstab";
|
||||
let content = fs::read_to_string(file).unwrap_or_default();
|
||||
if content.lines().any(|l| l.contains(path) && l.contains("swap")) {
|
||||
return Ok(());
|
||||
}
|
||||
let line = format!("{path} none swap sw,pri={priority} 0 0\n");
|
||||
fs::write(file, format!("{}{}", content, line))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run(program: &str, args: &[&str]) -> anyhow::Result<()> {
|
||||
let out = Command::new(program).args(args).output()?;
|
||||
if !out.status.success() {
|
||||
anyhow::bail!(
|
||||
"{} {:?} failed: {}",
|
||||
program,
|
||||
args,
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user