[projects/geninitrd] Mechanism for waiting until devices are available (wired for mod-md for now). Bit ugly but working.

arekm arekm at pld-linux.org
Mon Dec 17 15:22:21 CET 2018


commit efb675bf488a4cd3b3f69c452a5423a9a7dfd490
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date:   Mon Dec 17 15:21:11 2018 +0100

    Mechanism for waiting until devices are available (wired for mod-md for now). Bit ugly but working.

 Makefile         |  1 +
 functions.initrd | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 geninitrd        |  9 ++++++++-
 mod-md.sh        |  1 +
 4 files changed, 63 insertions(+), 1 deletion(-)
---
diff --git a/Makefile b/Makefile
index 5a17ac2..1994b97 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ install: $(NAME).8
 	install -p $(NAME) $(DESTDIR)$(sbindir)/$(NAME)
 	install -p -m644 $(NAME).8 $(DESTDIR)$(mandir)/man8/$(NAME).8
 	install -p -m644 functions $(DESTDIR)$(libdir)/functions
+	install -p -m644 functions.initrd $(DESTDIR)$(libdir)/functions.initrd
 	install -p -m644 $(FILES_MODS) $(DESTDIR)$(libdir)
 	install -p -m755 $(FILES_ADDON) $(DESTDIR)$(libdir)
 	install -p -m644 $(NAME).sysconfig $(DESTDIR)$(sysconfigdir)/$(NAME)
diff --git a/functions.initrd b/functions.initrd
new file mode 100644
index 0000000..31e25ad
--- /dev/null
+++ b/functions.initrd
@@ -0,0 +1,53 @@
+# Functions used from inside of initrd.
+
+
+# Wait for specific /dev files to appear but only one
+# time regardless how often this function is called
+# in one initrd run. Needs writable /tmp.
+wait_for_files() {
+	local i=0 timeout=30 all_found not_found wff_dir fpath file
+	while getopts t: opt "$@"; do
+		case "$opt" in
+			t)
+				timeout="${OPTARG}"
+				break
+				;;
+			*)
+				echo "$0: wait_for_files: unknown option" >&2
+				return 1
+				;;
+		esac
+	done
+	shift $((OPTIND - 1))
+
+	wff_dir="/tmp/wait_for_files"
+
+	[ ! -d "$wff_dir" ] && mkdir "$wff_dir"
+
+	echo "Waiting for files ($@) with ${timeout}s timeout"
+	i=0
+	timeout=$((timeout * 4))
+	while [ $i -lt $timeout ]; do
+		all_found=1
+		for file in $@; do
+			fpath="$wff_dir/${file}"
+			[ -e "$fpath" -o -e "$file" ] && continue
+			all_found=0
+		done
+		if [ $all_found -eq 1 ]; then
+			return 0
+		fi
+		usleep 250000
+		i=$((i + 1 ))
+	done
+	not_found=""
+	# mark files that we have waited for
+	for file in $@; do
+		fpath="$wff_dir/${file}"
+		[ ! -e "$fpath" ] && mkdir -p "$fpath"
+		[ ! -e "$file" ] && not_found="$not_found $file"
+	done
+	echo "Waiting for files ($@) finished. Missing files: $not_found"
+
+	return 0
+}
diff --git a/geninitrd b/geninitrd
index 50be081..a79b9c3 100755
--- a/geninitrd
+++ b/geninitrd
@@ -1530,7 +1530,7 @@ chmod a+rx "$RCFILE"
 ln -s linuxrc $DESTDIR/init
 
 # create dirs that we really need
-inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,run,sys}
+inst_d /{lib,bin,etc,dev{,/pts,/shm},loopfs,var,proc,run,sys,tmp}
 
 modules_install "$MODULES"
 
@@ -1559,7 +1559,11 @@ mount_proc
 
 kmsg "geninitrd/$VERSION starting"
 
+inst_d /lib/geninitrd/
+inst /lib/geninitrd/functions.initrd /lib/geninitrd/functions.initrd
+
 add_linuxrc <<-EOF
+	. /lib/geninitrd/functions.initrd
 	# builtin defaults from geninitrd
 	ROOT=$rootdev
 	ROOTFS=$rootFs
@@ -1631,6 +1635,9 @@ add_linuxrc <<-'EOF'
 	fi
 EOF
 
+# mount early
+mount_tmp
+
 modules_add_linuxrc $MODULES
 
 # TODO: rewrite for busybox
diff --git a/mod-md.sh b/mod-md.sh
index d9093d4..eed27c7 100644
--- a/mod-md.sh
+++ b/mod-md.sh
@@ -183,6 +183,7 @@ initrd_gen_md() {
 		done
 	done
 
+	echo "wait_for_files $cr_dev_list" | add_linuxrc
 	add_linuxrc <<-'EOF'
 	mdassemble
 
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/geninitrd.git/commitdiff/efb675bf488a4cd3b3f69c452a5423a9a7dfd490



More information about the pld-cvs-commit mailing list