Skip to content
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

Ensure container image layers are pruned #4720

Merged
merged 1 commit into from
Dec 12, 2023
Merged
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
38 changes: 35 additions & 3 deletions rpmostree-cxxrs.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,7 @@ enum class BubblewrapMutability : ::std::uint8_t;
struct Bubblewrap;
struct ContainerImageState;
struct ExportedManifestDiff;
struct PrunedContainerInfo;
enum class RefspecType : ::std::uint8_t;
struct TempEtcGuard;
struct FilesystemScriptPrep;
Expand Down Expand Up @@ -1438,6 +1439,19 @@ struct ContainerImageState final
};
#endif // CXXBRIDGE1_STRUCT_rpmostreecxx$ContainerImageState

#ifndef CXXBRIDGE1_STRUCT_rpmostreecxx$PrunedContainerInfo
#define CXXBRIDGE1_STRUCT_rpmostreecxx$PrunedContainerInfo
struct PrunedContainerInfo final
{
::std::uint32_t images;
::std::uint32_t layers;

bool operator== (PrunedContainerInfo const &) const noexcept;
bool operator!= (PrunedContainerInfo const &) const noexcept;
using IsRelocatable = ::std::true_type;
};
#endif // CXXBRIDGE1_STRUCT_rpmostreecxx$PrunedContainerInfo

#ifndef CXXBRIDGE1_ENUM_rpmostreecxx$RefspecType
#define CXXBRIDGE1_ENUM_rpmostreecxx$RefspecType
enum class RefspecType : ::std::uint8_t
Expand Down Expand Up @@ -2084,13 +2098,17 @@ extern "C"

::rust::repr::PtrLen
rpmostreecxx$cxxbridge1$deploy_from_self_entrypoint (::rust::Vec< ::rust::String> *args) noexcept;
bool
rpmostreecxx$cxxbridge1$PrunedContainerInfo$operator$eq (PrunedContainerInfo const &,
PrunedContainerInfo const &) noexcept;

::rust::repr::PtrLen rpmostreecxx$cxxbridge1$pull_container (
::rpmostreecxx::OstreeRepo const &repo, ::rpmostreecxx::GCancellable const &cancellable,
::rust::Str imgref, ::rust::Box< ::rpmostreecxx::ContainerImageState> *return$) noexcept;

::rust::repr::PtrLen
rpmostreecxx$cxxbridge1$container_prune (::rpmostreecxx::OstreeSysroot const &sysroot) noexcept;
rpmostreecxx$cxxbridge1$container_prune (::rpmostreecxx::OstreeSysroot const &sysroot,
::rpmostreecxx::PrunedContainerInfo *return$) noexcept;

::rust::repr::PtrLen rpmostreecxx$cxxbridge1$query_container_image_commit (
::rpmostreecxx::OstreeRepo const &repo, ::rust::Str c,
Expand Down Expand Up @@ -3682,6 +3700,18 @@ deploy_from_self_entrypoint (::rust::Vec< ::rust::String> args)
}
}

bool
PrunedContainerInfo::operator== (PrunedContainerInfo const &rhs) const noexcept
{
return rpmostreecxx$cxxbridge1$PrunedContainerInfo$operator$eq (*this, rhs);
}

bool
PrunedContainerInfo::operator!= (PrunedContainerInfo const &rhs) const noexcept
{
return !(*this == rhs);
}

::rust::Box< ::rpmostreecxx::ContainerImageState>
pull_container (::rpmostreecxx::OstreeRepo const &repo,
::rpmostreecxx::GCancellable const &cancellable, ::rust::Str imgref)
Expand All @@ -3696,14 +3726,16 @@ pull_container (::rpmostreecxx::OstreeRepo const &repo,
return ::std::move (return$.value);
}

void
::rpmostreecxx::PrunedContainerInfo
container_prune (::rpmostreecxx::OstreeSysroot const &sysroot)
{
::rust::repr::PtrLen error$ = rpmostreecxx$cxxbridge1$container_prune (sysroot);
::rust::MaybeUninit< ::rpmostreecxx::PrunedContainerInfo> return$;
::rust::repr::PtrLen error$ = rpmostreecxx$cxxbridge1$container_prune (sysroot, &return$.value);
if (error$.ptr)
{
throw ::rust::impl< ::rust::Error>::error (error$);
}
return ::std::move (return$.value);
}

::rust::Box< ::rpmostreecxx::ContainerImageState>
Expand Down
16 changes: 15 additions & 1 deletion rpmostree-cxxrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,7 @@ enum class BubblewrapMutability : ::std::uint8_t;
struct Bubblewrap;
struct ContainerImageState;
struct ExportedManifestDiff;
struct PrunedContainerInfo;
enum class RefspecType : ::std::uint8_t;
struct TempEtcGuard;
struct FilesystemScriptPrep;
Expand Down Expand Up @@ -1220,6 +1221,19 @@ struct ContainerImageState final
};
#endif // CXXBRIDGE1_STRUCT_rpmostreecxx$ContainerImageState

#ifndef CXXBRIDGE1_STRUCT_rpmostreecxx$PrunedContainerInfo
#define CXXBRIDGE1_STRUCT_rpmostreecxx$PrunedContainerInfo
struct PrunedContainerInfo final
{
::std::uint32_t images;
::std::uint32_t layers;

bool operator== (PrunedContainerInfo const &) const noexcept;
bool operator!= (PrunedContainerInfo const &) const noexcept;
using IsRelocatable = ::std::true_type;
};
#endif // CXXBRIDGE1_STRUCT_rpmostreecxx$PrunedContainerInfo

#ifndef CXXBRIDGE1_ENUM_rpmostreecxx$RefspecType
#define CXXBRIDGE1_ENUM_rpmostreecxx$RefspecType
enum class RefspecType : ::std::uint8_t
Expand Down Expand Up @@ -1793,7 +1807,7 @@ ::rust::Box< ::rpmostreecxx::ContainerImageState>
pull_container (::rpmostreecxx::OstreeRepo const &repo,
::rpmostreecxx::GCancellable const &cancellable, ::rust::Str imgref);

void container_prune (::rpmostreecxx::OstreeSysroot const &sysroot);
::rpmostreecxx::PrunedContainerInfo container_prune (::rpmostreecxx::OstreeSysroot const &sysroot);

::rust::Box< ::rpmostreecxx::ContainerImageState>
query_container_image_commit (::rpmostreecxx::OstreeRepo const &repo, ::rust::Str c);
Expand Down
8 changes: 7 additions & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,20 @@ pub mod ffi {
pub added_size: u64,
}

#[derive(Debug, PartialEq, Eq)]
struct PrunedContainerInfo {
images: u32,
layers: u32,
}

// sysroot_upgrade.rs
extern "Rust" {
fn pull_container(
repo: &OstreeRepo,
cancellable: &GCancellable,
imgref: &str,
) -> Result<Box<ContainerImageState>>;
fn container_prune(sysroot: &OstreeSysroot) -> Result<()>;
fn container_prune(sysroot: &OstreeSysroot) -> Result<PrunedContainerInfo>;
fn query_container_image_commit(
repo: &OstreeRepo,
c: &str,
Expand Down
11 changes: 8 additions & 3 deletions rust/src/sysroot_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,18 @@ pub(crate) fn pull_container(
}

/// Run a prune of container images that are not referenced by a deployment.
pub(crate) fn container_prune(sysroot: &crate::FFIOstreeSysroot) -> CxxResult<()> {
pub(crate) fn container_prune(
sysroot: &crate::FFIOstreeSysroot,
) -> CxxResult<crate::ffi::PrunedContainerInfo> {
let sysroot = &sysroot.glib_reborrow();
tracing::debug!("Pruning container images");
crate::try_fail_point!("sysroot-upgrade::container-prune");
let sysroot = &ostree_ext::sysroot::SysrootLock::from_assumed_locked(sysroot);
ostree_container::deploy::remove_undeployed_images(sysroot).context("Pruning images")?;
Ok(())
let images = ostree_container::deploy::remove_undeployed_images(sysroot)
.context("Pruning images")?
.len() as u32;
let layers = ostree_container::store::gc_image_layers(&sysroot.repo())?;
Ok(crate::ffi::PrunedContainerInfo { images, layers })
}

/// C++ wrapper for querying image state; requires a pulled image
Expand Down
7 changes: 6 additions & 1 deletion src/daemon/rpmostree-sysroot-core.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ rpmostree_syscore_cleanup (OstreeSysroot *sysroot, OstreeRepo *repo, GCancellabl
/* Refs for the live state */
ROSCXX_TRY (applylive_sync_ref (*sysroot), error);

CXX_TRY (rpmostreecxx::container_prune (*sysroot), error);
CXX_TRY_VAR (image_pruned, rpmostreecxx::container_prune (*sysroot), error);
if (image_pruned.images > 0 || image_pruned.layers > 0)
{
rpmostree_output_message ("Pruned images: %u (layers: %u)", image_pruned.images,
image_pruned.layers);
}

/* And do a prune */
guint64 freed_space;
Expand Down
5 changes: 5 additions & 0 deletions tests/kolainst/destructive/container-image
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
assert_streq "$(wc -l < imglist.txt)" 1

# Now revert back to the base image, but keep our layered package foo
echo $(ostree refs ostree/container/blob | wc -l) > /etc/previous-layers
Dismissed Show dismissed Hide dismissed
rpm-ostree rebase ostree-unverified-image:containers-storage:localhost/fcos
/tmp/autopkgtest-reboot 4
;;
Expand All @@ -207,6 +208,10 @@
fi
done
rpmostree_assert_status ".deployments[0][\"container-image-reference\"] == \"ostree-unverified-image:containers-storage:localhost/fcos\""
rpm-ostree cleanup -r
previous_layers=$(cat /etc/previous-layers)
new_layers=$(ostree refs ostree/container/blob | wc -l)
test $new_layers -lt $previous_layers
;;
*) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac