|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! Behaviors of the config reconciler in the presence of mupdate overrides. |
| 6 | +
|
| 7 | +use crate::InternalDisks; |
| 8 | +use crate::host_phase_2::HostPhase2PreparedContents; |
| 9 | +use camino::Utf8PathBuf; |
| 10 | +use nexus_sled_agent_shared::inventory::HostPhase2DesiredContents; |
| 11 | +use nexus_sled_agent_shared::inventory::OmicronZoneConfig; |
| 12 | +use nexus_sled_agent_shared::inventory::OmicronZoneImageSource; |
| 13 | +use nexus_sled_agent_shared::inventory::ZoneKind; |
| 14 | +use omicron_common::zone_images::ZoneImageFileSource; |
| 15 | +use sled_agent_types::zone_images::OmicronZoneFileSource; |
| 16 | +use sled_agent_types::zone_images::OmicronZoneImageLocation; |
| 17 | +use sled_agent_types::zone_images::PreparedOmicronZone; |
| 18 | +use sled_agent_types::zone_images::RAMDISK_IMAGE_PATH; |
| 19 | +use sled_agent_types::zone_images::ResolverStatus; |
| 20 | +use sled_agent_types::zone_images::ZoneImageLocationError; |
| 21 | +use slog::error; |
| 22 | +use slog_error_chain::InlineErrorChain; |
| 23 | +use tufaceous_artifact::ArtifactHash; |
| 24 | + |
| 25 | +/// An extension trait for `ResolverStatus`. |
| 26 | +/// |
| 27 | +/// This trait refers to types that aren't available within `sled-agent-types`. |
| 28 | +pub trait ResolverStatusExt { |
| 29 | + /// Look up the file source for an Omicron zone. |
| 30 | + fn omicron_file_source( |
| 31 | + &self, |
| 32 | + log: &slog::Logger, |
| 33 | + zone_kind: ZoneKind, |
| 34 | + image_source: &OmicronZoneImageSource, |
| 35 | + internal_disks: &InternalDisks, |
| 36 | + ) -> OmicronZoneFileSource; |
| 37 | + |
| 38 | + /// Prepare an Omicron zone for installation. |
| 39 | + fn prepare_omicron_zone<'a>( |
| 40 | + &self, |
| 41 | + log: &slog::Logger, |
| 42 | + zone_config: &'a OmicronZoneConfig, |
| 43 | + internal_disks: &InternalDisks, |
| 44 | + ) -> PreparedOmicronZone<'a> { |
| 45 | + let file_source = self.omicron_file_source( |
| 46 | + log, |
| 47 | + zone_config.zone_type.kind(), |
| 48 | + &zone_config.image_source, |
| 49 | + internal_disks, |
| 50 | + ); |
| 51 | + PreparedOmicronZone::new(zone_config, file_source) |
| 52 | + } |
| 53 | + |
| 54 | + fn prepare_host_phase_2_contents<'a>( |
| 55 | + &self, |
| 56 | + log: &slog::Logger, |
| 57 | + desired: &'a HostPhase2DesiredContents, |
| 58 | + ) -> HostPhase2PreparedContents<'a>; |
| 59 | +} |
| 60 | + |
| 61 | +impl ResolverStatusExt for ResolverStatus { |
| 62 | + fn omicron_file_source( |
| 63 | + &self, |
| 64 | + log: &slog::Logger, |
| 65 | + zone_kind: ZoneKind, |
| 66 | + image_source: &OmicronZoneImageSource, |
| 67 | + internal_disks: &InternalDisks, |
| 68 | + ) -> OmicronZoneFileSource { |
| 69 | + match image_source { |
| 70 | + OmicronZoneImageSource::InstallDataset => { |
| 71 | + let file_name = zone_kind.artifact_in_install_dataset(); |
| 72 | + |
| 73 | + // There's always at least one image path (the RAM disk below). |
| 74 | + let mut search_paths = Vec::with_capacity(1); |
| 75 | + |
| 76 | + // Inject an image path if requested by a test. |
| 77 | + if let Some(path) = &self.image_directory_override { |
| 78 | + search_paths.push(path.clone()); |
| 79 | + }; |
| 80 | + |
| 81 | + // Any zones not part of the RAM disk are managed via the zone |
| 82 | + // manifest. |
| 83 | + let hash = install_dataset_hash( |
| 84 | + log, |
| 85 | + self, |
| 86 | + zone_kind, |
| 87 | + internal_disks, |
| 88 | + |path| search_paths.push(path), |
| 89 | + ); |
| 90 | + |
| 91 | + // Look for the image in the RAM disk as a fallback. Note that |
| 92 | + // install dataset images are not stored on the RAM disk in |
| 93 | + // production, just in development or test workflows. |
| 94 | + search_paths.push(Utf8PathBuf::from(RAMDISK_IMAGE_PATH)); |
| 95 | + |
| 96 | + OmicronZoneFileSource { |
| 97 | + location: OmicronZoneImageLocation::InstallDataset { hash }, |
| 98 | + file_source: ZoneImageFileSource { |
| 99 | + file_name: file_name.to_owned(), |
| 100 | + search_paths, |
| 101 | + }, |
| 102 | + } |
| 103 | + } |
| 104 | + OmicronZoneImageSource::Artifact { hash } => { |
| 105 | + // TODO: implement mupdate override here. |
| 106 | + // |
| 107 | + // Search both artifact datasets. This iterator starts with the |
| 108 | + // dataset for the boot disk (if it exists), and then is followed |
| 109 | + // by all other disks. |
| 110 | + let search_paths = |
| 111 | + internal_disks.all_artifact_datasets().collect(); |
| 112 | + OmicronZoneFileSource { |
| 113 | + // TODO: with mupdate overrides, return InstallDataset here |
| 114 | + location: OmicronZoneImageLocation::Artifact { |
| 115 | + hash: Ok(*hash), |
| 116 | + }, |
| 117 | + file_source: ZoneImageFileSource { |
| 118 | + file_name: hash.to_string(), |
| 119 | + search_paths, |
| 120 | + }, |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + fn prepare_host_phase_2_contents<'a>( |
| 127 | + &self, |
| 128 | + #[expect(unused)] log: &slog::Logger, |
| 129 | + desired: &'a HostPhase2DesiredContents, |
| 130 | + ) -> HostPhase2PreparedContents<'a> { |
| 131 | + // TODO: Implement mupdate override logic. |
| 132 | + HostPhase2PreparedContents::NoMupdateOverride(desired) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +fn install_dataset_hash<F>( |
| 137 | + log: &slog::Logger, |
| 138 | + resolver_status: &ResolverStatus, |
| 139 | + zone_kind: ZoneKind, |
| 140 | + internal_disks: &InternalDisks, |
| 141 | + mut search_paths_cb: F, |
| 142 | +) -> Result<ArtifactHash, ZoneImageLocationError> |
| 143 | +where |
| 144 | + F: FnMut(Utf8PathBuf), |
| 145 | +{ |
| 146 | + // XXX: we ask for the boot zpool to be passed in here. But |
| 147 | + // `ResolverStatus` also caches the boot zpool. How should we |
| 148 | + // reconcile the two? |
| 149 | + let hash = if let Some(path) = internal_disks.boot_disk_install_dataset() { |
| 150 | + let hash = resolver_status.zone_manifest.zone_hash(zone_kind); |
| 151 | + match hash { |
| 152 | + Ok(hash) => { |
| 153 | + search_paths_cb(path); |
| 154 | + Ok(hash) |
| 155 | + } |
| 156 | + Err(error) => { |
| 157 | + error!( |
| 158 | + log, |
| 159 | + "zone {} not found in the boot disk zone manifest, \ |
| 160 | + not returning it as a source", |
| 161 | + zone_kind.report_str(); |
| 162 | + "file_name" => zone_kind.artifact_in_install_dataset(), |
| 163 | + "error" => InlineErrorChain::new(&error), |
| 164 | + ); |
| 165 | + Err(ZoneImageLocationError::ZoneHash(error)) |
| 166 | + } |
| 167 | + } |
| 168 | + } else { |
| 169 | + // The boot disk is not available, so we cannot add the |
| 170 | + // install dataset path from it. |
| 171 | + error!( |
| 172 | + log, |
| 173 | + "boot disk install dataset not available, \ |
| 174 | + not returning it as a source"; |
| 175 | + "zone_kind" => zone_kind.report_str(), |
| 176 | + ); |
| 177 | + Err(ZoneImageLocationError::BootDiskMissing) |
| 178 | + }; |
| 179 | + hash |
| 180 | +} |
| 181 | + |
| 182 | +// Tests for this module live inside sled-agent-zone-images. (This is a bit |
| 183 | +// weird and should probably be addressed at some point.) |
0 commit comments