From dc77df4492deed90856824f12bb7dd1279da6c57 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 7 Feb 2025 23:06:53 +0000 Subject: [PATCH] oci: Add a simple way how to create an OCI image Signed-off-by: Michael Tremer --- Makefile.am | 2 + src/cli/lib/image_create.c | 48 +++++++++++++++++++----- src/pakfire/oci.c | 75 ++++++++++++++++++++++++++++++++++++++ src/pakfire/oci.h | 30 +++++++++++++++ 4 files changed, 146 insertions(+), 9 deletions(-) create mode 100644 src/pakfire/oci.c create mode 100644 src/pakfire/oci.h diff --git a/Makefile.am b/Makefile.am index cddc264c..6813e47c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -253,6 +253,8 @@ libpakfire_la_SOURCES = \ src/pakfire/mirrorlist.h \ src/pakfire/mount.c \ src/pakfire/mount.h \ + src/pakfire/oci.c \ + src/pakfire/oci.h \ src/pakfire/os.c \ src/pakfire/os.h \ src/pakfire/package.c \ diff --git a/src/cli/lib/image_create.c b/src/cli/lib/image_create.c index e82d7670..f422b7e0 100644 --- a/src/cli/lib/image_create.c +++ b/src/cli/lib/image_create.c @@ -21,7 +21,9 @@ #include #include +#include #include +#include #include "command.h" #include "image_create.h" @@ -37,6 +39,11 @@ static const char* args_doc = "image create TYPE PATH"; static const char* doc = "Creates images"; +const char* packages[] = { + "system-release", + NULL, +}; + static error_t parse(int key, char* arg, struct argp_state* state, void* data) { struct cli_local_args* args = data; @@ -61,6 +68,7 @@ int cli_image_create(void* data, int argc, char* argv[]) { struct cli_global_args* global_args = data; struct cli_local_args local_args = {}; struct pakfire_build* build = NULL; + struct pakfire* pakfire = NULL; FILE* f = NULL; int r; @@ -76,19 +84,41 @@ int cli_image_create(void* data, int argc, char* argv[]) { goto ERROR; } - // Setup the build environment - r = cli_setup_build(&build, global_args, 0); - if (r < 0) - goto ERROR; - - // Create the image - r = pakfire_build_mkimage(build, local_args.type, f); - if (r) - goto ERROR; + // Handle OCI images separately + if (pakfire_string_equals(local_args.type, "oci")) { + // Setup Pakfire + r = cli_setup_pakfire(&pakfire, global_args); + if (r < 0) + goto ERROR; + + // Install everything + r = pakfire_install(pakfire, packages); + if (r < 0) + goto ERROR; + + // Create the image + r = pakfire_oci_mkimage(pakfire, f); + if (r < 0) + goto ERROR; + + // For the rest, launch a build environment + } else { + // Setup the build environment + r = cli_setup_build(&build, global_args, 0); + if (r < 0) + goto ERROR; + + // Create the image + r = pakfire_build_mkimage(build, local_args.type, f); + if (r) + goto ERROR; + } ERROR: if (build) pakfire_build_unref(build); + if (pakfire) + pakfire_unref(pakfire); if (f) fclose(f); diff --git a/src/pakfire/oci.c b/src/pakfire/oci.c new file mode 100644 index 00000000..70811a1f --- /dev/null +++ b/src/pakfire/oci.c @@ -0,0 +1,75 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +int pakfire_oci_mkimage(struct pakfire* pakfire, FILE* f) { + struct pakfire_filelist* filelist = NULL; + struct archive* archive = NULL; + int r; + + // Fetch the pakfire root + const char* root = pakfire_get_path(pakfire); + + // Create a new filelist + r = pakfire_filelist_create(&filelist, pakfire); + if (r < 0) + goto ERROR; + + // Scan for all files + r = pakfire_filelist_scan(filelist, root, NULL, NULL, 0); + if (r < 0) + goto ERROR; + + // Create a new archive + r = pakfire_compress_create_archive(pakfire, &archive, f, PAKFIRE_COMPRESS_GZIP, 9); + if (r < 0) + goto ERROR; + + // Write all files to the image + r = pakfire_compress(pakfire, archive, filelist, + _("Creating Image"), PAKFIRE_COMPRESS_SHOW_THROUGHPUT, 0); + if (r < 0) + goto ERROR; + + // Close archive + r = archive_write_close(archive); + if (r) { + //ERROR(pakfire, "Could not close archive: %s\n", archive_error_string(archive)); + goto ERROR; + } + +ERROR: + if (filelist) + pakfire_filelist_unref(filelist); + if (archive) + archive_free(archive); + + return r; +} diff --git a/src/pakfire/oci.h b/src/pakfire/oci.h new file mode 100644 index 00000000..27da5101 --- /dev/null +++ b/src/pakfire/oci.h @@ -0,0 +1,30 @@ +/*############################################################################# +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_OCI_H +#define PAKFIRE_OCI_H + +#include + +#include + +int pakfire_oci_mkimage(struct pakfire* pakfire, FILE* f); + +#endif /* PAKFIRE_OCI_H */ -- 2.39.5