The block-extroot package adds the capability for preinit to mount /overlay on 
an external USB device. The attached patch extends
the capability to allow /overlay to be mounted on a sub-directory of the 
external USB device.

I can see two uses for this:
  1. I want to be able to use the USB device for other purposes as well, and 
this keeps all the files in one place on the USB device
  2. I can have multiple difference configurations on the USB device, and 
select which one to boot through UCI configuration.

The option is configured through /etc/config/fstab, with an extra option, 
rootfs_subdir, which is the sub-directory relative to the
root of the USB device. For example:
        option rootfs_subdir    /openwrt
would mount /overlay on the /openwrt sub-directory of the USB device. The patch 
to /lib/functions/block.sh are purely to support this
additional option.

The patch also adds one extra level of searching for /etc/config/fstab, adding 
a first choice option of looking on the USB device,
and if the file does not exist there, then tries /tmp/overlay/etc/config/fstab 
(i.e. the internal flash jffs2 fs) and lastly the
/etc/config/fstab on the squashfs filesystem. This allows the configuration to 
be managed entirely on the USB device, without touching
the internal flash, other than the initial setup of the internal 
/etc/config/fstab to enable the external rootfs.

The patch has been done like this, rather than modifying 
/lib/preinit/50-determine_usb_root, so that it works in conjunction with
the patch I submitted yesterday for kexec'ing a new kernel from the USB device 
during the preinit process.

Signed-off-by: Quentin Armitage <[email protected]>

Index: package/block-extroot-subdir/files/52_usb_root_subdir
===================================================================
--- package/block-extroot-subdir/files/52_usb_root_subdir       (revision 0)
+++ package/block-extroot-subdir/files/52_usb_root_subdir       (revision 0)
@@ -0,0 +1,79 @@
+#!/bin/sh
+#  Copyright (C) 2010 OpenWrt.org
+
+# This is free software, licensed under the GNU General Public License v2.
+#
+# /etc/config/fstab configures this script. It requires that the extroot mount
+# was successful, and uses /overlay that it mounted.
+#
+#Example config:
+#config mount
+#      option  enabled         1
+#      option  is_rootfs       1
+#      option  rootfs_subdir   /usbroot
+#
+# This will remount /overlay/$rootfs_subdir on /overlay
+
+. /etc/functions.sh
+. /lib/functions/block.sh
+
+config_fstab_extroot() {
+       local cfg="$1"
+       local find_rootfs="$2"
+
+       mount_cb() {
+               shift
+               local enabled="$6"
+               shift
+               local is_rootfs="$9"
+               shift
+               local rootfs_subdir="$9"
+
+               if [ "$is_rootfs" = "1" ]; then
+                       extroot_enabled="$enabled"
+                       extroot_subdir="$rootfs_subdir"
+               fi
+       }
+       config_get_mount "$cfg"
+       reset_block_cb
+}
+
+check_extroot_enabled_or_subdir() {
+       local OLD_UCI_CONFIG_DIR="$UCI_CONFIG_DIR"
+
+       [ "$pi_extroot_mount_success" = "true" ] || return 0
+
+       # If the extroot (mounted on /overlay) has a config, use it, otherwise
+       # try jffs2 overlay
+       if [ -r "/overlay/etc/config/fstab" ]; then
+               UCI_CONFIG_DIR=/overlay/etc/config
+       elif [ "$jffs" = "/tmp/overlay" ] && [ -r 
"/tmp/overlay/etc/config/fstab" ]; then
+               UCI_CONFIG_DIR=/tmp/overlay/etc/config
+       fi
+
+       extroot_enabled=0
+       extroot_subdir=
+
+       config_load fstab
+       config_foreach config_fstab_extroot mount 1
+       
+       # check we want this enabled
+       if [ "$extroot_enabled" = "0" ]; then
+               # No
+               pi_extroot_mount_success=false
+               umount /overlay
+               pi_mount_skip_next="$pi_pre_extroot_skip_next"
+               pi_extroot_mount_success=0
+       else
+               [ -n "$extroot_subdir" ] && [ -d /overlay/"$extroot_subdir" ] &&
+                       mkdir -p /tmp/overlay1 && \
+                       mount --bind /overlay/"$extroot_subdir" /tmp/overlay1 
&& \
+                       umount /overlay && \
+                       mount --move /tmp/overlay1 /overlay
+       fi
+
+       UCI_CONFIG_DIR="$OLD_UCI_CONFIG_DIR"
+       return 0
+}
+
+boot_hook_add preinit_mount_root check_extroot_enabled_or_subdir
Index: package/block-extroot-subdir/files/49_save_skip_next
===================================================================
--- package/block-extroot-subdir/files/49_save_skip_next        (revision 0)
+++ package/block-extroot-subdir/files/49_save_skip_next        (revision 0)
@@ -0,0 +1,12 @@
+#!/bin/sh
+#  Copyright (C) 2010 OpenWrt.org
+
+# This is free software, licensed under the GNU General Public License v2.
+#
+# Saves pi_mount_skip_next
+
+save_mount_skip_next() {
+       pi_pre_extroot_skip_next="$pi_mount_skip_next"
+}
+
+boot_hook_add preinit_mount_root save_mount_skip_next
Index: package/block-extroot-subdir/Makefile
===================================================================
--- package/block-extroot-subdir/Makefile       (revision 0)
+++ package/block-extroot-subdir/Makefile       (revision 0)
@@ -0,0 +1,54 @@
+#
+# Copyright (C) 2010 OpenWrt.org
+#
+# This is free software, licensed under the GNU General Public License v2.
+#
+
+include $(TOPDIR)/rules.mk
+
+PKG_NAME:=block-extroot-subdir
+PKG_VERSION:=0.0.1
+PKG_RELEASE:=1
+
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/block-extroot-subdir/Default
+  SECTION:=utils
+  CATEGORY:=Utilities
+  TITLE:=root filesystem on secondary storage subdir
+  SUBMENU:=disc
+endef
+
+define Package/block-extroot-subdir
+  $(call Package/block-extroot-subdir/Default)
+  MENU:=1
+  DEPENDS:=+block-extroot
+endef
+
+define Package/block-extroot-subdir/description
+  Based on the moduluarized preinit and firstboot, adds the option to have
+  the root filesystem on a subdirectory on storage other than the jffs or the
+  boot root device.
+  For a squashfs image this package must be installed into the image, not as
+  a package to add later.
+endef
+
+define Build/Prepare
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+endef
+
+define Package/block-extroot-subdir/install
+       $(INSTALL_DIR) $(1)/lib/preinit
+       $(INSTALL_DATA) ./files/49_save_skip_next $(1)/lib/preinit/
+       $(INSTALL_DATA) ./files/52_usb_root_subdir $(1)/lib/preinit/
+endef
+
+$(eval $(call BuildPackage,block-extroot-subdir))
+
Index: package/block-mount/files/block.sh
===================================================================
--- package/block-mount/files/block.sh  (revision 20584)
+++ package/block-mount/files/block.sh  (working copy)
@@ -20,6 +20,8 @@
                dmc_label="$9"
                shift
                dmc_is_rootfs="$9"
+               shift
+               dmc_rootfs_subdir="$9"
                return 0
        }
        swap_cb() { 
@@ -47,6 +49,8 @@
                dmds_mount_label="$9"
                shift
                dmds_is_rootfs="$9"
+               shift
+               dmds_rootfs_subdir="$9"
                return 0
        }
        swap_dev_section_cb() { 
@@ -81,7 +85,8 @@
        config_get gm_uuid "$1" uuid
        config_get gm_label "$1" label
        config_get_bool gm_is_rootfs "$1" is_rootfs 0
-       mount_cb "$gm_cfg" "$gm_param" "$gm_target" "$gm_device" "$gm_fstype" 
"$gm_options" "$gm_enabled" "$gm_enabled_fsck" "$gm_uuid" "$gm_label" 
"$gm_is_rootfs"
+       config_get gm_rootfs_subdir "$1" rootfs_subdir
+       mount_cb "$gm_cfg" "$gm_param" "$gm_target" "$gm_device" "$gm_fstype" 
"$gm_options" "$gm_enabled" "$gm_enabled_fsck" "$gm_uuid" "$gm_label" 
"$gm_is_rootfs" "$gm_rootfs_subdir"
 }
 
 config_get_swap() {
@@ -193,6 +198,7 @@
        local msbd_uuid=
        local msbd_label=
        local msbd_is_rootfs
+       local msbd_rootfs_subdir
        local msbd_blkid_fstype_match=
        mount_cb() {
                local mc_cfg="$1"
@@ -205,6 +211,8 @@
                local mc_label="$9"
                shift
                local mc_is_rootfs="$9"
+               shift
+               local mc_rootfs_subdir="$9"
                local mc_found_device=""
                
                mc_found_device="$(libmount_find_device_by_id "$mc_uuid" 
"$mc_label" "$mc_device" "$mc_cfgdevice")"
@@ -219,12 +227,14 @@
                        msbd_uuid="$7"
                        msbd_label="$8"
                        msbd_is_rootfs="$9"
+                       shift
+                       msbd_rootfs_subdir="$9"
                fi
                return 0        
        }
        config_foreach config_get_mount mount "$msbd_device"
        [ -n "$msbd_mount_device" ] && config_create_mount_fstab_entry 
"$msbd_mount_device" "$msbd_target" "$msbd_fstype" "$msbd_options" 
"$msbd_enabled" 
-       mount_dev_section_cb "$msbd_mount_cfg" "$msbd_target" 
"$msbd_mount_device" "$msbd_fstype" "$msbd_options" "$msbd_enabled" 
"$msbd_enabled_fsck" "$msbd_uuid" "$msbd_label" "$msbd_is_rootfs"
+       mount_dev_section_cb "$msbd_mount_cfg" "$msbd_target" 
"$msbd_mount_device" "$msbd_fstype" "$msbd_options" "$msbd_enabled" 
"$msbd_enabled_fsck" "$msbd_uuid" "$msbd_label" "$msbd_is_rootfs" 
"$msbd_rootfs_subdir"
        reset_block_cb
 }
 


_______________________________________________
openwrt-devel mailing list
[email protected]
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to