Skip to content

Commit f4f4760

Browse files
committed
Support devicemapper operations without udev synchronization
Previously, UdevSync::begin() returned a hard error when udev was not running, blocking ALL DM ioctl operations — even those that don't require udev sync (CREATE, TABLE_LOAD, STATUS, etc.). Setting DM_UDEV_DISABLE_LIBRARY_FALLBACK could not bypass this because the flag was checked after the udev_running() gate. This commit aims to address the issue with the following key changes: (1) Clear event_nr in do_ioctl() for commands that don't carry a udev cookie (only REMOVE, RENAME, and SUSPEND do). A non-zero event_nr on other commands could cause the kernel to return EINVAL or wait indefinitely for a udev completion that never arrives. (2) Reorder UdevSync::begin() to check whether the ioctl actually requires sync before checking udev_running(). Non-sync ioctls now return an inactive UdevSync immediately. When sync is required but udev is disabled or not running, clear event_nr and return inactive instead of erroring. (3) Extract ioctl_uses_udev_cookie(ioctl: u8) -> bool into dm_ioctl.rs as the single source of truth, replacing duplicated match arms in do_ioctl and UdevSync::begin. (4) Add unit test (test_udevsync_no_udev_options_all_sync_cmds) and integration test (sudo_test_no_udev_device_lifecycle) validating the full dm-verity device lifecycle with all DM_UDEV_DISABLE_* flags set. Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
1 parent 1206bde commit f4f4760

3 files changed

Lines changed: 155 additions & 13 deletions

File tree

‎src/core/dm.rs‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ impl DM {
128128
hdr.version[1] = ioctl_version.1;
129129
hdr.version[2] = ioctl_version.2;
130130

131+
// Clear event_nr for ioctls that don't support udev cookies to avoid EINVAL
132+
if !dmi::ioctl_uses_udev_cookie(ioctl) {
133+
hdr.event_nr = 0;
134+
}
135+
131136
// Begin udev sync transaction and set DM_UDEV_PRIMARY_SOURCE_FLAG
132137
// if ioctl command generates uevents.
133138
let sync = UdevSync::begin(hdr, ioctl)?;
@@ -1142,4 +1147,66 @@ mod tests {
11421147
dm.device_remove(&DevId::Name(&name), DmOptions::default())
11431148
.unwrap();
11441149
}
1150+
1151+
use crate::core::dm_flags::DmUdevFlags;
1152+
1153+
/// Build DmOptions that fully disable udev synchronization.
1154+
/// Setting all DM_UDEV_DISABLE_* flags together with DM_UDEV_DISABLE_LIBRARY_FALLBACK
1155+
/// tells the kernel and the UdevSync layer to skip udev cookie handling entirely.
1156+
fn no_udev_dm_options() -> DmOptions {
1157+
DmOptions::default().set_udev_flags(
1158+
DmUdevFlags::DM_UDEV_DISABLE_LIBRARY_FALLBACK
1159+
| DmUdevFlags::DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG
1160+
| DmUdevFlags::DM_UDEV_DISABLE_DISK_RULES_FLAG
1161+
| DmUdevFlags::DM_UDEV_DISABLE_OTHER_RULES_FLAG
1162+
| DmUdevFlags::DM_UDEV_DISABLE_DM_RULES_FLAG,
1163+
)
1164+
}
1165+
1166+
/// The test verifies that DM operations work correctly when udev
1167+
/// synchronization is fully disabled, simulating the dm-verity device
1168+
/// creation flow in a no-udev guest VM environment.
1169+
///
1170+
/// It simulates the dm-verity device lifecycle:
1171+
/// device_create -> table_load -> device_suspend(resume) -> device_info -> device_remove
1172+
///
1173+
/// All operations use no-udev DmOptions. Uses a read-only error target
1174+
/// (no real block device needed).
1175+
#[test]
1176+
fn sudo_test_no_udev_device_lifecycle() {
1177+
let dm = DM::new().unwrap();
1178+
let name = test_name("no-udev-lifecycle").expect("is valid DM name");
1179+
let id = DevId::Name(&name);
1180+
1181+
let ro_opts = no_udev_dm_options().set_flags(DmFlags::DM_READONLY);
1182+
let opts = no_udev_dm_options();
1183+
1184+
// Step 1: Create device (read-only, no-udev)
1185+
let create_info = dm.device_create(&name, None, ro_opts).unwrap();
1186+
assert_eq!(create_info.name(), Some(&*name));
1187+
1188+
// Step 2: Load a trivial error target table
1189+
let table = vec![(
1190+
0u64,
1191+
1024u64, // 512KB in 512-byte sectors
1192+
"error".into(),
1193+
"".into(),
1194+
)];
1195+
dm.table_load(&id, &table, ro_opts).unwrap();
1196+
1197+
// Step 3: Resume device (activate the table)
1198+
// device_suspend without DM_SUSPEND flag = resume
1199+
dm.device_suspend(&id, opts).unwrap();
1200+
1201+
// Step 4: Verify device is active via device_info
1202+
// device_info uses DmOptions::default(), but DM_DEV_STATUS_CMD
1203+
// does not require udev sync, so it works in no-udev environments
1204+
let info = dm.device_info(&id).unwrap();
1205+
assert_eq!(info.name(), Some(&*name));
1206+
// Device should not be suspended after resume
1207+
assert!(!info.flags().contains(DmFlags::DM_SUSPEND));
1208+
1209+
// Step 5: Remove device with no-udev options
1210+
dm.device_remove(&id, no_udev_dm_options()).unwrap();
1211+
}
11451212
}

‎src/core/dm_ioctl.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,13 @@ pub(crate) fn ioctl_to_version(ioctl: u8) -> (u32, u32, u32) {
5454
unreachable!("Unknown device-mapper ioctl command: {}", ioctl);
5555
}
5656
}
57+
58+
/// Returns true for ioctls whose `event_nr` field carries a udev cookie /
59+
/// udev flags and is honored by the kernel for udev synchronization.
60+
#[inline]
61+
pub(crate) fn ioctl_uses_udev_cookie(ioctl: u8) -> bool {
62+
matches!(
63+
ioctl as u32,
64+
DM_DEV_REMOVE_CMD | DM_DEV_RENAME_CMD | DM_DEV_SUSPEND_CMD
65+
)
66+
}

‎src/core/dm_udev_sync.rs‎

Lines changed: 78 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,48 @@ pub mod sync_semaphore {
293293
/// Allocate a SysV semaphore according to the device-mapper udev cookie
294294
/// protocol and set the initial state of the semaphore counter.
295295
fn begin(hdr: &mut dmi::Struct_dm_ioctl, ioctl: u8) -> DmResult<Self> {
296-
if !udev_running() {
297-
return Err(DmError::Core(errors::Error::UdevSync(
298-
"Udev daemon is not running: unable to create devices.".to_string(),
299-
)));
296+
// First check if this ioctl command requires udev synchronization.
297+
// Only REMOVE, RENAME, and SUSPEND (non-suspended) operations need it.
298+
let requires_sync = dmi::ioctl_uses_udev_cookie(ioctl)
299+
&& *SYSV_SEM_SUPPORTED
300+
&& (hdr.flags & DmFlags::DM_SUSPEND.bits()) == 0;
301+
// If this operation doesn't require udev sync, return immediately
302+
if !requires_sync {
303+
// Strip any udev flags the caller put in event_nr; without a
304+
// semaphore behind them the kernel would otherwise wait for a
305+
// udev completion that will never arrive.
306+
hdr.event_nr = 0;
307+
return Ok(UdevSync {
308+
cookie: 0,
309+
semid: None,
310+
});
300311
}
301312

302-
match ioctl as u32 {
303-
dmi::DM_DEV_REMOVE_CMD | dmi::DM_DEV_RENAME_CMD | dmi::DM_DEV_SUSPEND_CMD
304-
if *SYSV_SEM_SUPPORTED && (hdr.flags & DmFlags::DM_SUSPEND.bits()) == 0 => {}
305-
_ => {
306-
return Ok(UdevSync {
307-
cookie: 0,
308-
semid: None,
309-
});
310-
}
313+
// Check if udev integration has been explicitly disabled via flags.
314+
let udev_disabled = {
315+
let event_flags = hdr.event_nr >> dmi::DM_UDEV_FLAGS_SHIFT;
316+
(event_flags & DmUdevFlags::DM_UDEV_DISABLE_LIBRARY_FALLBACK.bits()) != 0
311317
};
312318

319+
// If udev is disabled (via DM_UDEV_DISABLE_LIBRARY_FALLBACK) or udev
320+
// daemon is not running, gracefully degrade: skip semaphore allocation,
321+
// clear any stale cookie flags from event_nr, and return an inactive
322+
// UdevSync. This allows the library to function in environments without
323+
// udev (e.g. minimal VMs, containers) without requiring every caller
324+
// to explicitly set DM_UDEV_DISABLE_LIBRARY_FALLBACK.
325+
if udev_disabled || !udev_running() {
326+
// Clear event_nr to prevent sending non-zero cookie flags to the
327+
// kernel when we cannot actually perform udev synchronization.
328+
// A non-zero event_nr without a matching semaphore would cause the
329+
// kernel to wait indefinitely for udevd to process the cookie.
330+
hdr.event_nr = 0;
331+
return Ok(UdevSync {
332+
cookie: 0,
333+
semid: None,
334+
});
335+
}
336+
337+
// Udev sync is required and udev is available, allocate semaphore
313338
let (base_cookie, semid) = notify_sem_create()?;
314339

315340
// Encode the primary source flag and the random base cookie value into
@@ -479,6 +504,46 @@ pub mod sync_semaphore {
479504
);
480505
assert!(sync.end(DmFlags::empty().bits()).is_ok());
481506
}
507+
508+
/// Verify that the full no_udev_dm_options() flag combination causes all
509+
/// udev-sync commands to return inactive UdevSync. This is an end-to-end
510+
/// validation of the use case.
511+
#[test]
512+
fn test_udevsync_no_udev_options_all_sync_cmds() {
513+
let no_udev_flags = DmUdevFlags::DM_UDEV_DISABLE_LIBRARY_FALLBACK
514+
| DmUdevFlags::DM_UDEV_DISABLE_SUBSYSTEM_RULES_FLAG
515+
| DmUdevFlags::DM_UDEV_DISABLE_DISK_RULES_FLAG
516+
| DmUdevFlags::DM_UDEV_DISABLE_OTHER_RULES_FLAG
517+
| DmUdevFlags::DM_UDEV_DISABLE_DM_RULES_FLAG;
518+
519+
let event_nr = no_udev_flags.bits() << dmi::DM_UDEV_FLAGS_SHIFT;
520+
521+
for ioctl_cmd in [
522+
dmi::DM_DEV_REMOVE_CMD as u8,
523+
dmi::DM_DEV_RENAME_CMD as u8,
524+
dmi::DM_DEV_SUSPEND_CMD as u8, // resume (no DM_SUSPEND flag)
525+
] {
526+
let mut hdr: dmi::Struct_dm_ioctl = devicemapper_sys::dm_ioctl {
527+
..Default::default()
528+
};
529+
hdr.event_nr = event_nr;
530+
531+
let sync = UdevSync::begin(&mut hdr, ioctl_cmd).unwrap();
532+
assert_eq!(
533+
sync.cookie, 0,
534+
"ioctl {ioctl_cmd}: no_udev flags should produce inactive UdevSync"
535+
);
536+
assert_eq!(
537+
sync.semid, None,
538+
"ioctl {ioctl_cmd}: no semaphore should be allocated"
539+
);
540+
assert_eq!(
541+
hdr.event_nr, 0,
542+
"ioctl {ioctl_cmd}: event_nr must be cleared"
543+
);
544+
assert!(sync.end(DmFlags::empty().bits()).is_ok());
545+
}
546+
}
482547
}
483548
}
484549
#[cfg(target_os = "android")]

0 commit comments

Comments
 (0)