move configs to dkl crate

This commit is contained in:
Mikaël Cluseau
2025-07-17 16:15:36 +02:00
parent e30a46d62b
commit 423a9c53e6
15 changed files with 1419 additions and 350 deletions

View File

@ -0,0 +1 @@

View File

@ -1,11 +1,12 @@
use eyre::{format_err, Result};
use eyre::{Result, format_err};
use log::{error, info, warn};
use std::collections::BTreeSet as Set;
use std::os::unix::fs::symlink;
use tokio::sync::Mutex;
use tokio::{fs, process::Command};
use crate::{bootstrap::config::Config, cmd::version::version_string, dklog, input, utils};
use crate::{cmd::version::version_string, dklog, input, utils};
use dkl::bootstrap::Config;
mod bootstrap;
mod dmcrypt;
@ -390,9 +391,9 @@ fn cmd_str(prog: &str, args: &[&str]) -> (String, Command) {
#[allow(unused)]
async fn child_reaper() {
use nix::sys::wait::{waitpid, WaitPidFlag};
use nix::sys::wait::{WaitPidFlag, waitpid};
use nix::unistd::Pid;
use tokio::signal::unix::{signal, SignalKind};
use tokio::signal::unix::{SignalKind, signal};
let Ok(mut sigs) =
signal(SignalKind::child()).inspect_err(|e| warn!("failed to setup SIGCHLD handler: {e}"))
@ -416,7 +417,7 @@ async fn switch_root(root: &str) -> Result<()> {
info!("killing all processes and switching root");
dklog::LOG.close().await;
use nix::sys::signal::{kill, SIGKILL};
use nix::sys::signal::{SIGKILL, kill};
use nix::unistd::Pid;
if let Err(e) = kill(Pid::from_raw(-1), SIGKILL) {

View File

@ -1,14 +1,18 @@
use eyre::{format_err, Result};
use eyre::{Result, format_err};
use log::{info, warn};
use std::path::Path;
use tokio::{
fs,
io::{AsyncBufReadExt, BufReader},
};
use dkl::{
self,
apply::{self, chroot, set_perms},
bootstrap::Config,
};
use super::{exec, mount, retry, retry_or_ignore, try_exec};
use crate::bootstrap::config::Config;
use crate::{dkl, utils};
use crate::utils;
pub async fn bootstrap(cfg: Config) {
let verifier = retry(async || Verifier::from_config(&cfg)).await;
@ -50,7 +54,7 @@ pub async fn bootstrap(cfg: Config) {
})
.await;
retry_or_ignore(async || apply_files(&sys_cfg.files, "/system").await).await;
retry_or_ignore(async || apply::files(&sys_cfg.files, "/system").await).await;
apply_groups(&sys_cfg.groups, "/system").await;
apply_users(&sys_cfg.users, "/system").await;
@ -77,7 +81,7 @@ impl Verifier {
return Ok(Self { pubkey: None });
};
use base64::{prelude::BASE64_STANDARD, Engine};
use base64::{Engine, prelude::BASE64_STANDARD};
let pubkey = BASE64_STANDARD.decode(pubkey)?;
let pubkey = Some(pubkey);
@ -250,47 +254,6 @@ async fn mount_system(cfg: &dkl::Config, bs_dir: &str, verifier: &Verifier) {
.await;
}
fn chroot(root: &str, path: &str) -> String {
format!("{root}/{}", path.trim_start_matches(|c| c == '/'))
}
async fn apply_files(files: &[dkl::File], root: &str) -> Result<()> {
for file in files {
let path = chroot(root, &file.path);
let path = Path::new(&path);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).await?;
}
use crate::dkl::FileKind as K;
match &file.kind {
K::Content(content) => fs::write(path, content.as_bytes()).await?,
K::Dir(true) => fs::create_dir(path).await?,
K::Dir(false) => {} // shouldn't happen, but semantic is to ignore
K::Symlink(tgt) => fs::symlink(tgt, path).await?,
}
match file.kind {
K::Symlink(_) => {}
_ => set_perms(path, file.mode).await?,
}
info!("created {}", file.path);
}
Ok(())
}
async fn set_perms(path: impl AsRef<Path>, mode: Option<u32>) -> std::io::Result<()> {
if let Some(mode) = mode.filter(|m| *m != 0) {
use std::os::unix::fs::PermissionsExt;
let mode = std::fs::Permissions::from_mode(mode);
fs::set_permissions(path, mode).await?;
}
Ok(())
}
async fn apply_groups(groups: &[dkl::Group], root: &str) {
for group in groups {
let mut args = vec![root, "groupadd", "-r"];

View File

@ -1,4 +1,4 @@
use eyre::{format_err, Result};
use eyre::{Result, format_err};
use log::{error, info, warn};
use std::collections::BTreeSet as Set;
use std::process::Stdio;
@ -6,11 +6,11 @@ use tokio::io::AsyncWriteExt;
use tokio::process::Command;
use tokio::sync::Mutex;
use super::{retry_or_ignore, USED_DEVS};
use super::{USED_DEVS, retry_or_ignore};
use crate::blockdev::{is_uninitialized, uninitialize};
use crate::bootstrap::config::{CryptDev, DevFilter};
use crate::fs::walk_dir;
use crate::input;
use dkl::bootstrap::{CryptDev, DevFilter};
pub async fn setup(devs: &[CryptDev]) {
if devs.is_empty() {

View File

@ -1,11 +1,11 @@
use eyre::{format_err, Result};
use eyre::{Result, format_err};
use log::{error, info, warn};
use tokio::process::Command;
use super::{exec, retry, retry_or_ignore, USED_DEVS};
use crate::bootstrap::config::{Config, Filesystem, LvSize, LvmLV, LvmVG, TAKE_ALL};
use super::{USED_DEVS, exec, retry, retry_or_ignore};
use crate::fs::walk_dir;
use crate::{blockdev, lvm};
use dkl::bootstrap::{Config, Filesystem, LvSize, LvmLV, LvmVG, TAKE_ALL};
pub async fn setup(cfg: &Config) {
if cfg.lvm.is_empty() {

View File

@ -3,12 +3,12 @@ use log::{info, warn};
use std::collections::BTreeSet as Set;
use tokio::process::Command;
use super::{format_err, retry_or_ignore, Config, Result};
use super::{Result, format_err, retry_or_ignore};
use crate::{
bootstrap::config,
udev,
utils::{select_n_by_regex, NameAliases},
utils::{NameAliases, select_n_by_regex},
};
use dkl::bootstrap::{Config, Network};
pub async fn setup(cfg: &Config) {
if cfg.networks.is_empty() {
@ -23,7 +23,7 @@ pub async fn setup(cfg: &Config) {
}
}
async fn setup_network(net: &config::Network, assigned: &mut Set<String>) -> Result<()> {
async fn setup_network(net: &Network, assigned: &mut Set<String>) -> Result<()> {
info!("setting up network {}", net.name);
let netdevs = get_interfaces()?

View File

@ -7,7 +7,7 @@ use tokio::net;
use tokio::process::Command;
use super::retry_or_ignore;
use crate::bootstrap::config::{Config, SSHServer};
use dkl::bootstrap::{Config, SSHServer};
pub async fn start(cfg: &Config) {
retry_or_ignore(async || {