Skip to content

Commit e900eae

Browse files
committed
kata-agent: Add no-udev DmOptions builders and mknod device node helpers
The kata guest VM runs without udev, so device-mapper nodes under /dev/mapper are never created automatically. Add the foundational helpers that subsequent dm-verity integration will rely on: It focus on the following key points: (1) DmOptions builders that disable all udev synchronization flags, with read-only and deferred-remove variants. (2) mknod-based device node creation/removal under /dev/mapper, since devtmpfs nodes are not auto-created without udev. Also add the devicemapper crate dependency (default-features = false). But note that the commit depends on device mapper with no-udev support with the PR:stratis-storage/devicemapper-rs#1036 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
1 parent c471644 commit e900eae

4 files changed

Lines changed: 229 additions & 4 deletions

File tree

‎Cargo.lock‎

Lines changed: 150 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ ttrpc = "0.8.4"
211211
url = "2.5.4"
212212
which = "4.3.0"
213213
gpt = "4.1.0"
214+
devicemapper = { version = "0.34", default-features = false }
214215

215216
# Per-package release profile overrides for kata-deploy. The kata-deploy
216217
# binary runs once at pod start and then idles waiting for SIGTERM, so we
@@ -221,3 +222,4 @@ gpt = "4.1.0"
221222
[profile.release.package."kata-deploy"]
222223
opt-level = "z"
223224
codegen-units = 1
225+

‎src/agent/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ tracing-subscriber.workspace = true
6060
# TODO: bump tracing-opentelemetry to sync with version in workspace
6161
tracing-opentelemetry = "0.17.0"
6262
opentelemetry.workspace = true
63+
devicemapper.workspace = true
6364

6465
# Configuration
6566
serde.workspace = true

‎src/agent/src/storage/multi_layer_erofs.rs‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
//! - Storage with X-kata.overlay-lower: erofs layers (lowerdir)
1212
//! - Creates overlay to combine them
1313
//! - Supports X-kata.mkdir.path options to create directories in upper layer before overlay mount
14-
//! - Supports GPT-partitioned disks where each layer is a separate partition
14+
//! - Supports GPT-partitioned disks with dm-verity integrity verification for each partition
1515
16+
#[allow(unused_imports)]
17+
use nix::sys::stat::{self, Mode, SFlag};
1618
use std::collections::HashMap;
1719
use std::fs;
1820
use std::path::{Path, PathBuf};
@@ -36,6 +38,10 @@ use safe_path::scoped_join;
3638
use slog::Logger;
3739
use tokio::sync::Mutex;
3840

41+
// no-udev device-mapper helpers
42+
#[allow(unused_imports)]
43+
use devicemapper::{DmFlags, DmOptions, DmUdevFlags};
44+
3945
/// EROFS Type
4046
const EROFS_TYPE: &str = "erofs";
4147
/// ext4 Type (upper virtio disk based rw layer)
@@ -54,6 +60,75 @@ const OPT_GPT_PARTITIONED: &str = "X-kata.gpt-partitioned=true";
5460
const OPT_MKDIR_PATH: &str = "X-kata.mkdir.path=";
5561
const OPT_PARTITION_NUMBER: &str = "X-kata.partition-number=";
5662

63+
/// Build DmOptions that fully disable udev synchronization.
64+
#[allow(dead_code)]
65+
fn no_udev_dm_options() -> DmOptions {
66+
DmOptions::default().set_udev_flags(
67+
DmUdevFlags::DM_UDEV_DISABLE_LIBRARY_FALLBACK
68+
| DmUdevFlags::DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG
69+
| DmUdevFlags::DM_UDEV_DISABLE_DISK_RULES_FLAG
70+
| DmUdevFlags::DM_UDEV_DISABLE_OTHER_RULES_FLAG
71+
| DmUdevFlags::DM_UDEV_DISABLE_DM_RULES_FLAG,
72+
)
73+
}
74+
75+
/// Build DmOptions for read-only device removal in a no-udev environment.
76+
#[allow(dead_code)]
77+
fn dm_opts_readonly() -> DmOptions {
78+
no_udev_dm_options().set_flags(DmFlags::DM_READONLY)
79+
}
80+
81+
/// Build DmOptions for deferred device removal in a no-udev environment.
82+
#[allow(dead_code)]
83+
fn dm_opts_deferred_remove() -> DmOptions {
84+
no_udev_dm_options().set_flags(DmFlags::DM_DEFERRED_REMOVE)
85+
}
86+
87+
/// Create a block device node for a dm-verity device using mknod(2).
88+
#[allow(dead_code)]
89+
fn create_dm_dev_node(name: &str, dev: devicemapper::Device) -> Result<String> {
90+
// Ensure /dev/mapper exists.
91+
let mapper_dir = Path::new("/dev/mapper");
92+
if !mapper_dir.exists() {
93+
std::fs::create_dir_all(mapper_dir)
94+
.with_context(|| format!("failed to create directory {}", mapper_dir.display()))?;
95+
}
96+
97+
let dev_path = format!("/dev/mapper/{}", name);
98+
// Remove stale node from a previous failed run, if any
99+
if Path::new(&dev_path).exists() {
100+
std::fs::remove_file(&dev_path)
101+
.with_context(|| format!("failed to remove stale device node {}", dev_path))?;
102+
}
103+
104+
// Use Device -> dev_t conversion from the crate instead of raw makedev.
105+
let dev_t: nix::libc::dev_t = dev.into();
106+
stat::mknod(
107+
dev_path.as_str(),
108+
SFlag::S_IFBLK,
109+
Mode::from_bits_truncate(0o600),
110+
dev_t,
111+
)
112+
.with_context(|| format!("failed to mknod block device {}", dev_path))?;
113+
114+
Ok(dev_path)
115+
}
116+
117+
/// Remove a device node that was created by create_dm_dev_node.
118+
#[allow(dead_code)]
119+
fn remove_dm_dev_node(dev_path: &str) {
120+
if dev_path.starts_with("/dev/mapper/") && Path::new(dev_path).exists() {
121+
if let Err(e) = std::fs::remove_file(dev_path) {
122+
slog::warn!(
123+
slog_scope::logger(),
124+
"failed to remove dm device node";
125+
"path" => dev_path,
126+
"error" => %e,
127+
);
128+
}
129+
}
130+
}
131+
57132
#[derive(Debug)]
58133
pub struct MultiLayerErofsHandler {}
59134

0 commit comments

Comments
 (0)