From patchwork Fri Dec 12 23:43:54 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gergely Kiss X-Patchwork-Id: 420730 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 1500C1400D5 for ; Sat, 13 Dec 2014 10:44:14 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 3E403280419; Sat, 13 Dec 2014 00:42:19 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 204552802BA for ; Sat, 13 Dec 2014 00:42:12 +0100 (CET) X-policyd-weight: using cached result; rate: -8.5 Received: from mail-ig0-f173.google.com (mail-ig0-f173.google.com [209.85.213.173]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Sat, 13 Dec 2014 00:42:11 +0100 (CET) Received: by mail-ig0-f173.google.com with SMTP id r2so2312847igi.12 for ; Fri, 12 Dec 2014 15:43:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=xxN9Rwghxez9pCrYBiYMRuhetmbjzv65SZb6WKBu0kc=; b=GEevC1N3Z0z4LN87L6ZDj1GHQf8U9kwWoVhsB/+1/sqTkOwRn7zUt3SrgeaZr2YQDJ 7U3JEromNOGiz3/1lrStiheHP0tSKTtZEajzWOaP4Z4kMbM2hmHFBKcEUvo5L8fFVRW6 bP/LdlDik9J4BPkKp+mtJLj3srMwZhjX6eyQaUJbPyA90wrOeNwnHubk+Ct/KnPK0ns/ MNLl6eo0D68D6hJpggQCqchUmrHn1WmQzJnclFq+2FmHzLEjYBfG4Pv/A2uWarhnKYK6 iXQhNzZsrWd1JkchqmmW8n2zZXLRkA4GlVOJzNIK4vQiMJjNXn6qG1X6engPXhEqbeYm E0nw== MIME-Version: 1.0 X-Received: by 10.42.88.212 with SMTP id d20mr19299654icm.32.1418427834930; Fri, 12 Dec 2014 15:43:54 -0800 (PST) Received: by 10.107.170.79 with HTTP; Fri, 12 Dec 2014 15:43:54 -0800 (PST) Date: Sat, 13 Dec 2014 00:43:54 +0100 Message-ID: From: Gergely Kiss To: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH] [package] fstools: make extroot functionality work with ubifs X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" From: Gergely Kiss Fix extroot functionality for devices where rootfs is on a ubifs partition Signed-off-by: Gergely Kiss Tested-by: Gergely Kiss --- 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 #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)