[OE-core] [PATCH V2 07/10] initscripts: use a uniform way to handle directories in read-only rootfs

Qi.Chen at windriver.com Qi.Chen at windriver.com
Mon Jul 29 02:33:07 UTC 2013


From: Chen Qi <Qi.Chen at windriver.com>

Packages in our system may need to write to some directories to function
correctly. In read-only rootfs, these directories should be made writable.

This patch uses a convenient and uniform way to handle such situations.
The read-only-rootfs-hook.sh script searches the /etc/default/readonly
diretory for config files and then apply them one by one.

The config files simply have the following format.
<original diretory> <corresponding directory in volatile story>

For example, /etc/default/readonly/initscripts have the following content.
/var/lib /var/volatile/lib

This patch only has effect for systems with read-only rootfs.

[YOCTO #4103]
[YOCTO #4888]

Signed-off-by: Chen Qi <Qi.Chen at windriver.com>
---
 .../initscripts-1.0/read-only-rootfs-hook.sh       |   43 ++++++++++++++++++--
 meta/recipes-core/initscripts/initscripts_1.0.bb   |    5 +++
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/meta/recipes-core/initscripts/initscripts-1.0/read-only-rootfs-hook.sh b/meta/recipes-core/initscripts/initscripts-1.0/read-only-rootfs-hook.sh
index d523924..a3ed59d 100644
--- a/meta/recipes-core/initscripts/initscripts-1.0/read-only-rootfs-hook.sh
+++ b/meta/recipes-core/initscripts/initscripts-1.0/read-only-rootfs-hook.sh
@@ -1,15 +1,50 @@
 #!/bin/sh
 
 . /etc/init.d/functions
+READONLY_CFGDIR="/etc/default/readonly"
 
 ROOTFS_READ_ONLY=`is_rootfs_readonly`
 
 [ "$ROOTFS_READ_ONLY" = "no" ] && exit 0
 
+is_on_read_only_partition () {
+    DIRECTORY=$1
+    dir=`readlink -f $DIRECTORY`
+    while true; do
+	if [ ! -d "$dir" ]; then
+	    echo "ERROR: $dir is not a directory"
+	    exit 1
+	else
+	    for flag in `awk -v dir=$dir '{ if ($2 == dir) { print "FOUND"; split($4,FLAGS,",") } }; END { for (f in FLAGS) print FLAGS[f] }' < /proc/mounts`; do
+		[ "$flag" = "FOUND" ] && partition="read-write"
+		[ "$flag" = "ro" ] && { partition="read-only"; break; }
+	    done
+	fi
+	if [ "$dir" = "/" -o -n "$partition" ]; then
+	    break
+	else
+	    dir=`dirname $dir`
+	fi
+    done
+    [ "$partition" = "read-only" ] && echo "yes" || echo "no"
+}
+
+apply_conf () {
+    cfgfile=$1
+    cat $cfgfile | while read line; do
+	eval `echo "$line" | sed -n "s/\(.*\)\ \(.*\)/DIR_READONLY=\1; DIR_VOLATILE=\2;/p"`
+	if [ `is_on_read_only_partition $DIR_READONLY` = "yes" ]; then
+	    mkdir -p $DIR_VOLATILE
+	    cp -a $DIR_READONLY/* $DIR_VOLATILE
+	    mount --bind $DIR_VOLATILE $DIR_READONLY
+	fi
+    done
+}
+
 if [ "$1" = "start" ] ; then
-	grep -q "tmpfs /var/volatile" /proc/mounts || mount /var/volatile
-	mkdir -p /var/volatile/lib
-	cp -a /var/lib/* /var/volatile/lib
-	mount --bind /var/volatile/lib /var/lib
+    grep -q "tmpfs /var/volatile" /proc/mounts || mount /var/volatile
+    for file in `ls -1 "$READONLY_CFGDIR"`; do
+	apply_conf "$READONLY_CFGDIR/$file"
+    done
 fi
 
diff --git a/meta/recipes-core/initscripts/initscripts_1.0.bb b/meta/recipes-core/initscripts/initscripts_1.0.bb
index 52e1c9c..46c4c99 100644
--- a/meta/recipes-core/initscripts/initscripts_1.0.bb
+++ b/meta/recipes-core/initscripts/initscripts_1.0.bb
@@ -105,6 +105,11 @@ do_install () {
 	install -m 0755 ${WORKDIR}/umountfs	${D}${sysconfdir}/init.d/umountfs
 	install -m 0755		${WORKDIR}/device_table.txt		${D}${sysconfdir}/device_table
 #
+# Create config files for read-only rootfs
+#
+	install -d ${D}${sysconfdir}/default/readonly
+	echo "/var/lib /var/volatile/lib" > ${D}${sysconfdir}/default/readonly/initscripts
+#
 # Create runlevel links
 #
 	update-rc.d -r ${D} rmnologin.sh start 99 2 3 4 5 .
-- 
1.7.9.5




More information about the Openembedded-core mailing list