diff --git a/README.md b/README.md index 0325ce7df2..971603db19 100644 --- a/README.md +++ b/README.md @@ -566,7 +566,20 @@ you what it couldn't find instead. #### Driver installation `install driver` installs the AMD kernel driver on Linux, using DKMS or a native -package. +package. On bare metal it defaults to the floating `latest` amdgpu release; +set `ROCM_CLI_AMDGPU_DRIVER_VERSION` to pin a specific one. This replaces the +older `ROCM_CLI_AMDGPU_VERSION`/`ROCM_CLI_AMDGPU_PACKAGE_RELEASE` pair, which +pinned a fixed default and are no longer read; the plan is refused while either is still set, +so an old pin is never silently turned into `latest`. + +On Ubuntu 26.04, the amdgpu driver ships in-tree and `repo.radeon.com` does not +yet publish a matching DKMS repo, so `install driver` reports a non-mutating +`supported: false` plan instead and directs you to `apt install rocm` for the +userspace ROCm stack. + +Approving (`--yes`) a plan that is `supported: false` (Ubuntu 26.04, an +undocumented distro, or a refused version override) installs nothing and exits +non-zero, after printing the plan. On Windows, `install driver` is validate-only: it installs and changes nothing, with or without `--yes`, and it starts no driver command. It prints the AMD diff --git a/apps/rocm/src/driver_install.rs b/apps/rocm/src/driver_install.rs index 99c3a15f2b..156de45835 100644 --- a/apps/rocm/src/driver_install.rs +++ b/apps/rocm/src/driver_install.rs @@ -46,6 +46,14 @@ const fn should_execute(plan: &DriverInstallPlan, yes: bool, dry_run: bool) -> b yes && !dry_run && plan.supported && plan.mutating } +/// Whether an approved (`--yes`) run asks for a plan that installs nothing and +/// should say so with a failing exit status. Native Windows is exempt: its +/// plan only reports and promises to change nothing, so approving it is a +/// successful no-op. +fn approval_of_unsupported_plan(plan: &DriverInstallPlan, yes: bool, dry_run: bool) -> bool { + yes && !dry_run && !plan.supported && plan.policy != "windows_validate_only" +} + pub(crate) fn install_driver( paths: &AppPaths, dkms: bool, @@ -60,6 +68,16 @@ pub(crate) fn install_driver( let plan = build_driver_install_plan_read(&examine, &os_release, dkms, PrivilegeEscalation::detect()); let mut output = render_driver_install_plan(&plan, yes, dry_run); + if approval_of_unsupported_plan(&plan, yes, dry_run) { + return Err(DriverInstallError::new( + anyhow::anyhow!( + "{output}\nnothing was installed: this host has no supported driver plan ({}). {}", + plan.policy, + plan.reason + ), + false, + )); + } if !should_execute(&plan, yes, dry_run) { return Ok(DriverInstallResult { output, @@ -346,7 +364,7 @@ struct DriverInstallPlan { os_id: String, version_id: String, codename: String, - repo_version: String, + repo_version_expr: String, reason: String, preflight_checks: Vec, commands: Vec, @@ -483,8 +501,8 @@ struct DriverPassiveCheck { /// Release of ROCDXG installed on WSL2, overridable for trying another build. /// /// Resolved once at plan-build time via [`resolve_shell_default_template`], the -/// same way `ROCM_CLI_AMDGPU_VERSION` is handled for the bare-metal repository -/// pin. The concrete value is baked into the archive name, the release URL and +/// same way `ROCM_CLI_AMDGPU_DRIVER_VERSION` is validated for the bare-metal +/// repository pin. The concrete value is baked into the archive name, the release URL and /// the `repo_version:` line, so the plan a user reviews names the build the /// install will actually fetch rather than an unexpanded `${...}` placeholder. /// @@ -599,6 +617,19 @@ fn sha256_digest_is_well_formed(digest: &str) -> bool { digest.len() == 64 && digest.chars().all(|c| c.is_ascii_hexdigit()) } +/// Whether a string is safe to interpolate into the driver-install plan's +/// shell commands: these values (the `ROCM_CLI_AMDGPU_DRIVER_VERSION` +/// override, and `VERSION_ID`/`VERSION_CODENAME`/`UBUNTU_CODENAME` read from +/// `/etc/os-release`) end up both inside single-quoted `printf` arguments and, +/// for `VERSION_ID` in the SUSEConnect commands, directly unquoted on the +/// command line -- so only letters, digits, and `. _ -` are accepted, the +/// same charset a real amdgpu/distro version or codename is made of. +fn is_safe_shell_token(v: &str) -> bool { + !v.is_empty() + && v.chars() + .all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)) +} + /// How the downloaded ROCDXG package will be authenticated before it is /// installed as root. #[derive(Debug, Clone, Eq, PartialEq)] @@ -671,7 +702,7 @@ fn wsl_rocdxg_refusal_plan(repo_version: String, reason: String) -> DriverInstal os_id: "wsl".to_owned(), version_id: String::new(), codename: String::new(), - repo_version, + repo_version_expr: repo_version, reason, preflight_checks: Vec::new(), commands: Vec::new(), @@ -739,7 +770,7 @@ fn wsl_rocdxg_driver_plan(escalation: PrivilegeEscalation) -> DriverInstallPlan os_id: "wsl".to_owned(), version_id: String::new(), codename: String::new(), - repo_version: version, + repo_version_expr: version, reason: "WSL2 uses the Windows host driver plus ROCDXG, not Linux DKMS; this installs ROCDXG." .to_owned(), @@ -822,9 +853,9 @@ fn wsl_rocdxg_driver_plan(escalation: PrivilegeEscalation) -> DriverInstallPlan /// file that exists but could not be read /// ([`build_driver_install_plan_read`]'s `Unreadable` branch). Both name /// `reason`, never a distro, and differ only in it: shared here, including -/// `repo_version`'s own resolution, so a new early branch — or a bumped -/// default — added to one is not silently absent from the other. -fn unreadable_os_release_plan(reason: String) -> DriverInstallPlan { +/// `repo_version_expr`, so a new early branch added to one is not silently +/// absent from the other. +fn unreadable_os_release_plan(reason: String, repo_version_expr: String) -> DriverInstallPlan { DriverInstallPlan { supported: false, mutating: false, @@ -832,7 +863,7 @@ fn unreadable_os_release_plan(reason: String) -> DriverInstallPlan { os_id: String::new(), version_id: String::new(), codename: String::new(), - repo_version: resolve_shell_default_template("${ROCM_CLI_AMDGPU_VERSION:-7.2.4}"), + repo_version_expr, reason, preflight_checks: Vec::new(), commands: Vec::new(), @@ -841,9 +872,89 @@ fn unreadable_os_release_plan(reason: String) -> DriverInstallPlan { } } -/// The driver install plan for `examine` and the os-release `os_release_text` -/// read from `os_release_path`, naming that path in any unreadable-line -/// message. +const RETIRED_DRIVER_VERSION_VARS: [&str; 2] = + ["ROCM_CLI_AMDGPU_VERSION", "ROCM_CLI_AMDGPU_PACKAGE_RELEASE"]; + +/// The refusal plan `resolve_repo_version_expr` returns for any +/// `ROCM_CLI_AMDGPU_DRIVER_VERSION` value that cannot be safely interpolated +/// into a shell command -- whether it failed [`is_safe_shell_token`] or could +/// not even be decoded as UTF-8. +fn invalid_driver_version_override_plan(value_debug: String) -> DriverInstallPlan { + DriverInstallPlan { + supported: false, + mutating: false, + policy: "invalid_driver_version_override".to_owned(), + os_id: String::new(), + version_id: String::new(), + codename: String::new(), + repo_version_expr: value_debug, + reason: "ROCM_CLI_AMDGPU_DRIVER_VERSION is not a well-formed amdgpu driver release. It is interpolated into privileged shell commands, so only letters, digits, and `. _ -` are accepted.".to_owned(), + preflight_checks: Vec::new(), + commands: Vec::new(), + checks: vec!["rocm examine".to_owned()], + reboot_required: false, + } +} + +/// Resolves and validates `ROCM_CLI_AMDGPU_DRIVER_VERSION`, shared between +/// [`build_driver_install_plan_at`] (reached whenever the os-release text +/// itself was readable) and [`build_driver_install_plan_read`]'s `Unreadable` +/// branch (reached before `_at` is even called), so both report the same +/// `invalid_driver_version_override` plan on a malformed override rather than +/// one of them silently falling back to `latest`. +fn resolve_repo_version_expr() -> Result> { + // The pre-`latest` pin variables are no longer read; refuse rather than + // install a floating `latest` for a user who still exports one. + for retired in RETIRED_DRIVER_VERSION_VARS { + if std::env::var_os(retired).is_some_and(|v| !v.is_empty()) { + let mut plan = invalid_driver_version_override_plan(format!("{retired} is set")); + plan.policy = "retired_driver_version_override".to_owned(); + plan.reason = format!( + "{retired} is no longer read; unset it, and set ROCM_CLI_AMDGPU_DRIVER_VERSION to pin an amdgpu driver release." + ); + return Err(Box::new(plan)); + } + } + // Resolved here rather than left as a shell `${VAR:-default}` expression: + // these commands are built with `printf '%s\n' '...'` (single-quoted) so the + // written repo file has a working baseurl regardless of shell. The value is + // interpolated straight into that single-quoted command, so it's restricted + // to a safe URL path segment here rather than trusted verbatim: a value + // containing `'` would otherwise close the quoting and inject shell. An + // invalid override is refused rather than silently falling back to + // `latest`, the same way `wsl_rocdxg_driver_plan` refuses a malformed + // `ROCM_CLI_ROCDXG_VERSION` -- a user who set this expecting a specific + // release should see why it did not take effect, not an unexplained + // `latest`. + match std::env::var("ROCM_CLI_AMDGPU_DRIVER_VERSION") { + // Set-but-empty is treated the same as unset, matching every other + // optional override in this file (`resolve_rocdxg_verification`, + // `resolve_shell_default_template`): a blank env var is how an env + // file clears an override, not a request to refuse the plan. + Err(std::env::VarError::NotPresent) => Ok("latest".to_owned()), + Ok(v) if v.is_empty() => Ok("latest".to_owned()), + Ok(v) if is_safe_shell_token(&v) => Ok(v), + Ok(v) => Err(Box::new(invalid_driver_version_override_plan(format!( + "{v:?}" + )))), + // A non-UTF-8 value can never pass `is_safe_shell_token` anyway, but + // routing it through the same refusal as a malformed value (instead + // of folding it into the NotPresent/empty "unset" case) keeps the + // same "refuse, don't silently fall back to latest" guarantee for a + // user who set this to something `env::var` cannot even decode. + Err(std::env::VarError::NotUnicode(v)) => { + // Debug, not `v.display()`, is deliberate here: this is the one + // value worth escaping rather than rendering lossily, since the + // whole reason it reached this arm is that it isn't valid UTF-8. + #[allow(clippy::unnecessary_debug_formatting)] + let value_debug = format!("{v:?}"); + Err(Box::new(invalid_driver_version_override_plan(value_debug))) + } + } +} + +/// [`build_driver_install_plan_read`], naming `os_release_path` instead of always +/// `/etc/os-release` in an unreadable-line message. fn build_driver_install_plan_at( examine: &ExamineSummary, os_release_path: &str, @@ -851,17 +962,6 @@ fn build_driver_install_plan_at( dkms: bool, escalation: PrivilegeEscalation, ) -> DriverInstallPlan { - // Resolve the AMD graphics version and amdgpu-install package release once, - // here at plan-build time, so the concrete values are baked into both the - // human-readable summary and every command the plan runs. Keeping shell - // `${VAR:-default}` templates in the commands used to be load-bearing, but - // AMD's apt `sources.list` line embeds the template inside POSIX single - // quotes, which suppress all expansion — so the literal `${...}` would land - // in the repo file. Resolving up front fixes that and keeps the summary and - // the executed commands in agreement. - let repo_version = resolve_shell_default_template("${ROCM_CLI_AMDGPU_VERSION:-7.2.4}"); - let package_release = - resolve_shell_default_template("${ROCM_CLI_AMDGPU_PACKAGE_RELEASE:-70204}"); if install_driver_only_reports_on(&examine.os) { return DriverInstallPlan { supported: false, @@ -870,7 +970,15 @@ fn build_driver_install_plan_at( os_id: "windows".to_owned(), version_id: String::new(), codename: String::new(), - repo_version, + // Not interpolated into any command on this no-op plan (there are + // none), so an override is not validated before this early return. + // Always "latest" even if `ROCM_CLI_AMDGPU_DRIVER_VERSION` names + // something else or is malformed: unlike the Linux arms below, + // this value has not gone through `resolve_repo_version_expr()`, + // so do not read the printed `repo_version:` line here (or in + // `--dry-run` output) as confirmation that an override was seen or + // accepted. + repo_version_expr: "latest".to_owned(), // Uses the same inventory and adapter choice as `rocm examine` // and `rocm diagnose`. Separate probes can still fail independently. reason: rocm_core::windows_driver::install_driver_summary( @@ -887,24 +995,63 @@ fn build_driver_install_plan_at( return wsl_rocdxg_driver_plan(escalation); } + let repo_version_expr = match resolve_repo_version_expr() { + Ok(v) => v, + Err(plan) => return *plan, + }; + // An unreadable file is reported as itself, naming the line, rather than // falling through to "this distro is not supported" — which would send the // user looking for a support matrix when the fix is one line of a file. let os_release = match rocm_core::os_release::parse(os_release_text) { Ok(fields) => fields, Err(unreadable) => { - return unreadable_os_release_plan(format!( - "{os_release_path} {unreadable}; no driver commands were planned \ - because the distro cannot be read. To fix it, {}.", - unreadable.fix() - )); + return unreadable_os_release_plan( + format!( + "{os_release_path} {unreadable}; no driver commands were planned \ + because the distro cannot be read. To fix it, {}.", + unreadable.fix() + ), + repo_version_expr, + ); } }; + let field = |key: &str| os_release.get(key).cloned(); - let os_id = field("ID").unwrap_or_default(); - let version_id = field("VERSION_ID").unwrap_or_default(); + // Not interpolated into any shell command today (only matched against and + // stored on the plan), but it goes through the same allow-list as + // `version_id`/`codename`/the env override anyway: centralizing the + // sanitization here means a future `os_id`-based repo path can't + // reintroduce the shell-injection class those fields close. An invalid + // value is treated as absent, falling through to the "unsupported" plan + // the same way the other fields do. + // Lowercased, as `rocm-core`'s `dkms_driver_plan_is_supported` does, so the + // two copies of the support matrix agree on `ID=Ubuntu`. + let os_id = field("ID") + .filter(|v| is_safe_shell_token(v)) + .unwrap_or_default() + .to_ascii_lowercase(); + // `version_id` is interpolated unsanitized both into the same + // single-quoted `printf '...' | tee` commands as `repo_version_expr` + // above and, on SLES, directly unquoted on the SUSEConnect command line -- + // so it gets the same char allow-list as that override. A value that + // fails it is treated as absent (empty), which cannot match any + // `(os_id, version_id)` arm below and falls through to the safe, + // non-mutating "unsupported" plan, the same way a hostile codename falls + // through to the static mapping rather than being used verbatim. + let version_id = field("VERSION_ID") + .filter(|v| is_safe_shell_token(v)) + .unwrap_or_default(); + // `codename` is interpolated unsanitized into the same single-quoted + // `printf '...' | tee` command as `repo_version_expr` above, so an + // os-release-sourced value gets the same char allow-list: a crafted + // VERSION_CODENAME/UBUNTU_CODENAME (compromised or custom base image) + // could otherwise break out of the quoting and inject shell. An invalid + // value is treated as absent, falling through to the trusted static + // `codename_for_version` mapping rather than being used verbatim. let codename = field("VERSION_CODENAME") .or_else(|| field("UBUNTU_CODENAME")) + .filter(|v| is_safe_shell_token(v)) .or_else(|| codename_for_version(&os_id, &version_id).map(str::to_owned)) .unwrap_or_default(); let id_like = field("ID_LIKE").unwrap_or_default(); @@ -914,25 +1061,70 @@ fn build_driver_install_plan_at( os_id, version_id, codename, - repo_version, + repo_version_expr, dkms, true, escalation, ), + // TODO: AMD had not published a repo.radeon.com/amdgpu "resolute" + // suite as of 2026-08; re-check + // https://repo.radeon.com/amdgpu/latest/ubuntu/dists/ and switch this + // to apt_driver_plan(..., true) once it exists, the same way + // 22.04/24.04 are handled. + // + // This arm IS reachable through the CLI on a real host: the hosted + // `E2E tests` job's `ubuntu-latest` image (26.04 once GitHub's migration completes) runs `driver-install-06` in + // `driver_install.feature`, which asserts whichever arm the real host + // actually falls into, so this one has e2e coverage too, alongside + // the unit fixture in `driver_plan_ubuntu_2604_uses_native_archive_guidance` + // and its two neighbors below. + ("ubuntu", "26.04") => { + let sudo = escalation.prefix(); + DriverInstallPlan { + supported: false, + mutating: false, + policy: "ubuntu_native_archive".to_owned(), + os_id, + version_id, + // codename_for_version already resolves this to "resolute" when + // VERSION_CODENAME/UBUNTU_CODENAME are absent, so this is never + // empty here -- no fallback needed. + codename, + repo_version_expr, + reason: format!( + "Ubuntu 26.04 LTS ships ROCm natively via the standard archive and \ + includes the amdgpu kernel driver in-tree; AMD's repo.radeon.com/amdgpu \ + tree does not yet publish a 'resolute' suite, so no third-party DKMS repo \ + commands are planned. Run `{sudo}apt install rocm` for the userspace ROCm \ + stack instead." + ), + preflight_checks: Vec::new(), + commands: Vec::new(), + // `checks` renders as `post_reboot_checks:` and, like every other + // arm, holds passive verification targets only -- the mutating + // install command above belongs in `reason`, not here. + checks: vec!["rocm examine".to_owned(), "rocminfo".to_owned()], + // The amdgpu kernel driver ships in-tree with the kernel itself, + // not with this guidance's `apt install rocm` -- that command + // installs the userspace ROCm stack only, so nothing changes on + // the kernel side and no reboot is needed for it to take effect. + reboot_required: false, + } + } ("debian", "12" | "13") => { let repo_codename = if version_id == "13" { "noble" } else { "jammy" }; let mut plan = apt_driver_plan( os_id, version_id, repo_codename.to_owned(), - repo_version, + repo_version_expr, dkms, false, escalation, ); // Debian deliberately reuses AMD's Ubuntu-suite repository: AMD's // documented Debian install maps Debian 12 -> jammy and 13 -> noble - // and serves them from the .../ubuntu graphics tree. Surface that in + // and serves them from the .../ubuntu amdgpu tree. Surface that in // the plan so the Ubuntu codename on a Debian host doesn't read as a // misdetection. plan.reason = format!( @@ -945,8 +1137,7 @@ fn build_driver_install_plan_at( os_id, version_id, codename, - repo_version, - package_release, + repo_version_expr, dkms, DnfDriverDistro::Rhel, escalation, @@ -955,8 +1146,7 @@ fn build_driver_install_plan_at( os_id, version_id, codename, - repo_version, - package_release, + repo_version_expr, dkms, DnfDriverDistro::Oracle, escalation, @@ -965,30 +1155,20 @@ fn build_driver_install_plan_at( os_id, version_id, codename, - repo_version, - package_release, + repo_version_expr, dkms, DnfDriverDistro::Rocky, escalation, ), ("sles" | "sle", "15.7") => { - sles_driver_plan( - os_id, - version_id, - codename, - repo_version, - package_release, - dkms, - escalation, - ) + sles_driver_plan(os_id, version_id, codename, repo_version_expr, dkms, escalation) } _ => driver_plan_via_id_like( &os_id, &version_id, &id_like, &codename, - &repo_version, - &package_release, + &repo_version_expr, dkms, escalation, ) @@ -999,7 +1179,7 @@ fn build_driver_install_plan_at( os_id, version_id, codename, - repo_version, + repo_version_expr, reason: "Linux DKMS driver install is currently planned only for AMD-documented Ubuntu, Debian, RHEL, Oracle Linux, SLES, and Rocky versions; no commands were guessed for this distro.".to_owned(), preflight_checks: Vec::new(), commands: Vec::new(), @@ -1032,10 +1212,17 @@ fn build_driver_install_plan_read( if reaches_os_release_parse && let rocm_core::hardware_root::OsRelease::Unreadable { path, error } = read { - return unreadable_os_release_plan(format!( - "{}; no driver commands were planned because the distro cannot be read.", - rocm_core::hardware_root::unreadable_prefix(path, error) - )); + let repo_version_expr = match resolve_repo_version_expr() { + Ok(v) => v, + Err(plan) => return *plan, + }; + return unreadable_os_release_plan( + format!( + "{}; no driver commands were planned because the distro cannot be read.", + rocm_core::hardware_root::unreadable_prefix(path, error) + ), + repo_version_expr, + ); } build_driver_install_plan_at(examine, read.path(), read.text(), dkms, escalation) } @@ -1057,8 +1244,7 @@ fn driver_plan_via_id_like( version_id: &str, id_like: &str, codename: &str, - repo_version: &str, - package_release: &str, + repo_version_expr: &str, dkms: bool, escalation: PrivilegeEscalation, ) -> Option { @@ -1085,7 +1271,7 @@ fn driver_plan_via_id_like( os_id.to_owned(), version_id.to_owned(), codename, - repo_version.to_owned(), + repo_version_expr.to_owned(), dkms, true, escalation, @@ -1100,7 +1286,7 @@ fn driver_plan_via_id_like( os_id.to_owned(), version_id.to_owned(), repo_codename.to_owned(), - repo_version.to_owned(), + repo_version_expr.to_owned(), dkms, false, escalation, @@ -1121,8 +1307,7 @@ fn driver_plan_via_id_like( os_id.to_owned(), version_id.to_owned(), codename.to_owned(), - repo_version.to_owned(), - package_release.to_owned(), + repo_version_expr.to_owned(), dkms, DnfDriverDistro::Generic, escalation, @@ -1157,7 +1342,7 @@ fn apt_driver_plan( os_id: String, version_id: String, codename: String, - repo_version: String, + repo_version_expr: String, dkms: bool, include_linux_modules_extra: bool, escalation: PrivilegeEscalation, @@ -1193,13 +1378,13 @@ fn apt_driver_plan( driver_command( DriverCommandPhase::Prepare, &format!( - "curl -fsSL https://repo.radeon.com/rocm/rocm.gpg.key | {sudo}gpg --dearmor -o /etc/apt/keyrings/rocm.gpg" + "curl -fsSL https://repo.radeon.com/rocm/rocm.gpg.key | {sudo}gpg --batch --yes --dearmor -o /etc/apt/keyrings/rocm.gpg" ), ), driver_command( DriverCommandPhase::Prepare, &format!( - "printf '%s\\n' 'deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/graphics/{repo_version}/ubuntu {codename} main' | {sudo}tee /etc/apt/sources.list.d/amdgpu.list >/dev/null" + "printf '%s\\n' 'deb [arch=amd64 signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/amdgpu/{repo_version_expr}/ubuntu {codename} main' | {sudo}tee /etc/apt/sources.list.d/amdgpu.list >/dev/null" ), ), driver_command( @@ -1231,7 +1416,7 @@ fn apt_driver_plan( os_id, version_id, codename, - repo_version, + repo_version_expr, reason: if dkms { "Plan uses AMD's package-manager DKMS flow and requires explicit approval before execution." } else { @@ -1266,8 +1451,7 @@ fn dnf_driver_plan( os_id: String, version_id: String, codename: String, - repo_version: String, - package_release: String, + repo_version_expr: String, dkms: bool, distro: DnfDriverDistro, escalation: PrivilegeEscalation, @@ -1300,11 +1484,15 @@ fn dnf_driver_plan( )); } } + commands.push(driver_command( + DriverCommandPhase::Prepare, + &format!("{sudo}rpm --import https://repo.radeon.com/rocm/rocm.gpg.key"), + )); commands.push(driver_command( DriverCommandPhase::Prepare, &format!( - "{sudo}dnf install -y {}", - amdgpu_install_rpm_url(&repo_version, &package_release, &version_id, distro) + "printf '%s\\n' '[amdgpu]' 'name=amdgpu' 'baseurl={}' 'enabled=1' 'priority=50' 'gpgcheck=1' 'gpgkey=https://repo.radeon.com/rocm/rocm.gpg.key' | {sudo}tee /etc/yum.repos.d/amdgpu.repo >/dev/null", + dnf_repo_baseurl(&repo_version_expr, &version_id, distro) ), )); commands.push(driver_command( @@ -1332,7 +1520,7 @@ fn dnf_driver_plan( os_id, version_id, codename, - repo_version, + repo_version_expr, reason: if dkms { "Plan uses AMD's documented DNF DKMS flow and requires explicit approval before execution." } else { @@ -1368,8 +1556,7 @@ fn sles_driver_plan( os_id: String, version_id: String, codename: String, - repo_version: String, - package_release: String, + repo_version_expr: String, dkms: bool, escalation: PrivilegeEscalation, ) -> DriverInstallPlan { @@ -1395,26 +1582,35 @@ fn sles_driver_plan( ), driver_command( DriverCommandPhase::Prepare, - &format!("{sudo}zypper refresh"), + &format!("{sudo}zypper --non-interactive refresh"), + ), + driver_command( + DriverCommandPhase::Prepare, + &format!("{sudo}zypper --non-interactive install -y kernel-default-devel"), ), driver_command( DriverCommandPhase::Prepare, - &format!("{sudo}zypper install -y kernel-default-devel"), + &format!("{sudo}rpm --import https://repo.radeon.com/rocm/rocm.gpg.key"), ), driver_command( DriverCommandPhase::Prepare, &format!( - "{sudo}zypper --no-gpg-checks install -y {}", - amdgpu_install_sles_rpm_url(&repo_version, &package_release, &version_id) + "printf '%s\\n' '[amdgpu]' 'name=amdgpu' 'baseurl={}' 'enabled=1' 'autorefresh=0' 'type=rpm-md' 'gpgcheck=1' 'gpgkey=https://repo.radeon.com/rocm/rocm.gpg.key' | {sudo}tee /etc/zypp/repos.d/amdgpu.repo >/dev/null", + sles_repo_baseurl(&repo_version_expr, &version_id) ), ), driver_command( DriverCommandPhase::Prepare, - &format!("{sudo}zypper refresh"), + // `--gpg-auto-import-keys` is required here: `rpm --import` above + // only seeds the RPM keyring, but zypper tracks trusted + // *repository* signing keys separately and otherwise prompts to + // trust/reject the new repo's key on this refresh -- a prompt + // that would hang this unattended flow (no stdin attached). + &format!("{sudo}zypper --non-interactive --gpg-auto-import-keys refresh"), ), driver_command( DriverCommandPhase::Execute, - &format!("{sudo}zypper install -y amdgpu-dkms"), + &format!("{sudo}zypper --non-interactive install -y amdgpu-dkms"), ), driver_command(DriverCommandPhase::Verify, "dkms status amdgpu"), driver_command(DriverCommandPhase::Verify, "test -e /dev/kfd"), @@ -1432,7 +1628,7 @@ fn sles_driver_plan( os_id, version_id, codename, - repo_version, + repo_version_expr, reason: if dkms { "Plan uses AMD's documented SLES DKMS flow and requires explicit approval before execution." } else { @@ -1479,31 +1675,19 @@ fn rhel_kernel_prepare_commands(version_id: &str, escalation: PrivilegeEscalatio } } -fn amdgpu_install_rpm_url( - repo_version: &str, - package_release: &str, - version_id: &str, - distro: DnfDriverDistro, -) -> String { +fn dnf_repo_baseurl(repo_version_expr: &str, version_id: &str, distro: DnfDriverDistro) -> String { let repo_family = match distro { DnfDriverDistro::Rhel => "rhel", DnfDriverDistro::Oracle | DnfDriverDistro::Rocky | DnfDriverDistro::Generic => "el", }; - let repo_version_path = dnf_repo_version_path(version_id); - let el_major = linux_major_version(version_id); + let repo_version = dnf_repo_version_path(version_id); format!( - "https://repo.radeon.com/amdgpu-install/{repo_version}/{repo_family}/{repo_version_path}/amdgpu-install-{repo_version}.{package_release}-1.el{el_major}.noarch.rpm" + "https://repo.radeon.com/amdgpu/{repo_version_expr}/{repo_family}/{repo_version}/main/x86_64" ) } -fn amdgpu_install_sles_rpm_url( - repo_version: &str, - package_release: &str, - version_id: &str, -) -> String { - format!( - "https://repo.radeon.com/amdgpu-install/{repo_version}/sle/{version_id}/amdgpu-install-{repo_version}.{package_release}-1.noarch.rpm" - ) +fn sles_repo_baseurl(repo_version_expr: &str, version_id: &str) -> String { + format!("https://repo.radeon.com/amdgpu/{repo_version_expr}/sle/{version_id}/main/x86_64") } fn dnf_repo_version_path(version_id: &str) -> String { @@ -1551,7 +1735,7 @@ fn driver_command(phase: DriverCommandPhase, command: &str) -> DriverPlanCommand /// once at plan-build time so the concrete value is baked into both the /// human-readable summary and the commands the plan runs, rather than leaking an /// unexpanded `${...}` placeholder into user-facing output or depending on the -/// runtime shell — which, for the single-quoted apt `sources.list` line, would +/// runtime shell — which, for the single-quoted WSL ROCDXG download command, would /// never expand it at all. /// /// Only a single, flat `${VAR:-default}` template is recognized. Anything else — @@ -1596,7 +1780,7 @@ fn render_driver_install_plan(plan: &DriverInstallPlan, yes: bool, dry_run: bool empty_as_unknown(&plan.version_id) ); let _ = writeln!(output, " codename: {}", empty_as_unknown(&plan.codename)); - let _ = writeln!(output, " repo_version: {}", plan.repo_version); + let _ = writeln!(output, " repo_version: {}", plan.repo_version_expr); let _ = writeln!(output, " reason: {}", plan.reason); if !plan.preflight_checks.is_empty() { let _ = writeln!(output, " preflight_checks:"); @@ -1687,6 +1871,7 @@ fn codename_for_version(os_id: &str, version_id: &str) -> Option<&'static str> { match (os_id, version_id) { ("ubuntu", "22.04") => Some("jammy"), ("ubuntu", "24.04") => Some("noble"), + ("ubuntu", "26.04") => Some("resolute"), ("debian", "12") => Some("jammy"), ("debian", "13") => Some("noble"), _ => None, @@ -2036,6 +2221,38 @@ mod tests { ); } + /// The unreadable-os-release branch resolves `repo_version_expr` via the + /// same [`resolve_repo_version_expr`] call the normal path uses, before + /// it ever builds an `unreadable_os_release_plan`. A malformed override + /// must still be refused here, not silently dropped because the distro + /// itself could not be identified. + #[test] + fn an_unreadable_os_release_still_refuses_a_malformed_version_override() { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set( + "ROCM_CLI_AMDGPU_DRIVER_VERSION", + "'; touch /tmp/pwned; echo '", + ); + let read = rocm_core::hardware_root::OsRelease::Unreadable { + path: "/usr/lib/os-release", + error: std::io::Error::new(std::io::ErrorKind::PermissionDenied, "denied"), + }; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &read, + true, + PrivilegeEscalation::Sudo, + ); + assert!(!plan.supported); + assert_eq!(plan.policy, "invalid_driver_version_override"); + assert!( + !plan.mutating, + "a refused override must not claim to mutate" + ); + assert!(plan.reason.contains("ROCM_CLI_AMDGPU_DRIVER_VERSION")); + assert!(plan.commands.is_empty()); + } + /// The same malformed line as /// [`an_unreadable_os_release_names_its_line_and_plans_nothing`], but read /// through the fallback path: the reason must name `/usr/lib/os-release`, @@ -2148,6 +2365,7 @@ mod tests { // before any driver work. This asserts the ABSENCE of `sudo` across every // distro rather than checking known commands one by one — a templating // site missed on some distro fails here instead of shipping. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); for (label, os_release) in dkms_planning_os_releases() { let commands = plan_commands(os_release, PrivilegeEscalation::AlreadyRoot); assert!( @@ -2169,6 +2387,7 @@ mod tests { // it when a normal user runs the same plan. Verify-phase commands are // read-only probes and are deliberately unprivileged, so only the // mutating phases are required to escalate. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); for (label, os_release) in dkms_planning_os_releases() { let plan = build_driver_install_plan_read( &test_examine("linux", false), @@ -2202,6 +2421,7 @@ mod tests { // `sudo` also appears mid-pipeline (`| sudo tee`, `| sudo gpg`), which a // naive "strip a leading prefix" fix would miss. The pipeline must survive // with the escalation removed from the right-hand side only. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); let ubuntu = "ID=ubuntu\nVERSION_ID=\"24.04\"\nVERSION_CODENAME=noble\n"; let commands = plan_commands(ubuntu, PrivilegeEscalation::AlreadyRoot); assert!( @@ -2211,9 +2431,8 @@ mod tests { "the apt-source pipeline must still tee, unprefixed: {commands:?}" ); assert!( - commands - .iter() - .any(|command| command.contains("| gpg --dearmor -o /etc/apt/keyrings/rocm.gpg")), + commands.iter().any(|command| command + .contains("| gpg --batch --yes --dearmor -o /etc/apt/keyrings/rocm.gpg")), "the keyring pipeline must still call gpg, unprefixed: {commands:?}" ); } @@ -2223,6 +2442,7 @@ mod tests { // `execution_commands()` is what lands in state.json. It must agree with // the escalation the plan was built under, or the recorded history // describes commands that never ran. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); let ubuntu = "ID=ubuntu\nVERSION_ID=\"24.04\"\nVERSION_CODENAME=noble\n"; let as_root = build_driver_install_plan_read( &test_examine("linux", false), @@ -2257,6 +2477,7 @@ mod tests { // The preflight claimed `sudo` must be installed even when the plan no // longer uses it — the same contradiction the bug report called out // between the stated preconditions and what execution actually did. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); for (label, os_release) in dkms_planning_os_releases() { let as_root = build_driver_install_plan_read( &test_examine("linux", false), @@ -2288,6 +2509,88 @@ mod tests { } } + #[test] + fn resolve_shell_default_template_uses_default_when_env_unset() { + let _env = ScopedTestEnv::new(); + // A made-up variable name that nothing else sets, cleared under the lock, + // isolates the default path. + assert_eq!( + resolve_shell_default_template("${ROCM_CLI_TEST_UNSET_REPO_VERSION:-7.2.4}"), + "7.2.4" + ); + } + + #[test] + fn resolve_shell_default_template_prefers_env_value_when_set() { + let mut env = ScopedTestEnv::new(); + let var = "ROCM_CLI_TEST_REPO_VERSION_OVERRIDE"; + env.set(var, "9.9.9"); + assert_eq!( + resolve_shell_default_template(&format!("${{{var}:-7.2.4}}")), + "9.9.9" + ); + } + + #[test] + fn resolve_shell_default_template_treats_empty_env_as_unset() { + let mut env = ScopedTestEnv::new(); + let var = "ROCM_CLI_TEST_REPO_VERSION_EMPTY"; + env.set(var, ""); + assert_eq!( + resolve_shell_default_template(&format!("${{{var}:-7.2.4}}")), + "7.2.4" + ); + } + + #[test] + fn resolve_shell_default_template_passes_through_non_template() { + assert_eq!(resolve_shell_default_template("7.2.4"), "7.2.4"); + } + + #[test] + fn resolve_shell_default_template_leaves_bare_var_untouched() { + let _env = ScopedTestEnv::new(); + // No `:-default`, so there is nothing to resolve to; the input must pass + // through unchanged rather than being partially rewritten. + assert_eq!( + resolve_shell_default_template("${ROCM_CLI_TEST_UNSET_REPO_VERSION}"), + "${ROCM_CLI_TEST_UNSET_REPO_VERSION}" + ); + } + + #[test] + fn resolve_shell_default_template_leaves_nested_default_untouched() { + let _env = ScopedTestEnv::new(); + // A nested default is beyond the flat matcher; returning the literal + // input keeps a `${B:-x}` fragment from leaking as a "resolved" value. + assert_eq!( + resolve_shell_default_template("${ROCM_CLI_TEST_UNSET_A:-${ROCM_CLI_TEST_UNSET_B:-x}}"), + "${ROCM_CLI_TEST_UNSET_A:-${ROCM_CLI_TEST_UNSET_B:-x}}" + ); + } + + #[test] + fn driver_plan_dry_run_repo_version_line_is_resolved() { + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + // Regression for the dry-run output leaking the wrong field (or, under + // the old shell-template scheme this replaced, a raw unresolved + // placeholder) on the `repo_version:` line instead of the version the + // plan actually resolved. + let os_release = r#" +ID=rhel +VERSION_ID="9.7" +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + let rendered = render_driver_install_plan(&plan, false, true); + + assert!(rendered.contains(" repo_version: latest"), "{rendered}"); + } + #[test] fn driver_plan_ubuntu_2404_uses_official_dkms_commands() { let _env = ScopedTestEnv::with_amd_overrides_cleared(); @@ -2329,7 +2632,7 @@ VERSION_CODENAME=noble assert!( commands .iter() - .any(|command| command.contains("repo.radeon.com/graphics")) + .any(|command| command.contains("repo.radeon.com/amdgpu")) ); assert!( commands @@ -2388,6 +2691,7 @@ VERSION_CODENAME=noble #[test] fn driver_plan_executor_defers_verify_when_reboot_is_required() -> Result<()> { + let _env = ScopedTestEnv::with_amd_overrides_cleared(); let plan = build_driver_install_plan_read( &test_examine("linux", false), &os_release_found("ID=ubuntu\nVERSION_ID=\"24.04\"\nVERSION_CODENAME=noble\n"), @@ -2538,65 +2842,441 @@ VERSION_CODENAME=noble } #[test] - fn driver_reconcile_without_state_gives_non_privileged_guidance() -> Result<()> { - let (root, paths) = test_paths("driver-reconcile-empty"); - - let rendered = reconcile_driver_install(&paths)?; + fn driver_plan_ubuntu_2604_uses_native_archive_guidance() { + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + let os_release = r#" +ID=ubuntu +VERSION_ID="26.04" +VERSION_CODENAME=resolute +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + let rendered = render_driver_install_plan(&plan, false, false); - assert!(rendered.contains("driver install reconciliation")); + assert!(!plan.supported); + assert!(!plan.mutating); + assert_eq!(plan.policy, "ubuntu_native_archive"); + assert_eq!(plan.codename, "resolute"); assert!(rendered.contains("approval: not required")); - assert!(rendered.contains("privileged_commands: ")); - assert!(rendered.contains("no prior driver execution state found")); - assert!(rendered.contains("rocm install driver --dkms")); - assert!(!driver_install_state_path(&paths).exists()); - let _ = fs::remove_dir_all(root); - Ok(()) + assert!(rendered.contains("execution_commands: ")); + assert!(rendered.contains("sudo apt install rocm")); + assert!(!rendered.contains("amdgpu-dkms")); + assert!(plan.commands.is_empty()); + // The kernel driver ships in-tree on 26.04, so this arm must not + // claim a reboot is needed for `apt install rocm` (userspace only). + assert!(!plan.reboot_required); + // `checks` renders as `post_reboot_checks:` and must hold verification + // targets only -- the mutating install guidance belongs in `reason`. + assert!( + !plan + .checks + .iter() + .any(|check| check.contains("apt install")) + ); } #[test] - fn driver_reconcile_updates_state_after_reboot() -> Result<()> { - let (root, paths) = test_paths("driver-reconcile-state"); - let pre_driver = rocm_core::DriverSummary { - policy: "linux_official_amd_dkms_wrapper".to_owned(), - status: "not_detected".to_owned(), - detail: None, - windows: None, - }; - let current_driver = rocm_core::DriverSummary { - policy: "linux_official_amd_dkms_wrapper".to_owned(), - status: "amdgpu_available".to_owned(), - detail: Some("/dev/kfd is present".to_owned()), - windows: None, - }; - let mut state = DriverInstallState { - approved_at_unix_ms: 1, - executed_at_unix_ms: Some(2), - pre_driver, - post_driver: None, - boot_id_at_execution: Some("old-boot".to_owned()), - reboot_required: true, - reboot_observed: false, - commands: vec!["sudo apt-get install -y amdgpu-dkms".to_owned()], - reconciled_at_unix_ms: None, - reconciliation: None, - }; - let checks = vec![ - DriverPassiveCheck { - name: "/dev/kfd".to_owned(), - status: "present".to_owned(), - detail: "KFD device node".to_owned(), - }, - DriverPassiveCheck { - name: "/dev/dri/renderD*".to_owned(), - status: "missing".to_owned(), - detail: "DRM render node".to_owned(), - }, - ]; + fn driver_plan_ubuntu_2604_root_guidance_drops_sudo() { + // Companion to the Sudo case above: this arm derives its guidance from + // `escalation` rather than hardcoding `sudo`, so a root host must see + // the same command with no `sudo` prefix. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + let os_release = r#" +ID=ubuntu +VERSION_ID="26.04" +VERSION_CODENAME=resolute +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::AlreadyRoot, + ); + let rendered = render_driver_install_plan(&plan, false, false); - let rendered = reconcile_driver_install_state( - &paths, - &mut state, - current_driver, + assert!(rendered.contains("apt install rocm")); + assert!(!plan.reason.contains("sudo")); + assert!(!plan.checks.iter().any(|check| check.contains("sudo"))); + } + + #[test] + fn driver_plan_ubuntu_2604_falls_back_to_resolute_codename() { + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + // No VERSION_CODENAME/UBUNTU_CODENAME in os-release: codename_for_version + // must supply "resolute" on its own, and the policy must still be the + // native-archive one -- pinning both closes the gap where deleting the + // whole ("ubuntu", "26.04") arm would still leave this test green. + let os_release = r#" +ID=ubuntu +VERSION_ID="26.04" +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert_eq!(plan.codename, "resolute"); + assert_eq!(plan.policy, "ubuntu_native_archive"); + } + + #[cfg(unix)] + #[test] + fn driver_plan_rejects_a_non_utf8_version_override() { + // `std::env::var` reports a non-UTF-8 value as `Err(VarError::NotUnicode)`, + // which `resolve_repo_version_expr` must not fold into the + // `NotPresent`/empty "unset" case -- that would silently use "latest" + // for a user who explicitly set this to something. + use std::os::unix::ffi::OsStrExt; + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + let invalid = std::ffi::OsStr::from_bytes(&[0x66, 0xFF, 0x66]); + #[allow(unsafe_code)] // std::env::set_var is unsafe in edition 2024 + unsafe { + std::env::set_var("ROCM_CLI_AMDGPU_DRIVER_VERSION", invalid); + } + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04" +VERSION_CODENAME=noble +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert!(!plan.supported); + assert_eq!(plan.policy, "invalid_driver_version_override"); + assert!( + !plan.mutating, + "a refused override must not claim to mutate" + ); + assert!(plan.reason.contains("ROCM_CLI_AMDGPU_DRIVER_VERSION")); + assert!(plan.commands.is_empty()); + } + + #[test] + fn driver_plan_rejects_shell_metacharacters_in_version_override() { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set( + "ROCM_CLI_AMDGPU_DRIVER_VERSION", + "'; touch /tmp/pwned; echo '", + ); + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04" +VERSION_CODENAME=noble +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert!(!plan.supported); + assert_eq!(plan.policy, "invalid_driver_version_override"); + assert!( + !plan.mutating, + "a refused override must not claim to mutate" + ); + assert!(plan.reason.contains("ROCM_CLI_AMDGPU_DRIVER_VERSION")); + // Already proves no command can contain "pwned": a vacuous check over + // commands would be redundant with this. + assert!(plan.commands.is_empty()); + } + + #[test] + fn driver_plan_rejects_a_space_free_shell_injection_payload() { + // The metacharacter test above uses a payload full of spaces, which + // `is_safe_shell_token` already rejects on its own -- so it proves + // nothing about whether `$`, backticks, or `()` are actually in the + // denylist. Use a payload with no spaces at all so the rejection can + // only come from the metacharacters themselves. + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set("ROCM_CLI_AMDGPU_DRIVER_VERSION", "$(id)"); + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04" +VERSION_CODENAME=noble +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert!(!plan.supported); + assert_eq!(plan.policy, "invalid_driver_version_override"); + assert!( + !plan.mutating, + "a refused override must not claim to mutate" + ); + assert!(plan.commands.is_empty()); + } + + #[test] + fn driver_plan_treats_empty_version_override_as_unset() { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set("ROCM_CLI_AMDGPU_DRIVER_VERSION", ""); + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04" +VERSION_CODENAME=noble +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert_ne!(plan.policy, "invalid_driver_version_override"); + assert_eq!(plan.repo_version_expr, "latest"); + } + + #[test] + fn driver_plan_accepts_a_valid_version_override_and_uses_it_in_the_url() { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set("ROCM_CLI_AMDGPU_DRIVER_VERSION", "30.10"); + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04" +VERSION_CODENAME=noble +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + let rendered = render_driver_install_plan(&plan, false, false); + + assert_eq!(plan.repo_version_expr, "30.10"); + assert!(rendered.contains("amdgpu/30.10/")); + } + + #[test] + fn driver_plan_falls_back_on_a_hostile_codename() { + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04" +VERSION_CODENAME='; touch /tmp/pwned; echo ' +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert_eq!(plan.codename, "noble"); + assert!( + plan.commands + .iter() + .all(|command| !command.command.contains("pwned")) + ); + } + + #[test] + fn driver_plan_falls_back_on_a_malformed_version_id() { + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04'; touch /tmp/pwned; echo '" +VERSION_CODENAME=noble +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert_eq!(plan.version_id, ""); + assert!(!plan.supported); + assert!( + plan.commands + .iter() + .all(|command| !command.command.contains("pwned")) + ); + } + + #[test] + fn driver_plan_falls_back_on_a_space_free_hostile_codename() { + // The payload above is full of spaces, which `is_safe_shell_token` + // already rejects on its own -- it proves nothing about whether the + // metacharacters themselves are denylisted. Use a space-free payload + // so rejection can only come from the metacharacters. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + // Single-quoted so the os-release parser (which refuses an unquoted + // `$`/`(`/`)` outright, making the whole file unreadable rather than + // just this field) accepts the line and hands `$(id)` to + // `is_safe_shell_token` to reject on its own merits. + let os_release = r#" +ID=ubuntu +VERSION_ID="24.04" +VERSION_CODENAME='$(id)' +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert_eq!(plan.codename, "noble"); + assert!( + plan.commands + .iter() + .all(|command| !command.command.contains("$(id)")) + ); + } + + #[test] + fn driver_plan_falls_back_on_a_space_free_malformed_version_id() { + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + // Single-quoted for the same reason as the codename test above: a + // bare/double-quoted `$(id)` makes the whole os-release file + // unreadable, which would pass this test's assertions (empty + // version_id, unsupported) without ever reaching `is_safe_shell_token`. + let os_release = r" +ID=ubuntu +VERSION_ID='$(id)' +VERSION_CODENAME=noble +"; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert_eq!(plan.version_id, ""); + assert!(!plan.supported); + assert!( + plan.commands + .iter() + .all(|command| !command.command.contains("$(id)")) + ); + } + + #[test] + fn driver_plan_falls_back_on_a_space_free_hostile_os_id() { + // Closes the gap where `os_id`'s `is_safe_shell_token` filter (the + // `field("ID").filter(...)` call above `codename_for_version`) had no + // dedicated test at all: deleting that filter left every other test + // green, since none of them feed a hostile `ID` value. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + let os_release = r#" +ID='$(id)' +VERSION_ID="24.04" +VERSION_CODENAME=noble +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + + assert_eq!(plan.os_id, ""); + assert_eq!(plan.policy, "unsupported_linux_dkms_plan"); + assert!(!plan.supported); + assert!( + plan.commands + .iter() + .all(|command| !command.command.contains("$(id)")) + ); + } + + #[test] + fn is_safe_shell_token_rejects_quote_and_semicolon_payloads() { + // The hostile-codename/version-id/os-id tests above only ever send + // `$(id)` through this filter, which proves the metacharacter + // denylist works for *that* payload -- but a widened character class + // that happened to still reject `$`, `(`, `)` while now accepting + // `'`/`;` would pass every one of them undetected. `'` breaks out of + // this value's enclosing single-quoted shell string outright, and + // `;` chains a second command once out, so both must be checked + // directly against the function itself, not only indirectly. + assert!(!is_safe_shell_token("6.2.1'")); + assert!(!is_safe_shell_token("6.2.1;rm")); + assert!(!is_safe_shell_token("';rm -rf /;'")); + // `/` cannot break out of the quotes, but it would let the override + // rewrite the repo URL path. + assert!(!is_safe_shell_token("6.2.1/../x")); + assert!(is_safe_shell_token("6.2.1")); + } + + #[test] + fn driver_reconcile_without_state_gives_non_privileged_guidance() -> Result<()> { + let (root, paths) = test_paths("driver-reconcile-empty"); + + let rendered = reconcile_driver_install(&paths)?; + + assert!(rendered.contains("driver install reconciliation")); + assert!(rendered.contains("approval: not required")); + assert!(rendered.contains("privileged_commands: ")); + assert!(rendered.contains("no prior driver execution state found")); + assert!(rendered.contains("rocm install driver --dkms")); + assert!(!driver_install_state_path(&paths).exists()); + let _ = fs::remove_dir_all(root); + Ok(()) + } + + #[test] + fn driver_reconcile_updates_state_after_reboot() -> Result<()> { + let (root, paths) = test_paths("driver-reconcile-state"); + let pre_driver = rocm_core::DriverSummary { + policy: "linux_official_amd_dkms_wrapper".to_owned(), + status: "not_detected".to_owned(), + detail: None, + windows: None, + }; + let current_driver = rocm_core::DriverSummary { + policy: "linux_official_amd_dkms_wrapper".to_owned(), + status: "amdgpu_available".to_owned(), + detail: Some("/dev/kfd is present".to_owned()), + windows: None, + }; + let mut state = DriverInstallState { + approved_at_unix_ms: 1, + executed_at_unix_ms: Some(2), + pre_driver, + post_driver: None, + boot_id_at_execution: Some("old-boot".to_owned()), + reboot_required: true, + reboot_observed: false, + commands: vec!["sudo apt-get install -y amdgpu-dkms".to_owned()], + reconciled_at_unix_ms: None, + reconciliation: None, + }; + let checks = vec![ + DriverPassiveCheck { + name: "/dev/kfd".to_owned(), + status: "present".to_owned(), + detail: "KFD device node".to_owned(), + }, + DriverPassiveCheck { + name: "/dev/dri/renderD*".to_owned(), + status: "missing".to_owned(), + detail: "DRM render node".to_owned(), + }, + ]; + + let rendered = reconcile_driver_install_state( + &paths, + &mut state, + current_driver, Some("new-boot".to_owned()), checks, )?; @@ -2719,87 +3399,6 @@ VERSION_CODENAME=noble assert!(rendered.contains("add --dkms")); } - #[test] - fn resolve_shell_default_template_uses_default_when_env_unset() { - let _env = ScopedTestEnv::new(); - // A made-up variable name that nothing else sets, cleared under the lock, - // isolates the default path. - assert_eq!( - resolve_shell_default_template("${ROCM_CLI_TEST_UNSET_REPO_VERSION:-7.2.4}"), - "7.2.4" - ); - } - - #[test] - fn resolve_shell_default_template_prefers_env_value_when_set() { - let mut env = ScopedTestEnv::new(); - let var = "ROCM_CLI_TEST_REPO_VERSION_OVERRIDE"; - env.set(var, "9.9.9"); - assert_eq!( - resolve_shell_default_template(&format!("${{{var}:-7.2.4}}")), - "9.9.9" - ); - } - - #[test] - fn resolve_shell_default_template_treats_empty_env_as_unset() { - let mut env = ScopedTestEnv::new(); - let var = "ROCM_CLI_TEST_REPO_VERSION_EMPTY"; - env.set(var, ""); - assert_eq!( - resolve_shell_default_template(&format!("${{{var}:-7.2.4}}")), - "7.2.4" - ); - } - - #[test] - fn resolve_shell_default_template_passes_through_non_template() { - assert_eq!(resolve_shell_default_template("7.2.4"), "7.2.4"); - } - - #[test] - fn resolve_shell_default_template_leaves_bare_var_untouched() { - let _env = ScopedTestEnv::new(); - // No `:-default`, so there is nothing to resolve to; the input must pass - // through unchanged rather than being partially rewritten. - assert_eq!( - resolve_shell_default_template("${ROCM_CLI_TEST_UNSET_REPO_VERSION}"), - "${ROCM_CLI_TEST_UNSET_REPO_VERSION}" - ); - } - - #[test] - fn resolve_shell_default_template_leaves_nested_default_untouched() { - let _env = ScopedTestEnv::new(); - // A nested default is beyond the flat matcher; returning the literal - // input keeps a `${B:-x}` fragment from leaking as a "resolved" value. - assert_eq!( - resolve_shell_default_template("${ROCM_CLI_TEST_UNSET_A:-${ROCM_CLI_TEST_UNSET_B:-x}}"), - "${ROCM_CLI_TEST_UNSET_A:-${ROCM_CLI_TEST_UNSET_B:-x}}" - ); - } - - #[test] - fn driver_plan_dry_run_repo_version_line_is_resolved() { - let _env = ScopedTestEnv::with_amd_overrides_cleared(); - // Regression for the dry-run output leaking the raw shell placeholder on - // the `repo_version:` line instead of the effective version. - let os_release = r#" -ID=rhel -VERSION_ID="9.7" -"#; - let plan = build_driver_install_plan_read( - &test_examine("linux", false), - &os_release_found(os_release), - true, - PrivilegeEscalation::Sudo, - ); - let rendered = render_driver_install_plan(&plan, false, true); - - assert!(rendered.contains("repo_version: 7.2.4")); - assert!(!rendered.contains("repo_version: ${ROCM_CLI_AMDGPU_VERSION:-7.2.4}")); - } - #[test] fn driver_plan_debian_12_omits_linux_modules_extra() { let _env = ScopedTestEnv::with_amd_overrides_cleared(); @@ -2846,8 +3445,12 @@ VERSION_ID="9.7" assert!(rendered.contains("kernel-headers-$(uname -r)")); assert!(rendered.contains("kernel-devel-$(uname -r)")); assert!(rendered.contains("kernel-devel-matched-$(uname -r)")); - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/rhel/9.7/")); - assert!(rendered.contains("amdgpu-install-7.2.4.70204-1.el9.noarch.rpm")); + assert!(rendered.contains("sudo rpm --import https://repo.radeon.com/rocm/rocm.gpg.key")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/rhel/9.7/main/x86_64") + ); + assert!(rendered.contains("gpgcheck=1")); + assert!(rendered.contains("/etc/yum.repos.d/amdgpu.repo")); assert!(rendered.contains("Execute: sudo dnf install -y amdgpu-dkms")); assert!(rendered.contains("approval: required")); } @@ -2870,8 +3473,12 @@ VERSION_ID="10.1" assert!(plan.supported); assert!(rendered.contains("approval: not required")); assert!(rendered.contains("kernel-uek-devel-$(uname -r)")); - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/el/10/")); - assert!(rendered.contains("amdgpu-install-7.2.4.70204-1.el10.noarch.rpm")); + // `rpm --import` is pushed once after the distro-specific match, not + // inside the RHEL/Generic arm -- Oracle's flow must get it too. + assert!(rendered.contains("sudo rpm --import https://repo.radeon.com/rocm/rocm.gpg.key")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/el/10/main/x86_64") + ); assert!(rendered.contains("dry run only")); } @@ -2895,7 +3502,13 @@ VERSION_ID="9.7" rendered .contains("sudo dnf install -y kernel-headers kernel-devel kernel-devel-matched") ); - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/el/9.7/")); + // Rocky is `Generic`, not `Rhel`, in `DnfDriverDistro` -- confirms the + // `rpm --import` push is unconditional across the dnf match arms, not + // only wired for the `Rhel`/`Oracle` arms the other two tests cover. + assert!(rendered.contains("sudo rpm --import https://repo.radeon.com/rocm/rocm.gpg.key")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/el/9.7/main/x86_64") + ); assert!(rendered.contains("Execute: sudo dnf install -y amdgpu-dkms")); } @@ -2917,8 +3530,10 @@ VERSION_ID="9.4" assert!(plan.supported); assert!(plan.mutating); - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/el/9.4/")); - assert!(rendered.contains("amdgpu-install-7.2.4.70204-1.el9.noarch.rpm")); + assert!(rendered.contains("sudo rpm --import https://repo.radeon.com/rocm/rocm.gpg.key")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/el/9.4/main/x86_64") + ); assert!(rendered.contains("Execute: sudo dnf install -y amdgpu-dkms")); } @@ -2947,7 +3562,7 @@ VERSION_ID="9.4" fn driver_plan_debian_uses_intended_ubuntu_suite() { let _env = ScopedTestEnv::with_amd_overrides_cleared(); // AMD's documented Debian install deliberately serves Debian from the - // Ubuntu-suite graphics tree (Debian 12 -> jammy). Lock that in and + // Ubuntu-suite amdgpu tree (Debian 12 -> jammy). Lock that in and // ensure the plan explains the mapping is intentional. let os_release = r#" ID=debian @@ -2964,7 +3579,7 @@ VERSION_CODENAME=bookworm assert!(plan.supported); assert_eq!(plan.codename, "jammy"); - assert!(rendered.contains("https://repo.radeon.com/graphics/7.2.4/ubuntu jammy main")); + assert!(rendered.contains("https://repo.radeon.com/amdgpu/latest/ubuntu jammy main")); assert!( plan.reason .contains("intentionally uses AMD's Ubuntu-suite repository") @@ -2990,13 +3605,208 @@ VERSION_ID="15.7" assert!(rendered.contains("`zypper` package manager is available")); assert!(rendered.contains("SUSEConnect")); assert!(rendered.contains("sle-module-desktop-applications/15.7/x86_64")); - assert!(rendered.contains("sudo zypper install -y kernel-default-devel")); - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/sle/15.7/")); - assert!(rendered.contains("sudo zypper --no-gpg-checks install -y")); - assert!(rendered.contains("Execute: sudo zypper install -y amdgpu-dkms")); + assert!(rendered.contains("sudo zypper --non-interactive install -y kernel-default-devel")); + assert!(rendered.contains("sudo rpm --import https://repo.radeon.com/rocm/rocm.gpg.key")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/sle/15.7/main/x86_64") + ); + assert!(rendered.contains("gpgcheck=1")); + assert!(rendered.contains("/etc/zypp/repos.d/amdgpu.repo")); + assert!( + rendered.contains("sudo zypper --non-interactive --gpg-auto-import-keys refresh"), + "the post-repo-add refresh must auto-trust the new repo's signing key, \ + or zypper would prompt (and hang this unattended flow): {rendered}" + ); + assert!(rendered.contains("Execute: sudo zypper --non-interactive install -y amdgpu-dkms")); assert!(rendered.contains("approval: required")); } + #[test] + fn sles_repo_baseurl_uses_the_given_repo_version_expr_not_a_hardcoded_latest() { + // Mutation-proofs the parameter wiring itself: a mutation that + // hardcoded `latest` into this function's body left every existing + // SLES test green because their default env override also resolves + // to `latest`, so none of them could tell "wired correctly" apart + // from "hardcoded to the same value the default happens to be". + assert_eq!( + sles_repo_baseurl("6.2.1", "15.7"), + "https://repo.radeon.com/amdgpu/6.2.1/sle/15.7/main/x86_64" + ); + assert_eq!( + sles_repo_baseurl("latest", "15.7"), + "https://repo.radeon.com/amdgpu/latest/sle/15.7/main/x86_64" + ); + } + + #[test] + fn driver_plan_dnf_overrides_the_repo_version_via_the_driver_version_env() { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set("ROCM_CLI_AMDGPU_DRIVER_VERSION", "30.10"); + let os_release = "ID=rhel\nVERSION_ID=\"9.7\"\n"; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + let rendered = render_driver_install_plan(&plan, false, false); + + assert!(plan.supported); + assert!( + rendered.contains("amdgpu/30.10/rhel/9.7/"), + "an overridden ROCM_CLI_AMDGPU_DRIVER_VERSION must reach the dnf repo baseurl, \ + not a hardcoded `latest`: {rendered}" + ); + } + + #[test] + fn driver_plan_id_like_derivatives_honour_the_driver_version_env() { + // (os-release, expected repo path). Each derivative arm of + // `driver_plan_via_id_like` must thread the override, not `latest`. + let cases = [ + ( + "ID=almalinux\nID_LIKE=\"rhel centos fedora\"\nVERSION_ID=\"9.6\"\n", + "amdgpu/30.10/el/9.6", + ), + ( + "ID=pop\nID_LIKE=\"ubuntu debian\"\nVERSION_ID=\"24.04\"\nVERSION_CODENAME=noble\n", + "amdgpu/30.10/ubuntu", + ), + ( + "ID=devuan\nID_LIKE=debian\nVERSION_ID=\"12\"\n", + "amdgpu/30.10/ubuntu", + ), + ]; + for (os_release, want) in cases { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set("ROCM_CLI_AMDGPU_DRIVER_VERSION", "30.10"); + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + let rendered = render_driver_install_plan(&plan, false, false); + assert!(plan.supported, "{os_release}"); + assert!(rendered.contains(want), "{os_release}: {rendered}"); + assert!( + !rendered.contains("amdgpu/latest"), + "{os_release}: {rendered}" + ); + } + } + + #[test] + fn approving_an_unsupported_plan_is_an_error_but_not_a_dry_run_or_windows() { + let mut plan = unsupported_test_plan(); + assert!(approval_of_unsupported_plan(&plan, true, false)); + assert!(!approval_of_unsupported_plan(&plan, false, false)); + assert!(!approval_of_unsupported_plan(&plan, true, true)); + plan.policy = "windows_validate_only".to_owned(); + assert!(!approval_of_unsupported_plan(&plan, true, false)); + plan.policy = "x".to_owned(); + plan.supported = true; + assert!(!approval_of_unsupported_plan(&plan, true, false)); + } + + fn unsupported_test_plan() -> DriverInstallPlan { + invalid_driver_version_override_plan("x".to_owned()) + } + + #[test] + fn driver_plan_refuses_a_retired_version_pin_variable() { + for retired in RETIRED_DRIVER_VERSION_VARS { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set(retired, "7.2.4"); + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found("ID=ubuntu\nVERSION_ID=\"24.04\"\nVERSION_CODENAME=noble\n"), + true, + PrivilegeEscalation::Sudo, + ); + + assert!(!plan.supported && !plan.mutating, "{retired}"); + assert_eq!(plan.policy, "retired_driver_version_override"); + assert!(plan.reason.contains("ROCM_CLI_AMDGPU_DRIVER_VERSION")); + } + } + + #[test] + fn driver_plan_support_matches_rocm_core_diagnose() { + // `rocm-core` cannot call the plan builder, so diagnose keeps its own + // copy of the support matrix (`dkms_driver_plan_is_supported`); this + // fails if the two disagree on any host. + let _env = ScopedTestEnv::with_amd_overrides_cleared(); + let hosts = [ + ("ubuntu", "22.04", "debian"), + ("ubuntu", "24.04", "debian"), + ("Ubuntu", "24.04", "debian"), + ("ubuntu", "26.04", "debian"), + ("debian", "12", ""), + ("debian", "13", ""), + ("rhel", "9.7", "fedora"), + ("rhel", "10.0", "fedora"), + ("ol", "9.7", "fedora"), + ("ol", "9.7", "rhel fedora"), + ("ol", "9.5", "rhel fedora"), + ("rocky", "9.6", "rhel centos fedora"), + ("almalinux", "9.6", "rhel centos fedora"), + ("sles", "15.7", "suse"), + ("sle", "15.7", "suse"), + ("pop", "24.04", "ubuntu debian"), + ("linuxmint", "22", "ubuntu debian"), + ("fedora", "41", ""), + ("opensuse-leap", "15.7", "suse opensuse"), + ]; + for (id, version, like) in hosts { + let os_release = format!( + "ID={id}\nVERSION_ID=\"{version}\"\nID_LIKE=\"{like}\"\nVERSION_CODENAME=noble\n" + ); + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(&os_release), + true, + PrivilegeEscalation::Sudo, + ); + let host = rocm_core::examine::Examination { + distro_id: id.to_owned(), + distro_version: version.to_owned(), + distro_id_like: like.to_owned(), + ..rocm_core::examine::Examination::default() + }; + assert_eq!( + plan.supported, + rocm_core::diagnose::dkms_driver_plan_is_supported(&host), + "{id} {version} (ID_LIKE={like}): plan policy {}", + plan.policy + ); + } + } + + #[test] + fn driver_plan_sles_overrides_the_repo_version_via_the_driver_version_env() { + let mut env = ScopedTestEnv::with_amd_overrides_cleared(); + env.set("ROCM_CLI_AMDGPU_DRIVER_VERSION", "6.2.1"); + let os_release = r#" +ID=sles +VERSION_ID="15.7" +"#; + let plan = build_driver_install_plan_read( + &test_examine("linux", false), + &os_release_found(os_release), + true, + PrivilegeEscalation::Sudo, + ); + let rendered = render_driver_install_plan(&plan, false, false); + + assert!(plan.supported); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/6.2.1/sle/15.7/main/x86_64"), + "an overridden ROCM_CLI_AMDGPU_DRIVER_VERSION must reach the SLES repo baseurl, \ + not a hardcoded `latest`: {rendered}" + ); + } + #[test] fn driver_plan_unsupported_linux_is_non_mutating() { let _env = ScopedTestEnv::with_amd_overrides_cleared(); @@ -3323,12 +4133,12 @@ VERSION_ID="41" // `dkms: true` is passed deliberately: WSL2 has no kernel module to // build, so the flag must not pull in the bare-metal path. // - // `build_driver_install_plan_at` resolves `${ROCM_CLI_AMDGPU_VERSION:-...}` - // from process env before it reaches the WSL branch, and the WSL branch - // then reads the three ROCDXG vars — an exported + // The WSL branch reads the three ROCDXG vars — an exported // `ROCM_CLI_ROCDXG_VERSION` would steer this plan into a refusal and - // fail the `plan.supported` assertion below. So this reader takes the - // guard that clears both sets. + // fail the `plan.supported` assertion below. `ROCM_CLI_AMDGPU_DRIVER_VERSION` + // is never consulted on this path (the WSL dispatch happens before that + // var is read), but this reader still takes the combined guard so it + // isn't the one test that has to remember which half it needs. let _env = scoped_rocdxg_env(); let plan = build_driver_install_plan_read( &test_examine("linux", true), @@ -3442,10 +4252,10 @@ VERSION_ID="41" /// that is asserting on the default. /// /// Builds on [`ScopedTestEnv::with_amd_overrides_cleared`] rather than - /// `new` because a WSL plan reached through `build_driver_install_plan_read` - /// resolves the bare-metal AMDGPU overrides before it dispatches to the WSL - /// branch: a caller needing one of these two guards needs both, and one - /// helper spares every test from picking the wrong half. + /// `new` so a caller that also exercises the bare-metal AMDGPU override + /// through the same `build_driver_install_plan_at` call doesn't need a + /// second guard: one helper covers both input sets regardless of which + /// branch actually reads them. fn scoped_rocdxg_env() -> ScopedTestEnv { let mut env = ScopedTestEnv::with_amd_overrides_cleared(); env.clear("ROCM_CLI_ROCDXG_VERSION"); @@ -3681,7 +4491,7 @@ VERSION_ID="41" let _env = scoped_rocdxg_env(); let plan = wsl_rocdxg_driver_plan(PrivilegeEscalation::Sudo); - let version = plan.repo_version.clone(); + let version = plan.repo_version_expr.clone(); let pinned = ROCDXG_PINNED_DIGESTS .iter() .find_map(|(pinned_version, digest)| { @@ -3791,7 +4601,7 @@ VERSION_ID="41" let mut env = scoped_rocdxg_env(); let plan = wsl_rocdxg_driver_plan(PrivilegeEscalation::Sudo); - assert_eq!(plan.repo_version, "1.2.2"); + assert_eq!(plan.repo_version_expr, "1.2.2"); let commands = plan.execution_commands().join("\n"); // The version is resolved here, not deferred to the shell: the plan the // user approves has to name the build the install will actually fetch. @@ -3816,7 +4626,7 @@ VERSION_ID="41" env.set("ROCM_CLI_ROCDXG_VERSION", "9.9.9"); env.set(ROCDXG_SHA256_ENV, &"b".repeat(64)); let overridden = wsl_rocdxg_driver_plan(PrivilegeEscalation::Sudo); - assert_eq!(overridden.repo_version, "9.9.9"); + assert_eq!(overridden.repo_version_expr, "9.9.9"); let commands = overridden.execution_commands().join("\n"); assert!( commands.contains("rocdxg-roct_9.9.9_amd64.deb"), @@ -3862,7 +4672,7 @@ ID_LIKE="ubuntu debian" assert_eq!(plan.policy, "linux_official_amd_dkms_wrapper"); // Ubuntu-family derivatives ship the Ubuntu kernel, so linux-modules-extra applies. assert!(rendered.contains("linux-modules-extra-$(uname -r)")); - assert!(rendered.contains("https://repo.radeon.com/graphics/7.2.4/ubuntu jammy main")); + assert!(rendered.contains("https://repo.radeon.com/amdgpu/latest/ubuntu jammy main")); assert!(rendered.contains("Execute: sudo apt-get install -y amdgpu-dkms")); } @@ -3885,7 +4695,7 @@ ID_LIKE=debian assert!(plan.supported); // Debian-family maps to the Ubuntu jammy repo and omits linux-modules-extra. - assert!(rendered.contains("https://repo.radeon.com/graphics/7.2.4/ubuntu jammy main")); + assert!(rendered.contains("https://repo.radeon.com/amdgpu/latest/ubuntu jammy main")); assert!(!rendered.contains("linux-modules-extra-$(uname -r)")); assert!(rendered.contains("amdgpu-dkms")); } @@ -3911,11 +4721,13 @@ ID_LIKE="rhel centos fedora" assert!(plan.mutating); assert_eq!(plan.policy, "linux_official_amd_dkms_wrapper"); // EL rebuilds use the vendor-neutral el/ repo path, not rhel/. - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/el/9.6/")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/el/9.6/main/x86_64") + ); assert!(!rendered.contains("/rhel/9.6/")); - assert!(rendered.contains("amdgpu-install-7.2.4.70204-1.el9.noarch.rpm")); // el9 uses the version-aware standard-kernel prepare commands. assert!(rendered.contains("kernel-devel-matched-$(uname -r)")); + assert!(rendered.contains("sudo rpm --import https://repo.radeon.com/rocm/rocm.gpg.key")); assert!(rendered.contains("Execute: sudo dnf install -y amdgpu-dkms")); } @@ -3937,11 +4749,13 @@ ID_LIKE="rhel centos fedora" assert!(plan.supported); // EL 8 is served from the major-version path (el/8), matching AMD docs. - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/el/8/")); - assert!(rendered.contains("-1.el8.noarch.rpm")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/el/8/main/x86_64") + ); // el8 has no kernel-devel-matched package. assert!(!rendered.contains("kernel-devel-matched")); assert!(rendered.contains("kernel-devel-$(uname -r)")); + assert!(rendered.contains("sudo rpm --import https://repo.radeon.com/rocm/rocm.gpg.key")); } #[test] @@ -3986,7 +4800,9 @@ ID_LIKE=fedora let rendered = render_driver_install_plan(&plan, false, false); assert!(plan.supported); - assert!(rendered.contains("repo.radeon.com/amdgpu-install/7.2.4/rhel/9.7/")); + assert!( + rendered.contains("baseurl=https://repo.radeon.com/amdgpu/latest/rhel/9.7/main/x86_64") + ); assert!(!rendered.contains("/el/9.7/")); } diff --git a/apps/rocm/src/main.rs b/apps/rocm/src/main.rs index ba39a28992..6cecb79257 100644 --- a/apps/rocm/src/main.rs +++ b/apps/rocm/src/main.rs @@ -10879,12 +10879,13 @@ pub(crate) mod tests { } } - /// Clear the two AMD driver-install override vars for the duration of the - /// test, so a value exported in the developer's or runner's shell cannot - /// leak into a plan and make its resolved `repo_version`/package release - /// disagree with the assertion. + /// Clear the AMD driver-version override for the duration of the test, so + /// a value exported in the developer's or runner's shell cannot leak into + /// a plan and make its resolved `repo_version_expr` disagree with the + /// assertion. pub(crate) fn with_amd_overrides_cleared() -> Self { let mut env = Self::new(); + env.clear("ROCM_CLI_AMDGPU_DRIVER_VERSION"); env.clear("ROCM_CLI_AMDGPU_VERSION"); env.clear("ROCM_CLI_AMDGPU_PACKAGE_RELEASE"); env diff --git a/apps/rocm/src/remote/doctor.rs b/apps/rocm/src/remote/doctor.rs index aeb8d27254..891db07a21 100644 --- a/apps/rocm/src/remote/doctor.rs +++ b/apps/rocm/src/remote/doctor.rs @@ -286,6 +286,28 @@ mod tests { assert!(!examination.os_family.is_empty()); } + #[test] + fn distro_id_like_survives_the_remote_round_trip() { + // A remote examination is only ever seen through JSON, so if this + // field didn't round-trip, the ID_LIKE fallback would silently never + // fire for `rocm remote doctor`. + let mut value: serde_json::Value = serde_json::from_str(&examine_json()).unwrap(); + value.as_object_mut().unwrap().insert( + "distro_id_like".to_owned(), + serde_json::json!("rhel fedora"), + ); + let parsed = parse_examination(&value.to_string()).expect("examined"); + assert_eq!(parsed.distro_id_like, "rhel fedora"); + } + + #[test] + fn a_remote_on_an_older_cli_without_distro_id_like_still_parses() { + let mut value: serde_json::Value = serde_json::from_str(&examine_json()).unwrap(); + value.as_object_mut().unwrap().remove("distro_id_like"); + let parsed = parse_examination(&value.to_string()).expect("older remote must parse"); + assert_eq!(parsed.distro_id_like, ""); + } + #[test] fn a_remote_on_another_version_says_so_instead_of_failing_obscurely() { let error = parse_examination(r#"{"unexpected": true}"#) diff --git a/apps/rocm/src/runtime_install_finalize.rs b/apps/rocm/src/runtime_install_finalize.rs index ac1f8777ce..c833e60019 100644 --- a/apps/rocm/src/runtime_install_finalize.rs +++ b/apps/rocm/src/runtime_install_finalize.rs @@ -152,9 +152,8 @@ pub(crate) const fn empty_as_unknown(value: &str) -> &str { /// when the former is absent (per `os-release(5)`). /// /// Deliberately not routed through `rocm_core::host_path`: every caller builds -/// a package-manager plan (`apt`, `dnf`, `amdgpu-install`) that then runs -/// against the real machine, so a simulated host root must not choose its -/// distro. The descriptive distro name `examine` reports comes from +/// a package-manager plan (`apt`, `dnf`, `zypper`) that then runs against the +/// real machine, so a simulated host root must not choose its distro. The descriptive distro name `examine` reports comes from /// `ExamineSummary::gather`'s `distro` field, which does follow a simulated /// root. pub(crate) fn real_os_release() -> rocm_core::hardware_root::OsRelease { diff --git a/crates/rocm-core/src/diagnose.rs b/crates/rocm-core/src/diagnose.rs index 25766e904f..327fb73e07 100644 --- a/crates/rocm-core/src/diagnose.rs +++ b/crates/rocm-core/src/diagnose.rs @@ -428,7 +428,6 @@ const KEYWORDS_DPKG_BROKEN: KeywordTable = &[ 25, "apt mentions dpkg failure", ), - ("--accept-eula", 40, "user mentions --accept-eula"), ]; const KEYWORDS_PAGE_FAULT: KeywordTable = &[ @@ -839,18 +838,31 @@ fn check_3_rocm_kernel_unsupported(e: &Examination, symptom: &str) -> Diagnosis if score <= 0 { return zero("fix-3-rocm-kernel", "ROCm/distro/kernel triple unsupported"); } + let mut commands = vec![ + format!("# Current: kernel={kernel} distro={distro} {distro_v} rocm={rocm_version}"), + "# Compare to the live AMD matrix:".to_owned(), + "# https://rocm.docs.amd.com/projects/install-on-linux/en/latest/reference/system-requirements.html".to_owned(), + ]; + if ubuntu_driver_is_native_archive(e) { + commands.push("# Note on Ubuntu 26.04: the amdgpu driver ships in-tree there, so `rocm install driver --dkms` plans nothing; run `apt install rocm` (with sudo if not root) instead.".to_owned()); + } else if dkms_driver_plan_is_supported(e) { + commands.push("# If your kernel is above the supported range, install the HWE".to_owned()); + commands.push( + "# kernel that matches ROCm, then rerun `rocm install driver --dkms --yes`.".to_owned(), + ); + } else { + commands.push(format!( + "# `rocm install driver --dkms` plans nothing on {distro} {distro_v}: it isn't one of the AMD-documented distros/versions above, so rerunning it won't fix this." + )); + } let fix = Fix { summary: "Cross-check your kernel/distro against the live AMD compatibility matrix before reinstalling.".to_owned(), - commands: vec![ - format!("# Current: kernel={kernel} distro={distro} {distro_v} rocm={rocm_version}"), - "# Compare to the live AMD matrix:".to_owned(), - "# https://rocm.docs.amd.com/projects/install-on-linux/en/latest/reference/system-requirements.html".to_owned(), - "# If your kernel is above the supported range, install the HWE".to_owned(), - "# kernel that matches ROCm, or rerun amdgpu-install with --no-dkms.".to_owned(), - ], + commands, fix_id: "fix-3-rocm-kernel".to_owned(), needs_reboot: true, - verify: "lsmod | grep amdgpu && rocminfo | head -n 20".to_owned(), + // Check rocminfo's own exit code: piping through `head` would report + // the pipeline's last stage and mask a missing/failing `rocminfo`. + verify: "lsmod | grep amdgpu && rocminfo >/dev/null 2>&1".to_owned(), ..Fix::default() }; finalize( @@ -1088,6 +1100,25 @@ fn check_6_path_missing(e: &Examination, symptom: &str) -> Diagnosis { ) } +/// The package manager's own "re-resolve the repo metadata" command for +/// `family`, without a `sudo` prefix. Shared by `check_7_stale_repos` and +/// `check_12_repo_native_broken` so a future per-family tweak (e.g. adding +/// `--non-interactive` to one of these) only has one copy to update instead +/// of drifting across the two checks. +fn refresh_command_for_family(family: &str) -> Option<&'static str> { + match family { + "apt" => Some("apt update"), + // `dnf clean all` only deletes the local cache and always exits 0; it + // can't fail even when a repo file is malformed or unreachable, which + // is exactly what `verify` needs to catch. `dnf makecache` re-fetches + // and parses the metadata, so it fails loudly in that case, matching + // `apt update` and `zypper refresh` below. + "dnf" => Some("dnf makecache"), + "zypper" => Some("zypper refresh"), + _ => None, + } +} + fn check_7_stale_repos(e: &Examination, symptom: &str) -> Diagnosis { let mut score = 0; let mut evidence = Vec::new(); @@ -1106,30 +1137,81 @@ fn check_7_stale_repos(e: &Examination, symptom: &str) -> Diagnosis { if score <= 0 { return zero("fix-7-stale-repos", "Stale ROCm repos"); } - let mut commands = - vec!["ls /etc/apt/sources.list.d/ | grep -iE 'rocm|amdgpu|radeon' || true".to_owned()]; + let family = repo_pkg_family(e); + let mut commands = Vec::new(); + match family { + "apt" => commands + .push("ls /etc/apt/sources.list.d/ | grep -iE 'rocm|amdgpu|radeon' || true".to_owned()), + "dnf" => { + commands + .push("ls /etc/yum.repos.d/ | grep -iE 'rocm|amdgpu|radeon' || true".to_owned()); + } + "zypper" => commands + .push("ls /etc/zypp/repos.d/ | grep -iE 'rocm|amdgpu|radeon' || true".to_owned()), + _ => {} + } for r in repos { commands.push(format!( "# sudo mv {r} {r}.bak # quarantine, do not delete yet" )); } - commands.push("sudo apt update".to_owned()); - commands.push("# If apt now resolves, reinstall via the correct method only:".to_owned()); + if let Some(cmd) = refresh_command_for_family(family) { + commands.push(format!("sudo {cmd}")); + } commands.push( - "# amdgpu-install --usecase=rocm,hip --no-dkms # if you want amdgpu-install".to_owned(), + "# If the package manager now resolves, reinstall the driver only (review the plan, then approve):" + .to_owned(), ); - commands.push("# or use the distro packages exclusively".to_owned()); + if ubuntu_driver_is_native_archive(e) { + commands.push("# sudo apt install rocm # Ubuntu 26.04 ships the amdgpu driver in-tree, so there is no DKMS driver to reinstall".to_owned()); + } else if dkms_driver_plan_is_supported(e) { + commands.push("# rocm install driver --dkms # review the plan".to_owned()); + commands + .push("# rocm install driver --dkms --yes # reinstalls the driver only".to_owned()); + } else { + commands.push(format!( + "# `rocm install driver --dkms` plans nothing on {} {}: it isn't one of the AMD-documented distros/versions; see https://rocm.docs.amd.com/projects/install-on-linux/en/latest/reference/system-requirements.html", + e.distro_id, e.distro_version + )); + } + let verify = match refresh_command_for_family(family) { + // `set -o pipefail` so the exit status reflects `{cmd}` itself, not + // `tail` (which exits 0 regardless) -- otherwise a copy-pasted + // failure of this command silently reads as success. + Some(cmd) => format!("bash -c 'set -o pipefail; sudo {cmd} 2>&1 | tail -n 20'"), + // No package manager to refresh on this host, so the only real + // post-condition the suggested (commented-out) `mv ... .bak` + // commands above leave behind is that the flagged repo files are + // gone from their original paths -- unlike + // `rocm examine --json | grep -q '"rocm_install_method"'`, which is + // present in every `examine --json` output regardless of host state + // and so can never fail. When no repo files were actually flagged + // (the symptom text alone triggered this fix), there is nothing to + // assert about a `mv` command that never ran, so fall back to a + // real, still-falsifiable check: no ROCm/AMDGPU repo file exists + // under any of the three families' repo directories. + None if repos.is_empty() => { + "! ls /etc/apt/sources.list.d/ /etc/yum.repos.d/ /etc/zypp/repos.d/ 2>/dev/null \ + | grep -iE 'rocm|amdgpu|radeon'" + .to_owned() + } + None => repos + .iter() + .map(|r| format!("! test -e '{r}'")) + .collect::>() + .join(" && "), + }; let fix = Fix { - summary: "Quarantine duplicate ROCm/AMDGPU repo files and resolve apt before re-running any installer.".to_owned(), + summary: "Quarantine duplicate ROCm/AMDGPU repo files and resolve the package manager before re-running any installer.".to_owned(), commands, needs_sudo: true, fix_id: "fix-7-stale-repos".to_owned(), - verify: "sudo apt update 2>&1 | tail -n 20".to_owned(), + verify, ..Fix::default() }; finalize( "fix-7-stale-repos", - "Stale or conflicting APT/DNF repos from prior installer runs", + "Stale or conflicting APT/DNF/zypper repos from prior installer runs", score, evidence, fix, @@ -1420,43 +1502,255 @@ fn check_11_iommu_hang(e: &Examination, symptom: &str) -> Diagnosis { ) } -fn check_12_amdgpu_install_broken(e: &Examination, symptom: &str) -> Diagnosis { +/// Best-effort package-manager family for this host: prefer the family +/// implied by the repo dir the marker files live under, since that's the +/// config these commands act on, and fall back to the same `ID_LIKE`-aware +/// resolver `openmpi` uses only when no repo file was found -- restricted to +/// apt/dnf/zypper, the only families this check has remediation for: a +/// pacman host (which `openmpi`'s resolver can return, for its own, +/// unrelated library installers) falls back to `"unknown"`, rather than the +/// remediation commands mismatching a real "pacman" answer. +fn repo_pkg_family(e: &Examination) -> &'static str { + if e.rocm_repos_seen.iter().any(|r| r.contains("/etc/apt/")) { + return "apt"; + } + if e.rocm_repos_seen + .iter() + .any(|r| r.contains("/etc/yum.repos.d/")) + { + return "dnf"; + } + if e.rocm_repos_seen.iter().any(|r| r.contains("/etc/zypp/")) { + return "zypper"; + } + // The two `match family` sites below only implement apt/dnf/zypper + // remediation (this check has no pacman-based repo-native ROCm install to + // recover, unlike `openmpi`'s library installers); their `_` arms already + // treat any unhandled string, including a literal `"pacman"`, as "no + // package-manager-specific command" -- so collapsing pacman to + // `"unknown"` here doesn't change that match's behavior. It's kept so the + // family string this function returns always matches what its callers + // actually handle, rather than leaking a fourth value they merely happen + // to no-op on today. + crate::openmpi::resolve_package_manager( + &e.distro_id.to_ascii_lowercase(), + &e.distro_id_like.to_ascii_lowercase(), + ) + .filter(|pm| { + matches!( + pm, + crate::openmpi::PackageManager::Apt + | crate::openmpi::PackageManager::Dnf + | crate::openmpi::PackageManager::Zypper + ) + }) + .map_or("unknown", crate::openmpi::PackageManager::as_str) +} + +/// Whether `rocm_install_method` indicates a package-manager-driven install, +/// covering both the marker-file-based `"repo-native"` value and the +/// dpkg/rpm-query fallback's `"apt"`/`"dnf"` (see `probe_rocm_install` in +/// `examine.rs`): a host without one of `REPO_NATIVE_INSTALL_MARKERS` present +/// but whose package manager confirms `rocm-hip-runtime` is installed is +/// still repo-native-managed, just detected a different way. +/// +/// `"amdgpu-install"` is kept as a legacy alias of `"repo-native"`: an older +/// `rocm remote doctor` client can still send the retired value for a while +/// after this rename, and dropping it here would silently lose fix-12 +/// evidence for that machine rather than just serving a stale label. +fn is_repo_native_installed(method: &str) -> bool { + matches!(method, "repo-native" | "amdgpu-install" | "apt" | "dnf") +} + +/// Whether `rocm install driver --dkms` has nothing to reinstall on this +/// host. Ubuntu 26.04 ships the amdgpu kernel driver in-tree and +/// `build_driver_install_plan_at` in `apps/rocm/src/driver_install.rs` plans no +/// DKMS repo install there (see the `("ubuntu", "26.04")` arm); `rocm-core` +/// can't call that plan builder directly (`apps/rocm` depends on +/// `rocm-core`, not the reverse), so this mirrors just that one condition. +/// WSL hosts never reach `check_3`/`check_7`/`check_12` at all -- see +/// `CHECKERS`'s `&["linux"]` tag on each. +fn ubuntu_driver_is_native_archive(e: &Examination) -> bool { + e.distro_id.eq_ignore_ascii_case("ubuntu") && e.distro_version == "26.04" +} + +/// Whether `rocm install driver --dkms` plans any commands for this host. +/// +/// Mirrors the full `(os_id, version_id)` / `ID_LIKE`-derivative +/// matrix in `apps/rocm/src/driver_install.rs::build_driver_install_plan_at` +/// (`rocm-core` can't call that plan builder directly -- see +/// `ubuntu_driver_is_native_archive` above for why). Ubuntu 26.04 is +/// deliberately excluded here even though its plan is also "no DKMS repo +/// commands": that host has its own guidance (native archive, `apt install +/// rocm`), handled separately via `ubuntu_driver_is_native_archive`, not the +/// "no plan was guessed for this distro at all" case this function reports. +pub fn dkms_driver_plan_is_supported(e: &Examination) -> bool { + let os_id = e.distro_id.to_ascii_lowercase(); + let version_id = e.distro_version.as_str(); + match (os_id.as_str(), version_id) { + ("ubuntu", "22.04" | "24.04") => true, + ("debian", "12" | "13") => true, + ("rhel", "10.1" | "10.0" | "9.7" | "9.6" | "9.4" | "8.10") => true, + ("ol", "10.1" | "9.7" | "8.10") => true, + ("rocky", "9.4" | "9.6" | "9.7") => true, + ("sles" | "sle", "15.7") => true, + _ => { + let likes: Vec = e + .distro_id_like + .split_whitespace() + .map(str::to_ascii_lowercase) + .collect(); + let mentions = |family: &str| likes.iter().any(|like| like == family); + (mentions("ubuntu") && matches!(version_id, "22.04" | "24.04")) + || (mentions("debian") && matches!(version_id, "12" | "13")) + || (mentions("rhel") + && !matches!(os_id.as_str(), "ol" | "oracle") + && matches!(version_id, "10.1" | "10.0" | "9.7" | "9.6" | "9.4" | "8.10")) + } + } +} + +fn check_12_repo_native_broken(e: &Examination, symptom: &str) -> Diagnosis { let mut score = 0; let mut evidence = Vec::new(); let method = &e.rocm_install_method; - if method == "amdgpu-install" { - evidence.push("ROCm was installed via amdgpu-install".to_owned()); + if is_repo_native_installed(method) { + evidence.push("ROCm was installed via the repo-native package-manager flow".to_owned()); } let (kw_score, kw_ev) = keyword_score(symptom, KEYWORDS_DPKG_BROKEN); score += kw_score; evidence.extend(kw_ev); - if method == "amdgpu-install" && kw_score > 0 { + if is_repo_native_installed(method) && kw_score > 0 { score += 20; } if score <= 0 { - return zero("fix-12-installer", "amdgpu-install broken state"); + return zero("fix-12-installer", "repo-native install broken state"); + } + let family = repo_pkg_family(e); + // Purging `amdgpu-dkms` is only the first half of purge-then-reinstall: + // where rocm-cli plans no reinstall (undocumented distro) it would strip + // the driver with nothing to put back, so that step is withheld there. + let reinstall_planned = ubuntu_driver_is_native_archive(e) || dkms_driver_plan_is_supported(e); + let mut commands = vec!["# Clear the half-configured package/DKMS state first.".to_owned()]; + match family { + "apt" => { + commands.push("sudo dpkg --configure -a".to_owned()); + commands.push("sudo apt-get install -f".to_owned()); + if reinstall_planned { + commands.push("sudo apt-get purge -y amdgpu-dkms".to_owned()); + } + } + "dnf" if reinstall_planned => commands.push("sudo dnf remove -y amdgpu-dkms".to_owned()), + "zypper" if reinstall_planned => { + commands.push("sudo zypper remove -y amdgpu-dkms".to_owned()); + } + _ => {} + } + commands.push( + "# Quarantine (do not delete) the repo files, then let the package manager".to_owned(), + ); + commands.push("# forget the broken state before reinstalling.".to_owned()); + if e.rocm_repos_seen.is_empty() { + // No repo file was actually seen on this host, so show an example + // matching the detected package family rather than always the apt + // path -- a dnf/zypper host has no `/etc/apt/` at all. + let example = match family { + "dnf" => "/etc/yum.repos.d/amdgpu.repo", + "zypper" => "/etc/zypp/repos.d/amdgpu.repo", + _ => "/etc/apt/sources.list.d/amdgpu.list", + }; + commands.push(format!( + "# sudo mv {example} {example}.bak # or the equivalent for your package manager" + )); + } else { + for r in &e.rocm_repos_seen { + commands.push(format!( + "# sudo mv {r} {r}.bak # quarantine, do not delete yet" + )); + } } + if let Some(cmd) = refresh_command_for_family(family) { + commands.push(format!("sudo {cmd}")); + } + let summary = if ubuntu_driver_is_native_archive(e) { + commands + .push("# Ubuntu 26.04 ships the amdgpu kernel driver in-tree; there is no".to_owned()); + commands.push( + "# repo-native DKMS driver to reinstall here. Reinstall the userspace stack:" + .to_owned(), + ); + commands.push("sudo apt install rocm".to_owned()); + "Clear the half-configured package state, quarantine the repo-native install's repo files, THEN reinstall the userspace stack via `sudo apt install rocm` (Ubuntu 26.04 ships the amdgpu kernel driver in-tree, so there is no DKMS driver to reinstall).".to_owned() + } else if dkms_driver_plan_is_supported(e) { + commands.push("# Reinstall via rocm-cli's repo-native flow:".to_owned()); + commands.push("rocm install driver --dkms --yes".to_owned()); + "Clear the half-configured package/DKMS state, quarantine the repo-native install's repo files, THEN reinstall via `rocm install driver --dkms --yes`.".to_owned() + } else { + commands.push( + "# rocm-cli has no automated DKMS driver plan for this distro; reinstall the" + .to_owned(), + ); + commands.push("# driver manually per AMD's install docs:".to_owned()); + commands.push("# https://rocm.docs.amd.com/projects/install-on-linux/en/latest/reference/system-requirements.html".to_owned()); + format!( + "Clear the half-configured package/DKMS state and quarantine the repo-native install's repo files; rocm-cli has no automated DKMS driver plan for {} {}, so reinstall the driver manually per AMD's docs.", + e.distro_id, e.distro_version + ) + }; + + // Query the package manager's own records, not `rocm examine`'s + // self-reported `rocm_install_method` marker: that marker is set from + // repo-marker-file presence before dpkg/rpm is consulted (see + // `probe_rocm_install`), so a repo file left behind by a failed + // `install --dkms` keeps the marker true even though the package + // never actually installed. + // + // Must check the *targeted* package's installed status, not just that + // some `rocm`/`amdgpu` name appears anywhere: a bare `grep -qE + // 'rocm|amdgpu'` also matches unrelated stock packages (e.g. + // `libdrm-amdgpu1`), and matches a healthy `rocm-*` package sitting + // next to the broken `amdgpu-dkms` this fix targets. So query + // `amdgpu-dkms` specifically, via dpkg's full `Status` field + // (`install ok installed`, not a `^ii` substring) or `rpm -q`. Also + // confirm DKMS itself reports the module built, and check + // `rocminfo`'s own exit code directly -- piping through `head` would + // report the pipeline's last stage, masking a missing/failing + // `rocminfo`. + // + // Ubuntu 26.04 has no `amdgpu-dkms`/DKMS module to query (see + // `ubuntu_driver_is_native_archive` above): check the `rocm` package + // dpkg reinstalled instead. + let verify = if ubuntu_driver_is_native_archive(e) { + "dpkg-query -W -f='${Status}' rocm 2>/dev/null | grep -q 'install ok installed' \ + && rocminfo >/dev/null 2>&1" + .to_owned() + } else if dkms_driver_plan_is_supported(e) { + "(dpkg-query -W -f='${Status}' amdgpu-dkms 2>/dev/null | grep -q 'install ok installed' \ + || rpm -q amdgpu-dkms >/dev/null 2>&1) \ + && dkms status amdgpu 2>/dev/null | grep -q installed \ + && rocminfo >/dev/null 2>&1" + .to_owned() + } else { + // No AMD-documented package/DKMS name to check on an undocumented + // distro -- the manual reinstall above is on the user, not rocm-cli, + // so the only thing this fix itself can still assert is that the + // GPU stack actually works afterward. + "rocminfo >/dev/null 2>&1".to_owned() + }; let fix = Fix { - summary: "Run amdgpu-install's documented uninstall sequence to clear the half-configured state, THEN reinstall without the flag that broke it.".to_owned(), - commands: vec![ - "sudo amdgpu-install --uninstall".to_owned(), - "sudo apt autoremove --purge -y".to_owned(), - "sudo apt update".to_owned(), - "# Reinstall. Drop --accept-eula if you used it previously; the".to_owned(), - "# newer installer rejects it and leaves a half-configured repo.".to_owned(), - "sudo amdgpu-install --usecase=rocm,hip".to_owned(), - ], + summary, + commands, needs_sudo: true, needs_reboot: true, fix_id: "fix-12-installer".to_owned(), - verify: "dpkg -l | grep -E 'rocm|amdgpu' | head -n 20 && rocminfo | head -n 5".to_owned(), - notes: vec!["If `apt autoremove` warns it will remove unrelated packages, stop and resolve those by hand before continuing.".to_owned()], + verify, + notes: vec!["If the purge/remove step warns it will take unrelated packages with it, stop and resolve those by hand before continuing.".to_owned()], ..Fix::default() }; finalize( "fix-12-installer", - "amdgpu-install left a broken state (repo regression / partial DKMS)", + "Repo-native install left a broken state (repo regression / partial DKMS)", score, evidence, fix, @@ -2813,7 +3107,7 @@ const CHECKERS: &[Checker] = &[ (check_9_igpu_dgpu_collision, &["linux", "windows"]), (check_10_container_devices, &["linux"]), (check_11_iommu_hang, &["linux"]), - (check_12_amdgpu_install_broken, &["linux"]), + (check_12_repo_native_broken, &["linux"]), (check_13_hip_sdk_missing, &["windows"]), (check_14_adrenalin_too_old, &["windows"]), (check_15_msvc_redist, &["windows"]), @@ -7069,6 +7363,898 @@ mod tests { assert_eq!(v["high_confidence_threshold"], 75); } + #[test] + fn repo_pkg_family_prefers_seen_repos_over_distro_id() { + let mut e = linux_base(); + e.distro_id = "sles".to_owned(); + e.rocm_repos_seen = vec!["/etc/apt/sources.list.d/amdgpu.list".to_owned()]; + assert_eq!(repo_pkg_family(&e), "apt"); + } + + #[test] + fn repo_pkg_family_detects_dnf_and_zypper_from_seen_repos() { + let mut e = linux_base(); + e.rocm_repos_seen = vec!["/etc/yum.repos.d/amdgpu.repo".to_owned()]; + assert_eq!(repo_pkg_family(&e), "dnf"); + + e.rocm_repos_seen = vec!["/etc/zypp/repos.d/amdgpu.repo".to_owned()]; + assert_eq!(repo_pkg_family(&e), "zypper"); + } + + #[test] + fn repo_pkg_family_falls_back_to_distro_id_when_no_repo_seen() { + let mut e = linux_base(); + e.distro_id = "sles".to_owned(); + assert_eq!(repo_pkg_family(&e), "zypper"); + + e.distro_id = "rocky".to_owned(); + assert_eq!(repo_pkg_family(&e), "dnf"); + + e.distro_id = "ubuntu".to_owned(); + assert_eq!(repo_pkg_family(&e), "apt"); + } + + #[test] + fn repo_pkg_family_fallback_is_id_like_aware() { + // Amazon Linux has no ROCm repo file in this scenario and isn't in + // the old hardcoded list; it must not default to "apt". + let mut e = linux_base(); + e.distro_id = "amzn".to_owned(); + assert_eq!(repo_pkg_family(&e), "dnf"); + + // A RHEL rebuild with an unrecognized ID must still resolve via + // ID_LIKE, same as openmpi's resolver. + let mut e = linux_base(); + e.distro_id = "some-rhel-rebuild".to_owned(); + e.distro_id_like = "rhel fedora".to_owned(); + assert_eq!(repo_pkg_family(&e), "dnf"); + } + + #[test] + fn repo_pkg_family_degrades_to_unknown_on_pacman_hosts() { + // openmpi's resolver can answer "pacman" (it has its own, unrelated + // library installers for Arch et al.), but this check has no pacman + // remediation -- it must not surface "pacman" verbatim, nor coerce to + // "apt" (which would make check_12 print apt/dpkg commands on an Arch + // host); both `match family` arms in check_12 treat "unknown" as "no + // package-manager-specific command". + let mut e = linux_base(); + e.distro_id = "arch".to_owned(); + assert_eq!(repo_pkg_family(&e), "unknown"); + } + + #[test] + fn kernel_unsupported_on_ubuntu_26_04_names_the_native_archive_path() { + let mut e = linux_base(); + e.distro_id = "ubuntu".to_owned(); + e.distro_version = "26.04".to_owned(); + e.rocm_version = "7.0.0".to_owned(); + e.amdgpu_loaded = Some(false); + let report = diagnose(&e, "kernel/rocm mismatch"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-3-rocm-kernel") + .expect("unsupported kernel triple should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + commands.contains("Note on Ubuntu 26.04") && commands.contains("apt install rocm"), + "Ubuntu 26.04 must be told to use the native archive path, not a DKMS rerun: {commands}" + ); + assert!( + !commands.contains("rerun `rocm install driver --dkms --yes`"), + "Ubuntu 26.04 must not be told to rerun a command that plans nothing there: {commands}" + ); + } + + #[test] + fn kernel_unsupported_on_an_undocumented_distro_does_not_suggest_a_dkms_rerun() { + let mut e = linux_base(); + e.distro_id = "fedora".to_owned(); + e.distro_version = "40".to_owned(); + e.rocm_version = "7.0.0".to_owned(); + e.amdgpu_loaded = Some(false); + let report = diagnose(&e, "kernel/rocm mismatch"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-3-rocm-kernel") + .expect("unsupported kernel triple should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + commands.contains("plans nothing on fedora 40"), + "an undocumented distro must be told the dkms command plans nothing, not rerun it: {commands}" + ); + assert!( + !commands.contains("rerun `rocm install driver --dkms --yes`"), + "an undocumented distro must not be told to rerun a command that plans nothing: {commands}" + ); + } + + #[test] + fn stale_repos_on_ubuntu_26_04_suggests_apt_install_rocm_not_a_dkms_reinstall() { + let mut e = linux_base(); + e.distro_id = "ubuntu".to_owned(); + e.distro_version = "26.04".to_owned(); + e.rocm_repos_seen = vec![ + "/etc/apt/sources.list.d/amdgpu.list".to_owned(), + "/etc/apt/sources.list.d/rocm.list".to_owned(), + ]; + let report = diagnose(&e, "stale repo conflict"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("duplicate repo files should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + commands.contains("sudo apt install rocm") + && commands.contains("Ubuntu 26.04 ships the amdgpu driver in-tree"), + "Ubuntu 26.04 must be pointed at the native archive reinstall, not DKMS: {commands}" + ); + assert!( + !commands.contains("rocm install driver --dkms --yes"), + "Ubuntu 26.04 must not be offered a DKMS reinstall that plans nothing there: {commands}" + ); + } + + #[test] + fn stale_repos_on_an_undocumented_dnf_distro_does_not_suggest_a_dkms_reinstall() { + let mut e = linux_base(); + e.distro_id = "fedora".to_owned(); + e.distro_version = "40".to_owned(); + e.rocm_repos_seen = vec![ + "/etc/yum.repos.d/amdgpu.repo".to_owned(), + "/etc/yum.repos.d/rocm.repo".to_owned(), + ]; + let report = diagnose(&e, "stale repo conflict"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("duplicate repo files should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + commands.contains("plans nothing on fedora 40"), + "an undocumented dnf distro must be told the dkms command plans nothing: {commands}" + ); + assert!( + !commands.contains("rocm install driver --dkms --yes") + && !commands.contains("rocm install driver --dkms # review the plan"), + "an undocumented distro must not be offered a DKMS reinstall that plans nothing: {commands}" + ); + } + + #[test] + fn repo_native_broken_on_ubuntu_26_04_reinstalls_the_userspace_stack_not_dkms() { + let mut e = linux_base(); + e.distro_id = "ubuntu".to_owned(); + e.distro_version = "26.04".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + e.rocm_repos_seen = vec!["/etc/apt/sources.list.d/amdgpu.list".to_owned()]; + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let fix = hit.fix.as_ref().unwrap(); + let commands = fix.commands.join("\n"); + assert!( + commands.contains("sudo apt install rocm"), + "Ubuntu 26.04 must reinstall the userspace stack, not a DKMS driver: {commands}" + ); + assert!( + !commands.contains("rocm install driver --dkms --yes"), + "Ubuntu 26.04 must not be offered a DKMS reinstall that plans nothing there: {commands}" + ); + assert!( + fix.verify.contains("dpkg-query -W -f='${Status}' rocm") + && !fix.verify.contains("amdgpu-dkms"), + "Ubuntu 26.04's verify must check the `rocm` package, not a nonexistent DKMS module: {}", + fix.verify + ); + } + + #[test] + fn repo_native_broken_on_an_undocumented_distro_tells_the_user_to_reinstall_manually() { + let mut e = linux_base(); + e.distro_id = "fedora".to_owned(); + e.distro_version = "40".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + e.rocm_repos_seen = vec!["/etc/yum.repos.d/amdgpu.repo".to_owned()]; + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let fix = hit.fix.as_ref().unwrap(); + let commands = fix.commands.join("\n"); + assert!( + commands.contains("no automated DKMS driver plan for this distro") + && commands.contains("system-requirements.html"), + "an undocumented distro must be sent to AMD's manual install docs: {commands}" + ); + assert!( + !commands.contains("rocm install driver --dkms --yes"), + "an undocumented distro must not be offered a DKMS reinstall that plans nothing: {commands}" + ); + assert_eq!( + fix.verify, "rocminfo >/dev/null 2>&1", + "with no AMD-documented package/DKMS name to check, verify must fall back to \ + confirming the GPU stack works, not assert an unknowable package state: {}", + fix.verify + ); + } + + #[test] + fn stale_repos_on_a_sles_host_gets_zypper_commands_not_apt() { + // A SLES host whose repo files now live under /etc/zypp/repos.d/ (the + // new zypp scan this diff adds) must not be told to run apt/dpkg + // commands: `check_7` must branch on `repo_pkg_family` the same way + // `check_12` already does. + let mut e = linux_base(); + e.distro_id = "sles".to_owned(); + e.rocm_repos_seen = vec![ + "/etc/zypp/repos.d/amdgpu.repo".to_owned(), + "/etc/zypp/repos.d/rocm.repo".to_owned(), + ]; + let report = diagnose(&e, "stale repo conflict"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("duplicate repo files should be diagnosed"); + let fix = hit.fix.as_ref().unwrap(); + let commands = fix.commands.join("\n"); + assert!( + commands.contains("/etc/zypp/repos.d/") && commands.contains("sudo zypper refresh"), + "a SLES host must get zypper remediation: {commands}" + ); + assert!( + !commands.contains("apt") && !commands.contains("dpkg"), + "a SLES host must not be told to run apt/dpkg commands: {commands}" + ); + assert!( + fix.verify.contains("sudo zypper refresh"), + "a SLES host's verify step must also use zypper, not apt: {}", + fix.verify + ); + } + + #[test] + fn stale_repos_on_a_dnf_host_gets_dnf_commands_not_apt() { + // Same drift guard as the SLES/zypper test above, for the dnf family. + let mut e = linux_base(); + e.distro_id = "rocky".to_owned(); + e.rocm_repos_seen = vec![ + "/etc/yum.repos.d/amdgpu.repo".to_owned(), + "/etc/yum.repos.d/rocm.repo".to_owned(), + ]; + let report = diagnose(&e, "stale repo conflict"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("duplicate repo files should be diagnosed"); + let fix = hit.fix.as_ref().unwrap(); + let commands = fix.commands.join("\n"); + assert!( + commands.contains("/etc/yum.repos.d/") && commands.contains("sudo dnf makecache"), + "a dnf host must get dnf remediation: {commands}" + ); + assert!( + !commands.contains("apt") && !commands.contains("dpkg") && !commands.contains("zypper"), + "a dnf host must not be told to run apt/dpkg/zypper commands: {commands}" + ); + assert!( + fix.verify.contains("sudo dnf makecache"), + "a dnf host's verify step must also use dnf, not apt: {}", + fix.verify + ); + } + + #[test] + fn stale_repos_emits_no_package_manager_commands_on_pacman_hosts() { + let mut e = linux_base(); + e.distro_id = "arch".to_owned(); + e.rocm_repos_seen = vec!["/some/rocm.repo".to_owned(), "/some/amdgpu.repo".to_owned()]; + let report = diagnose(&e, "stale repo conflict"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("duplicate repo files should be diagnosed"); + let fix = hit.fix.as_ref().unwrap(); + let commands: Vec<&str> = fix + .commands + .iter() + .map(String::as_str) + .filter(|c| !c.trim_start().starts_with('#')) + .collect(); + assert!( + commands.iter().all(|c| !c.contains("apt") + && !c.contains("dpkg") + && !c.contains("dnf") + && !c.contains("zypper")), + "must not print a foreign package manager's commands on an unresolved/pacman host: {commands:?}" + ); + assert!( + !fix.verify.contains("apt") + && !fix.verify.contains("dnf") + && !fix.verify.contains("zypper"), + "verify step must not assume a foreign package manager on a pacman host: {}", + fix.verify + ); + // Not just "no foreign package manager" -- the verify must be a real, + // falsifiable post-condition. `rocm examine --json | grep -q + // '"rocm_install_method"'` is present in every `examine --json` output + // regardless of host state, so it would pass this assertion too while + // never actually being able to fail. Pin it to the one thing the + // suggested (commented) `mv {r} {r}.bak` commands actually change: the + // flagged repo files no longer existing at their original paths. + for r in &e.rocm_repos_seen { + assert!( + fix.verify.contains(r), + "verify must check each flagged repo file is gone, not a tautology: {}", + fix.verify + ); + } + } + + #[test] + fn stale_repos_verify_is_not_empty_when_symptom_alone_triggers_on_an_unresolved_host() { + // Same unresolved/pacman family as the test above, but this time no + // repo files were ever recorded -- only the symptom text scores the + // fix. `repos.iter().join(" && ")` over an empty `repos` produces an + // empty string, which would make `verify` a blank, always-passing + // check instead of the real repo-marker check below. + let mut e = linux_base(); + e.distro_id = "arch".to_owned(); + let report = diagnose(&e, "404 against repo.radeon.com"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("symptom-only match should still diagnose fix-7"); + let fix = hit.fix.as_ref().unwrap(); + assert!( + !fix.verify.trim().is_empty(), + "verify must not be blank when no repo files were flagged" + ); + assert!( + fix.verify.contains("rocm|amdgpu|radeon"), + "verify must fall back to a real, falsifiable repo-marker check: {}", + fix.verify + ); + } + + #[test] + fn repo_native_broken_emits_no_package_manager_commands_on_pacman_hosts() { + let mut e = linux_base(); + e.distro_id = "arch".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + // Only real (non-comment) commands are package-manager-specific here; + // the commented-out quarantine example always names the apt path as a + // generic illustration ("or the yum/zypp equivalent"), unrelated to + // `family` and out of scope for this check. + let commands: Vec<&str> = hit + .fix + .as_ref() + .unwrap() + .commands + .iter() + .map(String::as_str) + .filter(|c| !c.trim_start().starts_with('#')) + .collect(); + assert!( + commands.iter().all(|c| !c.contains("apt") + && !c.contains("dpkg") + && !c.contains("dnf") + && !c.contains("zypper")), + "must not print a foreign package manager's commands on an unresolved/pacman host: {commands:?}" + ); + } + + #[test] + fn is_repo_native_installed_still_accepts_the_retired_amdgpu_install_value() { + // An older `rocm remote doctor` client can report the pre-rename + // value for a while after this PR; it must still count as + // repo-native-managed, not silently fall through to "not a + // package-manager install" and lose fix-12 evidence. + assert!(is_repo_native_installed("amdgpu-install")); + assert!(is_repo_native_installed("repo-native")); + assert!(!is_repo_native_installed("runfile-or-tarball")); + assert!(!is_repo_native_installed("")); + } + + #[test] + fn repo_native_broken_verify_requires_dpkg_status_not_just_name_presence() { + // `method == "apt"` here is the exact pre-fix state `is_repo_native_installed` + // treats as already repo-native-installed (see its doc comment): the dpkg/rpm + // query fallback found an amdgpu/rocm package by name, with no distinction + // between a healthy install and a half-configured one. If `verify` only + // re-ran that same bare name grep, it would already be true here, before any + // of this fix's remediation commands run. + let mut e = linux_base(); + e.distro_id = "ubuntu".to_owned(); + e.distro_version = "22.04".to_owned(); + e.rocm_install_method = "apt".to_owned(); + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let verify = &hit.fix.as_ref().unwrap().verify; + assert!( + verify.contains("grep -q 'install ok installed'"), + "verify must check dpkg's full installed-status field, not just that the \ + package name appears anywhere -- a bare name grep also matches unrelated \ + stock packages (e.g. libdrm-amdgpu1) and a healthy rocm-* package sitting \ + next to the broken amdgpu-dkms this fix targets: {verify}" + ); + assert!( + verify.contains("dpkg-query -W -f='${Status}' amdgpu-dkms"), + "verify must query dpkg's status for amdgpu-dkms by name, not merely contain \ + the right-shaped status-field query against some other package -- pinning \ + the package name closes the gap where the targeted name could be swapped \ + for an unrelated one and this test would still pass: {verify}" + ); + assert!( + verify.contains("dkms status amdgpu"), + "verify must also confirm dkms reports the module actually built: {verify}" + ); + assert!( + !verify.contains("| head"), + "verify must not pipe rocminfo through head, which would report head's exit \ + code instead of rocminfo's: {verify}" + ); + } + + #[test] + fn repo_native_broken_fires_and_clears_state_before_reinstall() { + let mut e = linux_base(); + e.distro_id = "ubuntu".to_owned(); + e.distro_version = "22.04".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + e.rocm_repos_seen = vec!["/etc/apt/sources.list.d/amdgpu.list".to_owned()]; + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let fix = hit.fix.as_ref().unwrap(); + let commands = fix.commands.join("\n"); + assert!( + commands.contains("dpkg --configure -a"), + "must clear the half-configured dpkg state before reinstalling: {commands}" + ); + assert!( + commands.contains("purge -y amdgpu-dkms"), + "must purge the broken driver package: {commands}" + ); + assert!( + commands.contains("# sudo mv /etc/apt/sources.list.d/amdgpu.list"), + "repo quarantine command must be commented out, not ready-to-run: {commands}" + ); + assert!( + commands.contains("rocm install driver --dkms --yes"), + "reinstall command must actually install (bare 'rocm install driver' is a non-mutating preflight no-op): {commands}" + ); + assert!( + fix.verify.contains("dpkg") && fix.verify.contains("rpm"), + "verify must query real dpkg/rpm package state, not the self-reported \ + rocm_install_method marker (set from repo-file presence before the \ + package actually installs): {}", + fix.verify + ); + assert!( + hit.evidence + .iter() + .any(|l| l.contains("installed via the repo-native package-manager flow")), + "positive control: the repo-native evidence line must appear when the method really is repo-native: {:?}", + hit.evidence + ); + } + + #[test] + fn repo_native_broken_on_a_dnf_host_gets_dnf_commands_not_apt() { + let mut e = linux_base(); + e.distro_id = "rocky".to_owned(); + e.distro_version = "9.6".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + e.rocm_repos_seen = vec!["/etc/yum.repos.d/amdgpu.repo".to_owned()]; + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + commands.contains("sudo dnf remove -y amdgpu-dkms") + && commands.contains("sudo dnf makecache"), + "a dnf host must get dnf remediation: {commands}" + ); + assert!( + !commands.contains("apt") && !commands.contains("dpkg") && !commands.contains("zypper"), + "a dnf host must not be told to run apt/dpkg/zypper commands: {commands}" + ); + } + + #[test] + fn repo_native_broken_on_a_zypper_host_gets_zypper_commands_not_apt() { + let mut e = linux_base(); + e.distro_id = "sles".to_owned(); + e.distro_version = "15.7".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + e.rocm_repos_seen = vec!["/etc/zypp/repos.d/amdgpu.repo".to_owned()]; + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + commands.contains("sudo zypper remove -y amdgpu-dkms") + && commands.contains("sudo zypper refresh"), + "a zypper host must get zypper remediation: {commands}" + ); + assert!( + !commands.contains("apt") && !commands.contains("dpkg") && !commands.contains("dnf"), + "a zypper host must not be told to run apt/dpkg/dnf commands: {commands}" + ); + } + + #[test] + fn repo_native_broken_quarantine_example_matches_the_family_when_no_repo_was_seen() { + // With no repo file recorded (`rocm_repos_seen` empty), the quarantine + // line falls back to an example path -- it must match the host's own + // package family, not always show the apt path on a dnf/zypper host + // that has no `/etc/apt/` at all. + let mut e = linux_base(); + e.distro_id = "sles".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + e.rocm_repos_seen = Vec::new(); + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + commands.contains("/etc/zypp/repos.d/amdgpu.repo"), + "a zypper host's fallback quarantine example must show a zypp path: {commands}" + ); + assert!( + !commands.contains("/etc/apt/"), + "a zypper host must not show an apt path as its quarantine example: {commands}" + ); + } + + #[test] + fn repo_native_broken_does_not_claim_repo_native_evidence_for_other_install_methods() { + // Regression test: an empty symptom would short-circuit keyword_score to + // 0 before the install-method gate is ever reached, making this pass + // vacuously regardless of whether the gate works. Pass a real dpkg + // symptom instead, matching the positive-control test above, so fix-12 + // still fires (kw_score alone clears MIN_SCORE_FOR_MATCH) but must not + // claim repo-native evidence for a non-repo-native install. + let mut e = linux_base(); + e.rocm_install_method = "runfile-or-tarball".to_owned(); + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("dpkg keyword evidence alone should still surface fix-12"); + assert!( + !hit.evidence + .iter() + .any(|l| l.contains("installed via the repo-native package-manager flow")), + "must not claim repo-native evidence for a non-repo-native install: {:?}", + hit.evidence + ); + } + + #[test] + fn repo_native_broken_claims_repo_native_evidence_for_dpkg_rpm_fallback_methods() { + // probe_rocm_install (examine.rs) falls back to the literal "apt"/"dnf" + // when dpkg/rpm confirms the package is installed but no marker file + // was found. Those hosts are still repo-native-managed, so the evidence + // line must fire for them too, not just the marker-file-detected + // literal "repo-native" case covered above. The +20 score bonus that + // fires alongside it is asserted separately, in + // `repo_native_broken_score_bonus_actually_raises_confidence`. + for method in ["apt", "dnf"] { + let mut e = linux_base(); + e.rocm_install_method = method.to_owned(); + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("dpkg keyword evidence alone should still surface fix-12"); + assert!( + hit.evidence + .iter() + .any(|l| l.contains("installed via the repo-native package-manager flow")), + "method {method:?} is a repo-native fallback and must claim repo-native evidence: {:?}", + hit.evidence + ); + } + } + + #[test] + fn repo_native_broken_score_bonus_actually_raises_confidence() { + // The two tests above only assert the evidence *line*, which fires + // from the same `is_repo_native_installed(method)` check that gates + // the `+20` bonus a few lines later -- so they'd stay green even if + // the bonus itself were deleted. Diagnose the same dpkg symptom on a + // repo-native host and a runfile-or-tarball host and assert the + // former's score is exactly 20 higher. + let symptom = "dpkg: error processing package amdgpu-dkms (half-configured)"; + let score_for = |method: &str| { + let mut e = linux_base(); + e.rocm_install_method = method.to_owned(); + diagnose(&e, symptom) + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("dpkg keyword evidence alone should still surface fix-12") + .score + }; + assert_eq!( + score_for("apt"), + score_for("runfile-or-tarball") + 20, + "the +20 repo-native score bonus must actually raise the computed score, \ + not just the evidence text" + ); + } + + #[test] + fn repo_native_broken_fires_on_dpkg_symptom_regardless_of_install_method() { + // The keyword score alone (half-configured + generic dpkg error = 75) + // clears MIN_SCORE_FOR_MATCH without the +20 repo-native bonus, so + // fix-12 still fires even when rocm_install_method isn't + // "repo-native" -- the method only adds confidence, it doesn't gate + // whether the check fires at all. See the sibling test above for the + // repo-native-evidence-line assertion this used to also make. + let mut e = linux_base(); + e.rocm_install_method = "runfile-or-tarball".to_owned(); + let report = diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + assert!( + report.matched.iter().any(|d| d.id == "fix-12-installer"), + "dpkg keyword evidence alone should still surface fix-12" + ); + } + + #[test] + fn dkms_driver_plan_support_matrix_covers_every_arm() { + // (distro_id, distro_version, distro_id_like, supported). Kept in step + // with `build_driver_install_plan_at` by + // `driver_plan_support_matches_rocm_core_diagnose` in + // `apps/rocm/src/driver_install.rs`. + let table = [ + ("ubuntu", "22.04", "debian", true), + ("ubuntu", "24.04", "debian", true), + ("ubuntu", "26.04", "debian", false), + ("debian", "12", "", true), + ("debian", "13", "", true), + ("rhel", "9.7", "fedora", true), + ("rhel", "10.0", "fedora", true), + ("ol", "9.7", "fedora", true), + ("rocky", "9.6", "rhel centos fedora", true), + ("sles", "15.7", "suse", true), + ("sle", "15.7", "suse", true), + ("almalinux", "9.6", "rhel centos fedora", true), + ("pop", "24.04", "ubuntu debian", true), + ("ol", "9.7", "rhel fedora", true), + ("ol", "9.5", "rhel fedora", false), + // OL's own ID_LIKE row must not inherit the `rhel` family's + // versions: OL 9.6 is not in the documented OL list. + ("ol", "9.6", "rhel fedora", false), + ("fedora", "41", "", false), + ("opensuse-leap", "15.7", "suse opensuse", false), + ("linuxmint", "22", "ubuntu debian", false), + ]; + for (id, version, like, expected) in table { + let mut e = linux_base(); + e.distro_id = id.to_owned(); + e.distro_version = version.to_owned(); + e.distro_id_like = like.to_owned(); + assert_eq!( + dkms_driver_plan_is_supported(&e), + expected, + "{id} {version} (ID_LIKE={like})" + ); + } + } + + #[test] + fn repo_native_broken_on_an_undocumented_distro_does_not_purge_the_driver() { + let mut e = linux_base(); + e.distro_id = "fedora".to_owned(); + e.distro_version = "41".to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + let report = diagnose(&e, "dpkg: error processing package amdgpu-dkms"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let commands = hit.fix.as_ref().unwrap().commands.join("\n"); + assert!( + !commands.contains("amdgpu-dkms") && !commands.contains("rocm install driver"), + "no purge without a planned reinstall: {commands}" + ); + } + + fn fix_commands(e: &Examination, symptom: &str, id: &str) -> String { + diagnose(e, symptom) + .matched + .iter() + .find(|d| d.id == id) + .unwrap_or_else(|| panic!("{id} should be diagnosed")) + .fix + .as_ref() + .unwrap() + .commands + .join("\n") + } + + #[test] + fn fix_12_purges_the_driver_only_where_a_reinstall_is_planned() { + // (id, version, id_like, repo seen, purge command, expect purge) + let cases = [ + ( + "ubuntu", + "24.04", + "debian", + "/etc/apt/sources.list.d/amdgpu.list", + "sudo apt-get purge -y amdgpu-dkms", + true, + ), + ( + "linuxmint", + "22", + "ubuntu debian", + "/etc/apt/sources.list.d/amdgpu.list", + "sudo apt-get purge -y amdgpu-dkms", + false, + ), + ( + "rhel", + "9.7", + "fedora", + "/etc/yum.repos.d/amdgpu.repo", + "sudo dnf remove -y amdgpu-dkms", + true, + ), + ( + "fedora", + "41", + "", + "/etc/yum.repos.d/amdgpu.repo", + "sudo dnf remove -y amdgpu-dkms", + false, + ), + ( + "sles", + "15.7", + "suse", + "/etc/zypp/repos.d/amdgpu.repo", + "sudo zypper remove -y amdgpu-dkms", + true, + ), + ( + "opensuse-leap", + "15.7", + "suse opensuse", + "/etc/zypp/repos.d/amdgpu.repo", + "sudo zypper remove -y amdgpu-dkms", + false, + ), + ]; + for (id, version, like, repo, purge, expect) in cases { + let mut e = linux_base(); + e.distro_id = id.to_owned(); + e.distro_version = version.to_owned(); + e.distro_id_like = like.to_owned(); + e.rocm_install_method = "repo-native".to_owned(); + e.rocm_repos_seen = vec![repo.to_owned()]; + let commands = fix_commands( + &e, + "dpkg: error processing package amdgpu-dkms", + "fix-12-installer", + ); + assert_eq!( + commands.contains(purge), + expect, + "{id} {version}: {commands}" + ); + } + } + + #[test] + fn supported_distro_fix_3_and_fix_7_name_the_dkms_reinstall() { + let mut e = linux_base(); + e.distro_id = "ubuntu".to_owned(); + e.distro_version = "24.04".to_owned(); + e.rocm_version = "7.0.0".to_owned(); + e.amdgpu_loaded = Some(false); + e.rocm_repos_seen = vec![ + "/etc/apt/sources.list.d/amdgpu.list".to_owned(), + "/etc/apt/sources.list.d/rocm.list".to_owned(), + ]; + let fix3 = fix_commands(&e, "kernel/rocm mismatch", "fix-3-rocm-kernel"); + assert!(fix3.contains("rocm install driver --dkms --yes"), "{fix3}"); + let fix7 = fix_commands(&e, "stale repo conflict", "fix-7-stale-repos"); + assert!(fix7.contains("rocm install driver --dkms --yes"), "{fix7}"); + } + + #[test] + fn kernel_unsupported_does_not_offer_the_sdk_install_or_a_maskable_verify() { + let mut e = linux_base(); + e.distro_id = "ubuntu".to_owned(); + e.distro_version = "24.04".to_owned(); + e.rocm_version = "7.0.0".to_owned(); + e.amdgpu_loaded = Some(false); + let report = diagnose(&e, "kernel/rocm mismatch"); + let fix = report + .matched + .iter() + .find(|d| d.id == "fix-3-rocm-kernel") + .and_then(|d| d.fix.as_ref()) + .expect("unsupported kernel triple should be diagnosed"); + assert!(!fix.commands.join("\n").contains("rocm install sdk")); + assert!(!fix.verify.contains("| head"), "{}", fix.verify); + } + fn windows_driver_exam(version: &str, date: &str) -> Examination { Examination { os_family: "windows".to_owned(), @@ -7112,8 +8298,6 @@ mod tests { ); } - /// A Windows host with a healthy HIP SDK, so that the old driver is the - /// only entry that fires. fn old_driver_only_exam() -> Examination { Examination { hip_sdk_path: "C:\\Program Files\\AMD\\ROCm\\6.2".to_owned(), @@ -7188,9 +8372,6 @@ mod tests { ); } - /// The closing "Next step" block picks the first non-advice row when - /// nothing reaches HIGH_CONFIDENCE. A LIKELY or WEAK real cause listed - /// above the old-driver row must be the one named, never the advice row. #[test] fn a_below_high_real_cause_beside_an_old_driver_is_the_one_named_as_next_step() { for (score, tier) in [(60, "LIKELY"), (30, "WEAK")] { diff --git a/crates/rocm-core/src/examine.rs b/crates/rocm-core/src/examine.rs index 4be0c98a23..d5174f9759 100644 --- a/crates/rocm-core/src/examine.rs +++ b/crates/rocm-core/src/examine.rs @@ -47,14 +47,19 @@ const TRACKED_ENV_VARS: &[&str] = &[ "PATH", ]; -/// Repo files dropped by the `amdgpu-install` pipeline; their presence marks an -/// amdgpu-install-managed ROCm. -const AMDGPU_INSTALL_MARKERS: &[&str] = &[ +/// AMD ROCm/AMDGPU repo files: `amdgpu.list` / `amdgpu.repo` are written by +/// rocm-cli's own repo-native install flow; `rocm.list` / `radeon.list` / +/// `rocm.repo` are names from AMD's documented manual/legacy setup steps that +/// rocm-cli itself never creates. Either kind marks a package-manager-repo- +/// managed ROCm. +const REPO_NATIVE_INSTALL_MARKERS: &[&str] = &[ "/etc/apt/sources.list.d/amdgpu.list", "/etc/apt/sources.list.d/rocm.list", "/etc/apt/sources.list.d/radeon.list", "/etc/yum.repos.d/amdgpu.repo", "/etc/yum.repos.d/rocm.repo", + "/etc/zypp/repos.d/amdgpu.repo", + "/etc/zypp/repos.d/rocm.repo", ]; /// Marketing-name fragments that identify an AMD APU when `rocminfo` is absent. @@ -209,6 +214,20 @@ pub struct Examination { pub os_version: String, pub distro_id: String, pub distro_version: String, + /// `/etc/os-release` `ID_LIKE`, for `openmpi::resolve_package_manager` + /// fallback matching. Not part of the examine.py wire contract, but + /// included in JSON anyway (like the `comgr_*`/`hip_*` fields below): + /// `rocm remote doctor` diagnoses a remote host by deserializing its + /// `examine --json` output, and `repo_pkg_family`'s fallback needs this + /// field populated on that deserialized value, not just on a freshly + /// probed local `Examination`. + /// + /// `#[serde(default)]`: a remote on an older CLI version won't have + /// printed this field at all, and `parse_examination` must keep accepting + /// that document rather than failing outright over one missing field it + /// can safely fall back to "" (unknown) for. + #[serde(default)] + pub distro_id_like: String, pub kernel_release: String, pub kernel_cmdline: String, pub is_wsl: bool, @@ -385,6 +404,7 @@ impl Default for Examination { os_version: String::new(), distro_id: String::new(), distro_version: String::new(), + distro_id_like: String::new(), kernel_release: String::new(), kernel_cmdline: String::new(), is_wsl: false, @@ -791,6 +811,7 @@ fn record_distro(e: &mut Examination, read: &crate::hardware_root::OsRelease) { match crate::os_release::parse(read.text()) { Ok(mut fields) => { e.distro_id = fields.remove("ID").unwrap_or_default(); + e.distro_id_like = fields.remove("ID_LIKE").unwrap_or_default(); e.distro_version = fields.remove("VERSION_ID").unwrap_or_default(); } Err(unreadable) => e.probe_failures.push(format!( @@ -1960,6 +1981,67 @@ fn probe_secure_boot(e: &mut Examination) { // ROCm install probe (Linux) // --------------------------------------------------------------------------- +/// Pure marker-path -> install-method mapping, split out from +/// `probe_rocm_install` so it's testable without touching the real +/// filesystem. `existing` is the subset of `REPO_NATIVE_INSTALL_MARKERS` +/// found present on the host; returns `None` when none are present. +fn repo_native_method_from_markers(existing: &[&str]) -> Option<(&'static str, Vec)> { + let seen: Vec = REPO_NATIVE_INSTALL_MARKERS + .iter() + .filter(|m| existing.contains(m)) + .map(|m| (*m).to_owned()) + .collect(); + if seen.is_empty() { + None + } else { + Some(("repo-native", seen)) + } +} + +/// Whether a lowercased repo-directory entry name is a live ROCm/AMDGPU repo +/// file, as opposed to one fix-7/fix-12 already told the user to quarantine +/// (`mv foo foo.bak`). Without this, `amdgpu.list.bak` sitting next to the +/// reinstalled `amdgpu.list` after following that advice counts as a second +/// stale repo and re-triggers the same fix. +// `name` is already lowercased by the caller, so a plain suffix match is fine. +#[allow(clippy::case_sensitive_file_extension_comparisons)] +fn is_live_rocm_repo_file_name(name: &str) -> bool { + // Allowlist the known repo-file extensions (apt's one-line-style `.list`, + // apt's deb822-style `.sources`, dnf/zypper's `.repo`) rather than + // denylist known quarantine suffixes: quarantining appends to the name + // (`amdgpu.list` -> `amdgpu.list.bak`), so this also excludes any + // quarantine convention this file doesn't know about, and stops an + // unrelated file that merely mentions "rocm"/"amdgpu" in its name (a + // note, a log) from being swept up as a live repo file. + (name.ends_with(".list") || name.ends_with(".repo") || name.ends_with(".sources")) + && (name.contains("rocm") || name.contains("amdgpu") || name.contains("radeon")) +} + +/// Full paths of live (non-quarantined) ROCm/AMDGPU repo files directly inside +/// `dir`, split out from `probe_rocm_install` so the `.bak`/`.save`/`.disabled` +/// skip actually gets exercised against a real directory listing, not just the +/// pure `is_live_rocm_repo_file_name` name check in isolation. Silently empty +/// when `dir` does not exist or is not a directory. +fn live_rocm_repo_paths_in_dir(dir: &str) -> Vec { + // Read through `host_path` so a simulated host root re-roots the scan, but + // report the logical `dir/name`, which is what a user would `mv`. + let Ok(entries) = std::fs::read_dir(crate::host_path(dir)) else { + return Vec::new(); + }; + entries + .flatten() + .filter(|entry| { + is_live_rocm_repo_file_name(&entry.file_name().to_string_lossy().to_lowercase()) + }) + .map(|entry| { + Path::new(dir) + .join(entry.file_name()) + .to_string_lossy() + .into_owned() + }) + .collect() +} + fn probe_rocm_install(e: &mut Examination) { // Shared with the human `rocm examine` report and the fix-6 PATH runner so // all three agree on which install is the active one. Handles versioned @@ -1974,11 +2056,14 @@ fn probe_rocm_install(e: &mut Examination) { .and_then(|install| install.version) .unwrap_or_default(); - for marker in AMDGPU_INSTALL_MARKERS { - if Path::new(marker).exists() { - e.rocm_install_method = "amdgpu-install".to_owned(); - e.rocm_repos_seen.push((*marker).to_owned()); - } + let existing_markers: Vec<&str> = REPO_NATIVE_INSTALL_MARKERS + .iter() + .copied() + .filter(|m| Path::new(m).exists()) + .collect(); + if let Some((method, seen)) = repo_native_method_from_markers(&existing_markers) { + e.rocm_install_method = method.to_owned(); + e.rocm_repos_seen.extend(seen); } if e.rocm_install_method.is_empty() { @@ -1999,21 +2084,32 @@ fn probe_rocm_install(e: &mut Examination) { e.rocm_install_method = if rocm_dir.is_empty() { "none".to_owned() } else { - "tarball-or-other".to_owned() + "runfile-or-tarball".to_owned() }; } - for dir in ["/etc/apt/sources.list.d", "/etc/yum.repos.d"] { - let Ok(entries) = std::fs::read_dir(dir) else { - continue; - }; - for entry in entries.flatten() { - let name = entry.file_name().to_string_lossy().to_lowercase(); - if name.contains("rocm") || name.contains("amdgpu") || name.contains("radeon") { - let full = entry.path().to_string_lossy().into_owned(); - if !e.rocm_repos_seen.contains(&full) { - e.rocm_repos_seen.push(full); - } + merge_live_repo_paths(e, &REPO_SCAN_DIRS); +} + +/// The repo directories `probe_rocm_install` scans for live (non-quarantined) +/// ROCm/AMDGPU repo files, named so a test can assert each family's directory +/// is actually present rather than only being provable by reading real system +/// paths (SLES repo detection via `/etc/zypp/repos.d` is new in this PR). +const REPO_SCAN_DIRS: [&str; 3] = [ + "/etc/apt/sources.list.d", + "/etc/yum.repos.d", + "/etc/zypp/repos.d", +]; + +/// Split out from `probe_rocm_install` so the directory loop itself -- not +/// just `is_live_rocm_repo_file_name` and `live_rocm_repo_paths_in_dir` in +/// isolation -- is driven by a test against real (temp) directories standing +/// in for each family's repo path. +fn merge_live_repo_paths(e: &mut Examination, dirs: &[&str]) { + for dir in dirs { + for full in live_rocm_repo_paths_in_dir(dir) { + if !e.rocm_repos_seen.contains(&full) { + e.rocm_repos_seen.push(full); } } } @@ -3271,6 +3367,165 @@ fn oldest_runtime_version(output: &str) -> String { mod tests { use super::*; + #[test] + fn is_live_rocm_repo_file_name_skips_quarantined_files() { + assert!(is_live_rocm_repo_file_name("amdgpu.list")); + assert!(is_live_rocm_repo_file_name("rocm.repo")); + assert!(!is_live_rocm_repo_file_name("amdgpu.list.bak")); + assert!(!is_live_rocm_repo_file_name("amdgpu.list.save")); + assert!(!is_live_rocm_repo_file_name("amdgpu.list.disabled")); + assert!(!is_live_rocm_repo_file_name("unrelated.list")); + // Allowlisted by extension, not just denylisted by quarantine suffix: + // a stray file that merely mentions "rocm" isn't a repo file. + assert!(!is_live_rocm_repo_file_name("rocm-notes.txt")); + } + + #[test] + fn is_live_rocm_repo_file_name_recognizes_deb822_sources_files() { + // Newer apt defaults to the deb822 `.sources` format (as opposed to + // the one-line-style `.list`); fix-7's own `ls | grep` advice has no + // extension filter and already matches these, so this filter must + // not be the one place that silently misses them. + assert!(is_live_rocm_repo_file_name("amdgpu.sources")); + assert!(!is_live_rocm_repo_file_name("amdgpu.sources.bak")); + } + + #[test] + fn live_rocm_repo_paths_in_dir_skips_a_real_bak_file_on_disk() { + // `is_live_rocm_repo_file_name` above only proves the name filter is + // correct in isolation; this drives an actual `.bak` file (the kind + // fix-7/fix-12 quarantine advice produces) through the directory scan + // `probe_rocm_install` runs, closing the gap where only the live file + // being listed first would still pass if the `.bak` skip were deleted. + let dir = tempfile::tempdir().expect("create temp dir"); + std::fs::write(dir.path().join("amdgpu.list"), "deb ...\n").expect("write live repo"); + std::fs::write(dir.path().join("amdgpu.list.bak"), "deb ...\n") + .expect("write quarantined repo"); + std::fs::write(dir.path().join("unrelated.list"), "deb ...\n").expect("write unrelated"); + + let mut seen = live_rocm_repo_paths_in_dir(&dir.path().to_string_lossy()); + seen.sort(); + + assert_eq!(seen.len(), 1, "seen: {seen:?}"); + assert!(seen[0].ends_with("amdgpu.list")); + } + + #[test] + fn repo_scan_dirs_covers_all_three_package_families() { + // Mutation-proofs the literal dir list `probe_rocm_install` scans: + // deleting the `/etc/zypp/repos.d` entry (SLES repo detection, new in + // this PR) left every existing test green because nothing previously + // asserted this list's contents directly. + assert!(REPO_SCAN_DIRS.contains(&"/etc/apt/sources.list.d")); + assert!(REPO_SCAN_DIRS.contains(&"/etc/yum.repos.d")); + assert!(REPO_SCAN_DIRS.contains(&"/etc/zypp/repos.d")); + } + + #[test] + fn merge_live_repo_paths_scans_every_family_dir_including_zypp() { + // Drives `probe_rocm_install`'s actual scan loop (not just its two + // helpers in isolation) across temp directories standing in for all + // three package families, including the zypp one a mutation run + // showed was silently unscanned by every pre-existing test. + let apt_dir = tempfile::tempdir().expect("create apt temp dir"); + let yum_dir = tempfile::tempdir().expect("create yum temp dir"); + let zypp_dir = tempfile::tempdir().expect("create zypp temp dir"); + std::fs::write(apt_dir.path().join("amdgpu.list"), "deb ...\n").expect("write apt repo"); + std::fs::write(yum_dir.path().join("amdgpu.repo"), "[amdgpu]\n").expect("write yum repo"); + std::fs::write(zypp_dir.path().join("amdgpu.repo"), "[amdgpu]\n").expect("write zypp repo"); + + let mut e = Examination::default(); + let dirs = [ + apt_dir.path().to_string_lossy().into_owned(), + yum_dir.path().to_string_lossy().into_owned(), + zypp_dir.path().to_string_lossy().into_owned(), + ]; + let dir_refs: Vec<&str> = dirs.iter().map(String::as_str).collect(); + merge_live_repo_paths(&mut e, &dir_refs); + + assert_eq!(e.rocm_repos_seen.len(), 3, "seen: {:?}", e.rocm_repos_seen); + assert!( + e.rocm_repos_seen + .iter() + .any(|p| p.starts_with(dirs[0].as_str())) + ); + assert!( + e.rocm_repos_seen + .iter() + .any(|p| p.starts_with(dirs[1].as_str())) + ); + assert!( + e.rocm_repos_seen + .iter() + .any(|p| p.starts_with(dirs[2].as_str())) + ); + } + + /// `probe_rocm_install` must scan every family's repo directory: narrowing + /// its call to fewer of `REPO_SCAN_DIRS` leaves the constant and + /// `merge_live_repo_paths` tests green, so this drives the probe itself + /// against a simulated host root with a repo file in each directory. + #[cfg(all(feature = "e2e-test-hooks", target_os = "linux"))] + #[test] + fn probe_rocm_install_scans_every_repo_family_directory() { + use crate::hardware_root::TEST_HOST_ROOT_ENV; + use crate::test_env::RestoredEnvVar; + + let root = tempfile::tempdir().expect("create host root"); + for (dir, file) in [ + ("etc/apt/sources.list.d", "amdgpu.list"), + ("etc/yum.repos.d", "amdgpu.repo"), + ("etc/zypp/repos.d", "amdgpu.repo"), + ] { + std::fs::create_dir_all(root.path().join(dir)).expect("create repo dir"); + std::fs::write(root.path().join(dir).join(file), "x\n").expect("write repo file"); + } + let mut e = Examination::default(); + { + let _guard = crate::hardware_root::HOST_ROOT_TEST_LOCK + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let _root = RestoredEnvVar::set(TEST_HOST_ROOT_ENV, root.path()); + probe_rocm_install(&mut e); + } + for want in [ + "/etc/apt/sources.list.d/amdgpu.list", + "/etc/yum.repos.d/amdgpu.repo", + "/etc/zypp/repos.d/amdgpu.repo", + ] { + assert!( + e.rocm_repos_seen.iter().any(|p| p == want), + "missing {want}: {:?}", + e.rocm_repos_seen + ); + } + } + + #[test] + fn merge_live_repo_paths_does_not_duplicate_an_already_seen_path() { + let dir = tempfile::tempdir().expect("create temp dir"); + std::fs::write(dir.path().join("amdgpu.repo"), "[amdgpu]\n").expect("write repo"); + let dir_str = dir.path().to_string_lossy().into_owned(); + + let mut e = Examination::default(); + // `join`, not a hardcoded `/`: the scan records `entry.path()`, which + // uses the platform separator. + e.rocm_repos_seen.push( + dir.path() + .join("amdgpu.repo") + .to_string_lossy() + .into_owned(), + ); + merge_live_repo_paths(&mut e, &[dir_str.as_str()]); + + assert_eq!( + e.rocm_repos_seen.len(), + 1, + "an already-recorded path must not be duplicated: {:?}", + e.rocm_repos_seen + ); + } + /// A Linux host with an AMD GPU, so `compute_status` turns on the probe /// failures alone. fn linux_host_with_a_gpu() -> Examination { @@ -3323,6 +3578,23 @@ mod tests { assert!(e.probe_failures.is_empty(), "{:?}", e.probe_failures); } + /// `ID_LIKE` is parsed into `distro_id_like` for + /// `repo_pkg_family`/`openmpi::resolve_package_manager`'s fallback + /// matching, same as `ID`/`VERSION_ID`, and round-trips through JSON the + /// same way so `rocm remote doctor` gets it from a deserialized remote + /// `examine --json` output too. + #[test] + fn id_like_is_parsed_into_distro_id_like() { + let mut e = linux_host_with_a_gpu(); + record_distro( + &mut e, + &crate::hardware_root::OsRelease::found( + "ID=amzn\nID_LIKE=\"fedora\"\nVERSION_ID=\"2023\"\n", + ), + ); + assert_eq!(e.distro_id_like, "fedora"); + } + /// A file that exists but could not be read is reported as itself — not /// folded into an empty, unreported distro the way `Absent` is, and not /// pointing at the wrong candidate when the fallback was the one that @@ -3990,6 +4262,11 @@ mod tests { "os_version", "distro_id", "distro_version", + // CLI addition beyond examine.py, added deliberately: `ID_LIKE`, + // so `rocm remote doctor` can resolve a remote host's package + // manager family from its deserialized `examine --json` output, + // not only from a freshly probed local `Examination`. + "distro_id_like", "kernel_release", "kernel_cmdline", "is_wsl", @@ -6248,6 +6525,47 @@ mod tests { assert_eq!(cgroup_only.container_kind, "container"); } + #[test] + fn no_markers_present_yields_no_repo_native_method() { + assert!(repo_native_method_from_markers(&[]).is_none()); + } + + #[test] + fn every_declared_marker_individually_maps_to_repo_native() { + for marker in REPO_NATIVE_INSTALL_MARKERS { + let (method, seen) = + repo_native_method_from_markers(&[marker]).expect("marker should match"); + assert_eq!(method, "repo-native"); + assert_eq!(seen, vec![(*marker).to_owned()]); + } + } + + #[test] + fn multiple_markers_all_recorded_in_declaration_order() { + // Passed in reverse of REPO_NATIVE_INSTALL_MARKERS' declared order to + // confirm the output order tracks the constant, not the input. + let existing = [ + "/etc/zypp/repos.d/amdgpu.repo", + "/etc/apt/sources.list.d/amdgpu.list", + ]; + let (method, seen) = repo_native_method_from_markers(&existing).expect("some"); + assert_eq!(method, "repo-native"); + assert_eq!( + seen, + vec![ + "/etc/apt/sources.list.d/amdgpu.list".to_owned(), + "/etc/zypp/repos.d/amdgpu.repo".to_owned(), + ] + ); + } + + #[test] + fn unrelated_paths_do_not_match_any_marker() { + assert!( + repo_native_method_from_markers(&["/etc/apt/sources.list.d/some-other.list"]).is_none() + ); + } + /// Both `/etc/os-release` reads `rocm examine` makes — the distro fields /// and the distro name — answer from a simulated host root, and through the /// shared parser: a planted file naming a distro no runner has reads as diff --git a/crates/rocm-core/src/fix.rs b/crates/rocm-core/src/fix.rs index 81b953e521..6e3ec27269 100644 --- a/crates/rocm-core/src/fix.rs +++ b/crates/rocm-core/src/fix.rs @@ -321,7 +321,7 @@ const RECIPES: &[FixRecipe] = &[ FixRecipe { fix_id: "fix-3-rocm-kernel", title: "ROCm/distro/kernel triple unsupported", - rationale: "ROCm is installed but your kernel/distro combination is outside the supported matrix. Match the kernel to the matrix before reinstalling, or rerun with --no-dkms and accept the risk.", + rationale: "ROCm is installed but your kernel/distro combination is outside the supported matrix. Match the kernel to the matrix first, then rerun `rocm install driver --dkms --yes` (documented distros only; Ubuntu 26.04 ships the driver in-tree, use `apt install rocm`).", commands: &[ "# Cross-check the live AMD matrix before changing anything:", "# https://rocm.docs.amd.com/projects/install-on-linux/en/latest/reference/system-requirements.html", @@ -330,7 +330,7 @@ const RECIPES: &[FixRecipe] = &[ needs_sudo: false, needs_reboot: true, needs_relogin: false, - verify: "lsmod | grep amdgpu && rocminfo | head -n 5", + verify: "lsmod | grep amdgpu && rocminfo >/dev/null 2>&1", notes: &[], doc_url: Some( "https://rocm.docs.amd.com/projects/install-on-linux/en/latest/reference/system-requirements.html", @@ -406,15 +406,24 @@ const RECIPES: &[FixRecipe] = &[ title: "Quarantine duplicate AMD repos", rationale: "More than one ROCm/AMDGPU repo file exists. The package manager is mixing versions; quarantine the extras before reinstalling.", commands: &[ - "ls /etc/apt/sources.list.d/ | grep -iE 'rocm|amdgpu|radeon'", + "# Run only the line matching your package manager:", + "ls /etc/apt/sources.list.d/ | grep -iE 'rocm|amdgpu|radeon' || true # Debian/Ubuntu", + "ls /etc/yum.repos.d/ | grep -iE 'rocm|amdgpu|radeon' || true # RHEL/Fedora/Rocky/Alma/Oracle", + "ls /etc/zypp/repos.d/ | grep -iE 'rocm|amdgpu|radeon' || true # SLES/openSUSE", "# For each duplicate file:", - "sudo mv /etc/apt/sources.list.d/.list /etc/apt/sources.list.d/.list.bak", - "sudo apt update", + "sudo mv /etc/apt/sources.list.d/.list /etc/apt/sources.list.d/.list.bak # or the yum/zypp equivalent", + "# Run only the line matching your package manager:", + "sudo apt update # Debian/Ubuntu", + "sudo dnf makecache # RHEL/Fedora/Rocky/Alma/Oracle", + "sudo zypper refresh # SLES/openSUSE", ], needs_sudo: true, needs_reboot: false, needs_relogin: false, - verify: "sudo apt update 2>&1 | tail -n 20", + verify: "# Run only the line matching your package manager:\n\ + bash -c 'set -o pipefail; sudo apt update 2>&1 | tail -n 20' # Debian/Ubuntu\n\ + bash -c 'set -o pipefail; sudo dnf makecache 2>&1 | tail -n 20' # RHEL/Fedora/Rocky/Alma/Oracle\n\ + bash -c 'set -o pipefail; sudo zypper refresh 2>&1 | tail -n 20' # SLES/openSUSE", notes: &[], doc_url: None, applies_on: PRINT_ON_LINUX, @@ -513,20 +522,42 @@ const RECIPES: &[FixRecipe] = &[ }, FixRecipe { fix_id: "fix-12-installer", - title: "Reset amdgpu-install state and reinstall", - rationale: "amdgpu-install left a half-configured DKMS / repo state. Run the documented uninstall, clean up, and reinstall without the flag that broke things (commonly --accept-eula on newer installers).", + title: "Reset repo-native install state and reinstall", + rationale: "The repo-native package-manager install left a half-configured DKMS / repo state. Clear the broken package state, quarantine the ROCm/AMDGPU repo files found on this host, let the package manager forget the broken state, then reinstall via rocm-cli's own repo-native flow.", commands: &[ - "sudo amdgpu-install --uninstall", - "sudo apt autoremove --purge -y", - "sudo apt update", - "sudo amdgpu-install --usecase=rocm,hip", + "# Applies only where `rocm install driver --dkms` plans a reinstall (AMD-documented", + "# distros). On Ubuntu 26.04 the driver is in-tree: skip the purge/remove, the DKMS", + "# reinstall and the DKMS verify, and run `sudo apt install rocm` instead. On any other", + "# distro it plans nothing: skip the purge/remove and reinstall per AMD's install docs.", + "# Run only the block matching your package manager. On Debian/Ubuntu, run all", + "# three lines even if an earlier one reports an error -- the purge must not", + "# be skipped just because the half-configured state made `install -f` fail:", + "sudo dpkg --configure -a # Debian/Ubuntu", + "sudo apt-get install -f # Debian/Ubuntu", + "sudo apt-get purge -y amdgpu-dkms # Debian/Ubuntu", + "sudo dnf remove -y amdgpu-dkms # RHEL/Fedora/Rocky/Alma/Oracle", + "sudo zypper remove -y amdgpu-dkms # SLES/openSUSE", + "# sudo mv /etc/apt/sources.list.d/amdgpu.list /etc/apt/sources.list.d/amdgpu.list.bak # or the yum/zypp equivalent; quarantine, do not delete yet", + "# Run only the line matching your package manager:", + "sudo apt update # Debian/Ubuntu", + "sudo dnf makecache # RHEL/Fedora/Rocky/Alma/Oracle", + "sudo zypper refresh # SLES/openSUSE", + "rocm install driver --dkms --yes", ], needs_sudo: true, needs_reboot: true, needs_relogin: false, - verify: "dpkg -l | grep -E 'rocm|amdgpu' | head -n 20 && rocminfo | head -n 5", + // See the matching comment on this fix's `verify` in diagnose.rs: + // must query the targeted amdgpu-dkms package's installed status + // (not a bare rocm/amdgpu name grep), confirm dkms reports the + // module built, and check rocminfo's own exit code directly. + verify: "(dpkg-query -W -f='${Status}' amdgpu-dkms 2>/dev/null | grep -q 'install ok installed' \ + || rpm -q amdgpu-dkms >/dev/null 2>&1) \ + && dkms status amdgpu 2>/dev/null | grep -q installed \ + && rocminfo >/dev/null 2>&1", notes: &[ - "If `apt autoremove --purge` warns it will remove unrelated packages, stop and resolve those by hand before continuing.", + "If the purge/remove step warns it will take unrelated packages with it, stop and resolve those by hand before continuing.", + "The `rocm install driver --dkms --yes` line and the verify below apply only on AMD-documented distros; `rocm install driver --dkms --yes` exits non-zero where it plans nothing (Ubuntu 26.04, undocumented distros). There, verify with `rocminfo` alone, or `dpkg-query -W rocm` after `sudo apt install rocm` on Ubuntu 26.04.", ], doc_url: None, applies_on: PRINT_ON_LINUX, @@ -1418,10 +1449,19 @@ fn print_recipe(r: &FixRecipe) { println!("Note: {n}"); } if !r.verify.is_empty() { - println!("Verify: {}", r.verify); + println!( + "Verify: {}", + indent_continuation(r.verify, " ") + ); } } +/// Indent every line after the first so a multi-line `verify` (fix-7 lists one +/// command per package manager) stays under its `Verify:` / `$` label. +fn indent_continuation(text: &str, indent: &str) -> String { + text.replace('\n', &format!("\n{indent}")) +} + /// What `rocm fix` says when it is handed a `rocm diagnose` ranking position /// (`#1`) rather than a fix-id. /// @@ -1550,7 +1590,7 @@ fn act_on(recipe: &FixRecipe, opts: &FixOptions) -> i32 { println!("This fix is print-only (manual change required)."); println!("Copy the commands above, run them yourself, then verify with:"); if !recipe.verify.is_empty() { - println!(" $ {}", recipe.verify); + println!(" $ {}", indent_continuation(recipe.verify, " ")); } exit::OK } @@ -3701,4 +3741,242 @@ mod tests { has to say it: {named:?}" ); } + + #[test] + fn fix_7_apt_commands_match_the_diagnose_copy_verbatim() { + // Same drift guard as fix_12_apt_commands_match_the_diagnose_copy_verbatim: + // diagnose.rs builds its own per-family command list for fix-7 rather than + // reusing this recipe, so drive diagnose() itself instead of comparing + // against a hardcoded literal that can't detect diagnose.rs drifting. + let e = crate::examine::Examination { + os_family: "linux".to_owned(), + distro_id: "ubuntu".to_owned(), + rocm_repos_seen: vec![ + "/etc/apt/sources.list.d/rocm.list".to_owned(), + "/etc/apt/sources.list.d/amdgpu.list".to_owned(), + ], + ..crate::examine::Examination::default() + }; + let report = crate::diagnose::diagnose(&e, "stale repo conflict"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("duplicate repo files should be diagnosed"); + let diagnose_apt_list_cmd = hit + .fix + .as_ref() + .unwrap() + .commands + .first() + .expect("fix-7 always lists the repo dir first on a recognized family") + .as_str(); + + let recipe = RECIPES + .iter() + .find(|r| r.fix_id == "fix-7-stale-repos") + .expect("fix-7-stale-repos recipe must exist"); + let recipe_apt_list_cmd = recipe + .commands + .iter() + .find(|c| c.trim_end().ends_with("# Debian/Ubuntu") && c.contains("ls /etc/apt")) + .map(|c| c.split('#').next().unwrap().trim_end()) + .expect("recipe must have a commented apt list line"); + assert_eq!( + recipe_apt_list_cmd, diagnose_apt_list_cmd, + "this recipe's apt list command must match what diagnose.rs's apt family actually emits for fix-7-stale-repos" + ); + + // `verify` must also track diagnose.rs's per-family text, not just + // `commands`: diagnose.rs varies `verify` by family too, so compare + // each family's line against what diagnose() actually emits for it. + // The dnf/zypper `ls` lines get the same treatment as apt above -- + // only asserting the apt one left those two families' list commands + // able to drift from the recipe undetected. + for (family_repo_dir, marker) in [ + ("/etc/apt/sources.list.d/", "# Debian/Ubuntu"), + ("/etc/yum.repos.d/", "# RHEL/Fedora/Rocky/Alma/Oracle"), + ("/etc/zypp/repos.d/", "# SLES/openSUSE"), + ] { + let e = crate::examine::Examination { + os_family: "linux".to_owned(), + distro_id: "ubuntu".to_owned(), + rocm_repos_seen: vec![ + format!("{family_repo_dir}rocm.repo"), + format!("{family_repo_dir}amdgpu.repo"), + ], + ..crate::examine::Examination::default() + }; + let report = crate::diagnose::diagnose(&e, "stale repo conflict"); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-7-stale-repos") + .expect("duplicate repo files should be diagnosed"); + let fix = hit.fix.as_ref().unwrap(); + let diagnose_list_cmd = fix + .commands + .first() + .expect("fix-7 always lists the repo dir first on a recognized family") + .as_str(); + let diagnose_verify = fix.verify.as_str(); + + let recipe_list_cmd = recipe + .commands + .iter() + .find(|c| c.trim_end().ends_with(marker) && c.contains("ls /")) + .map_or_else( + || panic!("recipe must have a commented list line for {marker}"), + |c| c.split('#').next().unwrap().trim_end(), + ); + assert_eq!( + recipe_list_cmd, diagnose_list_cmd, + "this recipe's {marker} list command must match what diagnose.rs emits for fix-7-stale-repos" + ); + + let recipe_verify_line = recipe + .verify + .lines() + .find(|l| l.trim_end().ends_with(marker)) + .map_or_else( + || panic!("recipe verify must have a line for {marker}"), + |l| l.split('#').next().unwrap().trim(), + ); + assert_eq!( + recipe_verify_line, diagnose_verify, + "this recipe's {marker} verify line must match what diagnose.rs emits for fix-7-stale-repos" + ); + } + } + + #[test] + fn fix_12_recipe_clears_state_before_reinstall_and_keeps_mv_commented() { + let recipe = RECIPES + .iter() + .find(|r| r.fix_id == "fix-12-installer") + .expect("fix-12-installer recipe must exist"); + let commands = recipe.commands.join("\n"); + assert!( + commands.contains("dpkg --configure -a"), + "must clear the half-configured dpkg state before reinstalling: {commands}" + ); + assert!( + commands.contains("purge -y amdgpu-dkms"), + "must purge the broken driver package: {commands}" + ); + assert!( + commands.contains("# sudo mv /etc/apt/sources.list.d/amdgpu.list"), + "repo quarantine command must be commented out, not ready-to-run: {commands}" + ); + assert!( + commands.contains("rocm install driver --dkms --yes"), + "reinstall command must actually install (bare 'rocm install driver' is a non-mutating preflight no-op): {commands}" + ); + assert!( + recipe.verify.contains("dpkg") && recipe.verify.contains("rpm"), + "verify must query real dpkg/rpm package state, not the self-reported \ + rocm_install_method marker (that marker is set from repo-file presence \ + before the package actually installs): {}", + recipe.verify + ); + assert!( + !recipe + .commands + .iter() + .any(|c| c.contains("configure -a &&") || c.contains("install -f &&")), + "the apt block must not chain configure/install -f into the purge with `&&`: \ + a failure there must not skip the purge: {commands}" + ); + } + + #[test] + fn fix_3_verify_matches_the_diagnose_copy_verbatim() { + // No drift guard previously existed between this recipe and + // diagnose.rs's `check_3_rocm_kernel_unsupported` for fix-3 (unlike + // fix-7/fix-12's `commands`, fix-3's `commands` legitimately differ -- + // diagnose.rs injects the host's live kernel/distro/rocm values into a + // comment this host-unaware recipe can't know -- but `verify` has no + // such excuse to diverge). + let e = crate::examine::Examination { + os_family: "linux".to_owned(), + rocm_version: "6.4.1".to_owned(), + amdgpu_loaded: Some(false), + ..crate::examine::Examination::default() + }; + let report = crate::diagnose::diagnose(&e, ""); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-3-rocm-kernel") + .expect("rocm installed with amdgpu not loaded should be diagnosed"); + let diagnose_verify = hit.fix.as_ref().unwrap().verify.as_str(); + + let recipe = RECIPES + .iter() + .find(|r| r.fix_id == "fix-3-rocm-kernel") + .expect("fix-3-rocm-kernel recipe must exist"); + assert_eq!( + recipe.verify, diagnose_verify, + "this recipe's verify step must match what diagnose.rs emits for fix-3-rocm-kernel" + ); + } + + #[test] + fn fix_12_apt_commands_match_the_diagnose_copy_verbatim() { + // `crate::diagnose` builds its own apt command list for fix-12 rather + // than reusing this recipe (it branches per detected package-manager + // family, which this catalog entry cannot do -- it prints guidance for + // every family at once). Drive `diagnose()` itself so a drift between + // the two copies actually fails this test, rather than comparing + // against a hardcoded literal that can't detect diagnose.rs changing + // out from under it. + let e = crate::examine::Examination { + os_family: "linux".to_owned(), + distro_id: "ubuntu".to_owned(), + distro_version: "24.04".to_owned(), + rocm_install_method: "repo-native".to_owned(), + ..crate::examine::Examination::default() + }; + let report = crate::diagnose::diagnose( + &e, + "dpkg: error processing package amdgpu-dkms (half-configured)", + ); + let hit = report + .matched + .iter() + .find(|d| d.id == "fix-12-installer") + .expect("repo-native broken state should be diagnosed"); + let diagnose_apt_commands: Vec<&str> = hit + .fix + .as_ref() + .unwrap() + .commands + .iter() + .map(String::as_str) + .filter(|c| !c.trim_start().starts_with('#')) + .take(3) + .collect(); + + let recipe = RECIPES + .iter() + .find(|r| r.fix_id == "fix-12-installer") + .expect("fix-12-installer recipe must exist"); + let apt_commands_without_trailing_comment: Vec<&str> = recipe + .commands + .iter() + .filter(|c| c.trim_end().ends_with("# Debian/Ubuntu")) + .map(|c| c.split('#').next().unwrap().trim_end()) + .take(3) + .collect(); + assert_eq!( + apt_commands_without_trailing_comment, diagnose_apt_commands, + "this recipe's apt lines must match what diagnose.rs's apt family actually emits for fix-12-installer" + ); + } + + #[test] + fn indent_continuation_keeps_multi_line_verify_under_its_label() { + assert_eq!(indent_continuation("a\nb\nc", " "), "a\n b\n c"); + assert_eq!(indent_continuation("single", " "), "single"); + } } diff --git a/crates/rocm-core/src/openmpi.rs b/crates/rocm-core/src/openmpi.rs index 3fbbe5b1d6..5c7792ef93 100644 --- a/crates/rocm-core/src/openmpi.rs +++ b/crates/rocm-core/src/openmpi.rs @@ -546,7 +546,7 @@ fn scan_libnuma_path() -> Option { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -enum PackageManager { +pub(crate) enum PackageManager { Apt, Dnf, Zypper, @@ -554,7 +554,7 @@ enum PackageManager { } impl PackageManager { - const fn as_str(self) -> &'static str { + pub(crate) const fn as_str(self) -> &'static str { match self { Self::Apt => "apt", Self::Dnf => "dnf", @@ -1180,7 +1180,7 @@ pub fn is_protected_rocm_package(package: &str) -> bool { PREFIXES.iter().any(|prefix| package.starts_with(prefix)) } -fn resolve_package_manager(os_id: &str, id_like: &str) -> Option { +pub(crate) fn resolve_package_manager(os_id: &str, id_like: &str) -> Option { const APT: &[&str] = &[ "ubuntu", "debian", diff --git a/docs/manual-testing.md b/docs/manual-testing.md index 1fab216666..d8b1667581 100644 --- a/docs/manual-testing.md +++ b/docs/manual-testing.md @@ -254,6 +254,23 @@ fallback paired an architecture line with the wrong GPU. On that machine `rocm diagnose` must **not** report `info-20-gpu-no-rocm-support` at all — the discrete card does have ROCm support, so the whole-machine claim is false. +## 2b. Bare-Metal Driver Install + +On a real apt/dnf/zypper Linux host: + +```bash +rocm install driver --dry-run +``` + +Expected result: the plan names the floating `latest` amdgpu release unless +`ROCM_CLI_AMDGPU_DRIVER_VERSION` is set, in which case it names that version +instead. A malformed override (e.g. containing a space or `'`) is refused with +a reason naming `ROCM_CLI_AMDGPU_DRIVER_VERSION`, not silently ignored. + +On an Ubuntu 26.04 host, expect `supported: false` instead: `repo.radeon.com` +has no DKMS repo for it yet (the amdgpu driver ships in-tree), so the plan +directs you to `apt install rocm` rather than naming any DKMS commands. + ## 3. Lemonade GPU Verification Lemonade is the default local assistant/server engine. Serve a small assistant diff --git a/docs/testing.md b/docs/testing.md index 137d44a24b..bca3bbc656 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -1760,6 +1760,35 @@ rocm --bin rocm wsl_rocdxg`). Running it end to end needs a WSL2 host with `/dev/dxg` and dxcore present, since the plan refuses before installing otherwise. +## Bare-metal driver install + +On Linux bare metal, `rocm install driver` defaults to the floating `latest` +amdgpu release; `ROCM_CLI_AMDGPU_DRIVER_VERSION` overrides it (replacing the +retired `ROCM_CLI_AMDGPU_VERSION`/`ROCM_CLI_AMDGPU_PACKAGE_RELEASE` pair, which the plan refuses while either is still set). An +override that isn't a plain version token is refused rather than silently +falling back, since it is interpolated into privileged shell commands: + +```bash +cargo test -p rocm --bin rocm driver_install +``` + +`driver-install-06` in +[driver_install.feature](../tests/e2e-cucumber/features/driver_install.feature) +asserts whichever plan a real bare-metal Linux host actually produces: the apt +repo file and signing key on a release with a published +repo.radeon.com/amdgpu suite, or the native-archive refusal below on one +without. + +On Ubuntu 26.04, `repo.radeon.com` does not yet publish a matching `resolute` +DKMS repo (the amdgpu driver ships in-tree instead), so the plan comes back +`supported: false` and directs the user to `apt install rocm`. This arm is +covered at the unit level by +`driver_plan_ubuntu_2604_uses_native_archive_guidance` and its two neighbors in +[driver_install.rs](../apps/rocm/src/driver_install.rs), and at the e2e level +by `driver-install-06` above: the hosted `E2E tests` job's `ubuntu-latest` image (26.04 once GitHub's migration completes) +reaches this arm through the CLI itself, while the self-hosted GPU lane's +24.04 host exercises the apt arm instead. + ## /etc/os-release Every Rust reader of `/etc/os-release` parses it with @@ -1950,6 +1979,19 @@ does not apply on — and exits 3. cargo xtask e2e -- -n '(diagnose-40)' ``` +`diagnose-42` uses the same simulated-host mechanism for the driver-reinstall +advice: an Ubuntu 24.04 `os-release` and two stale repo files planted under the +root make `fix-7-stale-repos` fire, and the scenario checks that it names both +repo files by their real paths and `rocm install driver --dkms --yes`, and that +`rocm install driver --help` accepts those flags. `driver-install-08` covers the +retired `ROCM_CLI_AMDGPU_VERSION` / `ROCM_CLI_AMDGPU_PACKAGE_RELEASE` refusal +and checks that setting `ROCM_CLI_AMDGPU_DRIVER_VERSION`, as the refusal advises, +clears it. + +```bash +cargo xtask e2e -- -n '(diagnose-42|driver-install-08)' +``` + The other half of the same change — `fix-13-hip-sdk-missing` no longer firing just because no HIP SDK is installed — has **no** end-to-end coverage and cannot have any. `check_13_hip_sdk_missing` returns early on every non-Windows host, so diff --git a/doctor-catalog.json b/doctor-catalog.json index e59cbd558c..1406fa9f00 100644 --- a/doctor-catalog.json +++ b/doctor-catalog.json @@ -187,7 +187,7 @@ }, { "id": "fix-12-installer", - "title": "Reset amdgpu-install state and reinstall", + "title": "Reset repo-native install state and reinstall", "platforms": [ { "os": "linux", diff --git a/skills/rocm-doctor/SKILL.md b/skills/rocm-doctor/SKILL.md index a1687848e8..fb8a1b716d 100644 --- a/skills/rocm-doctor/SKILL.md +++ b/skills/rocm-doctor/SKILL.md @@ -11,7 +11,8 @@ description: >- permission denied on /dev/kfd, "ROCk module is NOT loaded", a missing libamdhip64.so / amdhip64_6.dll / hipblas.dll / vcruntime140_1.dll, an HSA_OVERRIDE_GFX_VERSION page fault, an iGPU+dGPU crash, a container that can't - see the GPU, or an amdgpu-install / DKMS failure. Backed by the `rocm` CLI + see the GPU, or a repo-native package-manager install (formerly `amdgpu-install`) / DKMS + failure. Backed by the `rocm` CLI (`rocm examine` / `rocm diagnose` / `rocm fix`); this skill is a thin driver over those commands, not a re-implementation. --- diff --git a/skills/rocm-doctor/reference.md b/skills/rocm-doctor/reference.md index 9bafc22aa3..c75201848d 100644 --- a/skills/rocm-doctor/reference.md +++ b/skills/rocm-doctor/reference.md @@ -121,12 +121,12 @@ reporting confident nonsense. | `fix-4-render-group` | linux | User not in `render`/`video` group (or `/dev/kfd` owned by the other group) | cannot open `/dev/kfd`, permission denied | auto | | `fix-5-amdgpu-load` | linux | `amdgpu` module not loaded (or blacklisted) | "ROCk module is NOT loaded", blacklist entry, Secure Boot | print-only | | `fix-6-path` | linux/windows/wsl | ROCm/HIP binaries not on PATH after install | `rocminfo: command not found`, `hipInfo` missing from PATH | auto | -| `fix-7-stale-repos` | linux | Stale/conflicting APT/DNF repos from prior installer runs | apt 404 `repo.radeon.com`, unmet deps, ≥2 ROCm repo files | print-only | +| `fix-7-stale-repos` | linux | Stale/conflicting APT/DNF/zypper repos from prior installer runs | apt 404 `repo.radeon.com`, unmet deps, ≥2 ROCm repo files | print-only | | `fix-8-wheel-rocm` | linux/windows/wsl | Framework wheel built for a different ROCm major than the system | `libamdhip64.so.X` / `amdhip64_X.dll` load failure | print-only | | `fix-9-igpu-dgpu` | linux/windows | iGPU enumerated alongside dGPU, destabilising the runtime | APU + discrete AMD present, `HIP_VISIBLE_DEVICES` unset, crash/segfault | needs-arg | | `fix-10-container` | linux | Container can't see `/dev/kfd` or `/dev/dri/renderD*` | running in docker/podman, kfd/render devices missing | print-only | | `fix-11-iommu` | linux | Multi-GPU hang with IOMMU enabled | ≥2 AMD GPUs, `iommu=` not `pt`, hang/deadlock/timeout | print-only | -| `fix-12-installer` | linux | `amdgpu-install` left a broken DKMS / repo state | dpkg half-configured, DKMS failed, `--accept-eula` | print-only | +| `fix-12-installer` | linux | Repo-native package-manager install left a broken DKMS / repo state | dpkg half-configured, DKMS build failure, dpkg/apt error | print-only | | `fix-13-hip-sdk-missing` | windows | HIP SDK not installed | **something on the host actually calls into HIP** — an ambient (non-managed-runtime) PyTorch HIP build, or a `GGML_HIP=ON` llama.cpp, either way with a detected AMD GPU — **and** no HIP SDK under Program Files, or `hipInfo.exe` missing from its `bin`. A paste naming any of `amdhip64` not found, "could not find HIP", `HIP_PATH` not set, or `hipInfo` not recognized counts as calling into HIP on its own, with no GPU required. A Vulkan-backed engine (the Lemonade default) or a managed-runtime wheel needs no system SDK, so its absence is not evidence there | print-only | | `fix-14-adrenalin-too-old` | windows | Adrenalin / kernel-mode driver too old for the HIP SDK | `hipInfo` can't enumerate, "driver too old", HSA "no agents found" | print-only | | `fix-15-msvc-redist` | windows | MSVC runtime missing (programs that link it, such as the HIP SDK DLLs and the llama.cpp server, can't load) | `vcruntime140.dll` / `vcruntime140_1.dll` missing | print-only | diff --git a/tests/e2e-cucumber/features/diagnose.feature b/tests/e2e-cucumber/features/diagnose.feature index 81ba7dd77b..3ee381eaac 100644 --- a/tests/e2e-cucumber/features/diagnose.feature +++ b/tests/e2e-cucumber/features/diagnose.feature @@ -633,3 +633,16 @@ Feature: Diagnosing failures and listing fixes Given a machine with an AMD GPU When the user diagnoses this machine in machine-readable form, with no symptom Then the diagnosis offers the old display driver advice only when the inspection reports an old driver + + # The remediation text names a command and the files to move aside, so it is + # checked against a simulated Ubuntu 24.04 machine (os-release and two stale + # repo files planted under the host root, as in diagnose-40) rather than the + # runner's own distro: the advice depends on the distro and the runner's is + # not fixed. The named command is then held to existing and accepting the + # flags it is advised with; its behaviour is covered by driver-install-06. + @id:diagnose-stale-repos-advice-names-the-driver-reinstall @requires-os:linux + Scenario: diagnose-42 - Stale repo advice on Ubuntu 24.04 names the planted repo files and a driver reinstall that exists + Given an Ubuntu 24.04 machine with two stale ROCm repo files + When the user diagnoses that machine with a stale repo symptom + Then the stale repo cause names both planted repo files and the driver reinstall command + And the advised driver reinstall command accepts the flags it is advised with diff --git a/tests/e2e-cucumber/features/driver_install.feature b/tests/e2e-cucumber/features/driver_install.feature index d3f5807012..c548cb2ab9 100644 --- a/tests/e2e-cucumber/features/driver_install.feature +++ b/tests/e2e-cucumber/features/driver_install.feature @@ -49,3 +49,50 @@ Feature: Driver installation planning When the user approves the driver install on this Windows host Then the CLI says that no driver is installed or changed And no driver command is shown or run, and no installer state is written + + # Every bare-metal Linux lane in this repo runs Ubuntu, but not always the + # same release -- the hosted `E2E tests` job's `ubuntu-latest` image (26.04 once GitHub's migration completes) reports + # 26.04 ("resolute"), which `build_driver_install_plan_at` routes to the + # `ubuntu_native_archive` arm instead of the apt one (no repo.radeon.com/ + # amdgpu suite published for "resolute" yet; see the `("ubuntu", "26.04")` + # arm in `driver_install.rs`), while the self-hosted GPU lane's real + # /etc/os-release reports 24.04 and takes the apt arm. `--dry-run` previews + # the plan without writing anything, so this is safe on a shared CI host + # either way: on an apt-arm release it asserts the real `apt_driver_plan` + # command text this binary would use to write + # /etc/apt/sources.list.d/amdgpu.list and import AMD's signing key, the + # same commands the unit fixtures in `driver_install.rs` exercise with a + # hand-written os-release; on 26.04 it asserts the native-archive refusal + # instead, so the scenario proves the CLI's actual output on whichever + # release the lane happens to run rather than hard-coding an apt-arm + # premise a lane can silently outgrow. + @id:driver-install-bare-metal-host-plan @requires-os:linux @requires-bare-metal + Scenario: driver-install-06 - Previewing the driver install on a real bare-metal host plans release-appropriate driver setup + When the user previews driver installation on this host + Then the driver plan matches this host's actual Ubuntu release + + # The override is interpolated into privileged shell commands, so a value + # that fails the shell-token allow-list must refuse the plan rather than + # silently falling back to "latest" -- user-observable as a + # `supported: false` plan naming the variable. `ROCM_CLI_AMDGPU_DRIVER_VERSION` + # is only consulted by the Linux DKMS arm, though: `build_driver_install_plan_at` + # checks `examine.wsl.is_wsl` and returns the WSL/ROCDXG plan before this override is + # ever resolved, so a WSL host has no premise here (it would hit + # `wsl_rocdxg_driver_plan` regardless of the override and the scenario would + # fail by construction) -- same WSL-vs-bare-metal split as driver-install-06. + @id:driver-install-invalid-version-override-refused @requires-os:linux @requires-bare-metal + Scenario: driver-install-07 - An invalid ROCM_CLI_AMDGPU_DRIVER_VERSION override refuses the plan + When the user previews driver installation with an invalid version override + Then the driver plan refuses the invalid version override and plans no commands + + # The pre-repo-native pin variables are retired, not ignored: a stale export + # would otherwise turn into the floating `latest` without telling the user. + # The refusal names `ROCM_CLI_AMDGPU_DRIVER_VERSION`, so the scenario also + # runs that remedy and checks it clears the refusal. Bare-metal Linux only, + # for the same reason as driver-install-07. + @id:driver-install-retired-version-variable-refused @requires-os:linux @requires-bare-metal + Scenario: driver-install-08 - A retired version pin variable refuses the plan until the advised variable replaces it + When the user previews driver installation with a retired version pin variable + Then the driver plan refuses the retired variable and plans no commands + And setting the advised variable instead clears the refusal + And approving the install with the retired variable still set fails and installs nothing diff --git a/tests/e2e-cucumber/features/examine.feature b/tests/e2e-cucumber/features/examine.feature index 38a8390118..e527e46d7e 100644 --- a/tests/e2e-cucumber/features/examine.feature +++ b/tests/e2e-cucumber/features/examine.feature @@ -119,9 +119,9 @@ Feature: GPU detection and system inspection Then the inspection reports Linux as the operating system And the inspection reports that the host is WSL - # The dry-run plan used to print the raw `${ROCM_CLI_AMDGPU_VERSION:-...}` shell - # placeholder on its `repo_version:` line instead of the effective version, so - # the preview a user reviews before approving disagreed with what the install + # The dry-run plan used to print the raw `${ROCM_CLI_AMDGPU_VERSION:-7.2.4}` + # shell placeholder on its `repo_version:` line instead of the effective version, + # so the preview a user reviews before approving disagreed with what the install # would actually pull. The plan is rendered on every Linux host regardless of # GPU (the driver is not yet installed when you preview it), so the mock lane # pins this without hardware. @@ -296,6 +296,7 @@ Feature: GPU detection and system inspection Given a machine whose release file quotes every value in single quotes When the user inspects that machine in machine-readable form Then the inspection reports the distribution "rhel" version "9.4" named "Red Hat Enterprise Linux 9.4 (Plow)" + And the inspection reports the distribution family "fedora" And the inspection reports no trouble reading the release file @id:examine-reads-last-os-release-assignment @requires-os:linux @@ -443,3 +444,24 @@ Feature: GPU detection and system inspection When the user inspects the system both for reading and for scripting Then the inspection reports the display driver's version and date as the machine-readable form does And the inspection calls the driver old only when its date is before 27 July 2023 + + # `rocm_install_method` used to report "amdgpu-install"/"tarball-or-other"; + # renamed to "repo-native"/"runfile-or-tarball" alongside the switch away + # from the amdgpu-install wrapper. It also feeds fix-12's applicability + # score (not which remediation commands it renders -- that comes from the + # package family and distro, not this field), so a silent revert of the + # rename would misfire there without any test catching it -- this pins the + # JSON contract directly rather than only + # asserting the human-readable form (examine-06), which never named it. A + # whitelist rather than one pinned value keeps this hermetic: which method + # actually fires depends on this host's real ROCm repo markers (some + # developer machines and self-hosted GPU runners have them, a clean mock + # lane does not), not just on the install this scenario plants. No GPU + # needed: the install is planted by the harness (`plant_unmanaged_rocm`), + # same as examine-06. Linux/WSL only, like examine-12: `probe_rocm_install` + # only runs there, so `rocm_install_method` is empty on Windows. + @id:examine-reports-a-current-install-method-name @requires-os:linux + Scenario: examine-30 - System inspection reports a current install-method name, not the retired amdgpu-install naming + Given a machine with a ROCm install that was not set up by the CLI + When the user inspects the system in machine-readable form + Then the inspection reports a current install-method name diff --git a/tests/e2e-cucumber/tests/e2e/diagnose_steps.rs b/tests/e2e-cucumber/tests/e2e/diagnose_steps.rs index 0f20f746ff..08e0ae1e85 100644 --- a/tests/e2e-cucumber/tests/e2e/diagnose_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/diagnose_steps.rs @@ -2671,6 +2671,90 @@ async fn user_diagnoses_planted_machine(world: &mut E2eWorld) { world.cli_rc = Some(rc); } +// ── Stale repo advice on a simulated Ubuntu host ─────────────────── + +#[given("an Ubuntu 24.04 machine with two stale ROCm repo files")] +async fn ubuntu_with_stale_repos(world: &mut E2eWorld) { + let host = world + .isolated_root + .as_ref() + .expect("no isolated root") + .path() + .join("host-root"); + std::fs::create_dir_all(host.join("etc/apt/sources.list.d")) + .expect("failed to create planted apt directory"); + std::fs::write( + host.join("etc/os-release"), + "ID=ubuntu\nVERSION_ID=\"24.04\"\nVERSION_CODENAME=noble\nPRETTY_NAME=\"Ubuntu 24.04 LTS\"\n", + ) + .expect("failed to write planted os-release"); + for name in ["amdgpu.list", "rocm.list"] { + std::fs::write( + host.join("etc/apt/sources.list.d").join(name), + "# planted\n", + ) + .expect("failed to plant a repo file"); + } + world + .command_env + .push(("ROCM_CLI_TEST_HOST_ROOT", host.into_os_string())); +} + +#[when("the user diagnoses that machine with a stale repo symptom")] +async fn user_diagnoses_stale_repo_symptom(world: &mut E2eWorld) { + let (stdout, stderr, rc) = crate::run_rocm_with_scenario_env( + world, + &[ + "diagnose", + "--symptom", + "stale repo conflict", + "--top", + "20", + ], + ); + world.cli_output = Some(stdout); + world.cli_stderr = Some(stderr); + world.cli_rc = Some(rc); +} + +#[then("the stale repo cause names both planted repo files and the driver reinstall command")] +async fn assert_stale_repo_advice(world: &mut E2eWorld) { + let output = world.cli_output.as_ref().expect("no diagnose output"); + let cause: Vec<&str> = output + .lines() + .skip_while(|line| !line.contains("(fix-7-stale-repos)")) + .take_while(|line| !line.trim().is_empty()) + .collect(); + let text = cause.join("\n"); + assert!( + !text.is_empty(), + "no stale repo cause was reported:\n{output}" + ); + // The logical paths a user would move, not the scenario's temp host root. + for file in ["amdgpu.list", "rocm.list"] { + assert!( + text.contains(&format!("/etc/apt/sources.list.d/{file}")), + "{file} must be named by its real path:\n{text}" + ); + } + assert!( + text.contains("rocm install driver --dkms --yes"), + "Ubuntu 24.04 has a driver plan, so the advice must name it:\n{text}" + ); +} + +#[then("the advised driver reinstall command accepts the flags it is advised with")] +async fn assert_advised_driver_command_exists(world: &mut E2eWorld) { + let (stdout, _stderr, rc) = crate::run_rocm(world, &["install", "driver", "--help"]); + assert_eq!(rc, 0, "`rocm install driver --help` must exist:\n{stdout}"); + for flag in ["--dkms", "--yes"] { + assert!( + stdout.contains(flag), + "{flag} is advised but not accepted:\n{stdout}" + ); + } +} + // ── Windows display driver advice ────────────────────────────────── #[when("the user diagnoses this machine in machine-readable form, with no symptom")] diff --git a/tests/e2e-cucumber/tests/e2e/driver_steps.rs b/tests/e2e-cucumber/tests/e2e/driver_steps.rs index 257f4b389e..c35d1180e3 100644 --- a/tests/e2e-cucumber/tests/e2e/driver_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/driver_steps.rs @@ -18,6 +18,87 @@ async fn preview_wsl_driver_install(world: &mut E2eWorld) { world.cli_rc = Some(rc); } +#[when("the user previews driver installation on this host")] +async fn preview_driver_install_on_this_host(world: &mut E2eWorld) { + // `--dkms` is required here: every driver-plan builder gates its entire + // command list (repo setup, signing key, package install) behind + // `dkms`, so a plan built without it is empty and the apt-repo-file/key + // assertion below would have nothing to find. + let (stdout, stderr, rc) = + crate::run_rocm_with_env(world, &["install", "driver", "--dkms", "--dry-run"], &[]); + world.cli_output = Some(stdout); + world.cli_stderr = Some(stderr); + world.cli_rc = Some(rc); +} + +#[then("the driver plan matches this host's actual Ubuntu release")] +async fn assert_driver_plan_matches_host_release(world: &mut E2eWorld) { + assert_eq!(world.cli_rc, Some(0), "driver dry-run should succeed"); + let output = world.cli_output.as_ref().expect("no driver plan output"); + + // Derived from the host's own `/etc/os-release`, not from `output` -- + // asserting against the very thing being checked would let a change that + // breaks the arm-selection logic pass by construction. The hosted + // `ubuntu-latest` E2E image (26.04 once GitHub's migration completes) reports 26.04 ("resolute"), which has no + // published repo.radeon.com/amdgpu suite yet and takes + // `ubuntu_native_archive`; the self-hosted GPU lane reports 24.04 and + // takes the apt arm. See the `("ubuntu", "26.04")` arm in + // `driver_install.rs`. + let os_release_text = + rocm_core::hardware_root::read_os_release(rocm_core::hardware_root::identity_path_for_str) + .text() + .to_owned(); + let Ok(os_release) = rocm_core::os_release::parse(&os_release_text) else { + panic!("host's /etc/os-release is unreadable:\n{output}"); + }; + if os_release.get("ID").map(String::as_str) != Some("ubuntu") { + // Every bare-metal Linux lane in this repo runs Ubuntu today, but + // this scenario's premise is Ubuntu specifically (every assertion + // below hard-codes Ubuntu's apt/native-archive command text), so a + // future non-Ubuntu bare-metal lane has no premise here rather than + // failing against text it was never meant to produce. The harness has + // no per-step skip, so say so rather than passing silently. + eprintln!("driver-install-06: not an Ubuntu host, nothing asserted"); + return; + } + + if os_release.get("VERSION_ID").map(String::as_str) == Some("26.04") { + assert!(output.contains("policy: ubuntu_native_archive"), "{output}"); + assert!(output.contains("supported: false"), "{output}"); + assert!( + output.contains("ships ROCm natively via the standard archive"), + "{output}" + ); + return; + } + + // The runner may already be root (a self-hosted runner may be), + // in which case the plan omits the `sudo ` prefix entirely. + let sudo = if rocm_core::openmpi::running_as_root() { + "" + } else { + "sudo " + }; + assert!( + output.contains("policy: linux_official_amd_dkms_wrapper"), + "{output}" + ); + assert!( + output.contains(&format!( + "curl -fsSL https://repo.radeon.com/rocm/rocm.gpg.key | {sudo}gpg --batch --yes --dearmor -o /etc/apt/keyrings/rocm.gpg" + )), + "{output}" + ); + assert!( + output.contains("signed-by=/etc/apt/keyrings/rocm.gpg] https://repo.radeon.com/amdgpu/"), + "{output}" + ); + assert!( + output.contains("tee /etc/apt/sources.list.d/amdgpu.list >/dev/null"), + "{output}" + ); +} + #[when("the user reviews driver installation on this WSL host without approval")] async fn review_wsl_driver_install_without_approval(world: &mut E2eWorld) { let (stdout, stderr, rc) = crate::run_rocm_with_env(world, &["install", "driver"], &[]); @@ -142,6 +223,21 @@ async fn assert_driver_plan_refuses_unverified(world: &mut E2eWorld) { ); } +#[when("the user previews driver installation with an invalid version override")] +async fn preview_driver_install_invalid_override(world: &mut E2eWorld) { + let (stdout, stderr, rc) = crate::run_rocm_with_env( + world, + &["install", "driver", "--dkms", "--dry-run"], + &[( + "ROCM_CLI_AMDGPU_DRIVER_VERSION", + "'; touch /tmp/pwned; echo '", + )], + ); + world.cli_output = Some(stdout); + world.cli_stderr = Some(stderr); + world.cli_rc = Some(rc); +} + // ── Windows: the driver install only reports ─────────────────────── /// Refuse any other host before the step runs the CLI. Explicit cucumber @@ -162,6 +258,81 @@ async fn ask_windows_driver_install(world: &mut E2eWorld) { world.cli_rc = Some(rc); } +#[when("the user previews driver installation with a retired version pin variable")] +async fn preview_driver_install_retired_variable(world: &mut E2eWorld) { + let (stdout, stderr, rc) = crate::run_rocm_with_env( + world, + &["install", "driver", "--dkms", "--dry-run"], + &[("ROCM_CLI_AMDGPU_VERSION", "7.2.4")], + ); + world.cli_output = Some(stdout); + world.cli_stderr = Some(stderr); + world.cli_rc = Some(rc); +} + +#[then("the driver plan refuses the retired variable and plans no commands")] +async fn assert_driver_plan_refuses_retired_variable(world: &mut E2eWorld) { + let output = world.cli_output.as_ref().expect("no driver plan output"); + assert!( + output.contains("policy: retired_driver_version_override"), + "{output}" + ); + assert!(output.contains("supported: false"), "{output}"); + assert!(output.contains("mutating: false"), "{output}"); + assert!( + output.contains("ROCM_CLI_AMDGPU_DRIVER_VERSION"), + "{output}" + ); + assert!(output.contains("execution_commands: "), "{output}"); +} + +#[then("setting the advised variable instead clears the refusal")] +async fn assert_advised_variable_clears_retired_refusal(world: &mut E2eWorld) { + // The refusal tells the user to unset the retired variable and set this + // one; run exactly that and hold the claim to the resulting plan. + let (stdout, _stderr, _rc) = crate::run_rocm_with_env( + world, + &["install", "driver", "--dkms", "--dry-run"], + &[("ROCM_CLI_AMDGPU_DRIVER_VERSION", "30.10")], + ); + assert!( + !stdout.contains("policy: retired_driver_version_override") + && !stdout.contains("policy: invalid_driver_version_override"), + "{stdout}" + ); +} + +#[then("approving the install with the retired variable still set fails and installs nothing")] +async fn assert_approving_retired_variable_fails(world: &mut E2eWorld) { + // Safe on a real host: the refusal precedes any command, so `--yes` has + // nothing to execute and no installer state is written. + let (stdout, stderr, rc) = crate::run_rocm_with_env( + world, + &["install", "driver", "--dkms", "--yes"], + &[("ROCM_CLI_AMDGPU_VERSION", "7.2.4")], + ); + assert_ne!(rc, 0, "an approved refused plan must not exit 0:\n{stdout}"); + let all = format!("{stdout}{stderr}"); + assert!(all.contains("nothing was installed"), "{all}"); + assert!(!all.contains("driver install completed"), "{all}"); +} + +#[then("the driver plan refuses the invalid version override and plans no commands")] +async fn assert_driver_plan_refuses_invalid_override(world: &mut E2eWorld) { + let output = world.cli_output.as_ref().expect("no driver plan output"); + assert!( + output.contains("policy: invalid_driver_version_override"), + "{output}" + ); + assert!(output.contains("supported: false"), "{output}"); + assert!(output.contains("mutating: false"), "{output}"); + assert!( + output.contains("ROCM_CLI_AMDGPU_DRIVER_VERSION"), + "{output}" + ); + assert!(output.contains("execution_commands: "), "{output}"); +} + #[when("the user approves the driver install on this Windows host")] async fn approve_windows_driver_install(world: &mut E2eWorld) { // On Linux/WSL, --yes approves a real package-manager plan. The host check diff --git a/tests/e2e-cucumber/tests/e2e/examine_steps.rs b/tests/e2e-cucumber/tests/e2e/examine_steps.rs index 0ab815f03c..42613a6cec 100644 --- a/tests/e2e-cucumber/tests/e2e/examine_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/examine_steps.rs @@ -201,11 +201,16 @@ async fn assert_driver_plan_repo_version_resolved(world: &mut E2eWorld) { !repo_version.contains("${"), "repo_version still shows an unresolved shell placeholder: {repo_version:?}\n{output}" ); + // The default, unoverridden value is the literal "latest" (ROCM_CLI_AMDGPU_DRIVER_VERSION + // is unset on a normal test host) -- not a numeric ROCm version -- so only + // non-empty and metacharacter-safe are asserted here, matching the same + // filter build_driver_install_plan_at applies before this value ever reaches + // a single-quoted shell command. + assert!(!repo_version.is_empty(), "repo_version is empty:\n{output}"); assert!( repo_version .chars() - .next() - .is_some_and(|first| first.is_ascii_digit()), + .all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)), "repo_version is not a concrete version string: {repo_version:?}\n{output}" ); } @@ -432,6 +437,34 @@ fn parsed_json(world: &E2eWorld) -> serde_json::Value { .unwrap_or_else(|e| panic!("`examine --json` did not emit valid JSON ({e}):\n{output}")) } +#[then("the inspection reports a current install-method name")] +async fn assert_install_method_is_current(world: &mut E2eWorld) { + let json = parsed_json(world); + let method = json + .get("rocm_install_method") + .and_then(serde_json::Value::as_str) + .unwrap_or_default(); + // `probe_rocm_install` used to name these "amdgpu-install" and + // "tarball-or-other"; a whitelist (rather than pinning one value) is what + // keeps this hermetic across hosts -- which branch actually fires here + // depends on this machine's real /etc/apt (etc.) ROCm repo markers, not + // just on the CLI-managed install this scenario plants, so a host that + // happens to have a real ROCm repo configured (this includes some + // developer machines and self-hosted GPU runners, not just a clean mock + // lane) legitimately resolves to "repo-native" instead of + // "runfile-or-tarball". Either current name passing and either retired + // name failing is exactly the coverage a revert of the rename needs. + // "zypper" is excluded: `probe_rocm_install` reports SUSE via its `rpm -q` + // branch, so it always yields "dnf", never "zypper". "none" is excluded + // too: this scenario plants an install, so "none" would mean the planted + // install went undetected, which is the regression this scenario exists + // to catch, not a value it should tolerate. + assert!( + matches!(method, "apt" | "dnf" | "repo-native" | "runfile-or-tarball"), + "expected a current rocm_install_method name, got {method:?}: {json}" + ); +} + #[when("the user inspects the system both for reading and for scripting")] async fn user_inspects_both_ways(world: &mut E2eWorld) { let (human, _, _) = crate::run_rocm(world, &["examine"]); @@ -1508,6 +1541,12 @@ async fn assert_reported_distribution( assert_eq!(inspection_str(&json, "/summary/distro"), name, "{json:#}"); } +#[then(expr = "the inspection reports the distribution family {string}")] +async fn assert_reported_distribution_family(world: &mut E2eWorld, like: String) { + let json = parsed_json(world); + assert_eq!(inspection_str(&json, "/distro_id_like"), like, "{json:#}"); +} + #[then("the inspection reports no trouble reading the release file")] async fn assert_os_release_read_cleanly(world: &mut E2eWorld) { let json = parsed_json(world);