erofs-utils: add OCI registry support
PATCH 1/1 erofs-utils: add OCI registry support
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
New mkfs.erofs option: --oci=registry/repo:tag[,options]
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)
- username/password (basic authentication)
--- configure.ac | 45 +++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 73 +++++ lib/remotes/oci.c | 665 +++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 155 ++++++++++- 5 files changed, 944 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +45 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,37 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -712,6 +752,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -784,6 +825,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -40,6 +40,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -71,6 +72,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +73 -0
73 lines not on this page · raw message
lib/remotes/oci.c new file +665 -0
665 lines not on this page · raw message
mkfs/main.c +154 -1
221 lines not on this page · raw message
Signature · 1 line
2.47.1
Hi Chengyu,
On 2025/8/21 15:32, ChengyuZhu6 wrote:
Thanks for the patch!
The author doesn't seem to be updated, you could
git commit --amend --author="Chengyu Zhu <[email protected]>"
or just use git config for global update.
Quoted 1 line
This patch adds support for building EROFS filesystems from OCI-compliant
72-char per line.
Quoted 11 lines
container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Quoted 6 lines
New mkfs.erofs option: --oci=registry/repo:tag[,options]
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)
Is it possible to enable anonymous access if username/password are not specified?
Quoted 4 lines
- username/password (basic authentication)
Signed-off-by Chengyu Zhu <[email protected]>Signed-off-by Changzhi Xie <[email protected]>
Your Signed-off-by needs to the last line of the commit message since you sent out the email:
Quoted 1 line
---
...
Quoted 26 lines
lib/liberofs_oci.h new file +20
-0@@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#ifndef __EROFS_OCI_H +#define __EROFS_OCI_H + +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +struct erofs_inode; + +/* OCI authentication modes */ +enum oci_auth_mode { + OCI_AUTH_ANONYMOUS, /* No authentication */ + OCI_AUTH_TOKEN, /* Bearer token authentication */
lib/liberofs_oci.h line 30
Quoted 10 lines
lib/liberofs_oci.h new file +10
-0+ OCI_AUTH_BASIC, /* Basic authentication */ +}; + +/* Maximum lengths for OCI parameters */ +#define OCI_REGISTRY_LEN 256 +#define OCI_REPOSITORY_LEN 256 +#define OCI_TAG_LEN 64 +#define OCI_PLATFORM_LEN 32 +#define OCI_USERNAME_LEN 64 +#define OCI_PASSWORD_LEN 256
Can we make those string as dynamic allocated?
Hard-coded strings look dangerous.
Quoted 4 lines
lib/liberofs_oci.h new file +4
-0+ +/** +* struct erofs_oci - OCI configuration +* @registry: registry hostname (e.g., "registry-1.docker.io")
lib/liberofs_oci.h line 44
Quoted 10 lines
lib/liberofs_oci.h new file +10
-0+* @repository: image repository (e.g., "library/ubuntu") +* @tag: image tag or digest (e.g., "latest" or sha256:...) +* @platform: target platform in "os/arch" format (e.g., "linux/amd64") +* @username: username for basic authentication +* @password: password for basic authentication +* @auth_mode: authentication mode to use +* @layer_index: specific layer to extract (-1 for all layers) +* @anonymous_access: whether to use anonymous access +*/ +struct erofs_oci {
is it possible to rename it as struct erofs_oci_params?
lib/liberofs_oci.h line 52
Quoted 8 lines
lib/liberofs_oci.h new file +8
-0+char registry[OCI_REGISTRY_LEN +1 ]; +char repository[OCI_REPOSITORY_LEN +1 ]; +char tag[OCI_TAG_LEN +1 ]; +char platform[OCI_PLATFORM_LEN +1 ]; +char username[OCI_USERNAME_LEN +1 ]; +char password[OCI_PASSWORD_LEN +1 ]; + +enum oci_auth_mode auth_mode;
What's the difference between
OCI_AUTH_ANONYMOUS
and
anonymous_access?
Quoted 61 lines
lib/liberofs_oci.h new file +21
-0+int layer_index; +bool anonymous_access; +}; + +/** +* ocierofs_build_trees - Build file trees from OCI container image layers +* @root: root inode to build the file tree under +* @oci: OCI configuration +* @ref: reference string (unused, kept for API compatibility) +* @fillzero: if true, only create inodes without downloading actual data +* +* Return: 0 on success, negative errno on failure +*/ +int ocierofs_build_trees (struct erofs_inode *root,struct erofs_oci *oci, +const char *ref,bool fillzero); + +#ifdef __cplusplus +} +#endif + +#endif /* __EROFS_OCI_H */ lib/remotes/oci.c new file +34
-0@@ -0,0 +1,665 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <curl/curl.h> +#include <json-c/json.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/diskbuf.h" +#include "erofs/rebuild.h" +#include "erofs/tar.h" +#include "liberofs_oci.h" + +/* Constants */ +#define OCI_URL_MAX_LEN 8192 +#define OCI_AUTH_HEADER_MAX_LEN 1024 +#define OCI_TEMP_FILENAME_MAX_LEN 256 + +/* Media types */ +#define DOCKER_MEDIATYPE_MANIFEST_V2 "application/vnd.docker.distribution.manifest.v2+json" +#define DOCKER_MEDIATYPE_MANIFEST_V1 "application/vnd.docker.distribution.manifest.v1+json" +#define DOCKER_MEDIATYPE_MANIFEST_LIST "application/vnd.docker.distribution.manifest.list.v2+json"
lib/remotes/oci.c line 44
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+#define OCI_MEDIATYPE_MANIFEST "application/vnd.oci.image.manifest.v1+json" +#define OCI_MEDIATYPE_INDEX "application/vnd.oci.image.index.v1+json" + +/* Registry constants */ +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" +#define QUAY_REGISTRY "quay.io" + +/* Global CURL handle */ +static CURL *g_curl;
and then make
struct erofs_oci {
CURL *curl;
struct erofs_oci_params *params;
};As a OCI context (it can be used for multiple instance usage such as runtime lazy loading.)
lib/remotes/oci.c line 50
Quoted 5 lines
lib/remotes/oci.c new file +5
-0+> +/* HTTP request/response structures */ +struct oci_request { +char url[OCI_URL_MAX_LEN ]; +struct curl_slist *headers; +};
struct erofs_oci_request {
};
lib/remotes/oci.c line 51
Quoted 2 lines
lib/remotes/oci.c new file +2
-0+ +struct oci_response {
erofs_oci_response...
lib/remotes/oci.c line 58
Quoted 6 lines
lib/remotes/oci.c new file +6
-0+char *data; +size_t size; +long http_code; +}; + +/* Layer information */
The comment can be droped since it's obvious.
lib/remotes/oci.c line 59
Quoted 1 line
lib/remotes/oci.c new file +1
-0+struct oci_layer {
erofs_oci_layer {
lib/remotes/oci.c line 66
Quoted 7 lines
lib/remotes/oci.c new file +7
-0+char *digest; +u64 size; +const char *media_type; +}; + +/* Layer streaming context */ +struct oci_stream {
Same here.
lib/remotes/oci.c line 67
Quoted 1 line
lib/remotes/oci.c new file +1
-0+struct erofs_tarfile tarfile;
what's the purpose of struct erofs_tarfile?
Quoted 91 lines
lib/remotes/oci.c new file +91
-0+FILE *temp_file; +char temp_filename[OCI_TEMP_FILENAME_MAX_LEN ]; +int layer_index; +}; + +/* Callback for writing response data to memory */ +static size_t oci_write_callback (void *contents,size_t size,size_t nmemb,void *userp) +{ +size_t realsize = size * nmemb; +struct oci_response *resp = userp; +char *ptr; + + ptr =realloc (resp->data, resp->size + realsize +1 ); +if (!ptr) +return 0 ; + + resp->data = ptr; +memcpy (&resp->data[resp->size], contents, realsize); + resp->size += realsize; + resp->data[resp->size] ='\0' ; +return realsize; +} + +/* Callback for writing layer data to file */ +static size_t oci_layer_write_callback (void *contents,size_t size,size_t nmemb,void *userp) +{ +struct oci_stream *stream = userp; +size_t realsize = size * nmemb; + +if (!stream->temp_file) +return 0 ; + +if (fwrite (contents,1 , realsize, stream->temp_file) != realsize) { +erofs_err ("failed to write layer data for layer %d" , stream->layer_index); +return 0 ; + } + +return realsize; +} + +/* Perform HTTP request */ +static int oci_request_perform (struct oci_request *req,struct oci_response *resp) +{ +CURLcode res; + +erofs_dbg ("requesting URL: %s" , req->url); + +curl_easy_setopt (g_curl,CURLOPT_URL , req->url); +curl_easy_setopt (g_curl,CURLOPT_WRITEDATA , resp); +curl_easy_setopt (g_curl,CURLOPT_WRITEFUNCTION , oci_write_callback); + +if (req->headers) +curl_easy_setopt (g_curl,CURLOPT_HTTPHEADER , req->headers); + + res =curl_easy_perform (g_curl); +if (res !=CURLE_OK ) { +erofs_err ("curl request failed: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + + res =curl_easy_getinfo (g_curl,CURLINFO_RESPONSE_CODE , &resp->http_code); +if (res !=CURLE_OK ) { +erofs_err ("failed to get HTTP response code: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + +if (resp->http_code <200 || resp->http_code >=300 ) { +erofs_err ("HTTP request failed with code %ld" , resp->http_code); +return -EIO ; + } + +return 0 ; +} + +/* Get authentication token */ +static char *oci_get_auth_token (const char *registry,const char *repository, +const char *username,const char *password) +{ +struct oci_request req = {}; +struct oci_response resp = {}; +json_object *root, *token_obj; +const char *token; +char *auth_header =NULL ; +int ret; + +if (!registry || !repository) +return ERR_PTR (-EINVAL ); + +if (!strcmp (registry,DOCKER_API_REGISTRY ) || !strcmp (registry,DOCKER_REGISTRY )) { + snprintf(req.url, sizeof(req.url), + "https://auth.docker.io/token?service=registry.docker.io&scope=repository:%s:pull",
lib/remotes/oci.c line 168
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ repository); + }else if (!strcmp (registry,QUAY_REGISTRY )) { +snprintf (req.url,sizeof (req.url), +"https://%s/v2/auth?service=%s&scope=repository:%s:pull" , +QUAY_REGISTRY ,QUAY_REGISTRY , repository); + }else { +snprintf (req.url,sizeof (req.url), +"https://%s/token?service=%s&scope=repository:%s:pull" , + registry, registry, repository); + }
You could use asprintf to allocate dynamic strings...
...
Quoted 23 lines
lib/remotes/oci.c new file +9
-1case 'V' :version ();@@ -1638,7 +1775,8 @@ int main(int argc, char **argv)erofs_uuid_generate (g_sbi.uuid);if ((source_mode ==EROFS_MKFS_SOURCE_TAR && !erofstar.index_mode) || - (source_mode ==EROFS_MKFS_SOURCE_S3 )) { + (source_mode ==EROFS_MKFS_SOURCE_S3 ) || + (source_mode ==EROFS_MKFS_SOURCE_OCI )) { err =erofs_diskbuf_init (1 );if (err) { erofs_err("failed to initialize diskbuf: %s",@@ -1756,12 +1894,27 @@ int main(int argc, char **argv) dataimport_mode == EROFS_MKFS_DATA_IMPORT_ZEROFILL);if (err) goto exit; +#endif +#ifdef OCIEROFS_ENABLED + }else if (source_mode ==EROFS_MKFS_SOURCE_OCI ) { +if (incremental_mode || + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_RVSP ) + err = -EOPNOTSUPP ; +else
lib/remotes/oci.c int main(int argc, char **argv) · line 1913
Quoted 10 lines
lib/remotes/oci.c new file +7
-0+ err =ocierofs_build_trees (root, &ocicfg, + cfg.c_src_path, + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_ZEROFILL ); +if (err) + goto exit; +erofs_info ("OCI build_trees completed, starting filesystem construction" );#endif } +erofs_info ("Starting erofs_rebuild_dump_tree..." );
The debugging info can be removed..
lib/remotes/oci.c int main(int argc, char **argv) · line 1917
Quoted 4 lines
lib/remotes/oci.c new file +1
-0err =erofs_rebuild_dump_tree (root, incremental_mode);if (err) goto exit; +erofs_info ("erofs_rebuild_dump_tree completed" );
Same here.
Thanks,
Gao Xiang
Hi Xiang,
Quoted 3 lines
The author doesn't seem to be updated, you could
git commit --amend --author="Chengyu Zhu <[email protected]>”
Done.
Quoted 3 lines
Quoted 1 line
This patch adds support for building EROFS filesystems from OCI-compliant
72-char per line.
Fixed.
Quoted 3 lines
Is it possible to enable anonymous access if username/password are not specified?
Yes, the new patch works.
Quoted 2 lines
Your
Signed-off-byneeds to the last line of the commit message since you sent out the email:
Fixed.
Quoted 2 lines
Can we make those string as dynamic allocated?
Hard-coded strings look dangerous.
Done.
Quoted 1 line
is it possible to rename it as
struct erofs_oci_params?
Done.
Quoted 4 lines
What's the difference between
OCI_AUTH_ANONYMOUS
andanonymous_access?
There isn’t a functional difference. anonymous_access was only used to capture the “anonymous” input, which duplicated OCI_AUTH_ANONYMOUS. I’ve removed anonymous_access and now rely solely on OCI_AUTH_ANONYMOUS.
Quoted 6 lines
and then make
struct erofs_oci { CURL *curl; struct erofs_oci_params *params; };
Done.
Quoted 3 lines
Quoted 1 line
+/* Layer information */
The comment can be droped since it's obvious.
Removed.
Quoted 7 lines
lib/remotes/oci.c line 168
Quoted 5 lines
lib/remotes/oci.c new file +5
-0+ }else { +snprintf (req.url,sizeof (req.url), +"https://%s/token?service=%s&scope=repository:%s:pull" , + registry, registry, repository); + }You could use asprintf to allocate dynamic strings…
Done.
Quoted 3 lines
lib/remotes/oci.c line 67
Quoted 1 line
lib/remotes/oci.c new file +1
-0+struct erofs_tarfile tarfile;what's the purpose of
struct erofs_tarfile?
It's used by the tarerofs_parse_tar() function to iterate through tar archive entries and extract file information.
Thanks,
Chengyu Zhu
Quoted 396 lines
2025年8月21日 15:52,Gao Xiang <[email protected]> 写道:
Hi Chengyu,
On 2025/8/21 15:32, ChengyuZhu6 wrote:
Thanks for the patch!
The author doesn't seem to be updated, you could
git commit --amend --author="Chengyu Zhu <[email protected]>"or just use
git configfor global update.Quoted 1 line
This patch adds support for building EROFS filesystems from OCI-compliant
72-char per line.
Quoted 6 lines
container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc. The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integrationQuoted 8 lines
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
New mkfs.erofs option: --oci=registry/repo:tag[,options] Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)Is it possible to enable anonymous access if username/password are not specified?
Quoted 3 lines
- username/password (basic authentication)
Signed-off-by Chengyu Zhu <[email protected]>Signed-off-by Changzhi Xie <[email protected]>Your
Signed-off-byneeds to the last line of the commit message since you sent out the email:Signed-off-by Changzhi Xie <[email protected]>Signed-off-by Chengyu Zhu <[email protected]>Quoted 1 line
---
...
Quoted 26 lines
lib/liberofs_oci.h new file +20
-0@@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#ifndef __EROFS_OCI_H +#define __EROFS_OCI_H + +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +struct erofs_inode; + +/* OCI authentication modes */ +enum oci_auth_mode { + OCI_AUTH_ANONYMOUS, /* No authentication */ + OCI_AUTH_TOKEN, /* Bearer token authentication */lib/liberofs_oci.h line 30
Quoted 10 lines
lib/liberofs_oci.h new file +10
-0+ OCI_AUTH_BASIC, /* Basic authentication */ +}; + +/* Maximum lengths for OCI parameters */ +#define OCI_REGISTRY_LEN 256 +#define OCI_REPOSITORY_LEN 256 +#define OCI_TAG_LEN 64 +#define OCI_PLATFORM_LEN 32 +#define OCI_USERNAME_LEN 64 +#define OCI_PASSWORD_LEN 256Can we make those string as dynamic allocated?
Hard-coded strings look dangerous.Quoted 4 lines
lib/liberofs_oci.h new file +4
-0+ +/** +* struct erofs_oci - OCI configuration +* @registry: registry hostname (e.g., "registry-1.docker.io") lib/liberofs_oci.h line 44
Quoted 10 lines
lib/liberofs_oci.h new file +10
-0+* @repository: image repository (e.g., "library/ubuntu") +* @tag: image tag or digest (e.g., "latest" or sha256:...) +* @platform: target platform in "os/arch" format (e.g., "linux/amd64") +* @username: username for basic authentication +* @password: password for basic authentication +* @auth_mode: authentication mode to use +* @layer_index: specific layer to extract (-1 for all layers) +* @anonymous_access: whether to use anonymous access +*/ +struct erofs_oci {is it possible to rename it as
struct erofs_oci_params?lib/liberofs_oci.h line 52
Quoted 8 lines
lib/liberofs_oci.h new file +8
-0+char registry[OCI_REGISTRY_LEN +1 ]; +char repository[OCI_REPOSITORY_LEN +1 ]; +char tag[OCI_TAG_LEN +1 ]; +char platform[OCI_PLATFORM_LEN +1 ]; +char username[OCI_USERNAME_LEN +1 ]; +char password[OCI_PASSWORD_LEN +1 ]; + +enum oci_auth_mode auth_mode;What's the difference between
OCI_AUTH_ANONYMOUS
andanonymous_access?Quoted 61 lines
lib/liberofs_oci.h new file +21
-0+int layer_index; +bool anonymous_access; +}; + +/** +* ocierofs_build_trees - Build file trees from OCI container image layers +* @root: root inode to build the file tree under +* @oci: OCI configuration +* @ref: reference string (unused, kept for API compatibility) +* @fillzero: if true, only create inodes without downloading actual data +* +* Return: 0 on success, negative errno on failure +*/ +int ocierofs_build_trees (struct erofs_inode *root,struct erofs_oci *oci, +const char *ref,bool fillzero); + +#ifdef __cplusplus +} +#endif + +#endif /* __EROFS_OCI_H */ lib/remotes/oci.c new file +34
-0@@ -0,0 +1,665 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <curl/curl.h> +#include <json-c/json.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/diskbuf.h" +#include "erofs/rebuild.h" +#include "erofs/tar.h" +#include "liberofs_oci.h" + +/* Constants */ +#define OCI_URL_MAX_LEN 8192 +#define OCI_AUTH_HEADER_MAX_LEN 1024 +#define OCI_TEMP_FILENAME_MAX_LEN 256 + +/* Media types */ +#define DOCKER_MEDIATYPE_MANIFEST_V2 "application/vnd.docker.distribution.manifest.v2+json" +#define DOCKER_MEDIATYPE_MANIFEST_V1 "application/vnd.docker.distribution.manifest.v1+json" +#define DOCKER_MEDIATYPE_MANIFEST_LIST "application/vnd.docker.distribution.manifest.list.v2+json"lib/remotes/oci.c line 44
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+#define OCI_MEDIATYPE_MANIFEST "application/vnd.oci.image.manifest.v1+json" +#define OCI_MEDIATYPE_INDEX "application/vnd.oci.image.index.v1+json" + +/* Registry constants */ +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" +#define QUAY_REGISTRY "quay.io" + +/* Global CURL handle */ +static CURL *g_curl;and then make
struct erofs_oci { CURL *curl; struct erofs_oci_params *params; };As a OCI context (it can be used for multiple instance usage such as runtime lazy loading.)
lib/remotes/oci.c line 50
Quoted 5 lines
lib/remotes/oci.c new file +5
-0+> +/* HTTP request/response structures */ +struct oci_request { +char url[OCI_URL_MAX_LEN ]; +struct curl_slist *headers; +};struct
erofs_oci_request{
};lib/remotes/oci.c line 51
Quoted 2 lines
lib/remotes/oci.c new file +2
-0+ +struct oci_response {
erofs_oci_response...lib/remotes/oci.c line 58
Quoted 6 lines
lib/remotes/oci.c new file +6
-0+char *data; +size_t size; +long http_code; +}; + +/* Layer information */ The comment can be droped since it's obvious.
lib/remotes/oci.c line 59
Quoted 1 line
lib/remotes/oci.c new file +1
-0+struct oci_layer {
erofs_oci_layer{lib/remotes/oci.c line 66
Quoted 7 lines
lib/remotes/oci.c new file +7
-0+char *digest; +u64 size; +const char *media_type; +}; + +/* Layer streaming context */ +struct oci_stream {Same here.
lib/remotes/oci.c line 67
Quoted 1 line
lib/remotes/oci.c new file +1
-0+struct erofs_tarfile tarfile;what's the purpose of
struct erofs_tarfile?Quoted 91 lines
lib/remotes/oci.c new file +91
-0+FILE *temp_file; +char temp_filename[OCI_TEMP_FILENAME_MAX_LEN ]; +int layer_index; +}; + +/* Callback for writing response data to memory */ +static size_t oci_write_callback (void *contents,size_t size,size_t nmemb,void *userp) +{ +size_t realsize = size * nmemb; +struct oci_response *resp = userp; +char *ptr; + + ptr =realloc (resp->data, resp->size + realsize +1 ); +if (!ptr) +return 0 ; + + resp->data = ptr; +memcpy (&resp->data[resp->size], contents, realsize); + resp->size += realsize; + resp->data[resp->size] ='\0' ; +return realsize; +} + +/* Callback for writing layer data to file */ +static size_t oci_layer_write_callback (void *contents,size_t size,size_t nmemb,void *userp) +{ +struct oci_stream *stream = userp; +size_t realsize = size * nmemb; + +if (!stream->temp_file) +return 0 ; + +if (fwrite (contents,1 , realsize, stream->temp_file) != realsize) { +erofs_err ("failed to write layer data for layer %d" , stream->layer_index); +return 0 ; + } + +return realsize; +} + +/* Perform HTTP request */ +static int oci_request_perform (struct oci_request *req,struct oci_response *resp) +{ +CURLcode res; + +erofs_dbg ("requesting URL: %s" , req->url); + +curl_easy_setopt (g_curl,CURLOPT_URL , req->url); +curl_easy_setopt (g_curl,CURLOPT_WRITEDATA , resp); +curl_easy_setopt (g_curl,CURLOPT_WRITEFUNCTION , oci_write_callback); + +if (req->headers) +curl_easy_setopt (g_curl,CURLOPT_HTTPHEADER , req->headers); + + res =curl_easy_perform (g_curl); +if (res !=CURLE_OK ) { +erofs_err ("curl request failed: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + + res =curl_easy_getinfo (g_curl,CURLINFO_RESPONSE_CODE , &resp->http_code); +if (res !=CURLE_OK ) { +erofs_err ("failed to get HTTP response code: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + +if (resp->http_code <200 || resp->http_code >=300 ) { +erofs_err ("HTTP request failed with code %ld" , resp->http_code); +return -EIO ; + } + +return 0 ; +} + +/* Get authentication token */ +static char *oci_get_auth_token (const char *registry,const char *repository, +const char *username,const char *password) +{ +struct oci_request req = {}; +struct oci_response resp = {}; +json_object *root, *token_obj; +const char *token; +char *auth_header =NULL ; +int ret; + +if (!registry || !repository) +return ERR_PTR (-EINVAL ); + +if (!strcmp (registry,DOCKER_API_REGISTRY ) || !strcmp (registry,DOCKER_REGISTRY )) { + snprintf(req.url, sizeof(req.url), + "https://auth.docker.io/token?service=registry.docker.io&scope=repository:%s:pull",lib/remotes/oci.c line 168
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ repository); + }else if (!strcmp (registry,QUAY_REGISTRY )) { +snprintf (req.url,sizeof (req.url), +"https://%s/v2/auth?service=%s&scope=repository:%s:pull" , +QUAY_REGISTRY ,QUAY_REGISTRY , repository); + }else { +snprintf (req.url,sizeof (req.url), +"https://%s/token?service=%s&scope=repository:%s:pull" , + registry, registry, repository); + }You could use asprintf to allocate dynamic strings...
...
Quoted 21 lines
lib/remotes/oci.c new file +8
-1case 'V' :version ();@@ -1638,7 +1775,8 @@ int main(int argc, char **argv)erofs_uuid_generate (g_sbi.uuid);if ((source_mode ==EROFS_MKFS_SOURCE_TAR && !erofstar.index_mode) || - (source_mode ==EROFS_MKFS_SOURCE_S3 )) { + (source_mode ==EROFS_MKFS_SOURCE_S3 ) || + (source_mode ==EROFS_MKFS_SOURCE_OCI )) { err =erofs_diskbuf_init (1 );if (err) { erofs_err("failed to initialize diskbuf: %s",@@ -1756,12 +1894,27 @@ int main(int argc, char **argv) dataimport_mode == EROFS_MKFS_DATA_IMPORT_ZEROFILL);if (err) goto exit; +#endif +#ifdef OCIEROFS_ENABLED + }else if (source_mode ==EROFS_MKFS_SOURCE_OCI ) { +if (incremental_mode || + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_RVSP ) + err = -EOPNOTSUPP ;lib/remotes/oci.c int main(int argc, char **argv) · line 1912
Quoted 10 lines
lib/remotes/oci.c new file +7
-0+ else + err =ocierofs_build_trees (root, &ocicfg, + cfg.c_src_path, + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_ZEROFILL ); +if (err) + goto exit; +erofs_info ("OCI build_trees completed, starting filesystem construction" );#endif } +erofs_info ("Starting erofs_rebuild_dump_tree..." );The debugging info can be removed..
lib/remotes/oci.c int main(int argc, char **argv) · line 1916
Quoted 4 lines
lib/remotes/oci.c new file +1
-0err =erofs_rebuild_dump_tree (root, incremental_mode);if (err) goto exit; +erofs_info ("erofs_rebuild_dump_tree completed" );Same here.
Thanks,
Gao Xiang
Hi Chengyu,
Quoted 7 lines · hudsonZhu
Quoted 4 lines
lib/remotes/oci.c line 67
Quoted 2 lines
lib/remotes/oci.c new file +1
-0+struct erofs_tarfile tarfile;what's the purpose of
struct erofs_tarfile?It's used by the
tarerofs_parse_tar()function to iterate through tar archive entries and extract file information.
Yeah, got it, see my latest reply on your v2.
I think you could leverage erofs_tmpfile() instead, and it returns a fd so you don't need to creat + open anymore.
Thanks,
Gao Xiang
Quoted 3 lines
Thanks,
Chengyu Zhu
PATCH v2 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
New mkfs.erofs option: --oci=registry/repo:tag[,options]
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)
- username/password (basic authentication)
--- configure.ac | 45 +++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 97 ++++++ lib/remotes/oci.c | 826 +++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 176 +++++++++- 5 files changed, 1150 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +45 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,37 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -712,6 +752,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -784,6 +825,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -40,6 +40,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -71,6 +72,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +97 -0
97 lines not on this page · raw message
lib/remotes/oci.c new file +826 -0
826 lines not on this page · raw message
mkfs/main.c +175 -1
248 lines not on this page · raw message
Signature · 1 line
2.51.0
Quoted 15 lines · On 2025/8/22 10:45, ChengyuZhu6 wrote:
From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Quoted 1 line
New mkfs.erofs option: --oci=registry/repo:tag[,options]
Could you just write down the full command line? e,g.
mkfs.erofs --oci=registry/repo:tag[,options] <IMAGE> <?>
what's the meaning of <?>? since users already pass in
registry/repo:tag and layer=N
Quoted 9 lines
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)
- username/password (basic authentication)Signed-off-by Changzhi Xie <[email protected]>Signed-off-by Chengyu Zhu <[email protected]>
..
Quoted 48 lines
(quoted diff) +2
-0+ +#endif /* __EROFS_OCI_H */lib/remotes/oci.c new file +40
-0@@ -0,0 +1,826 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <errno.h> +#include <curl/curl.h> +#include <json-c/json.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/diskbuf.h" +#include "erofs/rebuild.h" +#include "erofs/tar.h" +#include "liberofs_oci.h" + +#define OCI_AUTH_HEADER_MAX_LEN 1024 +#define OCI_TEMP_FILENAME_MAX_LEN 256 + +#define DOCKER_MEDIATYPE_MANIFEST_V2 "application/vnd.docker.distribution.manifest.v2+json" +#define DOCKER_MEDIATYPE_MANIFEST_V1 "application/vnd.docker.distribution.manifest.v1+json" +#define DOCKER_MEDIATYPE_MANIFEST_LIST "application/vnd.docker.distribution.manifest.list.v2+json" +#define OCI_MEDIATYPE_MANIFEST "application/vnd.oci.image.manifest.v1+json" +#define OCI_MEDIATYPE_INDEX "application/vnd.oci.image.index.v1+json" + +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" +#define QUAY_REGISTRY "quay.io" + +struct erofs_oci_request { +char *url;
lib/remotes/oci.c line 50
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+struct curl_slist *headers; +}; + +struct erofs_oci_response { +char *data; +size_t size; +long http_code; +}; + +struct oci_stream {
struct erofs_oci_stream {
It's prefered to use erofs_ prefix even the structure is internal-only or static.
lib/remotes/oci.c line 53
Quoted 3 lines
lib/remotes/oci.c new file +3
-0+struct erofs_tarfile tarfile; +FILE *temp_file; +char temp_filename[OCI_TEMP_FILENAME_MAX_LEN ];
I think you could just leave a fd for this?
and useerofs_tmpfile().
lib/remotes/oci.c line 58
Quoted 5 lines
lib/remotes/oci.c new file +5
-0+int layer_index; +}; + +/* Callback for writing response data to memory */ +static size_t oci_write_callback (void *contents,size_t size,size_t nmemb,void *userp)
same here.ocierofs_write_callback
Quoted 13 lines
lib/remotes/oci.c new file +13
-0+{ +size_t realsize = size * nmemb; +struct erofs_oci_response *resp = userp; +char *ptr; + +if (!resp || !contents) +return 0 ; + + ptr =realloc (resp->data, resp->size + realsize +1 ); +if (!ptr) { +erofs_err ("failed to allocate memory for response data" ); +return 0 ; + }
lib/remotes/oci.c line 81
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ + resp->data = ptr; +memcpy (&resp->data[resp->size], contents, realsize); + resp->size += realsize; + resp->data[resp->size] ='\0' ; +return realsize; +} + +/* Callback for writing layer data to file */ +static size_t oci_layer_write_callback (void *contents,size_t size,size_t nmemb,void *userp)
ocierofs_
Quoted 6 lines
lib/remotes/oci.c new file +6
-0+{ +struct oci_stream *stream = userp; +size_t realsize = size * nmemb; + +if (!stream->temp_file) +return 0 ;
lib/remotes/oci.c line 97
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ +if (fwrite (contents,1 , realsize, stream->temp_file) != realsize) { +erofs_err ("failed to write layer data for layer %d" , stream->layer_index); +return 0 ; + } + +return realsize; +} + +static int oci_curl_setup_common_options (CURL *curl)
ocierofs_
lib/remotes/oci.c line 100
Quoted 3 lines
lib/remotes/oci.c new file +3
-0+{ +if (!curl) +return -EINVAL ;
Since it's an internal helper, I think it's unneeded to check the invalid argument.
lib/remotes/oci.c line 111
Quoted 11 lines
lib/remotes/oci.c new file +11
-0+ +curl_easy_setopt (curl,CURLOPT_FOLLOWLOCATION ,1L ); +curl_easy_setopt (curl,CURLOPT_CONNECTTIMEOUT ,30L ); +curl_easy_setopt (curl,CURLOPT_TIMEOUT ,120L ); +curl_easy_setopt (curl,CURLOPT_NOSIGNAL ,1L ); +curl_easy_setopt (curl,CURLOPT_USERAGENT ,"ocierofs/" PACKAGE_VERSION ); + +return 0 ; +} + +static int oci_curl_setup_basic_auth (CURL *curl,const char *username,const char *password)
ocierofs_
lib/remotes/oci.c line 116
Quoted 5 lines
lib/remotes/oci.c new file +5
-0+{ +char *userpwd =NULL ; + +if (!curl || !username || !password) +return -EINVAL ;
Same here too.
Quoted 5 lines
lib/remotes/oci.c new file +5
-0+ +if (asprintf (&userpwd,"%s:%s" , username, password) ==-1 ) +return -ENOMEM ; + +curl_easy_setopt (curl,CURLOPT_USERPWD , userpwd);
lib/remotes/oci.c line 131
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+curl_easy_setopt (curl,CURLOPT_HTTPAUTH ,CURLAUTH_BASIC ); + +free (userpwd); +return 0 ; +} + +static int oci_curl_clear_auth (CURL *curl) +{ +if (!curl) +return -EINVAL ;
Same here too.
lib/remotes/oci.c line 143
Quoted 12 lines
lib/remotes/oci.c new file +12
-0+ +curl_easy_setopt (curl,CURLOPT_USERPWD ,NULL ); +curl_easy_setopt (curl,CURLOPT_HTTPAUTH ,CURLAUTH_NONE ); + +return 0 ; +} + +static int oci_curl_setup_request (CURL *curl,const char *url,size_t (*write_func)(void *,size_t ,size_t ,void *), +void *write_data,struct curl_slist *headers) +{ +if (!curl || !url || !write_func) +return -EINVAL ;
Same here too.
Quoted 34 lines
lib/remotes/oci.c new file +34
-0+ +curl_easy_setopt (curl,CURLOPT_URL , url); +curl_easy_setopt (curl,CURLOPT_WRITEFUNCTION , write_func); +curl_easy_setopt (curl,CURLOPT_WRITEDATA , write_data); + +if (headers) +curl_easy_setopt (curl,CURLOPT_HTTPHEADER , headers); + +return 0 ; +} + +static int oci_request_perform (struct erofs_oci *oci,struct erofs_oci_request *req, +struct erofs_oci_response *resp) +{ +CURLcode res; +int ret; + + ret =oci_curl_setup_request (oci->curl, req->url, oci_write_callback, resp, req->headers); +if (ret) +return ret; + + res =curl_easy_perform (oci->curl); +if (res !=CURLE_OK ) { +erofs_err ("curl request failed: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + + res =curl_easy_getinfo (oci->curl,CURLINFO_RESPONSE_CODE , &resp->http_code); +if (res !=CURLE_OK ) { +erofs_err ("failed to get HTTP response code: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + +if (resp->http_code <200 || resp->http_code >=300 ) {
lib/remotes/oci.c line 187
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+erofs_err ("HTTP request failed with code %ld" , resp->http_code); +return -EIO ; + } + +return 0 ; +} + +static char *oci_get_auth_token (struct erofs_oci *oci,const char *registry, +const char *repository,const char *username, +const char *password)
It seems your tab style is incorrect: one tab should be 8 spaces.
Quoted 1 line
lib/remotes/oci.c new file +1
-0+{
...
lib/remotes/oci.c line 235
Quoted 3 lines
lib/remotes/oci.c new file +3
-0+ +if (!resp.data) + {
brace should follow if (!resp.data)
if (!resp.data) {erofs-utils follows linux kernel style: K&R C style.
lib/remotes/oci.c line 243
Quoted 8 lines
lib/remotes/oci.c new file +8
-0+erofs_err ("empty response from auth server" ); + ret = -EINVAL ; + goto out_url; + } + + root =json_tokener_parse (resp.data); +if (!root) + {
here.
lib/remotes/oci.c line 250
Quoted 7 lines
lib/remotes/oci.c new file +7
-0+erofs_err ("failed to parse auth response" ); + ret = -EINVAL ; + goto out_url; + } + +if (!json_object_object_get_ex (root,"token" , &token_obj)) + {
here.
There are still many broken braces below, I think let's just fix the coding style first.
Quoted 17 lines
lib/remotes/oci.c new file +17
-0+erofs_err ("no token found in auth response" ); + ret = -EINVAL ; + goto out_json; + } + + token =json_object_get_string (token_obj); +if (!token) + { +erofs_err ("invalid token in auth response" ); + ret = -EINVAL ; + goto out_json; + } + +if (asprintf (&auth_header,"Authorization: Bearer %s" , token) ==-1 ) + { + ret = -ENOMEM ; + goto out_json;
lib/remotes/oci.c line 277
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ } + +out_json: +json_object_put (root); +out_url: +free (req.url); +free (resp.data); +return ret ?ERR_PTR (ret) : auth_header; +} +
Thanks,
Gao Xiang
PATCH v3 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci=registry/repo:tag[,options]
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8 \
image.erofs /tmp/output
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8,anonymous \
image.erofs /tmp/output
- mkfs.erofs \
--oci=zcy1234/test-private,username=zcy1234,password=PASSWD \
image.erofs /tmp/output
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8,layer=1 \
layer.erofs /tmp/output
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8,platform=linux/arm64 \
image.erofs /tmp/output
--- configure.ac | 45 +++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 96 ++++++ lib/remotes/oci.c | 835 +++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 190 ++++++++++- 5 files changed, 1172 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +45 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,37 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -712,6 +752,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -784,6 +825,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -40,6 +40,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -71,6 +72,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +96 -0
96 lines not on this page · raw message
lib/remotes/oci.c new file +835 -0
835 lines not on this page · raw message
mkfs/main.c +189 -1
262 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v4 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci=registry/repo:tag[,options]
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8 \
image.erofs /tmp/output
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8,anonymous \
image.erofs /tmp/output
- mkfs.erofs \
--oci=zcy1234/test-private,username=zcy1234,password=PASSWD \
image.erofs /tmp/output
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8,layer=1 \
layer.erofs /tmp/output
- mkfs.erofs \
--oci=quay.io/chengyuzhu6/golang:1.22.8,platform=linux/arm64 \
image.erofs /tmp/output
--- configure.ac | 45 +++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 96 ++++++ lib/remotes/oci.c | 835 +++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 190 ++++++++++- 5 files changed, 1172 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +45 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,37 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -712,6 +752,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -784,6 +825,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -40,6 +40,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -71,6 +72,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +96 -0
96 lines not on this page · raw message
lib/remotes/oci.c new file +835 -0
835 lines not on this page · raw message
mkfs/main.c +189 -1
262 lines not on this page · raw message
Signature · 1 line
2.51.0
Quoted 21 lines · On 2025/8/22 14:00, ChengyuZhu6 wrote:
From: Chengyu Zhu <[email protected]>
...
lib/remotes/oci.c new file +12
-0@@ -0,0 +1,835 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/types.h>
lib/remotes/oci.c line 22
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+#include <sys/stat.h> +#include <errno.h> +#include <curl/curl.h> +#include <json-c/json.h> +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/diskbuf.h" +#include "erofs/rebuild.h"
Hi Chengyu,
It seems inode.h blobchunk.h diskbuf.h rebuild.h is redundant.
Quoted 484 lines
lib/remotes/oci.c new file +484
-0+#include "erofs/tar.h" +#include "liberofs_oci.h" + +#define OCI_AUTH_HEADER_MAX_LEN 1024 + +#define DOCKER_MEDIATYPE_MANIFEST_V2 \ + "application/vnd.docker.distribution.manifest.v2+json" +#define DOCKER_MEDIATYPE_MANIFEST_V1 \ + "application/vnd.docker.distribution.manifest.v1+json" +#define DOCKER_MEDIATYPE_MANIFEST_LIST \ + "application/vnd.docker.distribution.manifest.list.v2+json" +#define OCI_MEDIATYPE_MANIFEST "application/vnd.oci.image.manifest.v1+json" +#define OCI_MEDIATYPE_INDEX "application/vnd.oci.image.index.v1+json" + +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" +#define QUAY_REGISTRY "quay.io" + +struct erofs_oci_request { +char *url; +struct curl_slist *headers; +}; + +struct erofs_oci_response { +char *data; +size_t size; +long http_code; +}; + +struct erofs_oci_stream { +struct erofs_tarfile tarfile; +int temp_fd; +int layer_index; +}; + +/* Callback for writing response data to memory */ +static size_t ocierofs_write_callback (void *contents,size_t size, +size_t nmemb,void *userp) +{ +size_t realsize = size * nmemb; +struct erofs_oci_response *resp = userp; +char *ptr; + +if (!resp || !contents) +return 0 ; + + ptr =realloc (resp->data, resp->size + realsize +1 ); +if (!ptr) { +erofs_err ("failed to allocate memory for response data" ); +return 0 ; + } + + resp->data = ptr; +memcpy (&resp->data[resp->size], contents, realsize); + resp->size += realsize; + resp->data[resp->size] ='\0' ; +return realsize; +} + +/* Callback for writing layer data to file */ +static size_t ocierofs_layer_write_callback (void *contents,size_t size, +size_t nmemb,void *userp) +{ +struct erofs_oci_stream *stream = userp; +size_t realsize = size * nmemb; + +if (stream->temp_fd <0 ) +return 0 ; + +if (write (stream->temp_fd, contents, realsize) != realsize) { +erofs_err ("failed to write layer data for layer %d" , + stream->layer_index); +return 0 ; + } + +return realsize; +} + +static int ocierofs_curl_setup_common_options (CURL *curl) +{ +curl_easy_setopt (curl,CURLOPT_FOLLOWLOCATION ,1L ); +curl_easy_setopt (curl,CURLOPT_CONNECTTIMEOUT ,30L ); +curl_easy_setopt (curl,CURLOPT_TIMEOUT ,120L ); +curl_easy_setopt (curl,CURLOPT_NOSIGNAL ,1L ); +curl_easy_setopt (curl,CURLOPT_USERAGENT ,"ocierofs/" PACKAGE_VERSION ); + +return 0 ; +} + +static int ocierofs_curl_setup_basic_auth (CURL *curl,const char *username, +const char *password) +{ +char *userpwd =NULL ; + +if (asprintf (&userpwd,"%s:%s" , username, password) ==-1 ) +return -ENOMEM ; + +curl_easy_setopt (curl,CURLOPT_USERPWD , userpwd); +curl_easy_setopt (curl,CURLOPT_HTTPAUTH ,CURLAUTH_BASIC ); + +free (userpwd); +return 0 ; +} + +static int ocierofs_curl_clear_auth (CURL *curl) +{ +curl_easy_setopt (curl,CURLOPT_USERPWD ,NULL ); +curl_easy_setopt (curl,CURLOPT_HTTPAUTH ,CURLAUTH_NONE ); + +return 0 ; +} + +static int ocierofs_curl_setup_request (CURL *curl,const char *url, +size_t (*write_func)(void *,size_t ,size_t ,void *), +void *write_data,struct curl_slist *headers) +{ +curl_easy_setopt (curl,CURLOPT_URL , url); +curl_easy_setopt (curl,CURLOPT_WRITEFUNCTION , write_func); +curl_easy_setopt (curl,CURLOPT_WRITEDATA , write_data); + +if (headers) +curl_easy_setopt (curl,CURLOPT_HTTPHEADER , headers); + +return 0 ; +} + +static int ocierofs_request_perform (struct erofs_oci *oci, +struct erofs_oci_request *req, +struct erofs_oci_response *resp) +{ +CURLcode res; +int ret; + + ret =ocierofs_curl_setup_request (oci->curl, req->url, + ocierofs_write_callback, resp, + req->headers); +if (ret) +return ret; + + res =curl_easy_perform (oci->curl); +if (res !=CURLE_OK ) { +erofs_err ("curl request failed: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + + res =curl_easy_getinfo (oci->curl,CURLINFO_RESPONSE_CODE , + &resp->http_code); +if (res !=CURLE_OK ) { +erofs_err ("failed to get HTTP response code: %s" , +curl_easy_strerror (res)); +return -EIO ; + } + +if (resp->http_code <200 || resp->http_code >=300 ) { +erofs_err ("HTTP request failed with code %ld" , + resp->http_code); +return -EIO ; + } + +return 0 ; +} + +static char *ocierofs_get_auth_token (struct erofs_oci *oci,const char *registry, +const char *repository,const char *username, +const char *password) +{ +struct erofs_oci_request req = {}; +struct erofs_oci_response resp = {}; +json_object *root, *token_obj; +const char *token; +char *auth_header =NULL ; +int ret; + +if (!registry || !repository) +return ERR_PTR (-EINVAL ); + +if (!strcmp (registry,DOCKER_API_REGISTRY ) || + !strcmp (registry,DOCKER_REGISTRY )) { +if (asprintf (&req.url, +"https://auth.docker.io/token?service=" +"registry.docker.io&scope=repository:%s:pull" , + repository) ==-1 ) { +return ERR_PTR (-ENOMEM ); + } + }else if (!strcmp (registry,QUAY_REGISTRY )) { +if (asprintf (&req.url, +"https://%s/v2/auth?service=%s&scope=" +"repository:%s:pull" , +QUAY_REGISTRY ,QUAY_REGISTRY , repository) ==-1 ) { +return ERR_PTR (-ENOMEM ); + } + }else { +if (asprintf (&req.url, +"https://%s/token?service=%s&scope=" +"repository:%s:pull" , + registry, registry, repository) ==-1 ) { +return ERR_PTR (-ENOMEM ); + } + } + +if (username && password && *username) { + ret =ocierofs_curl_setup_basic_auth (oci->curl, username, + password); +if (ret) + goto out_url; + } + + ret =ocierofs_request_perform (oci, &req, &resp); + +ocierofs_curl_clear_auth (oci->curl); + +if (ret) + goto out_url; + +if (!resp.data) { +erofs_err ("empty response from auth server" ); + ret = -EINVAL ; + goto out_url; + } + + root =json_tokener_parse (resp.data); +if (!root) { +erofs_err ("failed to parse auth response" ); + ret = -EINVAL ; + goto out_url; + } + +if (!json_object_object_get_ex (root,"token" , &token_obj)) { +erofs_err ("no token found in auth response" ); + ret = -EINVAL ; + goto out_json; + } + + token =json_object_get_string (token_obj); +if (!token) { +erofs_err ("invalid token in auth response" ); + ret = -EINVAL ; + goto out_json; + } + +if (asprintf (&auth_header,"Authorization: Bearer %s" , token) ==-1 ) { + ret = -ENOMEM ; + goto out_json; + } + +out_json: +json_object_put (root); +out_url: +free (req.url); +free (resp.data); +return ret ?ERR_PTR (ret) : auth_header; +} + +static char *ocierofs_get_manifest_digest (struct erofs_oci *oci, +const char *registry, +const char *repository,const char *tag, +const char *platform, +const char *auth_header) +{ +struct erofs_oci_request req = {}; +struct erofs_oci_response resp = {}; +json_object *root, *manifests, *manifest, *platform_obj, *arch_obj; +json_object *os_obj, *digest_obj, *schema_obj, *media_type_obj; +char *digest =NULL ; +const char *api_registry; +int ret, len, i; + +if (!registry || !repository || !tag || !platform) +return ERR_PTR (-EINVAL ); + +memset (&resp,0 ,sizeof (resp)); + + api_registry = (!strcmp (registry,DOCKER_REGISTRY )) ? +DOCKER_API_REGISTRY : registry; + +if (asprintf (&req.url,"https://%s/v2/%s/manifests/%s" , + api_registry, repository, tag) ==-1 ) { +return ERR_PTR (-ENOMEM ); + } + +if (auth_header &&strstr (auth_header,"Bearer" )) + req.headers =curl_slist_append (req.headers, auth_header); + + req.headers =curl_slist_append (req.headers, +"Accept: " DOCKER_MEDIATYPE_MANIFEST_LIST "," +OCI_MEDIATYPE_INDEX "," DOCKER_MEDIATYPE_MANIFEST_V1 "," +DOCKER_MEDIATYPE_MANIFEST_V2 ); + + ret =ocierofs_request_perform (oci, &req, &resp); +if (ret) + goto out; + +if (!resp.data) { +erofs_err ("empty response from manifest request" ); + ret = -EINVAL ; + goto out; + } + + root =json_tokener_parse (resp.data); +if (!root) { +erofs_err ("failed to parse manifest JSON" ); + ret = -EINVAL ; + goto out; + } + +if (json_object_object_get_ex (root,"schemaVersion" , &schema_obj)) { +if (json_object_get_int (schema_obj) ==1 ) { + digest =strdup (tag); + goto out_json; + } + } + +if (json_object_object_get_ex (root,"mediaType" , &media_type_obj)) { +const char *media_type =json_object_get_string (media_type_obj); + +if (!strcmp (media_type,DOCKER_MEDIATYPE_MANIFEST_V2 ) || + !strcmp (media_type,OCI_MEDIATYPE_MANIFEST )) { + digest =strdup (tag); + goto out_json; + } + } + +if (!json_object_object_get_ex (root,"manifests" , &manifests)) { +erofs_err ("no manifests found in manifest list" ); + ret = -EINVAL ; + goto out_json; + } + + len =json_object_array_length (manifests); +for (i =0 ; i < len; i++) { + manifest =json_object_array_get_idx (manifests, i); + +if (json_object_object_get_ex (manifest,"platform" , + &platform_obj) && +json_object_object_get_ex (platform_obj,"architecture" , + &arch_obj) && +json_object_object_get_ex (platform_obj,"os" , &os_obj) && +json_object_object_get_ex (manifest,"digest" , &digest_obj)) { +const char *arch =json_object_get_string (arch_obj); +const char *os =json_object_get_string (os_obj); +char manifest_platform[64 ]; + +snprintf (manifest_platform,sizeof (manifest_platform), +"%s/%s" , os, arch); +if (!strcmp (manifest_platform, platform)) { + digest =strdup (json_object_get_string (digest_obj)); +break ; + } + } + } + +if (!digest) + ret = -ENOENT ; + +out_json: +json_object_put (root); +out: +free (resp.data); +if (req.headers) +curl_slist_free_all (req.headers); +free (req.url); + +return ret ?ERR_PTR (ret) : digest; +} + +static char **ocierofs_get_layers_info (struct erofs_oci *oci, +const char *registry, +const char *repository, +const char *digest, +const char *auth_header, +int *layer_count) +{ +struct erofs_oci_request req = {}; +struct erofs_oci_response resp = {}; +json_object *root, *layers, *layer, *digest_obj; +char **layers_info =NULL ; +const char *api_registry; +int ret, len, i; + +if (!registry || !repository || !digest || !layer_count) +return ERR_PTR (-EINVAL ); + + *layer_count =0 ; +memset (&resp,0 ,sizeof (resp)); + api_registry = (!strcmp (registry,DOCKER_REGISTRY )) ? +DOCKER_API_REGISTRY : registry; + +if (asprintf (&req.url,"https://%s/v2/%s/manifests/%s" , + api_registry, repository, digest) ==-1 ) { +return ERR_PTR (-ENOMEM ); + } + +if (auth_header &&strstr (auth_header,"Bearer" )) + req.headers =curl_slist_append (req.headers, auth_header); + + req.headers =curl_slist_append (req.headers, +"Accept: " OCI_MEDIATYPE_MANIFEST "," DOCKER_MEDIATYPE_MANIFEST_V2 ); + + ret =ocierofs_request_perform (oci, &req, &resp); +if (ret) + goto out; + +if (!resp.data) { +erofs_err ("empty response from layers request" ); + ret = -EINVAL ; + goto out; + } + + root =json_tokener_parse (resp.data); +if (!root) { +erofs_err ("failed to parse manifest JSON" ); + ret = -EINVAL ; + goto out; + } + +if (!json_object_object_get_ex (root,"layers" , &layers) || +json_object_get_type (layers) != json_type_array) { +erofs_err ("no layers found in manifest" ); + ret = -EINVAL ; + goto out_json; + } + + len =json_object_array_length (layers); +if (len ==0 ) { +erofs_err ("empty layer list in manifest" ); + ret = -EINVAL ; + goto out_json; + } + + layers_info =calloc (len,sizeof (char *)); +if (!layers_info) { + ret = -ENOMEM ; + goto out_json; + } + +for (i =0 ; i < len; i++) { + layer =json_object_array_get_idx (layers, i); + +if (!json_object_object_get_ex (layer,"digest" , &digest_obj)) { +erofs_err ("failed to parse layer %d" , i); + ret = -EINVAL ; + goto out_free; + } + + layers_info[i] =strdup (json_object_get_string (digest_obj)); +if (!layers_info[i]) { + ret = -ENOMEM ; + goto out_free; + } + } + + *layer_count = len; +json_object_put (root); +free (resp.data); +if (req.headers) +curl_slist_free_all (req.headers); +free (req.url); +return layers_info; + +out_free: +if (layers_info) { +for (int j =0 ; j < i; j++) +free (layers_info[j]); + } +free (layers_info); +out_json: +json_object_put (root); +out: +free (resp.data); +if (req.headers) +curl_slist_free_all (req.headers); +free (req.url); +return ERR_PTR (ret); +} + +static int ocierofs_extract_layer (struct erofs_oci *oci,struct erofs_inode *root, +const char *layer_digest,const char *auth_header, +int layer_index) +{ +struct erofs_oci_request req = {}; +struct erofs_oci_stream stream = {}; +const char *api_registry; +int ret, fd =-1 ; +long http_code;
lib/remotes/oci.c line 516
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ +if (!oci || !root || !layer_digest || layer_index <0 ) { +erofs_err ("invalid parameters for layer extraction" ); +return -EINVAL ; + } + +memset (&stream,0 ,sizeof (stream)); + stream.layer_index = layer_index; + + stream.temp_fd =erofs_tmpfile ();
referencing erofs_tmpfile triggers a compiler warning, but will not cause
an runtime error because it happens have the signature int(void).
Do you forget to include "liberofs_private.h"?
Quoted 523 lines
lib/remotes/oci.c new file +319
-0+if (stream.temp_fd <0 ) { +erofs_err ("failed to create temporary file for layer %d" , + layer_index); +return -errno; + } + + api_registry = (!strcmp (oci->params.registry,DOCKER_REGISTRY )) ? +DOCKER_API_REGISTRY : oci->params.registry; + +if (asprintf (&req.url,"https://%s/v2/%s/blobs/%s" , + api_registry, oci->params.repository, + layer_digest) ==-1 ) { + ret = -ENOMEM ; + goto out; + } + +if (auth_header &&strstr (auth_header,"Bearer" )) + req.headers =curl_slist_append (req.headers, auth_header); + +curl_easy_reset (oci->curl); + + ret =ocierofs_curl_setup_common_options (oci->curl); +if (ret) + goto out; + + ret =ocierofs_curl_setup_request (oci->curl, req.url, + ocierofs_layer_write_callback, + &stream, req.headers); +if (ret) + goto out; + + ret =curl_easy_perform (oci->curl); + +if (ret !=CURLE_OK ) { +erofs_err ("failed to download layer: %s" , +curl_easy_strerror (ret)); + ret = -EIO ; + goto out; + } + +curl_easy_getinfo (oci->curl,CURLINFO_RESPONSE_CODE , &http_code); +if (http_code <200 || http_code >=300 ) { +erofs_err ("HTTP request failed with code %ld" , http_code); + ret = -EIO ; + goto out; + } + +if (lseek (stream.temp_fd,0 ,SEEK_SET ) <0 ) { +erofs_err ("failed to seek to beginning of temp file: %s" , +strerror (errno)); + ret = -errno; + goto out; + } + + fd =dup (stream.temp_fd); +if (fd <0 ) { +erofs_err ("failed to duplicate temp file descriptor: %s" , +strerror (errno)); + ret = -errno; + goto out; + } + +memset (&stream.tarfile,0 ,sizeof (stream.tarfile)); +init_list_head (&stream.tarfile.global.xattrs); + + ret =erofs_iostream_open (&stream.tarfile.ios, fd, +EROFS_IOS_DECODER_GZIP ); +if (ret) { +erofs_err ("failed to initialize tar stream: %s" , +erofs_strerror (ret)); + goto out; + } + +while (!(ret =tarerofs_parse_tar (root, &stream.tarfile))) { +/* Continue parsing until end of archive */ + } + +erofs_iostream_close (&stream.tarfile.ios); + +if (ret <0 && ret != -ENODATA ) { +erofs_err ("failed to process tar stream: %s" , +erofs_strerror (ret)); + goto out; + } + + ret =0 ; + +out: +if (fd >=0 ) +close (fd); + +if (stream.temp_fd >=0 ) +close (stream.temp_fd); + +if (req.headers) +curl_slist_free_all (req.headers); + +free (req.url); + +return ret; +} + +int ocierofs_build_trees (struct erofs_inode *root,struct erofs_oci *oci, +bool fillzero) +{ +char *auth_header =NULL ; +char *manifest_digest =NULL ; +char **layers_info =NULL ; +int layer_count =0 ; +int ret, i; + +if (!root || !oci) +return -EINVAL ; + +if (oci->params.auth_mode !=OCI_AUTH_ANONYMOUS ) { +if (oci->params.auth_mode ==OCI_AUTH_BASIC && + oci->params.username && oci->params.password && + oci->params.username[0 ] && oci->params.password[0 ]) { + auth_header =ocierofs_get_auth_token (oci, + oci->params.registry, + oci->params.repository, + oci->params.username, + oci->params.password); +if (IS_ERR (auth_header)) { + auth_header =NULL ; + ret =ocierofs_curl_setup_basic_auth (oci->curl, + oci->params.username, + oci->params.password); +if (ret) + goto out; + } + }else if (oci->params.auth_mode ==OCI_AUTH_TOKEN ) { + auth_header =ocierofs_get_auth_token (oci, + oci->params.registry, + oci->params.repository, +NULL ,NULL ); +if (IS_ERR (auth_header)) { + ret =PTR_ERR (auth_header); +erofs_err ("failed to get auth token: %s" , +erofs_strerror (ret)); + goto out; + } + } + } + + manifest_digest =ocierofs_get_manifest_digest (oci, oci->params.registry, + oci->params.repository, + oci->params.tag, + oci->params.platform, + auth_header); +if (IS_ERR (manifest_digest)) { + ret =PTR_ERR (manifest_digest); +erofs_err ("failed to get manifest digest: %s" , +erofs_strerror (ret)); + goto out_auth; + } + + layers_info =ocierofs_get_layers_info (oci, oci->params.registry, + oci->params.repository, + manifest_digest, auth_header, + &layer_count); +if (IS_ERR (layers_info)) { + ret =PTR_ERR (layers_info); +erofs_err ("failed to get layers info: %s" , +erofs_strerror (ret)); + goto out_manifest; + } + +if (oci->params.layer_index >=0 ) { +char *trimmed; + +if (oci->params.layer_index >= layer_count) { +erofs_err ("layer index %d exceeds available layers (%d)" , + oci->params.layer_index, layer_count); + ret = -EINVAL ; + goto out_layers; + } + + i = oci->params.layer_index; + trimmed =erofs_trim_for_progressinfo (layers_info[i], +sizeof ("Extracting layer ..." ) -1 ); +erofs_update_progressinfo ("Extracting layer %d: %s ..." , i, + trimmed); +free (trimmed); + +if (!fillzero) { + ret =ocierofs_extract_layer (oci, root, layers_info[i], + auth_header, i); +if (ret) { +erofs_err ("failed to extract layer %d: %s" , i, +erofs_strerror (ret)); + goto out_layers; + } + } + }else { +for (i =0 ; i < layer_count; i++) { +char *trimmed =erofs_trim_for_progressinfo (layers_info[i], +sizeof ("Extracting layer ..." ) -1 ); +erofs_update_progressinfo ("Extracting layer %s ..." , + trimmed); +free (trimmed); + +if (fillzero) +continue ; + + ret =ocierofs_extract_layer (oci, root, layers_info[i], + auth_header, i); +if (ret) { +erofs_err ("failed to extract layer %d: %s" , i, +erofs_strerror (ret)); + goto out_layers; + } + } + } + + ret =0 ; + +out_layers: +for (i =0 ; i < layer_count; i++) +free (layers_info[i]); +free (layers_info); +out_manifest: +free (manifest_digest); +out_auth: +free (auth_header); + +if (oci->params.username && oci->params.password && + oci->params.username[0 ] && oci->params.password[0 ] && + !auth_header) { +ocierofs_curl_clear_auth (oci->curl); + } + +out: +return ret; +} + +int ocierofs_init (struct erofs_oci *oci) +{ +if (!oci) +return -EINVAL ; + +memset (oci,0 ,sizeof (*oci)); + +if (curl_global_init (CURL_GLOBAL_DEFAULT ) !=CURLE_OK ) +return -EIO ; + + oci->curl =curl_easy_init (); +if (!oci->curl) { +curl_global_cleanup (); +return -EIO ; + } + +if (ocierofs_curl_setup_common_options (oci->curl)) { +ocierofs_cleanup (oci); +return -EIO ; + } + +if (erofs_oci_params_set_string (&oci->params.platform, +"linux/amd64" ) || +erofs_oci_params_set_string (&oci->params.registry, +"registry-1.docker.io" ) || +erofs_oci_params_set_string (&oci->params.tag,"latest" )) { +ocierofs_cleanup (oci); +return -ENOMEM ; + } + + oci->params.layer_index =-1 ;/* -1 means extract all layers */ + oci->params.auth_mode =OCI_AUTH_TOKEN ; + +return 0 ; +} + +void ocierofs_cleanup (struct erofs_oci *oci) +{ +if (!oci) +return ; + +if (oci->curl) { +curl_easy_cleanup (oci->curl); + oci->curl =NULL ; + } +curl_global_cleanup (); + +free (oci->params.registry); +free (oci->params.repository); +free (oci->params.tag); +free (oci->params.platform); +free (oci->params.username); +free (oci->params.password); + + oci->params.registry =NULL ; + oci->params.repository =NULL ; + oci->params.tag =NULL ; + oci->params.platform =NULL ; + oci->params.username =NULL ; + oci->params.password =NULL ; +} + +int erofs_oci_params_set_string (char **field,const char *value) +{ +char *new_value; + +if (!field) +return -EINVAL ; + +if (!value) { +free (*field); + *field =NULL ; +return 0 ; + } + + new_value =strdup (value); +if (!new_value) +return -ENOMEM ; + +free (*field); + *field = new_value; +return 0 ; +}mkfs/main.c +157
-0@@ -32,6 +32,7 @@#include "../lib/liberofs_uuid.h" #include "../lib/liberofs_metabox.h" #include "../lib/liberofs_s3.h" +#include "../lib/liberofs_oci.h" #include "../lib/compressor.h" static struct option long_options[] = {@@ -95,6 +96,9 @@ static struct option long_options[] = { {"vmdk-desc", required_argument, NULL, 532}, #ifdef S3EROFS_ENABLED {"s3", required_argument, NULL, 533}, +#endif +#ifdef OCIEROFS_ENABLED + {"oci", required_argument, NULL, 534}, #endif {0, 0, 0, 0}, };@@ -206,6 +210,14 @@ static void usage(int argc, char **argv)" [,passwd_file=Y] X=endpoint, Y=s3fs-compatible password file\n" " [,urlstyle=Z] S3 API calling style (Z = vhost|path) (default: vhost)\n" " [,sig=<2,4>] S3 API signature version (default: 2)\n" +#endif +#ifdef OCIEROFS_ENABLED +" --oci=X generate an image from OCI-compatible registry\n" +" [,platform=Y] X=registry/repo:tag, Y=platform (default: linux/amd64)\n" +" [,layer=Z] Z=layer index to extract (default: 0)\n" +" [,anonymous] use anonymous access (no authentication)\n" +" [,username=U] U=username for basic authentication\n" + " [,password=P] P=password for basic authentication\n"#endif " --tar=X generate a full or index-only image from a tarball(-ish) source\n" " (X = f|i|headerball; f=full mode, i=index mode,\n" @@ -261,6 +273,10 @@ static u8 metabox_algorithmid;static struct erofs_s3 s3cfg;#endif +#ifdef OCIEROFS_ENABLED +static struct erofs_oci ocicfg; +#endif +enum {EROFS_MKFS_DATA_IMPORT_DEFAULT ,EROFS_MKFS_DATA_IMPORT_FULLDATA ,@@ -272,6 +288,7 @@ static enum {EROFS_MKFS_SOURCE_LOCALDIR ,EROFS_MKFS_SOURCE_TAR ,EROFS_MKFS_SOURCE_S3 , +EROFS_MKFS_SOURCE_OCI ,EROFS_MKFS_SOURCE_REBUILD , } source_mode;@@ -668,6 +685,146 @@ static int mkfs_parse_s3_cfg(char *cfg_str) }#endif +#ifdef OCIEROFS_ENABLED +static int mkfs_parse_oci_cfg (char *cfg_str) +{ +char *p, *q, *opt, *ref_str; +char *slash, *colon, *dot; +const char *repo_part; +size_t len; + +if (source_mode !=EROFS_MKFS_SOURCE_LOCALDIR ) +return -EINVAL ; + source_mode =EROFS_MKFS_SOURCE_OCI ; + +if (!cfg_str) { +erofs_err ("oci: missing parameter" ); +return -EINVAL ; + } + +ocierofs_init (&ocicfg); + + p =strchr (cfg_str,',' ); + ref_str = p ?strndup (cfg_str, p - cfg_str) :strdup (cfg_str); +if (!ref_str) +return -ENOMEM ; + + slash =strchr (ref_str,'/' ); +if (slash) { + dot =strchr (ref_str,'.' ); +if (dot && dot < slash) { +char *registry_str; + + len = slash - ref_str; + registry_str =strndup (ref_str, len); + +if (!registry_str) { +erofs_err ("failed to allocate memory for registry" ); + goto err_free; + } +if (erofs_oci_params_set_string (&ocicfg.params.registry, + registry_str)) { +free (registry_str); +erofs_err ("failed to set registry" ); + goto err_free; + } +free (registry_str); + repo_part = slash +1 ; + }else { + repo_part = ref_str; + } + }else { + repo_part = ref_str; + } + + colon =strchr (repo_part,':' ); +if (colon) { +char *repo_str; + + len = colon - repo_part; + repo_str =strndup (repo_part, len); + +if (!repo_str) { +erofs_err ("failed to allocate memory for repository" ); + goto err_free; + } +if (erofs_oci_params_set_string (&ocicfg.params.repository, + repo_str)) { +free (repo_str); +erofs_err ("failed to set repository" ); + goto err_free; + } +free (repo_str); + +if (erofs_oci_params_set_string (&ocicfg.params.tag, + colon +1 )) { +erofs_err ("failed to set tag" ); + goto err_free; + } + }else { +if (erofs_oci_params_set_string (&ocicfg.params.repository, + repo_part)) { +erofs_err ("failed to set repository" ); + goto err_free; + } + } + +free (ref_str); + +if (!p) +return 0 ; + + opt = p +1 ; +while (opt) { + q =strchr (opt,',' ); +if (q) + *q ='\0' ; + +if ((p =strstr (opt,"platform=" ))) { +if (erofs_oci_params_set_string (&ocicfg.params.platform, + p +9 )) { +erofs_err ("failed to set platform" ); +return -ENOMEM ; + } + }else if ((p =strstr (opt,"layer=" ))) { + ocicfg.params.layer_index =atoi (p +6 ); +if (ocicfg.params.layer_index <0 ) { +erofs_err ("invalid layer index %d" , + ocicfg.params.layer_index); +return -EINVAL ; + } + }else if (!strcmp (opt,"anonymous" )) { + ocicfg.params.auth_mode =OCI_AUTH_ANONYMOUS ; + }else if ((p =strstr (opt,"username=" ))) { +if (erofs_oci_params_set_string (&ocicfg.params.username, + p +9 )) { +erofs_err ("failed to set username" ); +return -ENOMEM ; + } + ocicfg.params.auth_mode =OCI_AUTH_BASIC ; + }else if ((p =strstr (opt,"password=" ))) { +if (erofs_oci_params_set_string (&ocicfg.params.password, + p +9 )) { +erofs_err ("failed to set password" ); +return -ENOMEM ; + } + ocicfg.params.auth_mode =OCI_AUTH_BASIC ; + }else { +erofs_err ("invalid --oci option %s" , opt); +return -EINVAL ; + } + + opt = q ? q +1 :NULL ; + } + +return 0 ; + +err_free: +free (ref_str); +return -EINVAL ; +} +#endif +static int mkfs_parse_one_compress_alg (char *alg,struct erofs_compr_opts *copts) {
mkfs/main.c static int mkfs_parse_sources(int argc, char *argv[], int optind) · line 988
Quoted 10 lines
mkfs/main.c +7
-0@@ -822,6 +979,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)if (!cfg.c_src_path)return -ENOMEM ;break ; +#endif +#ifdef OCIEROFS_ENABLED +case EROFS_MKFS_SOURCE_OCI : + cfg.c_src_path =strdup (argv[optind++]); +if (!cfg.c_src_path) +return -ENOMEM ; +break ;
Could we reuse the code of case EROFS_MKFS_SOURCE_OCI and EROFS_MKFS_SOURCE_S3 here?
Quoted 32 lines
mkfs/main.c +10
-1#endif default :erofs_err ("unexpected source_mode: %d" , source_mode);@@ -1219,6 +1383,13 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])if (err)return err;break ; +#endif +#ifdef OCIEROFS_ENABLED +case 534 : + err =mkfs_parse_oci_cfg (optarg); +if (err) +return err; +break ;#endif case 'V' :version ();@@ -1638,7 +1809,8 @@ int main(int argc, char **argv)erofs_uuid_generate (g_sbi.uuid);if ((source_mode ==EROFS_MKFS_SOURCE_TAR && !erofstar.index_mode) || - (source_mode ==EROFS_MKFS_SOURCE_S3 )) { + (source_mode ==EROFS_MKFS_SOURCE_S3 ) || + (source_mode ==EROFS_MKFS_SOURCE_OCI )) { err =erofs_diskbuf_init (1 );if (err) { erofs_err("failed to initialize diskbuf: %s",@@ -1756,12 +1928,25 @@ int main(int argc, char **argv) dataimport_mode == EROFS_MKFS_DATA_IMPORT_ZEROFILL);if (err) goto exit; +#endif
mkfs/main.c int main(int argc, char **argv) · line 1941
Quoted 10 lines
mkfs/main.c +10
-0+#ifdef OCIEROFS_ENABLED + }else if (source_mode ==EROFS_MKFS_SOURCE_OCI ) { +if (incremental_mode || + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_RVSP ) + err = -EOPNOTSUPP ; +else + err =ocierofs_build_trees (root, &ocicfg, + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_ZEROFILL ); +if (err) + goto exit;
and here.
Thanks,
Yifan
Quoted 21 lines
mkfs/main.c +5
-0#endif } +erofs_info ("Starting erofs_rebuild_dump_tree..." ); err =erofs_rebuild_dump_tree (root, incremental_mode);if (err) goto exit; +erofs_info ("erofs_rebuild_dump_tree completed" ); }if (tar_index_512b) {@@ -1850,6 +2035,9 @@ exit:erofs_packedfile_exit (&g_sbi);erofs_xattr_cleanup_name_prefixes ();erofs_rebuild_cleanup (); +#ifdef OCIEROFS_ENABLED +ocierofs_cleanup (&ocicfg); +#endif erofs_diskbuf_exit ();erofs_exit_configure ();if (source_mode ==EROFS_MKFS_SOURCE_TAR ) {
Quoted 25 lines · On 2025/8/22 14:00, ChengyuZhu6 wrote:
From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integrationConfigure: ./configure --enable-oci
New mkfs.erofs option: --oci=registry/repo:tag[,options]Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- anonymous (use anonymous access)
- username/password (basic authentication)e.g.:
Quoted 15 lines
- mkfs.erofs \ --oci=quay.io/chengyuzhu6/golang:1.22.8 \ image.erofs /tmp/output - mkfs.erofs \ --oci=quay.io/chengyuzhu6/golang:1.22.8,anonymous \ image.erofs /tmp/output - mkfs.erofs \ --oci=zcy1234/test-private,username=zcy1234,password=PASSWD \ image.erofs /tmp/output - mkfs.erofs \ --oci=quay.io/chengyuzhu6/golang:1.22.8,layer=1 \ layer.erofs /tmp/output - mkfs.erofs \ --oci=quay.io/chengyuzhu6/golang:1.22.8,platform=linux/arm64 \ image.erofs /tmp/output
Hi Chengyu,
BTW, what does this /tmp/output do? I think this parameter is for <src_path>
and would it be weird to have /tmp/output here since the datasource is OCI registry?
Thanks,
Yifan Zhao
Quoted 3 lines
Signed-off-by Changzhi Xie <[email protected]>Signed-off-by Chengyu Zhu <[email protected]>
PATCH v5 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci-options=[option] target-erofs-image source-image-name
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci-options=platform=linux/amd64, \
username=1234,password=1234 \
image.erofs ubuntu
--- configure.ac | 45 ++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 89 ++++ lib/remotes/oci.c | 1031 ++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 298 ++++++++++++- 5 files changed, 1469 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +45 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,37 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -712,6 +752,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -784,6 +825,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -40,6 +40,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -71,6 +72,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +89 -0
89 lines not on this page · raw message
lib/remotes/oci.c new file +1031 -0
1,031 lines not on this page · raw message
mkfs/main.c +297 -1
370 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v6 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci-options=[option] target source-image
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci-options=platform=linux/amd64, \
username=1234,password=1234 \
image.erofs ubuntu
--- configure.ac | 45 ++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 89 ++++ lib/remotes/oci.c | 1031 ++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 296 ++++++++++++- 5 files changed, 1467 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +45 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,37 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -713,6 +753,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -785,6 +826,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -42,6 +42,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c global.c importer.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -73,6 +74,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +89 -0
89 lines not on this page · raw message
lib/remotes/oci.c new file +1031 -0
1,031 lines not on this page · raw message
mkfs/main.c +295 -1
362 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci-options=[option] target source-image
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci-options=platform=linux/amd64, \
username=1234,password=1234 \
image.erofs ubuntu
--- configure.ac | 45 ++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 90 ++++ lib/remotes/oci.c | 1032 ++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 296 ++++++++++++- 5 files changed, 1469 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +45 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,37 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -713,6 +753,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -785,6 +826,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -42,6 +42,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c global.c importer.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -73,6 +74,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +90 -0
90 lines not on this page · raw message
lib/remotes/oci.c new file +1032 -0
1,032 lines not on this page · raw message
mkfs/main.c +295 -1
362 lines not on this page · raw message
Signature · 1 line
2.51.0
Hi Chengyu,
Quoted 23 lines · On 2025/8/25 17:33, ChengyuZhu6 wrote:
From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integrationConfigure: ./configure --enable-oci
New mkfs.erofs option: --oci-options=[option] target source-imageSupported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)
Quoted 8 lines
e.g.:
- mkfs.erofs \ --oci-options=platform=linux/amd64, \ username=1234,password=1234 \ image.erofs ubuntuSigned-off-by Changzhi Xie <[email protected]>Signed-off-by Chengyu Zhu <[email protected]>
Can you apply the following diff to your v6-rebased first?
configure.ac +3 -1
@@ -651,7 +651,9 @@ AS_IF([test "x$with_json_c" != "xno"], [ LIBS="${saved_LIBS}" CPPFLAGS="${saved_CPPFLAGS}" ], [ - AC_MSG_ERROR([Cannot find proper json-c]) + AS_IF([test "x$with_json_c" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) ]) ])
lib/liberofs_oci.h +1 -3
@@ -76,12 +76,10 @@ int erofs_oci_params_set_string(char **field, const char *value);* ocierofs_build_trees - Build file trees from OCI container image layers * @root: root inode to build the file tree under * @oci: OCI client structure with configured parameters -* @fillzero: if true, only create inodes without downloading actual data * * Return: 0 on success, negative errno on failure */ -int ocierofs_build_trees (struct erofs_importer *importer,struct erofs_oci *oci, -bool fillzero); +int ocierofs_build_trees (struct erofs_importer *importer,struct erofs_oci *oci); #ifdef __cplusplus }
lib/remotes/oci.c +17 -49
124 lines not on this page · raw message
mkfs/main.c +3 -3
@@ -2000,11 +2000,11 @@ int main(int argc, char **argv) goto exit;if (incremental_mode || - dataimport_mode ==EROFS_MKFS_DATA_IMPORT_RVSP ) + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_RVSP || + dataimport_mode ==EROFS_MKFS_DATA_IMPORT_ZEROFILL ) err = -EOPNOTSUPP ;else - err =ocierofs_build_trees (&importer, &ocicfg, - dataimport_mode ==EROFS_MKFS_DATA_IMPORT_ZEROFILL ); + err =ocierofs_build_trees (&importer, &ocicfg);if (err) goto exit;#endif
--
2.43.5
Also I have some other comments when trying:
Quoted 1 line
---
...
Quoted 13 lines
mkfs/main.c +1
-0@@ -32,6 +32,7 @@#include "../lib/liberofs_uuid.h" #include "../lib/liberofs_metabox.h" #include "../lib/liberofs_s3.h" +#include "../lib/liberofs_oci.h" #include "../lib/compressor.h" static struct option long_options[] = {
mkfs/main.c static struct option long_options[] = { · line 101
Quoted 6 lines
mkfs/main.c +3
-0@@ -95,6 +96,9 @@ static struct option long_options[] = { {"vmdk-desc", required_argument, NULL, 532}, #ifdef S3EROFS_ENABLED {"s3", required_argument, NULL, 533}, +#endif +#ifdef OCIEROFS_ENABLED + {"oci-options", required_argument, NULL, 534},
Can we just use --oci instead? and optional_argument.
mkfs/main.c static void usage(int argc, char **argv) · line 215
Quoted 10 lines
mkfs/main.c +3
-0#endif {0, 0, 0, 0}, };@@ -206,6 +210,14 @@ static void usage(int argc, char **argv)" [,passwd_file=Y] X=endpoint, Y=s3fs-compatible password file\n" " [,urlstyle=Z] S3 API calling style (Z = vhost|path) (default: vhost)\n" " [,sig=<2,4>] S3 API signature version (default: 2)\n" +#endif +#ifdef OCIEROFS_ENABLED +" --oci-options=X specify OCI options\n"
" --oci[=platform=X] X=platform (default: linux/amd64)\n" " [,layer=Y] Y=layer index to extract (0-based; omit to extract all layers)\n" " [,username=Z] Z=" ....
Quoted 22 lines
mkfs/main.c +20
-0+" X=platform=Y,layer=Z,username=U,password=P\n" + +static int parse_oci_password_option(const char *opt, char *options_copy) +{ +const char *p; + + p =strstr (opt,"password=" ); +if (p) { +if (erofs_oci_params_set_string (&ocicfg.params.password, + p +strlen ("password=" ))) { +erofs_err ("failed to set password" ); +free (options_copy); +return -ENOMEM ; + } +return 0 ; + } +return -EINVAL ; +} + +static int mkfs_parse_oci_options (const char *options_str)
mkfs/main.c static void usage(int argc, char **argv)
Quoted 10 lines
mkfs/main.c +10
-0+{ +char *opt, *q, *options_copy; +int ret; + +if (!options_str) +return 0 ; + + options_copy =strdup (options_str); +if (!options_copy) +return -ENOMEM ;
Could we just avoid options_copy here? see:mkfs_parse_s3_cfg() as an example.
mkfs/main.c
Quoted 12 lines
mkfs/main.c +12
-0+ + opt = options_copy; +while (opt) { + q =strchr (opt,',' ); +if (q) + *q ='\0' ; + + ret =parse_oci_platform_option (opt, options_copy); +if (ret ==0 ) { + opt = q ? q +1 :NULL ; +continue ; + }
What if ret == -ENOMEM here? it will just step down to the rest, how about just follow mkfs_parse_s3_cfg() style?
Also I have a small question:
for example, I can pull image with:
$ nerdctl pull docker.xuanyuan.me/library/ubuntu:22.04
but with mkfs.erofs:
$mkfs/mkfs.erofs --oci-options=platform=linux/amd64 image.erofs docker.xuanyuan.me/library/ubuntu:22.04 -d9 mkfs.erofs 1.8.10-g15844856-dirty
<I> erofs_io: erofs_dev_open() Line[357] successfully to open image.erofs
c_version: [1.8.10-g15844856-dirty]
c_dbg_lvl: [ 9]
c_dry_run: [ 0]<D> erofs: ocierofs_get_auth_token_with_url() Line[313] Requesting auth token from: https://docker.xuanyuan.me/token/auth.docker.io?service=docker.xuanyuan.me&scope=repository:library/ubuntu:pull
<E> erofs: ocierofs_build_trees() Line[866] failed to get manifest digest: [Error 5] Input/output error
<E> erofs: main() Line[2089] Could not format the device : [Error 5] Input/output error
It just fails, it seems it still has some issue with anonymous access?
Thanks,
Gao Xiang
PATCH v7 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci=[option] target source-image
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci=platform=linux/amd64, \
username=1234,password=1234 \
image.erofs ubuntu
--- configure.ac | 47 ++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 95 ++++ lib/remotes/oci.c | 1170 ++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 267 +++++++++- 5 files changed, 1585 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +47 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,39 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AS_IF([test "x$with_json_c" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -713,6 +755,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -785,6 +828,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -42,6 +42,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c global.c importer.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -73,6 +74,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +95 -0
95 lines not on this page · raw message
lib/remotes/oci.c new file +1170 -0
1,170 lines not on this page · raw message
mkfs/main.c +266 -1
333 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci=[option] target source-image
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci=platform=linux/amd64, \
username=1234,password=1234 \
image.erofs ubuntu
--- configure.ac | 47 ++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 95 ++++ lib/remotes/oci.c | 1170 ++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 267 +++++++++- 5 files changed, 1585 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +47 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,39 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AS_IF([test "x$with_json_c" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -713,6 +755,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -785,6 +828,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -42,6 +42,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c global.c importer.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -73,6 +74,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +95 -0
95 lines not on this page · raw message
lib/remotes/oci.c new file +1170 -0
1,170 lines not on this page · raw message
mkfs/main.c +266 -1
333 lines not on this page · raw message
Signature · 1 line
2.51.0
Quoted 269 lines · On 2025/8/27 12:48, ChengyuZhu6 wrote:
From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integrationConfigure: ./configure --enable-oci
New mkfs.erofs option: --oci=[option] target source-imageSupported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)e.g.:
- mkfs.erofs \ --oci=platform=linux/amd64, \ username=1234,password=1234 \ image.erofs ubuntuSigned-off-by Changzhi Xie <[email protected]>Signed-off-by Chengyu Zhu <[email protected]>--- configure.ac | 47 ++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 95 ++++ lib/remotes/oci.c | 1170 ++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 267 +++++++++- 5 files changed, 1585 insertions(+), 2 deletions(-)create mode 100644
lib/liberofs_oci.h
create mode 100644lib/remotes/oci.cconfigure.ac +47
-0@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])@@ -624,6 +633,39 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AS_IF([test "x$with_json_c" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [@@ -713,6 +755,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])@@ -785,6 +828,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])lib/Makefile.am +7
-1@@ -42,6 +42,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c global.c importer.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c@@ -73,6 +74,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endiflib/liberofs_oci.h new file +95
-0@@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */ +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#ifndef __EROFS_OCI_H +#define __EROFS_OCI_H + +#include <stdbool.h> + +#define DOCKER_REGISTRY "docker.io" +#define DOCKER_API_REGISTRY "registry-1.docker.io" + +#ifdef __cplusplus +extern "C" { +#endif + +struct erofs_inode ; +struct CURL ; +struct erofs_importer ; + +/** +* struct erofs_oci_params - OCI configuration parameters +* @registry: registry hostname (e.g., "registry-1.docker.io") +* @repository: image repository (e.g., "library/ubuntu") +* @tag: image tag or digest (e.g., "latest" or sha256:...) +* @platform: target platform in "os/arch" format (e.g., "linux/amd64") +* @username: username for authentication (optional) +* @password: password for authentication (optional) +* @layer_index: specific layer to extract (-1 for all layers) +* +* Configuration structure for OCI image parameters including registry +* location, image identification, platform specification, and authentication +* credentials. +*/ +struct erofs_oci_params { +char *registry; +char *repository; +char *tag; +char *platform; +char *username; +char *password; +int layer_index; +}; + +/** +* struct erofs_oci - Combined OCI client structure +* @curl: CURL handle for HTTP requests +* @params: OCI configuration parameters +* +* Main OCI client structure combining CURL HTTP client with +* OCI-specific configuration parameters. +*/ +struct erofs_oci { +struct CURL *curl; +struct erofs_oci_params params; +}; + +/* +* ocierofs_init - Initialize OCI client with default parameters +* @oci: OCI client structure to initialize +* +* Return: 0 on success, negative errno on failure +*/ +int ocierofs_init (struct erofs_oci *oci); + +/* +* ocierofs_cleanup - Clean up OCI client and free allocated resources +* @oci: OCI client structure to clean up +*/ +void ocierofs_cleanup (struct erofs_oci *oci); + +/* +* erofs_oci_params_set_string - Set a string field with dynamic allocation +* @field: pointer to the string field to set +* @value: string value to set +* +* Return: 0 on success, negative errno on failure +*/ +int erofs_oci_params_set_string (char **field,const char *value); + +/* +* ocierofs_build_trees - Build file trees from OCI container image layers +* @root: root inode to build the file tree under +* @oci: OCI client structure with configured parameters +* +* Return: 0 on success, negative errno on failure +*/ +int ocierofs_build_trees (struct erofs_importer *importer,struct erofs_oci *oci); + +#ifdef __cplusplus +} +#endif + +#endif /* __EROFS_OCI_H */ lib/remotes/oci.c new file +13
-0@@ -0,0 +1,1170 @@ +// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 +/* +* Copyright (C) 2025 Tencent, Inc. +* http://www.tencent.com/ +*/ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h>
lib/remotes/oci.c line 23
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+#include <errno.h> +#include <curl/curl.h> +#include <json-c/json.h> +#include "erofs/importer.h" +#include "erofs/internal.h" +#include "erofs/print.h" +#include "erofs/inode.h" +#include "erofs/blobchunk.h" +#include "erofs/diskbuf.h" +#include "erofs/rebuild.h"
Yifan mentioned:
It seems inode.h blobchunk.h diskbuf.h rebuild.h is redundant.
Quoted 106 lines
lib/remotes/oci.c new file +106
-0+#include "erofs/tar.h" +#include "liberofs_oci.h" +#include "liberofs_private.h" + +#define DOCKER_MEDIATYPE_MANIFEST_V2 \ + "application/vnd.docker.distribution.manifest.v2+json" +#define DOCKER_MEDIATYPE_MANIFEST_V1 \ + "application/vnd.docker.distribution.manifest.v1+json" +#define DOCKER_MEDIATYPE_MANIFEST_LIST \ + "application/vnd.docker.distribution.manifest.list.v2+json" +#define OCI_MEDIATYPE_MANIFEST "application/vnd.oci.image.manifest.v1+json" +#define OCI_MEDIATYPE_INDEX "application/vnd.oci.image.index.v1+json" + +struct erofs_oci_request { +char *url; +struct curl_slist *headers; +}; + +struct erofs_oci_response { +char *data; +size_t size; +long http_code; +}; + +struct erofs_oci_stream { +struct erofs_tarfile tarfile; +int temp_fd; +int layer_index; +}; + +static size_t ocierofs_write_callback (void *contents,size_t size, +size_t nmemb,void *userp) +{ +size_t realsize = size * nmemb; +struct erofs_oci_response *resp = userp; +char *ptr; + +if (!resp || !contents) +return 0 ; + +if (resp->data ==NULL ) + resp->size =0 ; + + ptr =realloc (resp->data, resp->size + realsize +1 ); +if (!ptr) { +erofs_err ("failed to allocate memory for response data" ); +return 0 ; + } + + resp->data = ptr; +memcpy (&resp->data[resp->size], contents, realsize); + resp->size += realsize; + resp->data[resp->size] ='\0' ; +return realsize; +} + +static size_t ocierofs_layer_write_callback (void *contents,size_t size, +size_t nmemb,void *userp) +{ +struct erofs_oci_stream *stream = userp; +size_t realsize = size * nmemb; + +if (stream->temp_fd <0 ) +return 0 ; + +if (write (stream->temp_fd, contents, realsize) != realsize) { +erofs_err ("failed to write layer data for layer %d" , + stream->layer_index); +return 0 ; + } + +return realsize; +} + +static int ocierofs_curl_setup_common_options (struct CURL *curl) +{ +curl_easy_setopt (curl,CURLOPT_FOLLOWLOCATION ,1L ); +curl_easy_setopt (curl,CURLOPT_CONNECTTIMEOUT ,30L ); +curl_easy_setopt (curl,CURLOPT_TIMEOUT ,120L ); +curl_easy_setopt (curl,CURLOPT_NOSIGNAL ,1L ); +curl_easy_setopt (curl,CURLOPT_USERAGENT ,"ocierofs/" PACKAGE_VERSION ); +return 0 ; +} + +static int ocierofs_curl_setup_basic_auth (struct CURL *curl,const char *username, +const char *password) +{ +char *userpwd; + +if (asprintf (&userpwd,"%s:%s" , username, password) ==-1 ) +return -ENOMEM ; + +curl_easy_setopt (curl,CURLOPT_USERPWD , userpwd); +curl_easy_setopt (curl,CURLOPT_HTTPAUTH ,CURLAUTH_BASIC ); + +free (userpwd); +return 0 ; +} + +static int ocierofs_curl_clear_auth (struct CURL *curl) +{ +curl_easy_setopt (curl,CURLOPT_USERPWD ,NULL ); +curl_easy_setopt (curl,CURLOPT_HTTPAUTH ,CURLAUTH_NONE ); + +return 0 ; +}
lib/remotes/oci.c line 139
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ +enum ocierofs_http_method {OCIEROFS_HTTP_GET ,OCIEROFS_HTTP_HEAD }; + +static int ocierofs_curl_setup_request_ex (struct CURL *curl,const char *url, +enum ocierofs_http_method method, +struct curl_slist *headers, +size_t (*write_func)(void *,size_t ,size_t ,void *), +void *write_data, +size_t (*header_func)(void *,size_t ,size_t ,void *), +void *header_data)
Why ocierofs_curl_setup_request_ex? it seems there is no ocierofs_curl_setup_request, so I think it would be better to rename it as ocierofs_curl_setup_request.
Quoted 85 lines
lib/remotes/oci.c new file +85
-0+{ +curl_easy_setopt (curl,CURLOPT_URL , url); + +if (method ==OCIEROFS_HTTP_HEAD ) { +curl_easy_setopt (curl,CURLOPT_NOBODY ,1L ); + }else { +curl_easy_setopt (curl,CURLOPT_NOBODY ,0L ); +curl_easy_setopt (curl,CURLOPT_HTTPGET ,1L ); + } + +if (write_func) { +curl_easy_setopt (curl,CURLOPT_WRITEFUNCTION , write_func); +curl_easy_setopt (curl,CURLOPT_WRITEDATA , write_data); + } + +curl_easy_setopt (curl,CURLOPT_HEADERFUNCTION , header_func); +curl_easy_setopt (curl,CURLOPT_HEADERDATA , header_data); + +if (headers) +curl_easy_setopt (curl,CURLOPT_HTTPHEADER , headers); + +return 0 ; +} + +static int ocierofs_curl_perform (struct CURL *curl,long *http_code_out) +{ +CURLcode res; +long http_code =0 ; + + res =curl_easy_perform (curl); +if (res !=CURLE_OK ) { +erofs_err ("curl request failed: %s" ,curl_easy_strerror (res)); +return -EIO ; + } + +if (http_code_out) { + res =curl_easy_getinfo (curl,CURLINFO_RESPONSE_CODE , &http_code); +if (res !=CURLE_OK ) { +erofs_err ("failed to get HTTP response code: %s" , +curl_easy_strerror (res)); +return -EIO ; + } + *http_code_out = http_code; + } +return 0 ; +} + +static int ocierofs_request_perform (struct erofs_oci *oci, +struct erofs_oci_request *req, +struct erofs_oci_response *resp) +{ +int ret; + + ret =ocierofs_curl_setup_request_ex (oci->curl, req->url, +OCIEROFS_HTTP_GET , + req->headers, + ocierofs_write_callback, resp, +NULL ,NULL ); +if (ret) +return ret; + + ret =ocierofs_curl_perform (oci->curl, &resp->http_code); +if (ret) +return ret; + +if (resp->http_code <200 || resp->http_code >=300 ) +return -EIO ; + +return 0 ; +} + +/** +* ocierofs_parse_auth_header - Parse WWW-Authenticate header for Bearer auth +* @auth_header: authentication header string +* @realm_out: pointer to store realm value +* @service_out: pointer to store service value +* @scope_out: pointer to store scope value +* +* Parse Bearer authentication header and extract realm, service, and scope +* parameters for subsequent token requests. +* +* Return: 0 on success, negative errno on failure +*/ +static int ocierofs_parse_auth_header(const char *auth_header, + char **realm_out, char **service_out,
lib/remotes/oci.c line 234
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ char **scope_out) +{ +const char *p, *start; +char *value; +size_t len; +char *realm =NULL , *service =NULL , *scope =NULL ; +char *header_copy =NULL ; + +if (!auth_header ||strncmp (auth_header,"Bearer " ,strlen ("Bearer " )) !=0 ) +return -EINVAL ;
I think auth_header doesn't need to be checked since it's an internal function.
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ + header_copy =strdup (auth_header); +if (!header_copy) +return -ENOMEM ; + +for (char *q = header_copy; *q; q++) { +if (*q =='\n' || *q =='\r' ) + *q =' ' ; + } +
lib/remotes/oci.c line 254
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+for (char *q = header_copy; *q; q++) { +if (*q ==' ' && *(q+1 ) ==' ' ) { +memmove (q, q+1 ,strlen (q+1 ) +1 ); + q--; + } + } + + p = header_copy +strlen ("Bearer " ); + + p =strstr (p,"realm=\"" );
Is the order of realm, service and scope fixed?
I guess it needs to be worked out as the way ofmkfs_parse_oci_options() or similar?
Quoted 82 lines
lib/remotes/oci.c new file +82
-0+if (p) { + p +=strlen ("realm=\"" ); + start = p; + p =strchr (start,'"' ); +if (p) { + len = p - start; + value =strndup (start, len); +if (!value) + goto enomem; + realm = value; + } + } + + p =strstr (header_copy,"service=\"" ); +if (p) { + p +=strlen ("service=\"" ); + start = p; + p =strchr (start,'"' ); +if (p) { + len = p - start; + value =strndup (start, len); +if (!value) + goto enomem; + service = value; + } + } + + p =strstr (header_copy,"scope=\"" ); +if (p) { + p +=strlen ("scope=\"" ); + start = p; + p =strchr (start,'"' ); +if (p) { + len = p - start; + value =strndup (start, len); +if (!value) + goto enomem; + scope = value; + } + } + +if (realm_out) { + *realm_out = realm; + realm =NULL ; + } +if (service_out) { + *service_out = service; + service =NULL ; + } +if (scope_out) { + *scope_out = scope; + scope =NULL ; + } + +free (header_copy); +free (realm); +free (service); +free (scope); +return 0 ; + +enomem: +free (header_copy); +free (realm); +free (service); +free (scope); +return -ENOMEM ; +} + +/** +* ocierofs_extract_www_auth_info - Extract WWW-Authenticate header information +* @resp_data: HTTP response data containing headers +* @realm_out: pointer to store realm value (optional) +* @service_out: pointer to store service value (optional) +* @scope_out: pointer to store scope value (optional) +* +* Extract realm, service, and scope from WWW-Authenticate header in HTTP response. +* This function handles the common pattern of parsing WWW-Authenticate headers +* that appears in multiple places in the OCI authentication flow. +* +* Return: 0 on success, negative errno on failure +*/ +static int ocierofs_extract_www_auth_info(const char *resp_data,
lib/remotes/oci.c line 346
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ char **realm_out, char **service_out, + char **scope_out) +{ + char *www_auth; +char *line_end; +char *realm =NULL , *service =NULL , *scope =NULL ; +int ret = -ENOENT ; + +if (!resp_data) +return -EINVAL ;
resp_data won't be NULL all the time.
Quoted 54 lines
lib/remotes/oci.c new file +54
-0+ + www_auth =strcasestr (resp_data,"www-authenticate:" ); +if (!www_auth) +return -ENOENT ; + + line_end =strchr (www_auth,'\n' ); +if (line_end) + *line_end ='\0' ; + + www_auth +=strlen ("www-authenticate:" ); +while (*www_auth ==' ' ) + www_auth++; +> + ret = ocierofs_parse_auth_header(www_auth, &realm, &service, &scope); +if (ret ==0 ) { +if (realm_out) { + *realm_out = realm; + realm =NULL ; + } +if (service_out) { + *service_out = service; + service =NULL ; + } +if (scope_out) { + *scope_out = scope; + scope =NULL ; + } + } + +free (realm); +free (service); +free (scope); +return ret; +} + +/** +* ocierofs_get_auth_token_with_url - Get authentication token from auth server +* @oci: OCI client structure +* @auth_url: authentication server URL +* @service: service name for authentication +* @repository: repository name +* @username: username for basic auth (optional) +* @password: password for basic auth (optional) +* +* Request authentication token from the specified auth server URL using +* basic authentication if credentials are provided. +* +* Return: authentication header string on success, ERR_PTR on failure +*/ +static char *ocierofs_get_auth_token_with_url (struct erofs_oci *oci, +const char *auth_url, +const char *service, +const char *repository, +const char *username, +const char *password)
lib/remotes/oci.c line 411
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+{ +struct erofs_oci_request req = {}; +struct erofs_oci_response resp = {}; +json_object *root, *token_obj, *access_token_obj; +const char *token; +char *auth_header =NULL ; +int ret; + +if (!auth_url || !service || !repository) +return ERR_PTR (-EINVAL );
These three checks are unneeded too.
..
Quoted 438 lines
lib/remotes/oci.c new file +438
-0+ +static char *ocierofs_discover_auth_endpoint (struct erofs_oci *oci, +const char *registry, +const char *repository) +{ +struct erofs_oci_response resp = {}; +char *realm =NULL ; +char *service =NULL ; +char *result =NULL ; +char *test_url; +const char *api_registry; +CURLcode res; +long http_code; + + api_registry = (!strcmp (registry,DOCKER_REGISTRY )) ?DOCKER_API_REGISTRY : registry; + +if (asprintf (&test_url,"https://%s/v2/%s/manifests/nonexistent" , + api_registry, repository) ==-1 ) { +return NULL ; + } + +curl_easy_reset (oci->curl); +ocierofs_curl_setup_common_options (oci->curl); + +ocierofs_curl_setup_request_ex (oci->curl, test_url, +OCIEROFS_HTTP_HEAD , +NULL , +NULL ,NULL , + ocierofs_write_callback, &resp); + + res =curl_easy_perform (oci->curl); +curl_easy_getinfo (oci->curl,CURLINFO_RESPONSE_CODE , &http_code); + +if (res ==CURLE_OK && (http_code ==401 || http_code ==403 || + http_code ==404 ) && resp.data) { +if (ocierofs_extract_www_auth_info (resp.data, &realm, &service,NULL ) ==0 ) { + result = realm; + realm =NULL ; + } + } + +free (realm); +free (service); +free (resp.data); +free (test_url); +return result; +} + +static char *ocierofs_get_auth_token (struct erofs_oci *oci,const char *registry, +const char *repository,const char *username, +const char *password) +{ +char *auth_header =NULL ; +char *discovered_auth_url =NULL ; +char *discovered_service =NULL ; +const char *service = registry; + +if (!strcmp (registry,DOCKER_API_REGISTRY ) || + !strcmp (registry,DOCKER_REGISTRY )) { + service ="registry.docker.io" ; + auth_header =ocierofs_get_auth_token_with_url (oci, +"https://auth.docker.io/token" , + service, repository, + username, password); +if (!IS_ERR (auth_header)) +return auth_header; + } + + discovered_auth_url =ocierofs_discover_auth_endpoint (oci, registry, repository); +if (discovered_auth_url) { +struct erofs_oci_response resp = {}; +char *test_url; +const char *api_registry; +CURLcode res; +long http_code; + + api_registry = (!strcmp (registry,DOCKER_REGISTRY )) ?DOCKER_API_REGISTRY : registry; + +if (asprintf (&test_url,"https://%s/v2/%s/manifests/nonexistent" , + api_registry, repository) !=-1 ) { +curl_easy_reset (oci->curl); +ocierofs_curl_setup_common_options (oci->curl); + +ocierofs_curl_setup_request_ex (oci->curl, test_url, +OCIEROFS_HTTP_HEAD , +NULL , +NULL ,NULL , + ocierofs_write_callback, &resp); + + res =curl_easy_perform (oci->curl); +curl_easy_getinfo (oci->curl,CURLINFO_RESPONSE_CODE , &http_code); + +if (res ==CURLE_OK && (http_code ==401 || http_code ==403 || + http_code ==404 ) && resp.data) { +char *realm =NULL ; + +ocierofs_extract_www_auth_info (resp.data, &realm, &discovered_service,NULL ); +free (realm); + } +free (resp.data); +free (test_url); + } + +const char *auth_service = discovered_service ? discovered_service : service; + + auth_header =ocierofs_get_auth_token_with_url (oci, discovered_auth_url, + auth_service, repository, + username, password); +free (discovered_auth_url); +free (discovered_service); +if (!IS_ERR (auth_header)) +return auth_header; + } + +static const char *const auth_patterns[] = { +"https://%s/v2/auth" , +"https://auth.%s/token" , +"https://%s/token" , +NULL + }; + +for (int i =0 ; auth_patterns[i]; i++) { +char *auth_url; + +if (asprintf (&auth_url, auth_patterns[i], registry) ==-1 ) +continue ; + + auth_header =ocierofs_get_auth_token_with_url (oci, auth_url, + service, repository, + username, password); +free (auth_url); + +if (!IS_ERR (auth_header)) +return auth_header; + +if (strcmp (registry,DOCKER_API_REGISTRY ) && +strcmp (registry,DOCKER_REGISTRY )) { +break ; + } + } + +if (strcmp (registry,DOCKER_API_REGISTRY ) && +strcmp (registry,DOCKER_REGISTRY )) { +return NULL ; + } + +return ERR_PTR (-ENOENT ); +} + +static char *ocierofs_get_manifest_digest (struct erofs_oci *oci, +const char *registry, +const char *repository,const char *tag, +const char *platform, +const char *auth_header) +{ +struct erofs_oci_request req = {}; +struct erofs_oci_response resp = {}; +json_object *root, *manifests, *manifest, *platform_obj, *arch_obj; +json_object *os_obj, *digest_obj, *schema_obj, *media_type_obj; +char *digest =NULL ; +const char *api_registry; +int ret =0 , len, i; + +if (!registry || !repository || !tag || !platform) +return ERR_PTR (-EINVAL ); + +memset (&resp,0 ,sizeof (resp)); + + api_registry = (!strcmp (registry,DOCKER_REGISTRY )) ?DOCKER_API_REGISTRY : registry; + +if (asprintf (&req.url,"https://%s/v2/%s/manifests/%s" , + api_registry, repository, tag) ==-1 ) { +return ERR_PTR (-ENOMEM ); + } + +if (auth_header &&strstr (auth_header,"Bearer" )) + req.headers =curl_slist_append (req.headers, auth_header); + + req.headers =curl_slist_append (req.headers, +"Accept: " DOCKER_MEDIATYPE_MANIFEST_LIST "," +OCI_MEDIATYPE_INDEX "," DOCKER_MEDIATYPE_MANIFEST_V1 "," +DOCKER_MEDIATYPE_MANIFEST_V2 ); + + ret =ocierofs_request_perform (oci, &req, &resp); +if (ret) + goto out; + +if (!resp.data) { +erofs_err ("empty response from manifest request" ); + ret = -EINVAL ; + goto out; + } + + root =json_tokener_parse (resp.data); +if (!root) { +erofs_err ("failed to parse manifest JSON" ); + ret = -EINVAL ; + goto out; + } + +if (json_object_object_get_ex (root,"schemaVersion" , &schema_obj)) { +if (json_object_get_int (schema_obj) ==1 ) { + digest =strdup (tag); + ret =0 ; + goto out_json; + } + } + +if (json_object_object_get_ex (root,"mediaType" , &media_type_obj)) { +const char *media_type =json_object_get_string (media_type_obj); + +if (!strcmp (media_type,DOCKER_MEDIATYPE_MANIFEST_V2 ) || + !strcmp (media_type,OCI_MEDIATYPE_MANIFEST )) { + digest =strdup (tag); + ret =0 ; + goto out_json; + } + } + +if (!json_object_object_get_ex (root,"manifests" , &manifests)) { +erofs_err ("no manifests found in manifest list" ); + ret = -EINVAL ; + goto out_json; + } + + len =json_object_array_length (manifests); +for (i =0 ; i < len; i++) { + manifest =json_object_array_get_idx (manifests, i); + +if (json_object_object_get_ex (manifest,"platform" , + &platform_obj) && +json_object_object_get_ex (platform_obj,"architecture" , + &arch_obj) && +json_object_object_get_ex (platform_obj,"os" , &os_obj) && +json_object_object_get_ex (manifest,"digest" , &digest_obj)) { +const char *arch =json_object_get_string (arch_obj); +const char *os =json_object_get_string (os_obj); +char manifest_platform[64 ]; + +snprintf (manifest_platform,sizeof (manifest_platform), +"%s/%s" , os, arch); +if (!strcmp (manifest_platform, platform)) { + digest =strdup (json_object_get_string (digest_obj)); +break ; + } + } + } + +if (!digest) + ret = -ENOENT ; + +out_json: +json_object_put (root); +out: +free (resp.data); +if (req.headers) +curl_slist_free_all (req.headers); +free (req.url); + +return ret ?ERR_PTR (ret) : digest; +} + +static char **ocierofs_get_layers_info (struct erofs_oci *oci, +const char *registry, +const char *repository, +const char *digest, +const char *auth_header, +int *layer_count) +{ +struct erofs_oci_request req = {}; +struct erofs_oci_response resp = {}; +json_object *root, *layers, *layer, *digest_obj; +char **layers_info =NULL ; +const char *api_registry; +int ret, len, i; + +if (!registry || !repository || !digest || !layer_count) +return ERR_PTR (-EINVAL ); + + *layer_count =0 ; +memset (&resp,0 ,sizeof (resp)); + api_registry = (!strcmp (registry,DOCKER_REGISTRY )) ?DOCKER_API_REGISTRY : registry; + +if (asprintf (&req.url,"https://%s/v2/%s/manifests/%s" , + api_registry, repository, digest) ==-1 ) { +return ERR_PTR (-ENOMEM ); + } + +if (auth_header &&strstr (auth_header,"Bearer" )) + req.headers =curl_slist_append (req.headers, auth_header); + + req.headers =curl_slist_append (req.headers, +"Accept: " OCI_MEDIATYPE_MANIFEST "," DOCKER_MEDIATYPE_MANIFEST_V2 ); + + ret =ocierofs_request_perform (oci, &req, &resp); +if (ret) + goto out; + +if (!resp.data) { +erofs_err ("empty response from layers request" ); + ret = -EINVAL ; + goto out; + } + + root =json_tokener_parse (resp.data); +if (!root) { +erofs_err ("failed to parse manifest JSON" ); + ret = -EINVAL ; + goto out; + } + +if (!json_object_object_get_ex (root,"layers" , &layers) || +json_object_get_type (layers) != json_type_array) { +erofs_err ("no layers found in manifest" ); + ret = -EINVAL ; + goto out_json; + } + + len =json_object_array_length (layers); +if (len ==0 ) { +erofs_err ("empty layer list in manifest" ); + ret = -EINVAL ; + goto out_json; + } + + layers_info =calloc (len,sizeof (char *)); +if (!layers_info) { + ret = -ENOMEM ; + goto out_json; + } + +for (i =0 ; i < len; i++) { + layer =json_object_array_get_idx (layers, i); + +if (!json_object_object_get_ex (layer,"digest" , &digest_obj)) { +erofs_err ("failed to parse layer %d" , i); + ret = -EINVAL ; + goto out_free; + } + + layers_info[i] =strdup (json_object_get_string (digest_obj)); +if (!layers_info[i]) { + ret = -ENOMEM ; + goto out_free; + } + } + + *layer_count = len; +json_object_put (root); +free (resp.data); +if (req.headers) +curl_slist_free_all (req.headers); +free (req.url); +return layers_info; + +out_free: +if (layers_info) { +for (int j =0 ; j < i; j++) +free (layers_info[j]); + } +free (layers_info); +out_json: +json_object_put (root); +out: +free (resp.data); +if (req.headers) +curl_slist_free_all (req.headers); +free (req.url); +return ERR_PTR (ret); +} + +static int ocierofs_extract_layer (struct erofs_oci *oci,struct erofs_importer *importer, +const char *layer_digest,const char *auth_header, +int layer_index) +{ +struct erofs_oci_request req = {}; +struct erofs_oci_stream stream = {}; +const char *api_registry; +int ret, fd =-1 ; +long http_code; + +if (!oci || !importer || !layer_digest || layer_index <0 ) { +erofs_err ("invalid parameters for layer extraction" ); +return -EINVAL ; + } + +memset (&stream,0 ,sizeof (stream)); + stream.layer_index = layer_index; + + stream.temp_fd =erofs_tmpfile (); +if (stream.temp_fd <0 ) { +erofs_err ("failed to create temporary file for layer %d" , + layer_index); +return -errno; + } + + api_registry = (!strcmp (oci->params.registry,DOCKER_REGISTRY )) ? +DOCKER_API_REGISTRY : oci->params.registry; + +if (asprintf (&req.url,"https://%s/v2/%s/blobs/%s" , + api_registry, oci->params.repository, + layer_digest) ==-1 ) { + ret = -ENOMEM ; + goto out; + } + +if (auth_header &&strstr (auth_header,"Bearer" )) + req.headers =curl_slist_append (req.headers, auth_header); + +curl_easy_reset (oci->curl); + + ret =ocierofs_curl_setup_common_options (oci->curl); +if (ret) + goto out; + + ret =ocierofs_curl_setup_request_ex (oci->curl, req.url, +OCIEROFS_HTTP_GET , + req.headers, + ocierofs_layer_write_callback, + &stream, +NULL ,NULL ); +if (ret) + goto out; + + ret =ocierofs_curl_perform (oci->curl, &http_code); +if (ret) + goto out; + +if (http_code <200 || http_code >=300 ) { +erofs_err ("HTTP request failed with code %ld" , http_code); + ret = -EIO ; + goto out; + } + +if (lseek (stream.temp_fd,0 ,SEEK_SET ) <0 ) { +erofs_err ("failed to seek to beginning of temp file: %s" , +strerror (errno)); + ret = -errno;
lib/remotes/oci.c line 921
Quoted 10 lines
lib/remotes/oci.c new file +10
-0+ goto out; + } + + fd =dup (stream.temp_fd); +if (fd <0 ) { +erofs_err ("failed to duplicate temp file descriptor: %s" , +strerror (errno)); + ret = -errno; + goto out; + }
Just some quick quesion:
why we need dup a fd for this? the fd position is the same so
I don't see dup here is needed...
lib/remotes/oci.c line 925
Quoted 4 lines
lib/remotes/oci.c new file +4
-0+ +memset (&stream.tarfile,0 ,sizeof (stream.tarfile)); +init_list_head (&stream.tarfile.global.xattrs); +
Thanks,
Gao Xiang
PATCH v8 1/1 erofs-utils: add OCI registry support
In reply to ChengyuZhu6From: Chengyu Zhu <[email protected]>
This patch adds support for building EROFS filesystems from OCI-compliant container registries, enabling users to create EROFS images directly from container images stored in registries like Docker Hub, Quay.io, etc.
The implementation includes:
- OCI remote backend with registry authentication support
- Manifest parsing for Docker v2 and OCI v1 formats
- Layer extraction and tar processing integration
- Multi-platform image selection capability
- Both anonymous and authenticated registry access
- Comprehensive build system integration
Configure: ./configure --enable-oci
New mkfs.erofs option: --oci=[option] target source-image
Supported options:
- platform=os/arch (default: linux/amd64)
- layer=N (extract specific layer, default: all layers)
- username/password (basic authentication)
e.g.:
- mkfs.erofs \
--oci=platform=linux/amd64, \
username=1234,password=1234 \
image.erofs ubuntu
--- configure.ac | 47 ++ lib/Makefile.am | 8 +- lib/liberofs_oci.h | 95 ++++ lib/remotes/oci.c | 1147 ++++++++++++++++++++++++++++++++++++++++++++ mkfs/main.c | 267 ++++++++++- 5 files changed, 1562 insertions(+), 2 deletions(-) create mode 100644 lib/liberofs_oci.h create mode 100644 lib/remotes/oci.c
configure.ac +47 -0
@@ -177,10 +177,19 @@ AC_ARG_WITH(libxml2, [AS_HELP_STRING([--with-libxml2], [Enable and build with libxml2 support @<:@default=auto@:>@])]) +AC_ARG_WITH(json_c, + [AS_HELP_STRING([--with-json-c], + [Enable and build with json-c support @<:@default=auto@:>@])]) + AC_ARG_ENABLE(s3, [AS_HELP_STRING([--enable-s3], [enable s3 image generation support @<:@default=no@:>@])], [enable_s3="$enableval"], [enable_s3="no"]) +AC_ARG_ENABLE(oci, + AS_HELP_STRING([--enable-oci], + [enable OCI registry based input support @<:@default=no@:>@]), + [enable_oci="$enableval"],[enable_oci="no"]) + AC_ARG_ENABLE(fuse, [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])], [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -624,6 +633,39 @@ AS_IF([test "x$with_libcurl" != "xno"], [ ]) ]) +# Configure json-c +have_json_c="no" +AS_IF([test "x$with_json_c" != "xno"], [ + PKG_CHECK_MODULES([json_c], [json-c], [ + saved_LIBS="$LIBS" + saved_CPPFLAGS=${CPPFLAGS} + CPPFLAGS="${json_c_CFLAGS} ${CPPFLAGS}" + LIBS="${json_c_LIBS} $LIBS" + AC_CHECK_HEADERS([json-c/json.h],[ + AC_CHECK_DECL(json_tokener_parse, [have_json_c="yes"], + [AC_MSG_ERROR([json-c doesn't work properly])], [[ +#include <json-c/json.h> + ]]) + ]) + LIBS="${saved_LIBS}" + CPPFLAGS="${saved_CPPFLAGS}" + ], [ + AS_IF([test "x$with_json_c" = "xyes"], [ + AC_MSG_ERROR([Cannot find proper json-c]) + ]) + ]) +]) + +# Validate dependencies for OCI registry +AS_IF([test "x$enable_oci" = "xyes"], [ + AS_IF([test "x$have_libcurl" = "xyes" -a "x$have_json_c" = "xyes"], [ + have_oci="yes" + ], [ + have_oci="no" + AC_MSG_ERROR([OCI registry disabled: missing libcurl or json-c]) + ]) +], [have_oci="no"]) + # Configure openssl have_openssl="no" AS_IF([test "x$with_openssl" != "xno"], [
@@ -713,6 +755,7 @@ AM_CONDITIONAL([ENABLE_OPENSSL], [test "x${have_openssl}" = "xyes"]) AM_CONDITIONAL([ENABLE_LIBXML2], [test "x${have_libxml2}" = "xyes"]) AM_CONDITIONAL([ENABLE_S3], [test "x${have_s3}" = "xyes"]) AM_CONDITIONAL([ENABLE_STATIC_FUSE], [test "x${enable_static_fuse}" = "xyes"]) +AM_CONDITIONAL([ENABLE_OCI], [test "x${have_oci}" = "xyes"]) if test "x$have_uuid" = "xyes"; then AC_DEFINE([HAVE_LIBUUID], 1, [Define to 1 if libuuid is found])
@@ -785,6 +828,10 @@ if test "x$have_s3" = "xyes"; then AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 is enabled]) fi +if test "x$have_oci" = "xyes"; then + AC_DEFINE([OCIEROFS_ENABLED], 1, [Define to 1 if OCI registry is enabled]) +fi + # Dump maximum block size AS_IF([test "x$erofs_cv_max_block_size" = "x"], [$erofs_cv_max_block_size = 4096], [])
lib/Makefile.am +7 -1
@@ -42,6 +42,7 @@ liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \ vmdk.c metabox.c global.c importer.c liberofs_la_CFLAGS = -Wall ${libuuid_CFLAGS} -I$(top_srcdir)/include +liberofs_la_LDFLAGS = if ENABLE_LZ4 liberofs_la_CFLAGS += ${liblz4_CFLAGS} liberofs_la_SOURCES += compressor_lz4.c
@@ -73,6 +74,11 @@ if ENABLE_S3 liberofs_la_SOURCES += remotes/s3.c endif if ENABLE_EROFS_MT -liberofs_la_LDFLAGS = -lpthread +liberofs_la_LDFLAGS += -lpthread liberofs_la_SOURCES += workqueue.c endif +if ENABLE_OCI +liberofs_la_SOURCES += remotes/oci.c +liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${json_c_CFLAGS} +liberofs_la_LDFLAGS += ${libcurl_LIBS} ${json_c_LIBS} +endif
lib/liberofs_oci.h new file +95 -0
95 lines not on this page · raw message
lib/remotes/oci.c new file +1147 -0
1,147 lines not on this page · raw message
mkfs/main.c +266 -1
333 lines not on this page · raw message
Signature · 1 line
2.51.0