[readonly/geninitrd: 489/1068] - new functions: mount_{dev,sys,proc,tmp} to mount those filesystems and use them

draenog draenog at pld-linux.org
Sat Nov 2 19:53:30 CET 2013


commit b64f015b8175ed0c6269c16e657931e961b2f3b1
Author: Elan Ruusamäe <glen at pld-linux.org>
Date:   Tue Oct 30 21:25:46 2007 +0000

    - new functions: mount_{dev,sys,proc,tmp} to mount those filesystems and use them
    
    svn-id: @8907

 geninitrd | 274 +++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 147 insertions(+), 127 deletions(-)
---
diff --git a/geninitrd b/geninitrd
index 3353998..ea7d387 100755
--- a/geninitrd
+++ b/geninitrd
@@ -51,8 +51,15 @@ PROBESTATICMODULES=no
 
 # internal variables
 # is /dev on tmpfs
-tmpfs_dev=no
-# is /proc/devices entries already created
+dev_mounted=no
+# is /proc mounted
+proc_mounted=no
+# is /sys mounted
+sys_mounted=no
+# is /tmp mounted on tmpfs
+tmp_mounted=no
+
+# are /dev nodes already created from /proc/devices info?
 proc_partitions=no
 
 # LVM devices that should not be included in vgscan on initrd
@@ -86,7 +93,7 @@ if [ -x /sbin/multipath ]; then
 	USE_MULTIPATH=yes
 fi
 
-usage () {
+usage() {
 	uname_r=$(uname -r)
 	echo "usage: $PROGRAM [--version] [-v] [-f] [--ifneeded] [--preload <module>]"
 	echo "       [--with=<module>] [--image-version] [--fstab=<fstab>] [--nocompress]"
@@ -119,9 +126,96 @@ die() {
 # append text to /linuxrc
 # takes STDIN as input
 add_linuxrc() {
-	cat >> "$s"
+	cat >> "$RCFILE"
 }
 
+# generate code to mount /dev on tmpfs and create initial nodes
+# can be called multiple times. /dev is cleaned up (umounted) automatically at
+# the end of script.
+mount_dev() {
+    if [ "$INITRDFS" = "initramfs" ]; then
+		# initramfs is read-write filesystem, no need for tmpfs
+		return
+	fi
+
+	# we already generated tmpfs code; return
+	if is_yes "$dev_mounted"; then
+		return
+	fi
+
+	dev_mounted=yes
+
+	busybox_applet mount mknod mkdir
+	add_linuxrc <<-EOF
+		: 'Creating /dev'
+		mount -o mode=0755 -t tmpfs none /dev
+		mknod /dev/console c 5 1
+		mknod /dev/null c 1 3
+		mknod /dev/zero c 1 5
+		mkdir /dev/pts
+		mkdir /dev/shm
+	EOF
+}
+
+# generate code to mount /proc on initrd
+# can be called multiple times
+mount_proc() {
+	if is_yes "$proc_mounted"; then
+		return
+	fi
+
+	proc_mounted=yes
+	echo "mount -t proc none /proc" | add_linuxrc
+}
+
+# generate code to mount /sys on initrd
+# can be called multiple times
+mount_sys() {
+	if is_yes "$sys_mounted"; then
+		return
+	fi
+
+	sys_mounted=yes
+	echo "mount -t sysfs none /sys" | add_linuxrc
+}
+
+# generate code to mount /tmp on initrd
+# can be called multiple times
+mount_tmp() {
+    if [ "$INITRDFS" = "initramfs" ]; then
+		# initramfs is read-write filesystem, no need for tmpfs
+		return
+	fi
+
+	if is_yes "$tmp_mounted"; then
+		return
+	fi
+
+	tmp_mounted=yes
+	echo "mount -t tmpfs none /tmp" | add_linuxrc
+}
+
+# unmount all mountpoints mounted by geninitrd
+umount_all() {
+	if is_yes "$dev_mounted"; then
+		echo 'umount /dev' | add_linuxrc
+		dev_mounted=no
+	fi
+	if is_yes "$proc_mounted"; then
+		echo 'umount /proc' | add_linuxrc
+		proc_mounted=no
+	fi
+	if is_yes "$sys_mounted"; then
+		echo 'umount /sys' | add_linuxrc
+		sys_mounted=no
+	fi
+	if is_yes "$tmp_mounted"; then
+		echo 'umount /tmp' | add_linuxrc
+		tmp_mounted=no
+	fi
+}
+
+
 # Checks if busybox has support for APPLET(s)
 # Exits from geninitrd if the support is not present.
 #
@@ -803,7 +897,7 @@ find_modules_for_device() {
 	fi
 }
 
-firmware_install_module_pre() {
+firmware_install_module() {
 	local module="$1"
 	local firmware_files="$2"
 
@@ -827,21 +921,11 @@ EOF
 		inst /lib/firmware/$firmware /lib/firmware/$firmware
 	done
 
-	echo "mount -t proc none /proc" | add_linuxrc
-	echo "mount -t sysfs none /sys" | add_linuxrc
+	mount_proc
+	mount_sys
 	echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | add_linuxrc
 }
 
-firmware_install_module_post() {
-	local module="$1"
-	local firmware_files="$2"
-
-	add_linuxrc <<-'EOF'
-		umount /sys
-		umount /proc
-	EOF
-}
-
 modules_install() {
 	local modules="$1"
 	local mod
@@ -884,15 +968,12 @@ modules_add_linuxrc() {
 		fi
 
 		if [ -n "$firmware_var" ]; then
-			firmware_install_module_pre "$module" "$firmware_var"
+			firmware_install_module "$module" "$firmware_var"
 		fi
 		echo "$insmod /lib/modules/$kernel/$MODULE2 $options" | add_linuxrc
 		if [ -n "${sleep_var}" ]; then
 			echo "usleep $sleep_var" | add_linuxrc
 		fi
-		if [ -n "$firmware_var" ]; then
-			firmware_install_module_post "$module" "$firmware_var"
-		fi
 	done
 }
 
@@ -1230,9 +1311,6 @@ mknod "$MNTIMAGE/dev/null" c 1 3
 mknod "$MNTIMAGE/dev/zero" c 1 5
 inst_d /dev/pts /dev/shm
 
-s="$RCFILE"
-> "$s"
-chmod 755 "$s"
 ln -s /linuxrc $MNTIMAGE/init
 
 inst /bin/initrd-busybox /bin/initrd-busybox
@@ -1243,11 +1321,10 @@ if is_yes "$USEINSMODSTATIC"; then
 	inst "$INSMOD" /bin/insmod.static
 fi
 
+echo '#!/bin/sh' | add_linuxrc
+mount_proc
 add_linuxrc <<-'EOF'
-	#!/bin/sh
-	mount -t proc none /proc
 	export CMDLINE="$(cat /proc/cmdline)"
-	umount /proc
 
 	export DEBUGINITRD=$(echo "$CMDLINE" | awk '{ for (i=1; i<=NF; i++) { if ($i == "debuginitrd") { print "yes"; exit }; if ($i ~ /^debuginitrd=/) { gsub(/^debuginitrd=/, NIL, $i); print $i; exit; } } }')
 
@@ -1302,44 +1379,14 @@ initrd_gen_suspend() {
 
 initrd_gen_tuxonice() {
 	inst_d /sys /proc
+	mount_proc
+	mount_sys
 	add_linuxrc <<-'EOF'
-		mount -t proc none /proc
-		mount -t sysfs none /sys
 		if [ "$(echo "$CMDLINE" | awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
 			[ -e /proc/suspend2/do_resume ] && echo > /proc/suspend2/do_resume
 			[ -e /sys/power/suspend2/do_resume ] && echo > /sys/power/suspend2/do_resume
 			[ -e /sys/power/tuxonice/do_resume ] && echo >  /sys/power/tuxonice/do_resume
 		fi
-		umount /sys
-		umount /proc
-	EOF
-}
-
-# generate code to mount /dev on tmpfs and create initial nodes
-# can be called multiple times. /dev is cleaned up (umounted) automatically at
-# the end of script.
-initrd_gen_tmpfs_dev() {
-    if [ "$INITRDFS" = "initramfs" ]; then
-		# initramfs is read-write filesystem, no need for tmpfs
-		return
-	fi
-
-	# we already generated tmpfs code; return
-	if is_yes "$tmpfs_dev"; then
-		return
-	fi
-
-	tmpfs_dev=yes
-
-	busybox_applet mount mknod mkdir
-	add_linuxrc <<-EOF
-		: 'Creating /dev'
-		mount -o mode=0755 -t tmpfs none /dev
-		mknod /dev/console c 5 1
-		mknod /dev/null c 1 3
-		mknod /dev/zero c 1 5
-		mkdir /dev/pts
-		mkdir /dev/shm
 	EOF
 }
 
@@ -1354,30 +1401,25 @@ initrd_gen_udev() {
 	inst /sbin/initrd-udevd /sbin/udevd
 	inst /etc/udev/udev.conf /etc/udev/udev.conf
 
-	if is_yes "$USE_UDEV"; then
-		initrd_gen_tmpfs_dev
+	mount_dev
+	mount_sys
+	add_linuxrc <<-'EOF'
+		: 'Starting udev'
+		/sbin/udevd --daemon
+	EOF
+	if is_yes "$PROBESTATICMODS"; then
+		inst /sbin/initrd-udevtrigger /sbin/udevtrigger
+		inst /sbin/initrd-udevsettle /sbin/udevsettle
 		add_linuxrc <<-'EOF'
-			mount -t proc none /proc
-			mount -t sysfs none /sys
-			: 'Starting udev'
-			/sbin/udevd --daemon
-		EOF
-		if is_yes "$PROBESTATICMODS"; then
-			inst /sbin/initrd-udevtrigger /sbin/udevtrigger
-			inst /sbin/initrd-udevsettle /sbin/udevsettle
-			add_linuxrc <<-'EOF'
-				/sbin/udevtrigger
-				/sbin/udevsettle
-			EOF
-		fi
-
-		busybox_applet killall
-	   	add_linuxrc	<<-'EOF'
-			killall udevd
-			umount /proc
-			umount /sys
+			/sbin/udevtrigger
+			/sbin/udevsettle
 		EOF
 	fi
+
+	busybox_applet killall
+	add_linuxrc	<<-'EOF'
+		killall udevd
+	EOF
 }
 
 initrd_gen_multipath() {
@@ -1393,14 +1435,11 @@ initrd_gen_multipath() {
 		inst /var/lib/multipath/bindings /var/lib/multipath
 	fi
 
-	add_linuxrc <<-'EOF'
-		mount -t proc none /proc
-		mount -t sysfs none /sys
-	EOF
-
-	initrd_gen_tmpfs_dev
+	mount_proc
+	mount_dev
 	initrd_gen_devices
 
+	mount_sys
 	echo "export multipath_id=$MULTIPATH_ID" | add_linuxrc
 	add_linuxrc <<-'EOF'
 		debugshell
@@ -1411,9 +1450,6 @@ initrd_gen_multipath() {
 			/sbin/kpartx -a $a
 		done
 		debugshell
-
-		umount /sys
-		umount /proc
 	EOF
 }
 
@@ -1422,18 +1458,13 @@ initrd_gen_dmraid() {
 		die "/sbin/dmraid-initrd is missing!"
 	fi
 
-	# always make /dev on tmpfs
-	initrd_gen_tmpfs_dev
-
 	inst_d /sbin /sys
 	inst /sbin/dmraid-initrd /sbin/dmraid
-	add_linuxrc <<-EOF
-		mount -t proc none /proc
-		mount -t sysfs none /sys
-	EOF
 
+	mount_dev
+	mount_proc
+	mount_sys
 	initrd_gen_devices
-
 	add_linuxrc <<-EOF
 		# 2 secs was enough for my system to initialize. but really this is udev issue?
 		usleep 2000000
@@ -1441,9 +1472,6 @@ initrd_gen_dmraid() {
 		/sbin/dmraid -ay -i
 
 		debugshell
-
-		umount /sys
-		umount /proc
 	EOF
 }
 
@@ -1456,6 +1484,8 @@ initrd_gen_devices() {
 	fi
 	proc_partitions=yes
 
+	mount_proc
+	mount_dev
 	add_linuxrc <<-'EOF'
 		: 'Making device nodes'
 		cat /proc/partitions | (
@@ -1542,8 +1572,11 @@ initrd_gen_nfs() {
 				route add default gw $r dev $interface
 			done
 		fi
+	EOF
+
+	mount_proc
 
-		mount -t proc none /proc
+	cat <<-'EOF' > "$MNTIMAGE/bin/setdhcp"
 		for o in $(cat /proc/cmdline); do
 			case $o in
 			nfsroot=*)
@@ -1551,7 +1584,6 @@ initrd_gen_nfs() {
 				;;
 			esac
 		done
-		umount /proc
 
 		if [ -n "$rootpath" ]; then
 			mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
@@ -1578,10 +1610,10 @@ initrd_gen_lvm() {
 
 	# always make /dev on tmpfs for LVM2
 	if [ "$LVMTOOLSVERSION" = "2" ]; then
-		initrd_gen_tmpfs_dev
+		mount_dev
 	fi
 
-	if ! is_yes "$tmpfs_dev"; then
+	if ! is_yes "$dev_mounted"; then
 		inst_d /dev/mapper
 		mknod $MNTIMAGE/dev/mapper/control c 10 63
 		for device in $PVDEVICES; do
@@ -1590,17 +1622,13 @@ initrd_gen_lvm() {
 			inst $device /dev
 		done
 	fi
-	add_linuxrc <<-'EOF'
-		mount -t proc none /proc
-		mount -t tmpfs none /tmp
-	EOF
+
+	mount_proc
+	mount_tmp
 	if [ "$LVMTOOLSVERSION" = "1" ]; then
 		add_linuxrc <<-EOF
 			lvm vgscan -T
 			lvm vgchange -T -a y $VGVOLUME
-			umount /tmp
-			# fail to umount
-			umount /proc
 		EOF
 	else
 		echo "cat /etc/lvm.conf > /tmp/lvm.conf" | add_linuxrc
@@ -1655,8 +1683,6 @@ initrd_gen_lvm() {
 			# Pass it to kernel
 			val=\$((256 * \$major + \$minor))
 			echo \$val > /proc/sys/kernel/real-root-dev
-			umount /tmp
-			umount /proc
 		EOF
 	fi
 }
@@ -1664,8 +1690,8 @@ initrd_gen_lvm() {
 initrd_gen_procdata() {
 	debug "Adding rootfs finding based on root= option support."
 	inst_d /proc
+	mount_proc
 	add_linuxrc <<-'EOF'
-		mount -t proc none /proc
 		root="$(busybox awk ' /root=\/dev\// { gsub(/.*root=\/dev\//,NIL,$0); gsub(/ .*/,NIL,$0); print $0; } ' /proc/cmdline)"
 		if [ -n "$root" ]; then
 			rootnr="$(busybox awk -v root="$root" ' { if ($4 == root) { print 256*$1+$2; } } ' /proc/partitions)"
@@ -1673,12 +1699,10 @@ initrd_gen_procdata() {
 				echo "$rootnr" > /proc/sys/kernel/real-root-dev
 			fi
 		fi
-		umount /proc
 	EOF
 }
 
 # main generation
-
 if is_yes "$USE_UDEV"; then
 	initrd_gen_udev
 fi
@@ -1714,14 +1738,6 @@ else
 	initrd_gen_procdata
 fi
 
-# finally umount /dev if it was on tmpfs
-if is_yes "$tmpfs_dev"; then
-	add_linuxrc <<-EOF
-		umount /dev
-	EOF
-	tmpfs_dev=no
-fi
-
 # additional devs always needed
 [ ! -e "$MNTIMAGE/$rootdev_add" ] && inst $rootdev_add /dev
 
@@ -1730,9 +1746,8 @@ if [ "$INITRDFS" = "initramfs" ]; then
 	[ ! -e "$MNTIMAGE/$rootdev" ] && inst $rootdev /dev
 	# Parsing root parameter
 	# We support passing root as hda3 /dev/hda3 0303 0x0303 and 303
+	mount_proc
 	add_linuxrc <<-'EOF'
-		mount -t proc none /proc
-
 		eval "$(busybox awk -v c="$CMDLINE" '
 		BEGIN {
 			num_pattern_short = "[0-9a-f][0-9a-f][0-9a-f]";
@@ -1778,7 +1793,9 @@ if [ "$INITRDFS" = "initramfs" ]; then
 		if [ -z "\$init" -o ! -x "/newroot\$init" ]; then
 			init=/sbin/init
 		fi
-		umount /proc
+	EOF
+		umount_all
+	add_linuxrc <<-EOF
 		exec switch_root /newroot \$init
 
 		echo "Error ! initramfs should not reach this place."
@@ -1792,6 +1809,9 @@ if [ "$INITRDFS" = "initramfs" ]; then
 	# we need real file, not symlink
 	rm -f $MNTIMAGE/init
 	cp -a $MNTIMAGE/linuxrc $MNTIMAGE/init
+else
+	# other than initramfs
+	umount_all
 fi
 
 chmod +x "$RCFILE"
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/readonly/geninitrd.git/commitdiff/147754ca159d40ca5eb541074dc043d8cbd92090



More information about the pld-cvs-commit mailing list