Skip to contentSkip to conversation

Read-only rootfs followup patches

PATCH Superseded [email protected] 2 participants 9 messages Feb 19, 2013 – Feb 20, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Read-only rootfs followup patches

From: Chen Qi <[email protected]>

This patchset enables populate-volatile.sh script run at rootfs time.

For the purpose of easier review, changes for populate-volatile.sh script have been divided into two parts.

This patchset has been tested with core-image-minimal, with and without 'read-only-rootfs' image feature.

The following changes since commit c7b23ab68aafc04d9830ef318015912e5d4f0672:

maintainers.inc: update ownership of recipes (2013-02-17 22:33:30 +0000)

are available in the git repository at:

git://git.pokylinux.org/poky-contrib ChenQi/readonly-rootfs-followups
http://git.pokylinux.org/cgit.cgi/poky-contrib/log/?h=ChenQi/readonly-rootfs-followups

Chen Qi (4):
image.bbclass: add a hook funtion to support readonly rootfs core-image.bbclass: support read-only rootfs populate-volatile.sh: add ROOT_DIR variable to support running at
rootfs time
populate-volatile.sh: improve the handling of link config items

 meta/classes/core-image.bbclass                    |    4 +
 meta/classes/image.bbclass                         |   18 +++++
 .../initscripts-1.0/populate-volatile.sh           |   77 ++++++++++++++------
 3 files changed, 77 insertions(+), 22 deletions(-)
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/4 image.bbclass: add a hook funtion to support readonly rootfs

From: Chen Qi <[email protected]>

read_only_rootfs_hook: this funtion is invoked only if 'read-only-rootfs' is in IMAGE_FEATURES. It tweaks some basic files and makes populate- volatile.sh script run at rootfs time.

For now, it has real effect only if the init system is sysvinit.

[YOCTO #3406]

---
 meta/classes/image.bbclass |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
meta/classes/image.bbclass +18 -0
index dd78acb..7d533b6 100644
@@ -233,6 +233,24 @@ run_intercept_scriptlets () {
 	fi
 }
 
+# A hook function to support read-only-rootfs IMAGE_FEATURES
+# Currently, it only supports sysvinit system.
+read_only_rootfs_hook () {
+	if ${@base_contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
+	        # Tweak the mount option in fstab
+		sed -i '/rootfs/ s/defaults/ro/' ${IMAGE_ROOTFS}/etc/fstab
+	        # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
+		if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
+			sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
+		fi
+	        # Run populate-volatile.sh at rootfs time to set up basic files
+	        # and directories to support read-only rootfs.
+		if [ -e ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
+			${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
+		fi
+	fi
+}
+
 fakeroot do_rootfs () {
 	#set -x
 	# When use the rpm incremental image generation, don't remove the rootfs
Signature · 1 line
1.7.9.5
#4 lore raw

PATCH 2/4 core-image.bbclass: support read-only rootfs

In reply to [email protected]

From: Chen Qi <[email protected]>

The hook function is appended to ROOTFS_POSTPROCESS_COMMAND properly to support the 'read-only-rootfs' image feature.

[YOCTO #3406]

---
 meta/classes/core-image.bbclass |    4 ++++
 1 file changed, 4 insertions(+)
meta/classes/core-image.bbclass +4 -0
index 657d5f2..4c70d32 100644
@@ -29,6 +29,7 @@ LIC_FILES_CHKSUM = "file://${COREBASE}/LICENSE;md5=3f40d7994397109285ec7b81fdeb3
 # - dev-pkgs            - development packages (headers, etc.) for all installed packages in the rootfs
 # - dbg-pkgs            - debug symbol packages for all installed packages in the rootfs
 # - doc-pkgs            - documentation packages for all installed packages in the rootfs
+# - read-only-rootfs    - tweaks an image to support read-only rootfs
 #
 PACKAGE_GROUP_x11 = "packagegroup-core-x11"
 PACKAGE_GROUP_x11-base = "packagegroup-core-x11-base"
@@ -80,3 +81,6 @@ ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp ; "
 
 # Zap the root password if debug-tweaks feature is not enabled
 ROOTFS_POSTPROCESS_COMMAND += '${@base_contains("IMAGE_FEATURES", "debug-tweaks", "", "zap_root_password ; ",d)}'
+
+# Tweak the mount options for rootfs in /etc/fstab if read-only-rootfs is enabled
+ROOTFS_POSTPROCESS_COMMAND += '${@base_contains("IMAGE_FEATURES", "read-only-rootfs", "read_only_rootfs_hook; ", "",d)}'
Signature · 1 line
1.7.9.5
#5 lore raw

PATCH 3/4 populate-volatile.sh: add ROOT_DIR variable to support running at rootfs time

In reply to [email protected]

From: Chen Qi <[email protected]>

For populate-volatile.sh script to run correctly both at rootfs time and at system boot time, it needs to be aware of which situation it is now in.

We use the ROOT_DIR variable to indicate whether it is run at rootfs time or not. ROOT_DIR being "/" indicates that this script is run at system boot time, otherwise, it is run at rootfs time.

Also, we ignore failures when running this script at rootfs time. For example, if ${ROOT_DIR}/var/dir1 is symlink to /var/volatile/dir1, it's possible that the link is a dead link. So if we're going to create some file under ${ROOT_DIR}/var/dir1, it will fail. But the failure does no harm, because this script will always run at system boot time to set up the correct files and directories.

[YOCTO #3406]

---
 .../initscripts-1.0/populate-volatile.sh           |   57 +++++++++++++++-----
 1 file changed, 43 insertions(+), 14 deletions(-)
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +43 -14
index ab3af70..f1f8793 100755
@@ -8,10 +8,18 @@
 # Short-Description:  Populate the volatile filesystem
 ### END INIT INFO
 
-. /etc/default/rcS
-
-CFGDIR="/etc/default/volatiles"
-TMPROOT="/var/tmp"
+# Get ROOT_DIR
+DIRNAME=`dirname $0`
+ROOT_DIR=`echo $DIRNAME | sed -ne 's:etc/.*::p'`
+
+[ -e ${ROOT_DIR}/etc/default/rcS ] && . ${ROOT_DIR}/etc/default/rcS
+# When running populat-volatile.sh at rootfs time, disable cache.
+[ "$ROOT_DIR" != "/" ] && VOLATILE_ENABLE_CACHE=no
+# If rootfs is read-only, disable cache.
+[ "$ROOTFS_READ_ONLY" = "yes" ] && VOLATILE_ENABLE_CACHE=no
+
+CFGDIR="${ROOT_DIR}/etc/default/volatiles"
+TMPROOT="${ROOT_DIR}/var/tmp"
 COREDEF="00_core"
 
 [ "${VERBOSE}" != "no" ] && echo "Populating volatile Filesystems."
@@ -27,7 +35,15 @@ create_file() {
 	[ -e "$1" ] && {
 		[ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
 	} || {
-		eval $EXEC &
+		if [ "$ROOT_DIR" = "/" ]; then
+			eval $EXEC &
+		else
+			# Creating some files at rootfs time may fail and should fail,
+			# but these failures should not be logged to make sure the do_rootfs
+			# process doesn't fail. This does no harm, as this script will
+			# run on target to set up the correct files and directories.
+			eval $EXEC > /dev/null 2>&1
+		fi
 	}
 }
 
@@ -41,7 +57,13 @@ mk_dir() {
 	[ -e "$1" ] && {
 		[ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
 	} || {
-		eval $EXEC
+		if [ "$ROOT_DIR" = "/" ]; then
+			eval $EXEC
+		else
+			# For the same reason with create_file(), failures should
+			# not be logged.
+			eval $EXEC > /dev/null 2>&1
+		fi
 	}
 }
 
@@ -53,7 +75,13 @@ link_file() {
 	[ -e "$2" ] && {
 		echo "Cannot create link over existing -${TNAME}-." >&2
 	} || {
-		eval $EXEC &
+		if [ "$ROOT_DIR" = "/" ]; then
+			eval $EXEC &
+		else
+			# For the same reason with create_file(), failures should
+			# not be logged.
+			eval $EXEC > /dev/null 2>&1
+		fi
 	}
 }
 
@@ -71,7 +99,7 @@ check_requirements() {
 	TMP_DEFINED="${TMPROOT}/tmpdefined.$$"
 	TMP_COMBINED="${TMPROOT}/tmpcombined.$$"
 
-	cat /etc/passwd | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
+	cat ${ROOT_DIR}/etc/passwd | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
 	cat ${CFGFILE} | grep -v "^#" | cut -d " " -f 2 > "${TMP_INTERMED}"
 	cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
 	NR_DEFINED_USERS="`cat "${TMP_DEFINED}" | wc -l`"
@@ -85,7 +113,7 @@ check_requirements() {
 	}
 
 
-	cat /etc/group | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
+	cat ${ROOT_DIR}/etc/group | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
 	cat ${CFGFILE} | grep -v "^#" | cut -d " " -f 3 > "${TMP_INTERMED}"
 	cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
 
@@ -116,6 +144,7 @@ apply_cfgfile() {
 	cat ${CFGFILE} | grep -v "^#" | \
 		while read LINE; do
 		eval `echo "$LINE" | sed -n "s/\(.*\)\ \(.*\) \(.*\)\ \(.*\)\ \(.*\)\ \(.*\)/TTYPE=\1 ; TUSER=\2; TGROUP=\3; TMODE=\4; TNAME=\5 TLTARGET=\6/p"`
+		TNAME=${ROOT_DIR}/${TNAME}
 		[ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-."
 
 		[ "${TTYPE}" = "l" ] && {
@@ -168,19 +197,19 @@ do
 done
 exec 9>&-
 
-if test -e /etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
+if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
 then
-	sh /etc/volatile.cache
+	sh ${ROOT_DIR}/etc/volatile.cache
 else	
-	rm -f /etc/volatile.cache /etc/volatile.cache.build
+	rm -f ${ROOT_DIR}/etc/volatile.cache /etc/volatile.cache.build
 	for file in `ls -1 "${CFGDIR}" | sort`; do
 		apply_cfgfile "${CFGDIR}/${file}"
 	done
 
-	[ -e /etc/volatile.cache.build ] && sync && mv /etc/volatile.cache.build /etc/volatile.cache
+	[ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv /etc/volatile.cache.build /etc/volatile.cache
 fi
 
-if test -f /etc/ld.so.cache -a ! -f /var/run/ld.so.cache
+if [ "${ROOT_DIR}" = "/" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
 then
 	ln -s /etc/ld.so.cache /var/run/ld.so.cache
 fi
Signature · 1 line
1.7.9.5
#8 lore raw

PATCH 4/4 populate-volatile.sh: improve the handling of link config items

In reply to [email protected]

From: Chen Qi <[email protected]>

Previously, if there's a link config item in the config file like

l root root 1777 /tmp /var/tmp

and /tmp has existed, the symlink will not be created correctly.

Another example is the /run directory. If /run directory has been created by some recipe or script before populate-volatile.sh runs, the symlink of /run to /var/run will not be created correctly.

This patch ensures that the system creates symlinks exactly as the config file tells it.

[YOCTO #3404]
[YOCTO #3406]

---
 .../initscripts-1.0/populate-volatile.sh           |   36 +++++++++++---------
 1 file changed, 20 insertions(+), 16 deletions(-)
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +20 -16
index f1f8793..444c40d 100755
@@ -68,21 +68,27 @@ mk_dir() {
 }
 
 link_file() {
-	EXEC="test -e \"$2\" -o -L $2 || ln -s \"$1\" \"$2\" >/dev/tty0 2>&1" 
+	EXEC="
+	if [ -L \"$2\" ]; then 
+		[ \"$(readlink -f \"$2\")\" != \"$(readlink -f \"$1\")\" ] && { rm -f \"$2\"; ln -sf \"$1\" \"$2\"; }; 
+	elif [ -d \"$2\" ]; then 
+		for f in $2/* $2/.[^.]*; do [ -e \$f ] && cp -rf \$f $1; done;
+		rm -rf \"$2\";
+		ln -sf \"$1\" \"$2\";
+	else 
+		ln -sf \"$1\" \"$2\";
+	fi
+        "
 
 	test "$VOLATILE_ENABLE_CACHE" = yes && echo "	$EXEC" >> /etc/volatile.cache.build
 	
-	[ -e "$2" ] && {
-		echo "Cannot create link over existing -${TNAME}-." >&2
-	} || {
-		if [ "$ROOT_DIR" = "/" ]; then
-			eval $EXEC &
-		else
-			# For the same reason with create_file(), failures should
-			# not be logged.
-			eval $EXEC > /dev/null 2>&1
-		fi
-	}
+	if [ "$ROOT_DIR" = "/" ]; then
+		eval $EXEC &
+	else
+		# For the same reason with create_file(), failures should
+		# not be logged.
+		eval $EXEC > /dev/null 2>&1
+	fi
 }
 
 check_requirements() {
@@ -149,10 +155,8 @@ apply_cfgfile() {
 
 		[ "${TTYPE}" = "l" ] && {
 			TSOURCE="$TLTARGET"
-			[ -L "${TNAME}" ] || {
-				[ "${VERBOSE}" != "no" ] && echo "Creating link -${TNAME}- pointing to -${TSOURCE}-."
-				link_file "${TSOURCE}" "${TNAME}" &
-			}
+			[ "${VERBOSE}" != "no" ] && echo "Creating link -${TNAME}- pointing to -${TSOURCE}-."
+			link_file "${TSOURCE}" "${TNAME}"
 			continue
 		}
 
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH 1/4] image.bbclass: add a hook funtion to support readonly rootfs

In reply to [email protected]
Quoted 20 lines · On 19 February 2013 08:38, <[email protected]> wrote:

From: Chen Qi <[email protected]>

read_only_rootfs_hook: this funtion is invoked only if 'read-only-rootfs' is in IMAGE_FEATURES. It tweaks some basic files and makes populate- volatile.sh script run at rootfs time.

For now, it has real effect only if the init system is sysvinit.

[YOCTO #3406]

---
 meta/classes/image.bbclass |   18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
meta/classes/image.bbclass +0 -0
index dd78acb..7d533b6 100644

meta/classes/image.bbclass run_intercept_scriptlets () { · line 241

Quoted 9 lines
meta/classes/image.bbclass +6 -0
@@ -233,6 +233,24 @@ run_intercept_scriptlets () {
        fi
 }
 
+# A hook function to support read-only-rootfs IMAGE_FEATURES
+# Currently, it only supports sysvinit system.
+read_only_rootfs_hook () {
+       if ${@base_contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
+               # Tweak the mount option in fstab
+               sed -i '/rootfs/ s/defaults/ro/' ${IMAGE_ROOTFS}/etc/fstab

to be cautious i would say
sed -i -e '/^[#[:space:]]*rootfs/s/defaults/ro/' ${IMAGE_ROOTFS}/etc/fstab
for ro rootfs fs_passno should be set to 0, too, imo:
sed -i -e '/^[#[:space:]]*rootfs/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab

meta/classes/image.bbclass run_intercept_scriptlets () { · line 248

Quoted 7 lines
meta/classes/image.bbclass +7 -0
+               # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
+               if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
+                       sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
+               fi
+               # Run populate-volatile.sh at rootfs time to set up basic files
+               # and directories to support read-only rootfs.
+               if [ -e ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then

-x if you want to execute it below.

Quoted 8 lines
meta/classes/image.bbclass +5 -0
+                       ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
+               fi
+       fi
+}
+
 fakeroot do_rootfs () {
        #set -x
        # When use the rpm incremental image generation, don't remove the rootfs
#6 lore raw

Re: [PATCH 3/4] populate-volatile.sh: add ROOT_DIR variable to support running at rootfs time

In reply to [email protected]
Quoted 31 lines · On 19 February 2013 08:38, <[email protected]> wrote:

From: Chen Qi <[email protected]>

For populate-volatile.sh script to run correctly both at rootfs time and at system boot time, it needs to be aware of which situation it is now in.

We use the ROOT_DIR variable to indicate whether it is run at rootfs time or not. ROOT_DIR being "/" indicates that this script is run at system boot time, otherwise, it is run at rootfs time.

Also, we ignore failures when running this script at rootfs time. For example, if ${ROOT_DIR}/var/dir1 is symlink to /var/volatile/dir1, it's possible that the link is a dead link. So if we're going to create some file under ${ROOT_DIR}/var/dir1, it will fail. But the failure does no harm, because this script will always run at system boot time to set up the correct files and directories.

[YOCTO #3406]

---
 .../initscripts-1.0/populate-volatile.sh           |   57 +++++++++++++++-----
 1 file changed, 43 insertions(+), 14 deletions(-)
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +0 -0
index ab3af70..f1f8793 100755
@@ -8,10 +8,18 @@
 # Short-Description:  Populate the volatile filesystem
 ### END INIT INFO
 

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh line 16

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +6 -4
-. /etc/default/rcS
-
-CFGDIR="/etc/default/volatiles"
-TMPROOT="/var/tmp"
+# Get ROOT_DIR
+DIRNAME=`dirname $0`
+ROOT_DIR=`echo $DIRNAME | sed -ne 's:etc/.*::p'`
+
+[ -e ${ROOT_DIR}/etc/default/rcS ] && . ${ROOT_DIR}/etc/default/rcS
+# When running populat-volatile.sh at rootfs time, disable cache.

populate-volatile.sh, missing 'e'

And if you touch that script, please trim all that ugly trailing whitespace while at it:
sed -i -e 's/[[:space:]]*$//g' meta/recipes-core/initscripts/initscripts*/populate-volatile.sh

Quoted 12 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +6 -0
+[ "$ROOT_DIR" != "/" ] && VOLATILE_ENABLE_CACHE=no
+# If rootfs is read-only, disable cache.
+[ "$ROOTFS_READ_ONLY" = "yes" ] && VOLATILE_ENABLE_CACHE=no
+
+CFGDIR="${ROOT_DIR}/etc/default/volatiles"
+TMPROOT="${ROOT_DIR}/var/tmp"
 COREDEF="00_core"
 
 [ "${VERBOSE}" != "no" ] && echo "Populating volatile Filesystems."
@@ -27,7 +35,15 @@ create_file() {
        [ -e "$1" ] && {
                [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh create_file() { · line 45

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +8 -1
        } || {
-               eval $EXEC &
+               if [ "$ROOT_DIR" = "/" ]; then
+                       eval $EXEC &
+               else
+                       # Creating some files at rootfs time may fail and should fail,
+                       # but these failures should not be logged to make sure the do_rootfs
+                       # process doesn't fail. This does no harm, as this script will
+                       # run on target to set up the correct files and directories.
+                       eval $EXEC > /dev/null 2>&1

why don't you background here?

Quoted 20 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +8 -1
+               fi
        }
 }
 
@@ -41,7 +57,13 @@ mk_dir() {
        [ -e "$1" ] && {
                [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
        } || {
-               eval $EXEC
+               if [ "$ROOT_DIR" = "/" ]; then
+                       eval $EXEC
+               else
+                       # For the same reason with create_file(), failures should
+                       # not be logged.
+                       eval $EXEC > /dev/null 2>&1
+               fi
        }
 }
 

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh link_file() { · line 83

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +6 -1
@@ -53,7 +75,13 @@ link_file() {
        [ -e "$2" ] && {
                echo "Cannot create link over existing -${TNAME}-." >&2
        } || {
-               eval $EXEC &
+               if [ "$ROOT_DIR" = "/" ]; then
+                       eval $EXEC &
+               else
+                       # For the same reason with create_file(), failures should
+                       # not be logged.
+                       eval $EXEC > /dev/null 2>&1

why don't you background here?

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh check_requirements() { · line 102

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +2 -1
+               fi
        }
 }
 
@@ -71,7 +99,7 @@ check_requirements() {
        TMP_DEFINED="${TMPROOT}/tmpdefined.$$"
        TMP_COMBINED="${TMPROOT}/tmpcombined.$$"
 
-       cat /etc/passwd | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
+       cat ${ROOT_DIR}/etc/passwd | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"

I would have remove the cat.

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh check_requirements() { · line 116

Quoted 9 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -1
        cat ${CFGFILE} | grep -v "^#" | cut -d " " -f 2 > "${TMP_INTERMED}"
        cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
        NR_DEFINED_USERS="`cat "${TMP_DEFINED}" | wc -l`"
@@ -85,7 +113,7 @@ check_requirements() {
        }
 
 
-       cat /etc/group | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
+       cat ${ROOT_DIR}/etc/group | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"

likewise.

Quoted 13 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -0
        cat ${CFGFILE} | grep -v "^#" | cut -d " " -f 3 > "${TMP_INTERMED}"
        cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
 
@@ -116,6 +144,7 @@ apply_cfgfile() {
        cat ${CFGFILE} | grep -v "^#" | \
                while read LINE; do
                eval `echo "$LINE" | sed -n "s/\(.*\)\ \(.*\) \(.*\)\ \(.*\)\ \(.*\)\ \(.*\)/TTYPE=\1 ; TUSER=\2; TGROUP=\3; TMODE=\4; TNAME=\5 TLTARGET=\6/p"`
+               TNAME=${ROOT_DIR}/${TNAME}
                [ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-."
 
                [ "${TTYPE}" = "l" ] && {
@@ -168,19 +197,19 @@ do
 done

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh do · line 204

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +3 -3
 exec 9>&-
 
-if test -e /etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
+if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
 then
-       sh /etc/volatile.cache
+       sh ${ROOT_DIR}/etc/volatile.cache
 else
-       rm -f /etc/volatile.cache /etc/volatile.cache.build
+       rm -f ${ROOT_DIR}/etc/volatile.cache /etc/volatile.cache.build

why just the first one and not .build too?

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh do · line 209

Quoted 6 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -1
        for file in `ls -1 "${CFGDIR}" | sort`; do
                apply_cfgfile "${CFGDIR}/${file}"
        done
 
-       [ -e /etc/volatile.cache.build ] && sync && mv /etc/volatile.cache.build /etc/volatile.cache
+       [ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv /etc/volatile.cache.build /etc/volatile.cache

hm? Likewise, missing ${ROOT_DIR} ?

Quoted 7 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -1
 fi
 
-if test -f /etc/ld.so.cache -a ! -f /var/run/ld.so.cache
+if [ "${ROOT_DIR}" = "/" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
 then
        ln -s /etc/ld.so.cache /var/run/ld.so.cache
 fi
author
#3 lore raw

Re: [PATCH 1/4] image.bbclass: add a hook funtion to support readonly rootfs

In reply to Bernhard Reutner-Fischer
Quoted 24 lines · Bernhard Reutner-Fischer
Quoted 23 lines · On 19 February 2013 08:38, <[email protected]> wrote:

From: Chen Qi <[email protected]>

read_only_rootfs_hook: this funtion is invoked only if 'read-only-rootfs' is in IMAGE_FEATURES. It tweaks some basic files and makes populate- volatile.sh script run at rootfs time.

For now, it has real effect only if the init system is sysvinit.

[YOCTO #3406]

---
  meta/classes/image.bbclass |   18 ++++++++++++++++++
  1 file changed, 18 insertions(+)
meta/classes/image.bbclass +0 -0
index dd78acb..7d533b6 100644
@@ -233,6 +233,24 @@ run_intercept_scriptlets () {
         fi
  }
 
Quoted 11 lines

meta/classes/image.bbclass run_intercept_scriptlets () { · line 241

Quoted 6 lines
meta/classes/image.bbclass +6 -0
+# A hook function to support read-only-rootfs IMAGE_FEATURES
+# Currently, it only supports sysvinit system.
+read_only_rootfs_hook () {
+       if ${@base_contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
+               # Tweak the mount option in fstab
+               sed -i '/rootfs/ s/defaults/ro/' ${IMAGE_ROOTFS}/etc/fstab

to be cautious i would say
sed -i -e '/^[#[:space:]]*rootfs/s/defaults/ro/' ${IMAGE_ROOTFS}/etc/fstab
for ro rootfs fs_passno should be set to 0, too, imo:
sed -i -e '/^[#[:space:]]*rootfs/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab

Thank you for your suggestion.

Version 2 has been sent out.

Best Regards,
Chen Qi

Quoted 17 lines

meta/classes/image.bbclass run_intercept_scriptlets () { · line 248

Quoted 7 lines
meta/classes/image.bbclass +7 -0
+               # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
+               if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
+                       sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
+               fi
+               # Run populate-volatile.sh at rootfs time to set up basic files
+               # and directories to support read-only rootfs.
+               if [ -e ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then

-x if you want to execute it below.

Quoted 8 lines
meta/classes/image.bbclass +5 -0
+                       ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
+               fi
+       fi
+}
+
  fakeroot do_rootfs () {
         #set -x
         # When use the rpm incremental image generation, don't remove the rootfs
author
#7 lore raw

Re: [PATCH 3/4] populate-volatile.sh: add ROOT_DIR variable to support running at rootfs time

In reply to Bernhard Reutner-Fischer
Quoted 44 lines · Bernhard Reutner-Fischer
Quoted 31 lines · On 19 February 2013 08:38, <[email protected]> wrote:

From: Chen Qi <[email protected]>

For populate-volatile.sh script to run correctly both at rootfs time and at system boot time, it needs to be aware of which situation it is now in.

We use the ROOT_DIR variable to indicate whether it is run at rootfs time or not. ROOT_DIR being "/" indicates that this script is run at system boot time, otherwise, it is run at rootfs time.

Also, we ignore failures when running this script at rootfs time. For example, if ${ROOT_DIR}/var/dir1 is symlink to /var/volatile/dir1, it's possible that the link is a dead link. So if we're going to create some file under ${ROOT_DIR}/var/dir1, it will fail. But the failure does no harm, because this script will always run at system boot time to set up the correct files and directories.

[YOCTO #3406]

---
  .../initscripts-1.0/populate-volatile.sh           |   57 +++++++++++++++-----
  1 file changed, 43 insertions(+), 14 deletions(-)
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +0 -0
index ab3af70..f1f8793 100755
@@ -8,10 +8,18 @@
  # Short-Description:  Populate the volatile filesystem
  ### END INIT INFO
 

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh line 16

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +6 -4
-. /etc/default/rcS
-
-CFGDIR="/etc/default/volatiles"
-TMPROOT="/var/tmp"
+# Get ROOT_DIR
+DIRNAME=`dirname $0`
+ROOT_DIR=`echo $DIRNAME | sed -ne 's:etc/.*::p'`
+
+[ -e ${ROOT_DIR}/etc/default/rcS ] && . ${ROOT_DIR}/etc/default/rcS
+# When running populat-volatile.sh at rootfs time, disable cache.

populate-volatile.sh, missing 'e'

Quoted 5 lines

And if you touch that script, please trim all that ugly trailing whitespace while at it:
sed -i -e 's/[[:space:]]*$//g' meta/recipes-core/initscripts/initscripts*/populate-volatile.sh

Thank you for your careful review :)

Version 2 has been sent out.

Best Regards,
Chen Qi

Quoted 118 lines
Quoted 12 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +6 -0
+[ "$ROOT_DIR" != "/" ] && VOLATILE_ENABLE_CACHE=no
+# If rootfs is read-only, disable cache.
+[ "$ROOTFS_READ_ONLY" = "yes" ] && VOLATILE_ENABLE_CACHE=no
+
+CFGDIR="${ROOT_DIR}/etc/default/volatiles"
+TMPROOT="${ROOT_DIR}/var/tmp"
  COREDEF="00_core"
 
  [ "${VERBOSE}" != "no" ] && echo "Populating volatile Filesystems."
@@ -27,7 +35,15 @@ create_file() {
         [ -e "$1" ] && {
                 [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh create_file() { · line 45

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +8 -1
         } || {
-               eval $EXEC &
+               if [ "$ROOT_DIR" = "/" ]; then
+                       eval $EXEC &
+               else
+                       # Creating some files at rootfs time may fail and should fail,
+                       # but these failures should not be logged to make sure the do_rootfs
+                       # process doesn't fail. This does no harm, as this script will
+                       # run on target to set up the correct files and directories.
+                       eval $EXEC > /dev/null 2>&1

why don't you background here?

Quoted 20 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +8 -1
+               fi
         }
  }
 
@@ -41,7 +57,13 @@ mk_dir() {
         [ -e "$1" ] && {
                 [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
         } || {
-               eval $EXEC
+               if [ "$ROOT_DIR" = "/" ]; then
+                       eval $EXEC
+               else
+                       # For the same reason with create_file(), failures should
+                       # not be logged.
+                       eval $EXEC > /dev/null 2>&1
+               fi
         }
  }
 

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh link_file() { · line 83

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +6 -1
@@ -53,7 +75,13 @@ link_file() {
         [ -e "$2" ] && {
                 echo "Cannot create link over existing -${TNAME}-." >&2
         } || {
-               eval $EXEC &
+               if [ "$ROOT_DIR" = "/" ]; then
+                       eval $EXEC &
+               else
+                       # For the same reason with create_file(), failures should
+                       # not be logged.
+                       eval $EXEC > /dev/null 2>&1

why don't you background here?

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh check_requirements() { · line 102

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +2 -1
+               fi
         }
  }
 
@@ -71,7 +99,7 @@ check_requirements() {
         TMP_DEFINED="${TMPROOT}/tmpdefined.$$"
         TMP_COMBINED="${TMPROOT}/tmpcombined.$$"
 
-       cat /etc/passwd | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
+       cat ${ROOT_DIR}/etc/passwd | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"

I would have remove the cat.

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh check_requirements() { · line 116

Quoted 9 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -1
         cat ${CFGFILE} | grep -v "^#" | cut -d " " -f 2 > "${TMP_INTERMED}"
         cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
         NR_DEFINED_USERS="`cat "${TMP_DEFINED}" | wc -l`"
@@ -85,7 +113,7 @@ check_requirements() {
         }
 
 
-       cat /etc/group | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"
+       cat ${ROOT_DIR}/etc/group | sed 's@\(^:\)*:.*@\1@' | sort | uniq > "${TMP_DEFINED}"

likewise.

Quoted 13 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -0
         cat ${CFGFILE} | grep -v "^#" | cut -d " " -f 3 > "${TMP_INTERMED}"
         cat "${TMP_DEFINED}" "${TMP_INTERMED}" | sort | uniq > "${TMP_COMBINED}"
 
@@ -116,6 +144,7 @@ apply_cfgfile() {
         cat ${CFGFILE} | grep -v "^#" | \
                 while read LINE; do
                 eval `echo "$LINE" | sed -n "s/\(.*\)\ \(.*\) \(.*\)\ \(.*\)\ \(.*\)\ \(.*\)/TTYPE=\1 ; TUSER=\2; TGROUP=\3; TMODE=\4; TNAME=\5 TLTARGET=\6/p"`
+               TNAME=${ROOT_DIR}/${TNAME}
                 [ "${VERBOSE}" != "no" ] && echo "Checking for -${TNAME}-."
 
                 [ "${TTYPE}" = "l" ] && {
@@ -168,19 +197,19 @@ do
  done

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh do · line 204

Quoted 10 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +3 -3
  exec 9>&-
 
-if test -e /etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
+if test -e ${ROOT_DIR}/etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "xupdate" -a "x$clearcache" = "x0"
  then
-       sh /etc/volatile.cache
+       sh ${ROOT_DIR}/etc/volatile.cache
  else
-       rm -f /etc/volatile.cache /etc/volatile.cache.build
+       rm -f ${ROOT_DIR}/etc/volatile.cache /etc/volatile.cache.build

why just the first one and not .build too?

meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh do · line 209

Quoted 6 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -1
         for file in `ls -1 "${CFGDIR}" | sort`; do
                 apply_cfgfile "${CFGDIR}/${file}"
         done
 
-       [ -e /etc/volatile.cache.build ] && sync && mv /etc/volatile.cache.build /etc/volatile.cache
+       [ -e ${ROOT_DIR}/etc/volatile.cache.build ] && sync && mv /etc/volatile.cache.build /etc/volatile.cache

hm? Likewise, missing ${ROOT_DIR} ?

Quoted 7 lines
meta/recipes-core/initscripts/initscripts-1.0/populate-volatile.sh +1 -1
  fi
 
-if test -f /etc/ld.so.cache -a ! -f /var/run/ld.so.cache
+if [ "${ROOT_DIR}" = "/" ] && [ -f /etc/ld.so.cache ] && [ ! -f /var/run/ld.so.cache ]
  then
         ln -s /etc/ld.so.cache /var/run/ld.so.cache
  fi