From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 5E1381FF0B6 for ; Thu, 12 Nov 2026 15:15:34 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B7C6A21618; Thu, 12 Nov 2026 15:15:30 +0100 (CET) From: Filip Schauer To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox 17/26] oci: add a public pull_image_as_tar function Date: Thu, 12 Nov 2026 15:10:44 +0100 Message-ID: <20261112141109.137298-18-f.schauer@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261112141109.137298-1-f.schauer@proxmox.com> References: <20261112141109.137298-1-f.schauer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794492926915 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.185 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: IAYYJDS6YWWBDTZJJYFZCXPQMRAYQZLC X-Message-ID-Hash: IAYYJDS6YWWBDTZJJYFZCXPQMRAYQZLC X-MailFrom: f.schauer@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Signed-off-by: Filip Schauer --- proxmox-oci/Cargo.toml | 1 + proxmox-oci/debian/control | 2 + proxmox-oci/src/lib.rs | 111 +++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/proxmox-oci/Cargo.toml b/proxmox-oci/Cargo.toml index 45248658..87754e50 100644 --- a/proxmox-oci/Cargo.toml +++ b/proxmox-oci/Cargo.toml @@ -27,6 +27,7 @@ zstd.workspace = true proxmox-base64.workspace = true proxmox-http = { workspace = true, features = [ "client-sync" ] } proxmox-io.workspace = true +proxmox-lang.workspace = true [dev-dependencies] anyhow.workspace = true diff --git a/proxmox-oci/debian/control b/proxmox-oci/debian/control index 9ec73059..62dc2cd2 100644 --- a/proxmox-oci/debian/control +++ b/proxmox-oci/debian/control @@ -14,6 +14,7 @@ Build-Depends-Arch: cargo:native , librust-proxmox-http-1+client-sync-dev (>= 1.0.5-~~) , librust-proxmox-http-1+default-dev (>= 1.0.5-~~) , librust-proxmox-io-1+default-dev (>= 1.2.1-~~) , + librust-proxmox-lang-1+default-dev (>= 1.5-~~) , librust-serde-1+default-dev , librust-serde-json-1+default-dev , librust-sha2-0.10+default-dev , @@ -41,6 +42,7 @@ Depends: librust-proxmox-http-1+client-sync-dev (>= 1.0.5-~~), librust-proxmox-http-1+default-dev (>= 1.0.5-~~), librust-proxmox-io-1+default-dev (>= 1.2.1-~~), + librust-proxmox-lang-1+default-dev (>= 1.5-~~), librust-serde-1+default-dev, librust-serde-json-1+default-dev, librust-sha2-0.10+default-dev, diff --git a/proxmox-oci/src/lib.rs b/proxmox-oci/src/lib.rs index 672d81e0..542c9cee 100644 --- a/proxmox-oci/src/lib.rs +++ b/proxmox-oci/src/lib.rs @@ -1,9 +1,15 @@ +use std::fs::{File, remove_file}; +use std::io; use std::path::Path; +use oci_spec::OciSpecError; pub use oci_spec::distribution::Reference; pub use oci_spec::image::{Arch, Config, Os}; +use oci_spec::image::{Descriptor, ImageIndexBuilder, MediaType, OciLayoutBuilder}; use thiserror::Error; +use proxmox_lang::try_block; + pub mod client; use client::{Client, ClientConfig, OciClientError, RegistryAuth}; @@ -69,3 +75,108 @@ pub fn list_repository_tags( let tag_list = client.list_tags(reference, None, None)?; Ok(tag_list.tags().to_vec()) } + +#[derive(Debug, Error)] +pub enum PullTarError { + #[error(transparent)] + Client(#[from] OciClientError), + #[error(transparent)] + Io(#[from] io::Error), + #[error(transparent)] + OciSpec(#[from] OciSpecError), +} + +/// Pull an OCI image from a registry and save it as a tar archive. +/// +/// This removes the tar archive on error. +/// +/// # Arguments +/// +/// * `reference` - OCI reference specifying the image repository and tag/digest to pull +/// * `auth` - Authentication credentials/method to access the registry +/// * `tar_path` - Destination file path where the tar archive will be written to +pub fn pull_image_as_tar( + reference: &Reference, + auth: RegistryAuth, + tar_path: impl AsRef, +) -> Result<(), PullTarError> { + let mut client = Client::new(ClientConfig::default(), auth); + println!("Pulling image manifest and image config"); + let meta = client.pull_image_manifest_and_config(reference, None, None)?; + + let layout = OciLayoutBuilder::default() + .image_layout_version("1.0.0") + .build()?; + let manifest_descriptor = Descriptor::new( + MediaType::ImageManifest, + meta.manifest.raw.len() as u64, + meta.manifest.digest.clone(), + ); + let index = ImageIndexBuilder::default() + .schema_version(2u32) + .manifests([manifest_descriptor]) + .build()?; + + let oci_tar_file = File::create(&tar_path)?; + let mut tar = tar::Builder::new(oci_tar_file); + + try_block!({ + let mut layout_header = tar::Header::new_old(); + let mut layout_entry = tar.append_writer(&mut layout_header, "oci-layout")?; + layout.to_writer(&mut layout_entry)?; + layout_entry.finish()?; + + let mut index_header = tar::Header::new_old(); + let mut index_entry = tar.append_writer(&mut index_header, "index.json")?; + index.to_writer(&mut index_entry)?; + index_entry.finish()?; + + let mut manifest_header = tar::Header::new_old(); + manifest_header.set_size(meta.manifest.raw.len() as u64); + manifest_header.set_cksum(); + tar.append_data( + &mut manifest_header, + format!( + "blobs/{}/{}", + meta.manifest.digest.algorithm(), + meta.manifest.digest.digest() + ), + &meta.manifest.raw[..], + )?; + + let mut config_header = tar::Header::new_old(); + config_header.set_size(meta.config.raw.len() as u64); + config_header.set_cksum(); + tar.append_data( + &mut config_header, + format!( + "blobs/{}/{}", + meta.config.digest.algorithm(), + meta.config.digest.digest() + ), + &meta.config.raw[..], + )?; + + for layer in meta.manifest.manifest.layers() { + let layer_digest = layer.digest(); + println!("Pulling blob {layer_digest}"); + let mut layer_header = tar::Header::new_old(); + let mut layer_entry = tar.append_writer( + &mut layer_header, + format!( + "blobs/{}/{}", + layer_digest.algorithm(), + layer_digest.digest() + ), + )?; + client.pull_blob(reference, layer, &mut layer_entry)?; + layer_entry.finish()?; + } + + Ok(tar.finish()?) + }) + .inspect_err(|_| { + drop(tar); + let _ = remove_file(tar_path); + }) +} -- 2.47.3