Skip to content

[7/n] [installinator] write out zone hashes to mupdate-override.json #8155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion common/src/update/mupdate_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use std::collections::BTreeSet;

use id_map::{IdMap, IdMappable};
use omicron_uuid_kinds::MupdateOverrideUuid;
use serde::{Deserialize, Serialize};
use tufaceous_artifact::ArtifactHashId;
use tufaceous_artifact::{ArtifactHash, ArtifactHashId};

/// MUPdate override information, typically serialized as JSON (RFD 556).
///
Expand All @@ -20,10 +21,39 @@ pub struct MupdateOverrideInfo {
pub mupdate_uuid: MupdateOverrideUuid,

/// Artifact hashes written out to the install dataset.
///
/// Currently includes the host phase 2 and composite control plane
/// artifacts. Information about individual zones is included in
/// [`Self::zones`].
pub hash_ids: BTreeSet<ArtifactHashId>,

/// Control plane zone file names and hashes.
pub zones: IdMap<MupdateOverrideZone>,
}

impl MupdateOverrideInfo {
/// The name of the file on the install dataset.
pub const FILE_NAME: &'static str = "mupdate-override.json";
}

/// Control plane zone information written out to the install dataset.
///
/// Part of [`MupdateOverrideInfo`].
#[derive(
Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize,
)]
pub struct MupdateOverrideZone {
/// The file name.
pub file_name: String,

/// The hash of the file.
pub hash: ArtifactHash,
}

impl IdMappable for MupdateOverrideZone {
type Id = String;

fn id(&self) -> Self::Id {
self.file_name.clone()
}
}
3 changes: 3 additions & 0 deletions installinator-common/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use schemars::{
use serde::{Deserialize, Serialize};
use serde_with::rust::deserialize_ignore_any;
use thiserror::Error;
use tokio::task::JoinError;
use update_engine::{AsError, StepSpec, errors::NestedEngineError};

// ---
Expand Down Expand Up @@ -239,6 +240,8 @@ pub enum WriteError {
ChecksumValidationError(#[source] anyhow::Error),
#[error("error removing files from {path}: {error}")]
RemoveFilesError { path: Utf8PathBuf, error: std::io::Error },
#[error("error computing control plane hashes")]
ControlPlaneHashComputeError(#[source] JoinError),
#[error("error fsyncing output directory: {error}")]
SyncOutputDirError { error: std::io::Error },
#[error("error interacting with zpool: {error}")]
Expand Down
1 change: 1 addition & 0 deletions installinator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ display-error-chain.workspace = true
futures.workspace = true
hex.workspace = true
http.workspace = true
id-map.workspace = true
illumos-utils.workspace = true
installinator-client.workspace = true
installinator-common.workspace = true
Expand Down
59 changes: 49 additions & 10 deletions installinator/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ use std::{
use anyhow::{Context, Result, anyhow, ensure};
use async_trait::async_trait;
use buf_list::BufList;
use bytes::Buf;
use bytes::{Buf, Bytes};
use camino::{Utf8Path, Utf8PathBuf};
use id_map::IdMap;
use illumos_utils::zpool::{Zpool, ZpoolName};
use installinator_common::{
ControlPlaneZonesSpec, ControlPlaneZonesStepId, RawDiskWriter, StepContext,
StepProgress, StepResult, StepSuccess, UpdateEngine, WriteComponent,
WriteError, WriteOutput, WriteSpec, WriteStepId,
};
use omicron_common::{disk::M2Slot, update::MupdateOverrideInfo};
use omicron_common::{
disk::M2Slot,
update::{MupdateOverrideInfo, MupdateOverrideZone},
};
use omicron_uuid_kinds::MupdateOverrideUuid;
use sha2::{Digest, Sha256};
use slog::{Logger, info, warn};
use tokio::{
fs::File,
io::{AsyncWrite, AsyncWriteExt},
task::{JoinError, JoinSet},
};
use tufaceous_artifact::{ArtifactHash, ArtifactHashId};
use tufaceous_lib::ControlPlaneZoneImages;
Expand Down Expand Up @@ -677,8 +682,12 @@ impl ControlPlaneZoneWriteContext<'_> {
"Writing MUPdate override file",
async move |cx| {
let transport = transport.into_value(cx.token()).await;
let mupdate_json =
self.mupdate_override_artifact(mupdate_uuid);
let mupdate_json = self
.mupdate_override_artifact(mupdate_uuid)
.await
.map_err(|error| {
WriteError::ControlPlaneHashComputeError(error)
})?;

let out_path = self
.output_directory
Expand Down Expand Up @@ -759,21 +768,51 @@ impl ControlPlaneZoneWriteContext<'_> {
.register();
}

fn mupdate_override_artifact(
async fn mupdate_override_artifact(
&self,
mupdate_uuid: MupdateOverrideUuid,
) -> BufList {
// Might be worth writing out individual hash IDs for each zone in the
// future.
) -> Result<BufList, JoinError> {
let hash_ids =
[self.host_phase_2_id.clone(), self.control_plane_id.clone()]
.into_iter()
.collect();
let mupdate_override = MupdateOverrideInfo { mupdate_uuid, hash_ids };
let zones = compute_zone_hashes(&self.zones).await?;

let mupdate_override =
MupdateOverrideInfo { mupdate_uuid, hash_ids, zones };
let json_bytes = serde_json::to_vec(&mupdate_override)
.expect("this serialization is infallible");
BufList::from(json_bytes)
Ok(BufList::from(json_bytes))
}
}

/// Computes the zone hash IDs.
///
/// Hash computation is done in parallel on blocking tasks. If the runtime shuts
/// down causing a task abort, or a task panics (should not happen in normal
/// use), a `JoinError` is returned.
async fn compute_zone_hashes(
images: &ControlPlaneZoneImages,
) -> Result<IdMap<MupdateOverrideZone>, JoinError> {
let mut tasks = JoinSet::new();
for (file_name, data) in &images.zones {
let file_name = file_name.clone();
// data is a Bytes so is cheap to clone.
let data: Bytes = data.clone();
// Compute hashes in parallel.
tasks.spawn_blocking(move || {
let mut hasher = Sha256::new();
hasher.update(data);
let hash = hasher.finalize();
MupdateOverrideZone { file_name, hash: ArtifactHash(hash.into()) }
});
}

let mut output = IdMap::new();
while let Some(res) = tasks.join_next().await {
output.insert(res?);
}
Ok(output)
}

fn remove_contents_of(path: &Utf8Path) -> io::Result<()> {
Expand Down
2 changes: 2 additions & 0 deletions sled-agent/zone-images/src/mupdate_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,13 +970,15 @@ mod tests {
MupdateOverrideInfo {
mupdate_uuid: OVERRIDE_UUID,
hash_ids: BTreeSet::new(),
zones: IdMap::new(),
}
}

fn override_info_2() -> MupdateOverrideInfo {
MupdateOverrideInfo {
mupdate_uuid: OVERRIDE_2_UUID,
hash_ids: BTreeSet::new(),
zones: IdMap::new(),
}
}

Expand Down
12 changes: 12 additions & 0 deletions wicketd/tests/integration_tests/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ async fn test_installinator_fetch() {
"mupdate override info matches across A and B drives",
);

// Check that the zone1 and zone2 images are present in the zone set. (The
// names come from fake-non-semver.toml, under
// [artifact.control-plane.source]).
assert!(
a_override_info.zones.contains_key("zone1.tar.gz"),
"zone1 is present in the zone set"
);
assert!(
a_override_info.zones.contains_key("zone2.tar.gz"),
"zone2 is present in the zone set"
);

recv_handle.await.expect("recv_handle succeeded");

wicketd_testctx.teardown().await;
Expand Down
Loading