From e54208b1466da34577c195ce16be4a4b98a40931 Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Thu, 19 Nov 2026 17:34:42 -0300 Subject: [PATCH 1/4] fix(target): resolve device links to their device node - Canonicalize device targets such as /dev/disk/by-label/* to the node they point at - Formatting rewrites the label, so udev recreates the link and a mount right after mkfs can find it missing; the node itself is stable - Keep unresolvable paths as given so validation still reports them as missing - Add a test for link resolution --- updatehub/src/utils/definitions.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/updatehub/src/utils/definitions.rs b/updatehub/src/utils/definitions.rs index c4905c29..13b3087c 100644 --- a/updatehub/src/utils/definitions.rs +++ b/updatehub/src/utils/definitions.rs @@ -79,7 +79,10 @@ impl TargetTypeExt for TargetType { .ok_or_else(|| Error::DeviceDoesNotExist(device.clone()))?; mtd::target_device_from_ubi_volume_name(volume) } else { - Ok(device.clone()) + // mkfs makes udev recreate /dev/disk/by-label/* links, so + // a mount right after formatting can find them missing. + // An unresolvable path is kept so `valid` reports it. + Ok(std::fs::canonicalize(device).unwrap_or_else(|_| device.clone())) } } TargetType::UBIVolume(s) => mtd::target_device_from_ubi_volume_name(s), @@ -116,3 +119,22 @@ impl IdExt for Uid { } } } + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + #[test] + fn device_target_resolves_links() { + let dir = tempfile::tempdir().unwrap(); + let node = dir.path().join("mmcblk0p2"); + std::fs::write(&node, b"").unwrap(); + let link = dir.path().join("system_a"); + std::os::unix::fs::symlink(&node, &link).unwrap(); + + let target = TargetType::Device(link).get_target().unwrap(); + + assert_eq!(target, node.canonicalize().unwrap()); + } +} From 2bf6de964268fb50e85a5be2643e8f8acc0c9fee Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Sat, 21 Nov 2026 10:47:20 -0300 Subject: [PATCH 2/4] fix(target): report device targets that cannot be resolved - A missing link now fails as DeviceDoesNotExist naming the configured path, instead of falling back to the link and failing later at mount with ENOENT - Other resolution errors, such as a symlink loop or EACCES, are propagated instead of being reported as a missing device - Trace which node a target resolves to, since errors now name the node rather than the link --- updatehub/src/utils/definitions.rs | 35 ++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/updatehub/src/utils/definitions.rs b/updatehub/src/utils/definitions.rs index 13b3087c..7416b917 100644 --- a/updatehub/src/utils/definitions.rs +++ b/updatehub/src/utils/definitions.rs @@ -8,6 +8,7 @@ use pkg_schema::definitions::{ TargetType, target_permissions::{Gid, Uid}, }; +use slog_scope::trace; use std::path::PathBuf; /// How a handler reaches its target, which decides whether a filesystem the @@ -81,8 +82,16 @@ impl TargetTypeExt for TargetType { } else { // mkfs makes udev recreate /dev/disk/by-label/* links, so // a mount right after formatting can find them missing. - // An unresolvable path is kept so `valid` reports it. - Ok(std::fs::canonicalize(device).unwrap_or_else(|_| device.clone())) + match std::fs::canonicalize(device) { + Ok(node) => { + trace!("target {:?} resolves to {:?}", device, node); + Ok(node) + } + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + Err(Error::DeviceDoesNotExist(device.clone())) + } + Err(e) => Err(e.into()), + } } } TargetType::UBIVolume(s) => mtd::target_device_from_ubi_volume_name(s), @@ -137,4 +146,26 @@ mod tests { assert_eq!(target, node.canonicalize().unwrap()); } + + #[test] + fn dangling_device_target_is_reported_by_its_configured_path() { + let dir = tempfile::tempdir().unwrap(); + let link = dir.path().join("system_a"); + std::os::unix::fs::symlink(dir.path().join("gone"), &link).unwrap(); + + let err = TargetType::Device(link.clone()).get_target().unwrap_err(); + + assert!(matches!(err, Error::DeviceDoesNotExist(ref p) if *p == link), "{err:?}"); + } + + #[test] + fn device_target_reports_unresolvable_links() { + let dir = tempfile::tempdir().unwrap(); + let link = dir.path().join("loop"); + std::os::unix::fs::symlink(&link, &link).unwrap(); + + let err = TargetType::Device(link).get_target().unwrap_err(); + + assert!(matches!(err, Error::Io(_)), "{err:?}"); + } } From 0691b80811871256aab3bc089d2fbdc51fa79436 Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Sat, 21 Nov 2026 10:47:20 -0300 Subject: [PATCH 3/4] fix(raw): check and write the resolved target - Raw and raw-delta validated the resolved node but ran their space check, install-if-different read and write through the configured link - Use the resolved target throughout, like the other installers, so a link udev is recreating cannot be reached mid-install --- updatehub/src/object/installer/raw.rs | 9 ++++++--- updatehub/src/object/installer/raw_delta.rs | 7 ++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/updatehub/src/object/installer/raw.rs b/updatehub/src/object/installer/raw.rs index 99337b43..d814227b 100644 --- a/updatehub/src/object/installer/raw.rs +++ b/updatehub/src/object/installer/raw.rs @@ -45,10 +45,11 @@ impl Installer for objects::Raw { let target = resolve_target(self, context)?; let access = checked_region(self) .map_or(Access::Exclusive, |(offset, len)| Access::Region { offset, len }); - if let definitions::TargetType::Device(dev) = + if let definitions::TargetType::Device(_) = target.valid(access).log_error_msg("device failed vaidation")? { - utils::fs::ensure_disk_space(dev, self.required_install_size()) + let dev = target.get_target().log_error_msg("failed to get target device")?; + utils::fs::ensure_disk_space(&dev, self.required_install_size()) .log_error_msg("not enough disk space")?; return Ok(()); } @@ -60,9 +61,11 @@ impl Installer for objects::Raw { info!("'raw' handler Install {} ({})", self.filename, self.sha256sum); let boot_emmc = matches!(self.target_type, definitions::TargetType::BootEmmc(_)); - let definitions::TargetType::Device(ref device) = resolve_target(self, context)? else { + let target = resolve_target(self, context)?; + let definitions::TargetType::Device(_) = target else { unreachable!("device should be secured by check_requirements") }; + let device = &target.get_target().log_error_msg("failed to get target device")?; if boot_emmc { info!("writing to {:?}", device); } diff --git a/updatehub/src/object/installer/raw_delta.rs b/updatehub/src/object/installer/raw_delta.rs index e7239b46..1a34e513 100644 --- a/updatehub/src/object/installer/raw_delta.rs +++ b/updatehub/src/object/installer/raw_delta.rs @@ -20,14 +20,15 @@ impl Installer for objects::RawDelta { async fn check_requirements(&self, context: &Context) -> Result<()> { info!("'raw-delta' handle checking requirements"); - if let definitions::TargetType::Device(dev) = + if let definitions::TargetType::Device(_) = self.target.valid(Access::Exclusive).log_error_msg("device failed validation")? { + let dev = self.target.get_target().log_error_msg("failed to get target device")?; let seed = get_seed_path(self, context); - let required_size = delta::get_required_size(&seed, dev) + let required_size = delta::get_required_size(&seed, &dev) .await .log_error_msg("failed to fetch delta required install size")?; - utils::fs::ensure_disk_space(dev, required_size) + utils::fs::ensure_disk_space(&dev, required_size) .log_error_msg("not enough disk space")?; return Ok(()); } From 0d9980e661e74e0e32510068a3ab5ac8aad748da Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Thu, 19 Nov 2026 17:40:08 -0300 Subject: [PATCH 4/4] fix(fs): wait for udev after formatting a device - mkfs rewrites the label, so udev recreates /dev/disk/by-label/* while a later object in the same update may already be resolving that link - Run `udevadm settle --timeout=30` after formatting when it is available: the queue is global, so unrelated events must not hold the update for the 120 s default - Skip it for JFFS2 and UBIFS, whose MTD devices have no /dev/disk links; boards without udev keep working and a settle failure is only logged - Tests replace the tools with scripts on PATH to confirm the call, the MTD skip and that a failing settle does not fail the format --- updatehub/src/utils/fs.rs | 58 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/updatehub/src/utils/fs.rs b/updatehub/src/utils/fs.rs index 326bd5af..c7befaf1 100644 --- a/updatehub/src/utils/fs.rs +++ b/updatehub/src/utils/fs.rs @@ -405,6 +405,17 @@ pub(crate) fn format(target: &Path, fs: Filesystem, options: Option<&str>) -> Re }; easy_process::run(&cmd)?; + + // The new label makes udev recreate the /dev/disk/by-label/* link, and a + // later object may reach the device through it. The queue is global, so + // the wait is bounded; JFFS2 and UBIFS sit on MTD, which has no such links. + if !matches!(fs, Filesystem::Jffs2 | Filesystem::Ubifs) + && is_executable_in_path("udevadm").is_ok() + && let Err(e) = easy_process::run("udevadm settle --timeout=30") + { + warn!("udevadm settle failed after formatting: {}", e); + } + Ok(()) } @@ -462,6 +473,53 @@ pub(crate) mod tests { dir } + /// Formats a plain file with the tools replaced by scripts that log their + /// arguments, and returns the log. + fn format_with_mocks(fs: Filesystem, bins: &[&str]) -> String { + use crate::{object::installer::tests::create_echo_bins, utils::test_env::PathEnvGuard}; + + let target = tempfile::NamedTempFile::new().unwrap(); + let (mocks, calls) = create_echo_bins(bins).unwrap(); + let _search_path = SearchPathGuard::set([mocks.path()]); + let _path = PathEnvGuard::prepend(mocks.path()); + + format(target.path(), fs, None).unwrap(); + + std::fs::read_to_string(calls).unwrap().replace(&*target.path().to_string_lossy(), "TARGET") + } + + #[test] + fn format_waits_for_udev_after_mkfs() { + let calls = format_with_mocks(Filesystem::Ext4, &["mkfs.ext4", "udevadm"]); + + assert_eq!(calls, "mkfs.ext4 -F TARGET\nudevadm settle --timeout=30\n"); + } + + #[test] + fn format_does_not_wait_for_udev_on_mtd() { + let calls = format_with_mocks(Filesystem::Jffs2, &["flash_erase", "udevadm"]); + + assert_eq!(calls, "flash_erase -j TARGET 0 0\n"); + } + + #[test] + fn format_succeeds_when_udev_settle_fails() { + use crate::utils::test_env::PathEnvGuard; + use std::os::unix::fs::PermissionsExt; + + let target = tempfile::NamedTempFile::new().unwrap(); + let mocks = tempfile::tempdir().unwrap(); + for (bin, status) in [("mkfs.ext4", 0), ("udevadm", 1)] { + let path = mocks.path().join(bin); + std::fs::write(&path, format!("#!/bin/sh\nexit {status}\n")).unwrap(); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap(); + } + let _search_path = SearchPathGuard::set([mocks.path()]); + let _path = PathEnvGuard::prepend(mocks.path()); + + format(target.path(), Filesystem::Ext4, None).unwrap(); + } + #[test] fn search_path_override_replaces_the_process_path() { // `sh` is on the real PATH of every host that runs this suite.