Skip to contentSkip to conversation

initscripts: Modify 'populate-volatile.sh' to atomically update 'volatile.cache'

PATCH In review James Limbouris 2 participants 2 messages Jun 7, 2011

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 initscripts: Modify 'populate-volatile.sh' to atomically update 'volatile.cache'

When power is lost before a new volatile.cache has been flushed to disk, it can be corrupted, leaving the volatiles uncreated and causing software failure. Using a temporary file on the same volume and renaming it avoids this issue. The file is sync'd before the rename to avoid problems with newer caching filesystems, in which the rename is atomic, but the contents of the file may not yet be flushed.

---
 .../initscripts-1.0/populate-volatile.sh           |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
recipes/initscripts/initscripts-1.0/populate-volatile.sh +6 -4
index 3bb3d94..e355680 100755
@@ -14,7 +14,7 @@ create_file() {
 	chown ${TUSER}.${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\" >/dev/tty0 2>&1; 
 	chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" >/dev/tty0 2>&1 " 
 
-	test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache
+	test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build
 
 	[ -e "$1" ] && {
 	  [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
@@ -29,7 +29,7 @@ mk_dir() {
 	chown ${TUSER}.${TGROUP} $1 || echo \"Failed to set owner -${TUSER}- for -$1-.\" >/dev/tty0 2>&1; 
 	chmod ${TMODE} $1 || echo \"Failed to set mode -${TMODE}- for -$1-.\" >/dev/tty0 2>&1 "
 
-	test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache
+	test "$VOLATILE_ENABLE_CACHE" = yes && echo "$EXEC" >> /etc/volatile.cache.build
 	
 	[ -e "$1" ] && {
 	  [ "${VERBOSE}" != "no" ] && echo "Target already exists. Skipping."
@@ -41,7 +41,7 @@ mk_dir() {
 link_file() {
 	EXEC="test -e \"$2\" -o -L $2 || ln -s \"$1\" \"$2\" >/dev/tty0 2>&1" 
 
-	test "$VOLATILE_ENABLE_CACHE" = yes && echo "	$EXEC" >> /etc/volatile.cache
+	test "$VOLATILE_ENABLE_CACHE" = yes && echo "	$EXEC" >> /etc/volatile.cache.build
 	
 	[ -e "$2" ] && {
 	  echo "Cannot create link over existing -${TNAME}-." >&2
@@ -164,8 +164,10 @@ if test -e /etc/volatile.cache -a "$VOLATILE_ENABLE_CACHE" = "yes" -a "x$1" != "
 then
 	sh /etc/volatile.cache
 else	
-	rm -f /etc/volatile.cache
+	rm -f /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
 fi
Signature · 1 line
1.7.3.4
#1 lore raw

Dear James,

thank you for the updated patch.

Am Dienstag, den 07.06.2011, 17:35 +0800 schrieb James Limbouris:

Quoted 2 lines

When power is lost before a new volatile.cache has been flushed to disk, it can be corrupted, leaving the volatiles uncreated and causing software failure.

So the device is still booting up although the power is lost? I am still confused how this patch can fix this. I am sorry for my ignorance.

Quoted 5 lines

Using a temporary file on the same volume and renaming it avoids this issue. The file is sync'd before the rename to avoid problems with newer caching filesystems, in which the rename is atomic, but the contents of the file may not yet be flushed.

I will commit your patch during the next days to leave some time for others to comment/review.

Quoted 3 lines
---
 .../initscripts-1.0/populate-volatile.sh           |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

[…]

Thanks,

Paul