[OpenWrt-Devel] [PATCH] [package] fstools: make extroot functionality work with ubifs

Gergely Kiss mail.gery at gmail.com
Fri Dec 12 18:43:54 EST 2014


From: Gergely Kiss <mail.gery at gmail.com>

Fix extroot functionality for devices where rootfs is on a ubifs partition

Signed-off-by: Gergely Kiss <mail.gery at gmail.com>
Tested-by: Gergely Kiss <mail.gery at gmail.com>
---
Originally created by forum user "Hiro.AK47" for the 14.07 branch.
I've created a new diff to make it work with the master branch.

Tested on a Netgear WNDR4300 router, working fine in both overlay and
pivot root modes.

diff -rupN fstools.old/block.c fstools.new/block.c
--- fstools.old/block.c    2014-12-12 17:32:23.833641055 +0100
+++ fstools.new/block.c    2014-12-12 17:36:59.532478289 +0100
@@ -35,6 +35,7 @@
 #include <libubox/avl-cmp.h>

 #include "libblkid-tiny/libblkid-tiny.h"
+#include "libubi/libubi.h"

 #define ERROR(fmt, ...) do { \
         syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
@@ -823,13 +824,70 @@ static int find_block_mtd(char *name, ch
     return 0;
 }

+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;
+}
+
 static int check_extroot(char *path)
 {
     struct blkid_struct_probe *pr = NULL;
     char fs[32];

-    if (find_block_mtd("rootfs", fs, sizeof(fs)))
-        return -1;
+       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;
+       }

     list_for_each_entry(pr, &devices, list) {
         if (!strcmp(pr->dev, fs)) {
@@ -933,6 +991,7 @@ static int main_extroot(int argc, char *
     char fs[32] = { 0 };
     char fs_data[32] = { 0 };
     int err = -1;
+    libubi_t libubi;

     if (!getenv("PREINIT"))
         return -1;
@@ -947,8 +1006,13 @@ static int main_extroot(int argc, char *

     find_block_mtd("rootfs", fs, sizeof(fs));
     if (!fs[0]) {
-        ERROR("extroot: unable to locate rootfs mtdblock\n");
-        return -2;
+               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;
+               }
     }

     pr = find_block_info(NULL, NULL, fs);
@@ -975,6 +1039,24 @@ static int main_extroot(int argc, char *
         }
     }

+       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;
+       }
+
     return mount_extroot(NULL);
 }

diff -rupN fstools.old/CMakeLists.txt fstools.new/CMakeLists.txt
--- fstools.old/CMakeLists.txt    2014-12-12 17:32:23.833641055 +0100
+++ fstools.new/CMakeLists.txt    2014-12-12 17:31:48.729637303 +0100
@@ -48,7 +48,7 @@ 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)
+TARGET_LINK_LIBRARIES(block blkid-tiny uci ubox blobmsg_json ubi-utils)
 INSTALL(TARGETS block RUNTIME DESTINATION sbin)

 ADD_EXECUTABLE(jffs2reset jffs2reset.c)
_______________________________________________
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