diff mbox series

[1/2] diskpart: add support for btrfs filesystem

Message ID 20230512125211.160912-1-sbabic@denx.de
State Accepted
Delegated to: Stefano Babic
Headers show
Series [1/2] diskpart: add support for btrfs filesystem | expand

Commit Message

Stefano Babic May 12, 2023, 12:52 p.m. UTC
This allows to link with a modified version of btrfs-tools, that makes
availale mkfs.btrfs as library and SWupdate can call it :

https://github.com/kdave/btrfs-progs/compare/master...sbabic:btrfs-progs:swupdate

Standard default parameters are taken, there is currently no support for btrfs
tuning.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 Kconfig                |  4 ++++
 Makefile.deps          |  4 ++++
 Makefile.flags         |  4 ++++
 fs/Config.in           | 10 ++++++++
 fs/Makefile            |  5 +++-
 fs/btrfs.c             | 52 ++++++++++++++++++++++++++++++++++++++++++
 fs/diskformat.c        |  3 +++
 include/fs_interface.h |  3 +++
 8 files changed, 84 insertions(+), 1 deletion(-)
 create mode 100644 fs/btrfs.c
diff mbox series

Patch

diff --git a/Kconfig b/Kconfig
index fb2e70f..0bf3ae1 100644
--- a/Kconfig
+++ b/Kconfig
@@ -65,6 +65,10 @@  config HAVE_LIBEXT2FS
 	bool
 	option env="HAVE_LIBEXT2FS"
 
+config HAVE_LIBBTRFS
+	bool
+	option env="HAVE_LIBBTRFS"
+
 config HAVE_LIBZEROMQ
 	bool
 	option env="HAVE_LIBZEROMQ"
diff --git a/Makefile.deps b/Makefile.deps
index 6486a2f..56028c5 100644
--- a/Makefile.deps
+++ b/Makefile.deps
@@ -66,6 +66,10 @@  ifeq ($(HAVE_LIBEXT2FS),)
 export HAVE_LIBEXT2FS = y
 endif
 
+ifeq ($(HAVE_LIBBTRFS),)
+export HAVE_LIBBTRFS = y
+endif
+
 ifeq ($(HAVE_LIBSSL),)
 export HAVE_LIBSSL = y
 endif
diff --git a/Makefile.flags b/Makefile.flags
index c44412d..0224a39 100644
--- a/Makefile.flags
+++ b/Makefile.flags
@@ -193,6 +193,10 @@  ifeq ($(CONFIG_EXT_FILESYSTEM),y)
 LDLIBS += ext2fs uuid blkid
 endif
 
+ifeq ($(CONFIG_BTRFS_FILESYSTEM),y)
+LDLIBS += mkfsbtrfs btrfs btrfsutil udev
+endif
+
 ifeq ($(CONFIG_UNIQUEUUID),y)
 LDLIBS += blkid
 endif
diff --git a/fs/Config.in b/fs/Config.in
index dae855a..df6c185 100644
--- a/fs/Config.in
+++ b/fs/Config.in
@@ -18,4 +18,14 @@  config EXT_FILESYSTEM
 
 comment "EXT2 / EXT3 / EXT4 file system creation support needs libext2fs"
 	depends on !HAVE_LIBEXT2FS
+
+config BTRFS_FILESYSTEM
+	bool "BTRFS file system creation support"
+	depends on HAVE_LIBBTRFS
+	default n
+	help
+	  Enable support for creating BTRFS file systems.
+
+comment "BTRFS file system creation support needs libbtrfs libbtrfsutil"
+	depends on !HAVE_LIBBTRFS
 endif
diff --git a/fs/Makefile b/fs/Makefile
index 0ffe4e1..c7ceef8 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -6,4 +6,7 @@  lib-$(CONFIG_DISKFORMAT) += diskformat.o
 lib-$(CONFIG_FAT_FILESYSTEM) += diskio.o \
 				fat_fs.o \
 				ff.o
-lib-$(CONFIG_EXT_FILESYSTEM) += mke2fs.o \
+lib-$(CONFIG_EXT_FILESYSTEM) += mke2fs.o
+
+#lib-$(CONFIG_BTRFS_FILESYSTEM) += btrfs/hash.o btrfs/crc32c.o btrfs/btrfs.o btrfs/common.o btrfs/hash.o btrfs/xxhash.o btrfs/blake2b-ref.o btrfs/sha224-256.o btrfs/inode-item.o
+lib-$(CONFIG_BTRFS_FILESYSTEM) += btrfs.o
diff --git a/fs/btrfs.c b/fs/btrfs.c
new file mode 100644
index 0000000..b7e2f87
--- /dev/null
+++ b/fs/btrfs.c
@@ -0,0 +1,52 @@ 
+/*
+ * Copyright (C) 2023 Stefano Babic, sbabic@denx.de
+ *
+ * SPDX-License-Identifier:     GPL-2.0-only
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <ctype.h>
+#include <time.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <limits.h>
+#include <blkid/blkid.h>
+#include <uuid/uuid.h>
+#include "util.h"
+#include "fs_interface.h"
+#include "pctl.h"
+
+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)
+		return -EINVAL;
+
+	fd = open(device_name, O_RDWR);
+
+	if (fd < 0) {
+		ERROR("%s cannot be opened", device_name);
+		return -EINVAL;
+	}
+	close(fd);
+
+	optind = 1;
+	argv[2] = device_name;
+	argc = 3;
+
+	ret = run_function_background(mkfs_main, argc, (char **)argv);
+
+	return ret;
+}
diff --git a/fs/diskformat.c b/fs/diskformat.c
index 8d58fc3..ed1f9f0 100644
--- a/fs/diskformat.c
+++ b/fs/diskformat.c
@@ -33,6 +33,9 @@  static struct supported_filesystems fs[] = {
 	{"ext3", ext_mkfs_short},
 	{"ext4", ext_mkfs_short},
 #endif
+#if defined(CONFIG_BTRFS_FILESYSTEM)
+	{"btrfs", btrfs_mkfs},
+#endif
 };
 
 /*
diff --git a/include/fs_interface.h b/include/fs_interface.h
index 581f02a..dabdec3 100644
--- a/include/fs_interface.h
+++ b/include/fs_interface.h
@@ -21,4 +21,7 @@  extern int ext_mkfs(const char *device_name, const char *fstype, unsigned long f
 		const char *volume_label);
 #endif
 
+#if defined (CONFIG_BTRFS_FILESYSTEM) 
+extern int btrfs_mkfs(const char *device_name, const char *fstype);
+#endif
 #endif