[PATCH v2] [package] fstools: make extroot functionality work with ubifs

Gergely Kiss mail.gery at gmail.com
Sun Dec 14 08:23:05 EST 2014


Signed-off-by: Gergely Kiss <mail.gery at gmail.com>
Tested-by: Gergely Kiss <mail.gery at gmail.com>
---

Hi John,

here's the new version of my patch providing the following improvements:

* Whitespace issues should be fixed now (changed to a new MUA), however while applying the patch,
there are some warnings generated. I couldn't make them disappear, sorry. If that's a problem
please let me know how to fix it (I'm still a newbie on how to make properly formatted patches).

* Config option added to enable/disable extroot support as requested (enabled by default
if NAND_SUPPORT is defined, otherwise hidden).

Hope everything will be fine with the patch this time.


Regards,
Gergely

 package/system/fstools/Makefile                    |  12 ++
 .../system/fstools/patches/001-ubifs-extroot.patch | 165 +++++++++++++++++++++
 2 files changed, 177 insertions(+)
 create mode 100644 package/system/fstools/patches/001-ubifs-extroot.patch

diff --git a/package/system/fstools/Makefile b/package/system/fstools/Makefile
index 2688f49..9eca044 100644
--- a/package/system/fstools/Makefile
+++ b/package/system/fstools/Makefile
@@ -31,12 +31,24 @@ include $(INCLUDE_DIR)/package.mk
 include $(INCLUDE_DIR)/cmake.mk
 
 TARGET_LDFLAGS += $(if $(CONFIG_USE_EGLIBC),-lrt)
+CMAKE_OPTIONS += $(if $(CONFIG_FSTOOLS_UBIFS_EXTROOT),-DCMAKE_UBIFS_EXTROOT=y)
 
 define Package/fstools
   SECTION:=base
   CATEGORY:=Base system
   DEPENDS:=+ubox +USE_EGLIBC:librt +NAND_SUPPORT:ubi-utils
   TITLE:=OpenWrt filesystem tools
+  MENU:=1
+endef
+
+define Package/fstools/config
+	config FSTOOLS_UBIFS_EXTROOT
+		depends on PACKAGE_fstools
+		depends on NAND_SUPPORT
+		bool "Support extroot functionality with UBIFS"
+		default y
+		help
+			This option makes it possible to use extroot functionality if the root filesystem resides on an UBIFS partition
 endef
 
 define Package/block-mount
diff --git a/package/system/fstools/patches/001-ubifs-extroot.patch b/package/system/fstools/patches/001-ubifs-extroot.patch
new file mode 100644
index 0000000..9ddfb6c
--- /dev/null
+++ b/package/system/fstools/patches/001-ubifs-extroot.patch
@@ -0,0 +1,165 @@
+diff -rupN fstools-2014-12-01.orig/block.c fstools-2014-12-01/block.c
+--- fstools-2014-12-01.orig/block.c	2014-12-14 14:15:20.000000000 +0100
++++ fstools-2014-12-01/block.c	2014-12-14 14:19:33.537985812 +0100
+@@ -36,6 +36,10 @@
+ 
+ #include "libblkid-tiny/libblkid-tiny.h"
+ 
++#ifdef UBIFS_EXTROOT
++#include "libubi/libubi.h"
++#endif
++
+ #define ERROR(fmt, ...) do { \
+ 		syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
+ 		fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
+@@ -823,13 +827,77 @@ static int find_block_mtd(char *name, ch
+ 	return 0;
+ }
+ 
++#ifdef UBIFS_EXTROOT
++static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
++{
++	int dev = 0;
++
++	while (ubi_dev_present(libubi, dev))
++	{
++		struct ubi_dev_info dev_info;
++		struct ubi_vol_info vol_info;
++
++		if (ubi_get_dev_info1(libubi, dev++, &dev_info))
++			continue;
++		if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
++			continue;
++
++		*dev_num = dev_info.dev_num;
++		*vol_id = vol_info.vol_id;
++
++		return 0;
++	}
++
++	return -1;
++}
++
++static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
++{
++	int dev_num;
++	int vol_id;
++	int err = -1;
++
++	err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
++	if (!err)
++		snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
++
++	return err;
++}
++
++static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
++{
++	int dev_num;
++	int vol_id;
++	int err = -1;
++
++	err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
++	if (!err)
++		snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
++
++	return err;
++}
++#endif
++
+ static int check_extroot(char *path)
+ {
+ 	struct blkid_struct_probe *pr = NULL;
+ 	char fs[32];
+ 
++#ifdef UBIFS_EXTROOT
++	if (find_block_mtd("rootfs", fs, sizeof(fs))) {
++		int err = -1;
++		libubi_t libubi;
++
++		libubi = libubi_open();
++		err = find_block_ubi_RO(libubi, "rootfs", fs, sizeof(fs));
++		libubi_close(libubi);
++		if (err)
++			return -1;
++	}
++#else
+ 	if (find_block_mtd("rootfs", fs, sizeof(fs)))
+ 		return -1;
++#endif
+ 
+ 	list_for_each_entry(pr, &devices, list) {
+ 		if (!strcmp(pr->dev, fs)) {
+@@ -933,6 +1001,9 @@ static int main_extroot(int argc, char *
+ 	char fs[32] = { 0 };
+ 	char fs_data[32] = { 0 };
+ 	int err = -1;
++#ifdef UBIFS_EXTROOT
++	libubi_t libubi;
++#endif
+ 
+ 	if (!getenv("PREINIT"))
+ 		return -1;
+@@ -947,8 +1018,18 @@ static int main_extroot(int argc, char *
+ 
+ 	find_block_mtd("rootfs", fs, sizeof(fs));
+ 	if (!fs[0]) {
++#ifdef UBIFS_EXTROOT
++		libubi = libubi_open();
++		find_block_ubi_RO(libubi, "rootfs", fs, sizeof(fs));
++		libubi_close(libubi);
++		if (!fs[0]) {
++			ERROR("extroot: unable to locate rootfs mtdblock / ubiblock\n");
++			return -2;
++		}
++#else
+ 		ERROR("extroot: unable to locate rootfs mtdblock\n");
+ 		return -2;
++#endif
+ 	}
+ 
+ 	pr = find_block_info(NULL, NULL, fs);
+@@ -975,6 +1056,26 @@ static int main_extroot(int argc, char *
+ 		}
+ 	}
+ 
++#ifdef UBIFS_EXTROOT
++	memset(fs_data, 0, sizeof(fs_data));
++	libubi = libubi_open();
++	find_block_ubi(libubi, "rootfs_data", fs_data, sizeof(fs_data));
++	libubi_close(libubi);
++	if (fs_data[0]) {
++		char cfg[] = "/tmp/ubifs_cfg";
++
++		mkdir_p(cfg);
++		if (!mount(fs_data, cfg, "ubifs", MS_NOATIME, NULL)) {
++			err = mount_extroot(cfg);
++			umount2(cfg, MNT_DETACH);
++		}
++		if (err < 0)
++			rmdir("/tmp/overlay");
++		rmdir(cfg);
++		return err;
++       }
++#endif
++
+ 	return mount_extroot(NULL);
+ }
+ 
+diff -rupN fstools-2014-12-01.orig/CMakeLists.txt fstools-2014-12-01/CMakeLists.txt
+--- fstools-2014-12-01.orig/CMakeLists.txt	2014-12-14 14:15:20.000000000 +0100
++++ fstools-2014-12-01/CMakeLists.txt	2014-12-14 14:19:18.140667538 +0100
+@@ -48,7 +48,12 @@ TARGET_LINK_LIBRARIES(mount_root fstools
+ INSTALL(TARGETS mount_root RUNTIME DESTINATION sbin)
+ 
+ ADD_EXECUTABLE(block block.c)
+-TARGET_LINK_LIBRARIES(block blkid-tiny uci ubox blobmsg_json)
++IF(DEFINED CMAKE_UBIFS_EXTROOT)
++	ADD_DEFINITIONS(-DUBIFS_EXTROOT)
++	TARGET_LINK_LIBRARIES(block blkid-tiny uci ubox blobmsg_json ubi-utils)
++ELSE(DEFINED CMAKE_UBIFS_EXTROOT)
++	TARGET_LINK_LIBRARIES(block blkid-tiny uci ubox blobmsg_json)
++ENDIF(DEFINED CMAKE_UBIFS_EXTROOT)
+ INSTALL(TARGETS block RUNTIME DESTINATION sbin)
+ 
+ ADD_EXECUTABLE(jffs2reset jffs2reset.c)
-- 
1.8.3.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list