Skip to contentSkip to conversation

erofs-utils: mkfs: introduce source_mode enumeration

PATCH Superseded Gao Xiang 2 participants 7 messages Aug 7, 2025 Library code

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH v6 1/4 erofs-utils: mkfs: introduce source_mode enumeration

From: Yifan Zhao <[email protected]>

Currently, mkfs controls different image build execution flows through the global variables tar_mode and rebuild_mode, while these two modes together with localdir mode are mutually exclusive.

Let's replace them with a new variable source_mode to simplify the logic.

---
 mkfs/main.c | 78 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 44 insertions(+), 34 deletions(-)
mkfs/main.c +44 -34
index dc2df06..ab27b77 100644
@@ -244,7 +244,7 @@ static int pclustersize_metabox = -1;
 static struct erofs_tarfile erofstar = {
 	.global.xattrs = LIST_HEAD_INIT(erofstar.global.xattrs)
 };
-static bool tar_mode, rebuild_mode, incremental_mode;
+static bool incremental_mode;
 static u8 metabox_algorithmid;
 
 enum {
@@ -254,6 +254,12 @@ enum {
 	EROFS_MKFS_DATA_IMPORT_SPARSE,
 } dataimport_mode;
 
+static enum {
+	EROFS_MKFS_SOURCE_LOCALDIR,
+	EROFS_MKFS_SOURCE_TAR,
+	EROFS_MKFS_SOURCE_REBUILD,
+} source_mode;
+
 static unsigned int rebuild_src_count, total_ccfgs;
 static LIST_HEAD(rebuild_src_list);
 static u8 fixeduuid[16];
@@ -499,7 +505,7 @@ static void mkfs_parse_tar_cfg(char *cfg)
 {
 	char *p;
 
-	tar_mode = true;
+	source_mode = EROFS_MKFS_SOURCE_TAR;
 	if (!cfg)
 		return;
 	p = strchr(cfg, ',');
@@ -616,7 +622,30 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
 	int err, fd;
 	char *s;
 
-	if (tar_mode) {
+	switch (source_mode) {
+	case EROFS_MKFS_SOURCE_LOCALDIR:
+		err = lstat((s = argv[optind++]), &st);
+		if (err) {
+			erofs_err("failed to stat %s: %s", s,
+				  erofs_strerror(-errno));
+			return -ENOENT;
+		}
+		if (S_ISDIR(st.st_mode)) {
+			cfg.c_src_path = realpath(s, NULL);
+			if (!cfg.c_src_path) {
+				erofs_err("failed to parse source directory: %s",
+					  erofs_strerror(-errno));
+				return -ENOENT;
+			}
+			erofs_set_fs_root(cfg.c_src_path);
+		} else {
+			cfg.c_src_path = strdup(s);
+			if (!cfg.c_src_path)
+				return -ENOMEM;
+			source_mode = EROFS_MKFS_SOURCE_REBUILD;
+		}
+		break;
+	case EROFS_MKFS_SOURCE_TAR:
 		cfg.c_src_path = strdup(argv[optind++]);
 		if (!cfg.c_src_path)
 			return -ENOMEM;
@@ -640,30 +669,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
 			}
 			erofstar.ios.dumpfd = fd;
 		}
-	} else {
-		err = lstat((s = argv[optind++]), &st);
-		if (err) {
-			erofs_err("failed to stat %s: %s", s,
-				  erofs_strerror(-errno));
-			return -ENOENT;
-		}
-		if (S_ISDIR(st.st_mode)) {
-			cfg.c_src_path = realpath(s, NULL);
-			if (!cfg.c_src_path) {
-				erofs_err("failed to parse source directory: %s",
-					  erofs_strerror(-errno));
-				return -ENOENT;
-			}
-			erofs_set_fs_root(cfg.c_src_path);
-		} else {
-			cfg.c_src_path = strdup(s);
-			if (!cfg.c_src_path)
-				return -ENOMEM;
-			rebuild_mode = true;
-		}
+		break;
+	default:
+		erofs_err("unexpected source_mode: %d", source_mode);
+		return -EINVAL;
 	}
 
-	if (rebuild_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
 		char *srcpath = cfg.c_src_path;
 		struct erofs_sb_info *src;
 
@@ -1083,7 +1095,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
 		err = mkfs_parse_sources(argc, argv, optind);
 		if (err)
 			return err;
-	} else if (!tar_mode) {
+	} else if (source_mode != EROFS_MKFS_SOURCE_TAR) {
 		erofs_err("missing argument: SOURCE(s)");
 		return -EINVAL;
 	} else {
@@ -1383,7 +1395,7 @@ int main(int argc, char **argv)
 	if (cfg.c_random_pclusterblks)
 		srand(time(NULL));
 #endif
-	if (tar_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
 		if (dataimport_mode == EROFS_MKFS_DATA_IMPORT_RVSP)
 			erofstar.rvsp_mode = true;
 		erofstar.dev = rebuild_src_count + 1;
@@ -1403,9 +1415,7 @@ int main(int argc, char **argv)
 			g_sbi.blkszbits = 9;
 			tar_index_512b = true;
 		}
-	}
-
-	if (rebuild_mode) {
+	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
 		struct erofs_sb_info *src;
 
 		erofs_warn("EXPERIMENTAL rebuild mode in use. Use at your own risk!");
@@ -1465,7 +1475,7 @@ int main(int argc, char **argv)
 	else if (!incremental_mode)
 		erofs_uuid_generate(g_sbi.uuid);
 
-	if (tar_mode && !erofstar.index_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR && !erofstar.index_mode) {
 		err = erofs_diskbuf_init(1);
 		if (err) {
 			erofs_err("failed to initialize diskbuf: %s",
@@ -1528,7 +1538,7 @@ int main(int argc, char **argv)
 
 	erofs_inode_manager_init();
 
-	if (tar_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
 		root = erofs_rebuild_make_root(&g_sbi);
 		if (IS_ERR(root)) {
 			err = PTR_ERR(root);
@@ -1543,7 +1553,7 @@ int main(int argc, char **argv)
 		err = erofs_rebuild_dump_tree(root, incremental_mode);
 		if (err < 0)
 			goto exit;
-	} else if (rebuild_mode) {
+	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
 		root = erofs_rebuild_make_root(&g_sbi);
 		if (IS_ERR(root)) {
 			err = PTR_ERR(root);
@@ -1663,7 +1673,7 @@ exit:
 	erofs_rebuild_cleanup();
 	erofs_diskbuf_exit();
 	erofs_exit_configure();
-	if (tar_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
 		erofs_iostream_close(&erofstar.ios);
 		if (erofstar.ios.dumpfd >= 0)
 			close(erofstar.ios.dumpfd);
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH v6 2/4 erofs-utils: introduce extra build dependencies for S3 support

From: Yifan Zhao <[email protected]>

This patch adds additional dependencies on libcurl, openssl and libxml2 library for the upcoming S3 data source support, with libcurl to interact with S3 API, openssl to generate S3 auth signature and libxml2 to parse response body.

erofs-utils: introduce extra build dependencies for S3 support

This patch adds additional dependencies on libcurl, openssl, and libxml2 for the upcoming S3 data source support:
- libcurl is used to interact with the S3 API;
- openssl is used to generate S3 authentication signatures;
- libxml2 is used to parse response bodies;

These dependencies are optional and controlled using the --with-{libcurl,openssl,libxml2} configure options.

Additionally, a new --enable-s3 option is introduced to enable S3 support. It will report an error if any of the three required libraries doesn't work properly.

---
 configure.ac     | 129 ++++++++++++++++++++++++++++++++++++++++++++++-
 lib/Makefile.am  |   1 +
 mkfs/Makefile.am |   3 +-
 3 files changed, 131 insertions(+), 2 deletions(-)
configure.ac +128 -1
index da6ae48..7769ac9 100644
@@ -24,7 +24,7 @@ esac
 # OS-specific treatment
 AM_CONDITIONAL([OS_LINUX], [test "$build_linux" = "yes"])
 
-AM_INIT_AUTOMAKE([foreign -Wall])
+AM_INIT_AUTOMAKE([foreign subdir-objects -Wall])
 
 # Checks for programs.
 AM_PROG_AR
@@ -165,6 +165,22 @@ AC_ARG_WITH(xxhash,
    [AS_HELP_STRING([--with-xxhash],
       [Enable and build with libxxhash support @<:@default=auto@:>@])])
 
+AC_ARG_WITH(libcurl,
+   [AS_HELP_STRING([--with-libcurl],
+      [Enable and build with libcurl support @<:@default=auto@:>@])])
+
+AC_ARG_WITH(openssl,
+   [AS_HELP_STRING([--with-openssl],
+      [Enable and build with openssl support @<:@default=auto@:>@])])
+
+AC_ARG_WITH(libxml2,
+   [AS_HELP_STRING([--with-libxml2],
+      [Enable and build with libxml2 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(fuse,
    [AS_HELP_STRING([--enable-fuse], [enable erofsfuse @<:@default=no@:>@])],
    [enable_fuse="$enableval"], [enable_fuse="no"])
@@ -583,6 +599,91 @@ AS_IF([test "x$with_xxhash" != "xno"], [
   ])
 ])
 
+# Configure libcurl
+have_libcurl="no"
+AS_IF([test "x$with_libcurl" != "xno"], [
+  PKG_CHECK_MODULES([libcurl], [libcurl], [
+    # Paranoia: don't trust the result reported by pkgconfig before trying out
+    saved_LIBS="$LIBS"
+    saved_CPPFLAGS=${CPPFLAGS}
+    CPPFLAGS="${libcurl_CFLAGS} ${CPPFLAGS}"
+    LIBS="${libcurl_LIBS} $LIBS"
+    AC_CHECK_HEADERS([curl/curl.h],[
+      AC_CHECK_LIB(curl, curl_easy_perform, [], [
+        AC_MSG_ERROR([libcurl doesn't work properly])])
+      AC_CHECK_DECL(curl_easy_perform, [have_libcurl="yes"],
+        [AC_MSG_ERROR([libcurl doesn't work properly])], [[
+#include <curl/curl.h>
+      ]])
+    ])
+    LIBS="${saved_LIBS}"
+    CPPFLAGS="${saved_CPPFLAGS}"], [
+    AS_IF([test "x$with_libcurl" = "xyes"], [
+      AC_MSG_ERROR([Cannot find proper libcurl])
+    ])
+  ])
+])
+
+# Configure openssl
+have_openssl="no"
+AS_IF([test "x$with_openssl" != "xno"], [
+  PKG_CHECK_MODULES([openssl], [openssl], [
+    # Paranoia: don't trust the result reported by pkgconfig before trying out
+    saved_LIBS="$LIBS"
+    saved_CPPFLAGS=${CPPFLAGS}
+    CPPFLAGS="${openssl_CFLAGS} ${CPPFLAGS}"
+    LIBS="${openssl_LIBS} $LIBS"
+    AC_CHECK_HEADERS([openssl/hmac.h],[
+      AC_CHECK_LIB(ssl, EVP_sha1, [], [
+        AC_MSG_ERROR([openssl doesn't work properly])])
+      AC_CHECK_DECL(EVP_sha1, [have_openssl="yes"],
+        [AC_MSG_ERROR([openssl doesn't work properly])], [[
+#include <openssl/hmac.h>
+      ]])
+    ])
+    LIBS="${saved_LIBS}"
+    CPPFLAGS="${saved_CPPFLAGS}"], [
+    AS_IF([test "x$with_openssl" = "xyes"], [
+      AC_MSG_ERROR([Cannot find proper openssl])
+    ])
+  ])
+])
+
+# Configure libxml2
+have_libxml2="no"
+AS_IF([test "x$with_libxml2" != "xno"], [
+  PKG_CHECK_MODULES([libxml2], [libxml-2.0], [
+    # Paranoia: don't trust the result reported by pkgconfig before trying out
+    saved_LIBS="$LIBS"
+    saved_CPPFLAGS=${CPPFLAGS}
+    CPPFLAGS="${libxml2_CFLAGS} ${CPPFLAGS}"
+    LIBS="${libxml2_LIBS} $LIBS"
+    AC_CHECK_HEADERS([libxml/parser.h],[
+      AC_CHECK_LIB(xml2, xmlReadMemory, [], [
+        AC_MSG_ERROR([libxml2 doesn't work properly])])
+      AC_CHECK_DECL(xmlReadMemory, [have_libxml2="yes"],
+        [AC_MSG_ERROR([libxml2 doesn't work properly])], [[
+#include <libxml/parser.h>
+      ]])
+    ])
+    LIBS="${saved_LIBS}"
+    CPPFLAGS="${saved_CPPFLAGS}"], [
+    AS_IF([test "x$with_libxml2" = "xyes"], [
+      AC_MSG_ERROR([Cannot find proper libxml2])
+    ])
+  ])
+])
+
+AS_IF([test "x$enable_s3" != "xno"], [
+  AS_IF(
+    [test "x$have_libcurl" = "xyes" && \
+     test "x$have_openssl" = "xyes" && \
+     test "x$have_libxml2" = "xyes"],
+    [have_s3="yes"],
+    [have_s3="no"
+     AC_MSG_ERROR([S3 disabled: missing libcurl, openssl, or libxml2])])
+  ], [have_s3="no"])
+
 # Enable 64-bit off_t
 CFLAGS+=" -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
 
@@ -606,6 +707,10 @@ AM_CONDITIONAL([ENABLE_LIBDEFLATE], [test "x${have_libdeflate}" = "xyes"])
 AM_CONDITIONAL([ENABLE_LIBZSTD], [test "x${have_libzstd}" = "xyes"])
 AM_CONDITIONAL([ENABLE_QPL], [test "x${have_qpl}" = "xyes"])
 AM_CONDITIONAL([ENABLE_XXHASH], [test "x${have_xxhash}" = "xyes"])
+AM_CONDITIONAL([ENABLE_LIBCURL], [test "x${have_libcurl}" = "xyes"])
+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"])
 
 if test "x$have_uuid" = "xyes"; then
@@ -657,6 +762,28 @@ if test "x$have_xxhash" = "xyes"; then
   AC_DEFINE([HAVE_XXHASH], 1, [Define to 1 if xxhash is found])
 fi
 
+if test "x$have_libcurl" = "xyes"; then
+  AC_DEFINE([HAVE_LIBCURL], 1, [Define to 1 if libcurl is found])
+  AC_SUBST([libcurl_LIBS])
+  AC_SUBST([libcurl_CFLAGS])
+fi
+
+if test "x$have_openssl" = "xyes"; then
+  AC_DEFINE([HAVE_OPENSSL], 1, [Define to 1 if openssl is found])
+  AC_SUBST([openssl_LIBS])
+  AC_SUBST([openssl_CFLAGS])
+fi
+
+if test "x$have_libxml2" = "xyes"; then
+  AC_DEFINE([HAVE_LIBXML2], 1, [Define to 1 if libxml2 is found])
+  AC_SUBST([libxml2_LIBS])
+  AC_SUBST([libxml2_CFLAGS])
+fi
+
+if test "x$have_s3" = "xyes"; then
+  AC_DEFINE([S3EROFS_ENABLED], 1, [Define to 1 if s3 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 +1 -0
index 0db81df..6458acf 100644
@@ -66,6 +66,7 @@ liberofs_la_CFLAGS += ${libxxhash_CFLAGS}
 else
 liberofs_la_SOURCES += xxhash.c
 endif
+liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS}
 if ENABLE_EROFS_MT
 liberofs_la_LDFLAGS = -lpthread
 liberofs_la_SOURCES += workqueue.c
mkfs/Makefile.am +2 -1
index 2499242..b84b4c1 100644
@@ -7,4 +7,5 @@ mkfs_erofs_SOURCES = main.c
 mkfs_erofs_CFLAGS = -Wall -I$(top_srcdir)/include
 mkfs_erofs_LDADD = $(top_builddir)/lib/liberofs.la ${libselinux_LIBS} \
 	${libuuid_LIBS} ${liblz4_LIBS} ${liblzma_LIBS} ${zlib_LIBS} \
-	${libdeflate_LIBS} ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS}
+	${libdeflate_LIBS} ${libzstd_LIBS} ${libqpl_LIBS} ${libxxhash_LIBS} \
+	${libcurl_LIBS} ${openssl_LIBS} ${libxml2_LIBS}
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH v6 3/4 erofs-utils: mkfs: introduce --s3=... option

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

It introduces configuration options for the upcoming experimental S3 support, including configuration parsing and passwd_file reading logic.

Users can specify the following options:
- S3 service endpoint (required);
- S3 credentials file in the format $ak:%sk (optional);
- S3 API calling style (optional);
- S3 API signature version (optional, only V2 is currently supported).

---
 lib/liberofs_s3.h |  40 ++++++++
 mkfs/main.c       | 226 +++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 234 insertions(+), 32 deletions(-)
 create mode 100644 lib/liberofs_s3.h
lib/liberofs_s3.h new file +40 -0
new file mode 100644
index 0000000..fbf2f80
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+/*
+ * Copyright (C) 2025 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Yifan Zhao <[email protected]>
+ */
+#ifndef __EROFS_S3_H
+#define __EROFS_S3_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum s3erofs_url_style {
+    S3EROFS_URL_STYLE_PATH,          // Path style: https://s3.amazonaws.com/bucket/object
+    S3EROFS_URL_STYLE_VIRTUAL_HOST,  // Virtual host style: https://bucket.s3.amazonaws.com/object
+};
+
+enum s3erofs_signature_version {
+	S3EROFS_SIGNATURE_VERSION_2,
+	S3EROFS_SIGNATURE_VERSION_4,
+};
+
+#define S3_ACCESS_KEY_LEN 256
+#define S3_SECRET_KEY_LEN 256
+
+struct erofs_s3 {
+	const char *endpoint, *bucket;
+	char access_key[S3_ACCESS_KEY_LEN + 1];
+	char secret_key[S3_SECRET_KEY_LEN + 1];
+
+	enum s3erofs_url_style url_style;
+	enum s3erofs_signature_version sig;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
mkfs/main.c +194 -32

301 lines not on this page · raw message

Signature · 1 line
2.43.5
#3 lore raw

Re: [PATCH v6 3/4] erofs-utils: mkfs: introduce --s3=... option

Hi Xiang,

lib/liberofs_s3.h

Quoted 25 lines · Gao Xiang

From: Yifan Zhao <[email protected]>

It introduces configuration options for the upcoming experimental S3 support, including configuration parsing and passwd_file reading logic.

Users can specify the following options:
- S3 service endpoint (required);
- S3 credentials file in the format $ak:%sk (optional);
- S3 API calling style (optional);
- S3 API signature version (optional, only V2 is currently supported).

---
  lib/liberofs_s3.h |  40 ++++++++
  mkfs/main.c       | 226 +++++++++++++++++++++++++++++++++++++++-------
  2 files changed, 234 insertions(+), 32 deletions(-)

create mode 100644 lib/liberofs_s3.h

lib/liberofs_s3.h new file +0 -0
new file mode 100644
index 0000000..fbf2f80

How about moving liberofs_s3.h to include/erofs/? Because s3erofs_build_trees can also be exported and used in other cases.

Thanks,
Hongbo

Quoted 357 lines
lib/liberofs_s3.h new file +40 -0
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
+/*
+ * Copyright (C) 2025 HUAWEI, Inc.
+ *             http://www.huawei.com/
+ * Created by Yifan Zhao <[email protected]>
+ */
+#ifndef __EROFS_S3_H
+#define __EROFS_S3_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum s3erofs_url_style {
+    S3EROFS_URL_STYLE_PATH,          // Path style: https://s3.amazonaws.com/bucket/object
+    S3EROFS_URL_STYLE_VIRTUAL_HOST,  // Virtual host style: https://bucket.s3.amazonaws.com/object
+};
+
+enum s3erofs_signature_version {
+	S3EROFS_SIGNATURE_VERSION_2,
+	S3EROFS_SIGNATURE_VERSION_4,
+};
+
+#define S3_ACCESS_KEY_LEN 256
+#define S3_SECRET_KEY_LEN 256
+
+struct erofs_s3 {
+	const char *endpoint, *bucket;
+	char access_key[S3_ACCESS_KEY_LEN + 1];
+	char secret_key[S3_SECRET_KEY_LEN + 1];
+
+	enum s3erofs_url_style url_style;
+	enum s3erofs_signature_version sig;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
mkfs/main.c +194 -32
index ab27b77..36cbbcc 100644
@@ -31,6 +31,7 @@
  #include "../lib/liberofs_private.h"
  #include "../lib/liberofs_uuid.h"
  #include "../lib/liberofs_metabox.h"
+#include "../lib/liberofs_s3.h"
  #include "../lib/compressor.h"
 
  static struct option long_options[] = {
@@ -92,6 +93,9 @@ static struct option long_options[] = {
  #endif
  	{"fsalignblks", required_argument, NULL, 531},
  	{"vmdk-desc", required_argument, NULL, 532},
+#ifdef S3EROFS_ENABLED
+	{"s3", required_argument, NULL, 533},
+#endif
  	{0, 0, 0, 0},
  };
 
@@ -174,8 +178,8 @@ static void usage(int argc, char **argv)
  		" --chunksize=#         generate chunk-based files with #-byte chunks\n"
  		" --clean=X             run full clean build (default) or:\n"
  		" --incremental=X       run incremental build\n"
-		"                       (X = data|rvsp; data=full data, rvsp=space is allocated\n"
-		"                                       and filled with zeroes)\n"
+		"                       X = data|rvsp|0 (data: full data, rvsp: space fallocated\n"
+		"                                        0: inodes zeroed)\n"
  		" --compress-hints=X    specify a file to configure per-file compression strategy\n"
  		" --dsunit=#            align all data block addresses to multiples of #\n"
  		" --exclude-path=X      avoid including file X (X = exact literal path)\n"
@@ -197,6 +201,12 @@ static void usage(int argc, char **argv)
  		" --root-xattr-isize=#  ensure the inline xattr size of the root directory is # bytes at least\n"
  		" --aufs                replace aufs special files with overlayfs metadata\n"
  		" --sort=<path,none>    data sorting order for tarballs as input (default: path)\n"
+#ifdef S3EROFS_ENABLED
+		" --s3=X                generate an image from S3-compatible object store\n"
+		"   [,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
  		" --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"
  		"                                            headerball=file data is omited in the source stream)\n"
@@ -247,16 +257,23 @@ static struct erofs_tarfile erofstar = {
  static bool incremental_mode;
  static u8 metabox_algorithmid;
 
+#ifdef S3EROFS_ENABLED
+static struct erofs_s3 s3cfg;
+#endif
+
  enum {
  	EROFS_MKFS_DATA_IMPORT_DEFAULT,
  	EROFS_MKFS_DATA_IMPORT_FULLDATA,
  	EROFS_MKFS_DATA_IMPORT_RVSP,
-	EROFS_MKFS_DATA_IMPORT_SPARSE,
+	EROFS_MKFS_DATA_IMPORT_ZEROFILL,
  } dataimport_mode;
 
  static enum {
  	EROFS_MKFS_SOURCE_LOCALDIR,
  	EROFS_MKFS_SOURCE_TAR,
+#ifdef S3EROFS_ENABLED
+	EROFS_MKFS_SOURCE_S3,
+#endif
  	EROFS_MKFS_SOURCE_REBUILD,
  } source_mode;
 
@@ -522,6 +539,137 @@ static void mkfs_parse_tar_cfg(char *cfg)
  		erofstar.index_mode = true;
  }
 
+#ifdef S3EROFS_ENABLED
+static int mkfs_parse_s3_cfg_passwd(const char *filepath, char *ak, char *sk)
+{
+	struct stat st;
+	int fd, n, ret;
+	char buf[S3_ACCESS_KEY_LEN + S3_SECRET_KEY_LEN + 3];
+	char *colon;
+
+	fd = open(filepath, O_RDONLY);
+	if (fd < 0) {
+		erofs_err("failed to open passwd_file %s", filepath);
+		return -errno;
+	}
+
+	ret = fstat(fd, &st);
+	if (ret) {
+		ret = -errno;
+		goto err;
+	}
+
+	if (!S_ISREG(st.st_mode)) {
+		erofs_err("%s is not a regular file", filepath);
+		ret = -EINVAL;
+		goto err;
+	}
+
+	if ((st.st_mode & 077) != 0)
+		erofs_warn("passwd_file %s should not be accessible by group or others",
+			   filepath);
+
+	if (st.st_size > S3_ACCESS_KEY_LEN + S3_SECRET_KEY_LEN + 3) {
+		erofs_err("passwd_file %s is too large (size: %llu)", filepath,
+			  st.st_size | 0ULL);
+		ret = -EINVAL;
+		goto err;
+	}
+
+	n = read(fd, buf, st.st_size);
+	if (n < 0) {
+		ret = -errno;
+		goto err;
+	}
+	buf[n] = '\0';
+
+	while (n > 0 && (buf[n - 1] == '\n' || buf[n - 1] == '\r'))
+		buf[--n] = '\0';
+
+	colon = strchr(buf, ':');
+	if (!colon) {
+		ret = -EINVAL;
+		goto err;
+	}
+	*colon = '\0';
+
+	strcpy(ak, buf);
+	strcpy(sk, colon + 1);
+
+err:
+	close(fd);
+	return ret;
+}
+
+static int mkfs_parse_s3_cfg(char *cfg_str)
+{
+	char *p, *q, *opt;
+	int ret = 0;
+
+	if (source_mode != EROFS_MKFS_SOURCE_LOCALDIR)
+		return -EINVAL;
+	source_mode = EROFS_MKFS_SOURCE_S3;
+
+	if (!cfg_str) {
+		erofs_err("s3: missing parameter");
+		return -EINVAL;
+	}
+
+	s3cfg.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST;
+	s3cfg.sig = S3EROFS_SIGNATURE_VERSION_2;
+
+	p = strchr(cfg_str, ',');
+	if (p) {
+		s3cfg.endpoint = strndup(cfg_str, p - cfg_str);
+	} else {
+		s3cfg.endpoint = strdup(cfg_str);
+		return 0;
+	}
+
+	opt = p + 1;
+	while (opt) {
+		q = strchr(opt, ',');
+		if (q)
+			*q = '\0';
+
+		if ((p = strstr(opt, "passwd_file="))) {
+			p += sizeof("passwd_file=") - 1;
+			ret = mkfs_parse_s3_cfg_passwd(p, s3cfg.access_key,
+						       s3cfg.secret_key);
+			if (ret)
+				return ret;
+		} else if ((p = strstr(opt, "urlstyle="))) {
+			p += sizeof("urlstyle=") - 1;
+			if (strncmp(p, "vhost", 5) == 0) {
+				s3cfg.url_style = S3EROFS_URL_STYLE_VIRTUAL_HOST;
+			} else if (strncmp(p, "path", 4) == 0) {
+				s3cfg.url_style = S3EROFS_URL_STYLE_PATH;
+			} else {
+				erofs_err("invalid S3 addressing style %s", p);
+				return -EINVAL;
+			}
+		} else if ((p = strstr(opt, "sig="))) {
+			p += strlen("sig=");
+			if (strncmp(p, "4", 1) == 0) {
+				erofs_warn("AWS Signature Version 4 is not supported yet, using Version 2");
+			} else if (strncmp(p, "2", 1) == 0) {
+				s3cfg.sig = S3EROFS_SIGNATURE_VERSION_2;
+			} else {
+				erofs_err("Invalid AWS Signature Version %s", p);
+				return -EINVAL;
+			}
+		} else {
+			erofs_err("invalid --s3 option %s", opt);
+			return -EINVAL;
+		}
+
+		opt = q ? q + 1 : NULL;
+	}
+
+	return 0;
+}
+#endif
+
  static int mkfs_parse_one_compress_alg(char *alg,
  				       struct erofs_compr_opts *copts)
  {
@@ -670,6 +818,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
  			erofstar.ios.dumpfd = fd;
  		}
  		break;
+#ifdef S3EROFS_ENABLED
+	case EROFS_MKFS_SOURCE_S3:
+		s3cfg.bucket = strdup(argv[optind++]);
+		if (!s3cfg.bucket)
+			return -ENOMEM;
+		break;
+#endif
  	default:
  		erofs_err("unexpected source_mode: %d", source_mode);
  		return -EINVAL;
@@ -997,6 +1152,8 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
  				dataimport_mode = EROFS_MKFS_DATA_IMPORT_FULLDATA;
  			} else if (!strcmp(optarg, "rvsp")) {
  				dataimport_mode = EROFS_MKFS_DATA_IMPORT_RVSP;
+			} else if (!strcmp(optarg, "0")) {
+				dataimport_mode = EROFS_MKFS_DATA_IMPORT_ZEROFILL;
  			} else {
  				errno = 0;
  				dataimport_mode = strtol(optarg, &endptr, 0);
@@ -1058,6 +1215,13 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
  				return -EINVAL;
  			}
  			break;
+#ifdef S3EROFS_ENABLED
+		case 533:
+			err = mkfs_parse_s3_cfg(optarg);
+			if (err)
+				return err;
+			break;
+#endif
  		case 'V':
  			version();
  			exit(0);
@@ -1538,35 +1702,7 @@ int main(int argc, char **argv)
 
  	erofs_inode_manager_init();
 
-	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
-		root = erofs_rebuild_make_root(&g_sbi);
-		if (IS_ERR(root)) {
-			err = PTR_ERR(root);
-			goto exit;
-		}
-
-		while (!(err = tarerofs_parse_tar(root, &erofstar)));
-
-		if (err < 0)
-			goto exit;
-
-		err = erofs_rebuild_dump_tree(root, incremental_mode);
-		if (err < 0)
-			goto exit;
-	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
-		root = erofs_rebuild_make_root(&g_sbi);
-		if (IS_ERR(root)) {
-			err = PTR_ERR(root);
-			goto exit;
-		}
-
-		err = erofs_mkfs_rebuild_load_trees(root);
-		if (err)
-			goto exit;
-		err = erofs_rebuild_dump_tree(root, incremental_mode);
-		if (err)
-			goto exit;
-	} else {
+	if (source_mode == EROFS_MKFS_SOURCE_LOCALDIR) {
  		err = erofs_build_shared_xattrs_from_path(&g_sbi, cfg.c_src_path);
  		if (err) {
  			erofs_err("failed to build shared xattrs: %s",
@@ -1583,6 +1719,32 @@ int main(int argc, char **argv)
  			root = NULL;
  			goto exit;
  		}
+	} else {
+		root = erofs_rebuild_make_root(&g_sbi);
+		if (IS_ERR(root)) {
+			err = PTR_ERR(root);
+			goto exit;
+		}
+
+		if (source_mode == EROFS_MKFS_SOURCE_TAR) {
+			while (!(err = tarerofs_parse_tar(root, &erofstar)))
+				;
+			if (err < 0)
+				goto exit;
+		} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
+			err = erofs_mkfs_rebuild_load_trees(root);
+			if (err)
+				goto exit;
+#ifdef S3EROFS_ENABLED
+		} else if (source_mode == EROFS_MKFS_SOURCE_S3) {
+			err = -EOPNOTSUPP;
+			goto exit;
+#endif
+		}
+
+		err = erofs_rebuild_dump_tree(root, incremental_mode);
+		if (err)
+			goto exit;
  	}
 
  	if (tar_index_512b) {
author
#4 lore raw

Re: [PATCH v6 3/4] erofs-utils: mkfs: introduce --s3=... option

Quoted 23 lines · Hongbo Li

Hi Xiang,

Quoted 20 lines · Gao Xiang

From: Yifan Zhao <[email protected]>

It introduces configuration options for the upcoming experimental S3 support, including configuration parsing and passwd_file reading logic.

Users can specify the following options:
  - S3 service endpoint (required);
  - S3 credentials file in the format $ak:%sk (optional);
  - S3 API calling style (optional);
  - S3 API signature version (optional, only V2 is currently supported).

---
  lib/liberofs_s3.h |  40 ++++++++
  mkfs/main.c       | 226 +++++++++++++++++++++++++++++++++++++++-------

  2 files changed, 234 insertions(+), 32 deletions(-)

  create mode 100644 lib/liberofs_s3.h

Quoted 7 lines

lib/liberofs_s3.h

Quoted 5 lines
lib/liberofs_s3.h new file +0 -0
new file mode 100644
index 0000000..fbf2f80

How about moving liberofs_s3.h to include/erofs/? Because s3erofs_build_trees can also be exported and used in other cases.

Public APIs need to be redesigned later, current APIs are unfriendly for external uses.

I tend to export a common api set to import data from:
localdir
tar / cpio / ...
s3
oci
etc.

Thanks,
Gao Xiang

author
#5 lore raw

PATCH v6 4/4 erofs-utils: mkfs: support EROFS meta-only image generation from S3

In reply to Gao Xiang

From: Yifan Zhao <[email protected]>

This patch introduces experimental S3 support for mkfs.erofs, allowing EROFS images to be generated from AWS S3 and/or other S3 API-compatible services.

Here are the current limitations:
- Only meta-only EROFS image generation is supported;
- Only AWS Signature Version 2 is supported;
- The S3 object names and sizes are strictly respected during image generation.

---
 lib/Makefile.am   |   3 +
 lib/liberofs_s3.h |   2 +
 lib/remotes/s3.c  | 622 ++++++++++++++++++++++++++++++++++++++++++++++
 mkfs/main.c       |   9 +-
 4 files changed, 634 insertions(+), 2 deletions(-)
 create mode 100644 lib/remotes/s3.c
lib/Makefile.am +3 -0
index 6458acf..b079897 100644
@@ -67,6 +67,9 @@ else
 liberofs_la_SOURCES += xxhash.c
 endif
 liberofs_la_CFLAGS += ${libcurl_CFLAGS} ${openssl_CFLAGS} ${libxml2_CFLAGS}
+if ENABLE_S3
+liberofs_la_SOURCES += remotes/s3.c
+endif
 if ENABLE_EROFS_MT
 liberofs_la_LDFLAGS = -lpthread
 liberofs_la_SOURCES += workqueue.c
lib/liberofs_s3.h +2 -0
index fbf2f80..fbcb8a6 100644
@@ -33,6 +33,8 @@ struct erofs_s3 {
 	enum s3erofs_signature_version sig;
 };
 
+int s3erofs_build_trees(struct erofs_inode *root, struct erofs_s3 *s3cfg);
+
 #ifdef __cplusplus
 }
 #endif
lib/remotes/s3.c new file +622 -0

622 lines not on this page · raw message

mkfs/main.c +7 -2
index 36cbbcc..58b3d53 100644
@@ -1737,8 +1737,13 @@ int main(int argc, char **argv)
 				goto exit;
 #ifdef S3EROFS_ENABLED
 		} else if (source_mode == EROFS_MKFS_SOURCE_S3) {
-			err = -EOPNOTSUPP;
-			goto exit;
+			if (incremental_mode ||
+			    dataimport_mode != EROFS_MKFS_DATA_IMPORT_ZEROFILL)
+				err = -EOPNOTSUPP;
+			else
+				err = s3erofs_build_trees(root, &s3cfg);
+			if (err)
+				goto exit;
 #endif
 		}
 
Signature · 1 line
2.43.5
Reviewed-by on v6 1/4
#6 lore raw
In reply to Gao Xiang
Quoted 11 lines · Gao Xiang

From: Yifan Zhao <[email protected]>

Currently, mkfs controls different image build execution flows through the global variables tar_mode and rebuild_mode, while these two modes together with localdir mode are mutually exclusive.

Let's replace them with a new variable source_mode to simplify the logic.

Quoted 172 lines
---
  mkfs/main.c | 78 ++++++++++++++++++++++++++++++-----------------------
  1 file changed, 44 insertions(+), 34 deletions(-)
mkfs/main.c +44 -34
index dc2df06..ab27b77 100644
@@ -244,7 +244,7 @@ static int pclustersize_metabox = -1;
  static struct erofs_tarfile erofstar = {
  	.global.xattrs = LIST_HEAD_INIT(erofstar.global.xattrs)
  };
-static bool tar_mode, rebuild_mode, incremental_mode;
+static bool incremental_mode;
  static u8 metabox_algorithmid;
 
  enum {
@@ -254,6 +254,12 @@ enum {
  	EROFS_MKFS_DATA_IMPORT_SPARSE,
  } dataimport_mode;
 
+static enum {
+	EROFS_MKFS_SOURCE_LOCALDIR,
+	EROFS_MKFS_SOURCE_TAR,
+	EROFS_MKFS_SOURCE_REBUILD,
+} source_mode;
+
  static unsigned int rebuild_src_count, total_ccfgs;
  static LIST_HEAD(rebuild_src_list);
  static u8 fixeduuid[16];
@@ -499,7 +505,7 @@ static void mkfs_parse_tar_cfg(char *cfg)
  {
  	char *p;
 
-	tar_mode = true;
+	source_mode = EROFS_MKFS_SOURCE_TAR;
  	if (!cfg)
  		return;
  	p = strchr(cfg, ',');
@@ -616,7 +622,30 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
  	int err, fd;
  	char *s;
 
-	if (tar_mode) {
+	switch (source_mode) {
+	case EROFS_MKFS_SOURCE_LOCALDIR:
+		err = lstat((s = argv[optind++]), &st);
+		if (err) {
+			erofs_err("failed to stat %s: %s", s,
+				  erofs_strerror(-errno));
+			return -ENOENT;
+		}
+		if (S_ISDIR(st.st_mode)) {
+			cfg.c_src_path = realpath(s, NULL);
+			if (!cfg.c_src_path) {
+				erofs_err("failed to parse source directory: %s",
+					  erofs_strerror(-errno));
+				return -ENOENT;
+			}
+			erofs_set_fs_root(cfg.c_src_path);
+		} else {
+			cfg.c_src_path = strdup(s);
+			if (!cfg.c_src_path)
+				return -ENOMEM;
+			source_mode = EROFS_MKFS_SOURCE_REBUILD;
+		}
+		break;
+	case EROFS_MKFS_SOURCE_TAR:
  		cfg.c_src_path = strdup(argv[optind++]);
  		if (!cfg.c_src_path)
  			return -ENOMEM;
@@ -640,30 +669,13 @@ static int mkfs_parse_sources(int argc, char *argv[], int optind)
  			}
  			erofstar.ios.dumpfd = fd;
  		}
-	} else {
-		err = lstat((s = argv[optind++]), &st);
-		if (err) {
-			erofs_err("failed to stat %s: %s", s,
-				  erofs_strerror(-errno));
-			return -ENOENT;
-		}
-		if (S_ISDIR(st.st_mode)) {
-			cfg.c_src_path = realpath(s, NULL);
-			if (!cfg.c_src_path) {
-				erofs_err("failed to parse source directory: %s",
-					  erofs_strerror(-errno));
-				return -ENOENT;
-			}
-			erofs_set_fs_root(cfg.c_src_path);
-		} else {
-			cfg.c_src_path = strdup(s);
-			if (!cfg.c_src_path)
-				return -ENOMEM;
-			rebuild_mode = true;
-		}
+		break;
+	default:
+		erofs_err("unexpected source_mode: %d", source_mode);
+		return -EINVAL;
  	}
 
-	if (rebuild_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
  		char *srcpath = cfg.c_src_path;
  		struct erofs_sb_info *src;
 
@@ -1083,7 +1095,7 @@ static int mkfs_parse_options_cfg(int argc, char *argv[])
  		err = mkfs_parse_sources(argc, argv, optind);
  		if (err)
  			return err;
-	} else if (!tar_mode) {
+	} else if (source_mode != EROFS_MKFS_SOURCE_TAR) {
  		erofs_err("missing argument: SOURCE(s)");
  		return -EINVAL;
  	} else {
@@ -1383,7 +1395,7 @@ int main(int argc, char **argv)
  	if (cfg.c_random_pclusterblks)
  		srand(time(NULL));
  #endif
-	if (tar_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
  		if (dataimport_mode == EROFS_MKFS_DATA_IMPORT_RVSP)
  			erofstar.rvsp_mode = true;
  		erofstar.dev = rebuild_src_count + 1;
@@ -1403,9 +1415,7 @@ int main(int argc, char **argv)
  			g_sbi.blkszbits = 9;
  			tar_index_512b = true;
  		}
-	}
-
-	if (rebuild_mode) {
+	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
  		struct erofs_sb_info *src;
 
  		erofs_warn("EXPERIMENTAL rebuild mode in use. Use at your own risk!");
@@ -1465,7 +1475,7 @@ int main(int argc, char **argv)
  	else if (!incremental_mode)
  		erofs_uuid_generate(g_sbi.uuid);
 
-	if (tar_mode && !erofstar.index_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR && !erofstar.index_mode) {
  		err = erofs_diskbuf_init(1);
  		if (err) {
  			erofs_err("failed to initialize diskbuf: %s",
@@ -1528,7 +1538,7 @@ int main(int argc, char **argv)
 
  	erofs_inode_manager_init();
 
-	if (tar_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
  		root = erofs_rebuild_make_root(&g_sbi);
  		if (IS_ERR(root)) {
  			err = PTR_ERR(root);
@@ -1543,7 +1553,7 @@ int main(int argc, char **argv)
  		err = erofs_rebuild_dump_tree(root, incremental_mode);
  		if (err < 0)
  			goto exit;
-	} else if (rebuild_mode) {
+	} else if (source_mode == EROFS_MKFS_SOURCE_REBUILD) {
  		root = erofs_rebuild_make_root(&g_sbi);
  		if (IS_ERR(root)) {
  			err = PTR_ERR(root);
@@ -1663,7 +1673,7 @@ exit:
  	erofs_rebuild_cleanup();
  	erofs_diskbuf_exit();
  	erofs_exit_configure();
-	if (tar_mode) {
+	if (source_mode == EROFS_MKFS_SOURCE_TAR) {
  		erofs_iostream_close(&erofstar.ios);
  		if (erofstar.ios.dumpfd >= 0)
  			close(erofstar.ios.dumpfd);