diff mbox series

BTRFS: exec btrfs-tools if library is not available

Message ID 20230519092156.264645-1-sbabic@denx.de
State Accepted
Headers show
Series BTRFS: exec btrfs-tools if library is not available | expand

Commit Message

Stefano Babic May 19, 2023, 9:21 a.m. UTC
It is planned and desired that libbtrfsutil will add the mkfs feature,
and this can get rid of patching btrfs-util. This remains an option, but
also add the (temporary) way to exec mkfs.btrfs in context of the
diskpart handler.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 Makefile.flags |  6 +++++-
 fs/Config.in   | 12 ++++++++++++
 fs/btrfs.c     | 15 +++++++++++++--
 3 files changed, 30 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/Makefile.flags b/Makefile.flags
index 0224a390..c235d2ce 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -193,8 +193,12 @@  ifeq ($(CONFIG_EXT_FILESYSTEM),y)
 LDLIBS += ext2fs uuid blkid
 endif
 
+ifeq ($(CONFIG_BTRFS_FILESYSTEM_USELIBMKFS),y)
+LDLIBS += mkfsbtrfs
+endif
+
 ifeq ($(CONFIG_BTRFS_FILESYSTEM),y)
-LDLIBS += mkfsbtrfs btrfs btrfsutil udev
+LDLIBS += btrfs btrfsutil udev
 endif
 
 ifeq ($(CONFIG_UNIQUEUUID),y)
diff --git a/fs/Config.in b/fs/Config.in
index df6c185b..5be540f5 100644
--- a/fs/Config.in
+++ b/fs/Config.in
@@ -28,4 +28,16 @@  config BTRFS_FILESYSTEM
 
 comment "BTRFS file system creation support needs libbtrfs libbtrfsutil"
 	depends on !HAVE_LIBBTRFS
+
+config BTRFS_FILESYSTEM_USELIBMKFS
+	bool "Use modified source from btrfs tool to be linked as lib"
+	depends on HAVE_LIBBTRFS
+	default n
+	help
+	  This requires at the moment a patched version of btrfs-tool to export
+	  mkfs.btrf as library.
+
+comment "BTRFS file system creation support needs libbtrfs libbtrfsutil"
+	depends on !HAVE_LIBBTRFS
+
 endif
diff --git a/fs/btrfs.c b/fs/btrfs.c
index b7e2f871..425974f9 100644
--- a/fs/btrfs.c
+++ b/fs/btrfs.c
@@ -28,7 +28,6 @@  extern int mkfs_main(int argc, const char **argv);
 int btrfs_mkfs(const char *device_name, const char __attribute__ ((__unused__)) *fstype)
 {
 	int fd, ret;
-	int argc;
 	const char *argv[3] = { "mkfs.btrfs", "-f", NULL };
 
 	if (!device_name)
@@ -44,9 +43,21 @@  int btrfs_mkfs(const char *device_name, const char __attribute__ ((__unused__))
 
 	optind = 1;
 	argv[2] = device_name;
-	argc = 3;
 
+#if defined(CONFIG_BTRFS_FILESYSTEM_USELIBMKFS)
+	int argc;
+	argc = 3;
 	ret = run_function_background(mkfs_main, argc, (char **)argv);
+#else
+	char *cmd;
+
+	if (asprintf(&cmd, "%s %s %s\n", argv[0], argv[1], argv[2]) == -1) {
+		ERROR("Error allocating memory");
+		return -ENOMEM;
+	}
+
+	ret = run_system_cmd(cmd);
 
+#endif
 	return ret;
 }