factorize mounting read-only filesystems (squashfs, erofs)

This commit is contained in:
Mikaël Cluseau
2026-06-11 11:35:50 +02:00
parent ee03452591
commit ee0ff1373f
4 changed files with 40 additions and 28 deletions
+33 -3
View File
@@ -4,7 +4,7 @@ use std::collections::BTreeSet as Set;
use std::convert::Infallible;
use std::os::unix::fs::symlink;
use tokio::sync::Mutex;
use tokio::{fs, process::Command};
use tokio::{fs, io::AsyncReadExt, process::Command};
use crate::{cmd::version::version_string, dklog, input, utils};
use dkl::bootstrap::Config;
@@ -154,7 +154,8 @@ use std::path::Path;
async fn mount_modules(modules: &str, kernel_version: &str) -> Result<()> {
info!("mounting modules");
mount(Some(modules), "/modules", "squashfs", None).await;
mount_ro_fs(modules, "/modules").await?;
fs::create_dir_all("/lib/modules").await?;
let modules_path = &format!("/modules/lib/modules/{kernel_version}");
@@ -243,6 +244,35 @@ async fn mount<S: AsRef<Path>>(
.await
}
async fn mount_ro_fs(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
let src = src.as_ref();
let dst = dst.as_ref();
// identify RO fs type
let mut buf = [0u8; 1028];
fs::File::open(src)
.await
.map_err(|e| format_err!("open {}: {e}", src.display()))?
.read_exact(&mut buf)
.await
.map_err(|e| format_err!("read {}: {e}", src.display()))?;
let fstype = if buf[1024..1028] == 0xE0F5E1E2u32.to_le_bytes() {
"erofs"
} else {
"squashfs"
};
if let Err(e) = fs::create_dir_all(dst).await {
error!("failed to create dir {dst}: {e}", dst = dst.display());
}
let mut cmd = Command::new("mount");
cmd.args(["-t", fstype]).arg(src).arg(dst);
try_exec_cmd(cmd).await
}
async fn start_daemon(prog: &str, args: &[&str]) {
let (cmd_str, mut cmd) = cmd_str(prog, args);
retry_or_ignore(async || {
@@ -253,7 +283,7 @@ async fn start_daemon(prog: &str, args: &[&str]) {
.await;
}
async fn try_exec_cmd(mut cmd: tokio::process::Command) -> Result<()> {
async fn try_exec_cmd(mut cmd: Command) -> Result<()> {
info!(
"# {} {}",
cmd.as_std().get_program().to_string_lossy(),