Patchwork [V5,07/10] initscripts: use a uniform way to handle directories in read-only rootfs

login
register
mail settings
Submitter Qi.Chen@windriver.com
Date Aug. 8, 2013, 6:16 a.m.
Message ID <728212f8a628627971ffb6267d47e2182d229e14.1375941355.git.Qi.Chen@windriver.com>
Download mbox | patch
Permalink /patch/55305/
State New
Headers show

Comments

Qi.Chen@windriver.com - Aug. 8, 2013, 6:16 a.m.
From: Chen Qi <Qi.Chen@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@windriver.com>
---
 .../initscripts-1.0/read-only-rootfs-hook.sh       |   44 ++++++++++++++++++--
 meta/recipes-core/initscripts/initscripts_1.0.bb   |    5 +++
 2 files changed, 45 insertions(+), 4 deletions(-)

Patch

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..eb89e18 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,51 @@ 
 #!/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 2>/dev/null
+	    cp -a $DIR_READONLY/.[!.]* $DIR_VOLATILE 2>/dev/null
+	    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 .