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
+3 -21
View File
@@ -3,7 +3,7 @@ use log::{debug, info, warn};
use std::path::{Path, PathBuf};
use tokio::{
fs,
io::{AsyncBufReadExt, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader},
io::{AsyncBufReadExt, AsyncWrite, AsyncWriteExt, BufReader},
};
use dkl::{
@@ -13,7 +13,7 @@ use dkl::{
bootstrap::Config,
};
use super::{exec, mount, retry, retry_or_ignore, try_exec, try_exec_cmd};
use super::{exec, mount, mount_ro_fs, retry, retry_or_ignore, try_exec, try_exec_cmd};
use crate::{fs::walk_dir, utils};
pub async fn bootstrap(cfg: Config) {
@@ -340,25 +340,7 @@ impl LayerMounter<'_> {
.await
};
retry(async || {
let mut buf = [0u8; 1028];
fs::File::open(&mount_src)
.await
.map_err(|e| format_err!("open {}: {e}", mount_src.display()))?
.read_exact(&mut buf)
.await
.map_err(|e| format_err!("read {}: {e}", mount_src.display()))?;
let fstype = if buf[1024..1028] == 0xE0F5E1E2u32.to_le_bytes() {
"erofs"
} else {
"squashfs"
};
mount(Some(&mount_src), &tgt_dir, fstype, None).await;
Ok(())
})
.await;
retry(async || mount_ro_fs(&mount_src, &tgt_dir).await).await;
if !self.lower_dir.is_empty() {
self.lower_dir.push(':');