]> git.ipfire.org Git - pakfire.git/commitdiff
oci: Add a simple way how to create an OCI image
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 7 Feb 2025 23:06:53 +0000 (23:06 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 7 Feb 2025 23:06:53 +0000 (23:06 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/cli/lib/image_create.c
src/pakfire/oci.c [new file with mode: 0644]
src/pakfire/oci.h [new file with mode: 0644]

index cddc264cb6c981182ffd764108f351823db80ae3..6813e47c6a456c79450fbd2d3b77f9b5e9347502 100644 (file)
@@ -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 \
index e82d7670bd2285e88e5770404bee34bf4ecc5c48..f422b7e0a8ef189051e24dbd25c138e43818b452 100644 (file)
@@ -21,7 +21,9 @@
 #include <argp.h>
 
 #include <pakfire/build.h>
+#include <pakfire/oci.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/string.h>
 
 #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 (file)
index 0000000..70811a1
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pakfire/compress.h>
+#include <pakfire/filelist.h>
+#include <pakfire/i18n.h>
+#include <pakfire/logging.h>
+#include <pakfire/oci.h>
+#include <pakfire/pakfire.h>
+
+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 (file)
index 0000000..27da510
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_OCI_H
+#define PAKFIRE_OCI_H
+
+#include <stdio.h>
+
+#include <pakfire/pakfire.h>
+
+int pakfire_oci_mkimage(struct pakfire* pakfire, FILE* f);
+
+#endif /* PAKFIRE_OCI_H */