Author: glen
Date: Tue Oct 30 16:08:43 2007
New Revision: 8899

Modified:
   geninitrd/trunk/geninitrd
Log:
- add function add_linuxrc() and generate all output via that function
- add debuginitrd=sh possibility to invoke shell at some points in initrd

Modified: geninitrd/trunk/geninitrd
==============================================================================
--- geninitrd/trunk/geninitrd   (original)
+++ geninitrd/trunk/geninitrd   Tue Oct 30 16:08:43 2007
@@ -116,6 +116,12 @@
        exit $rc
 }
 
+# append text to /linuxrc
+# takes STDIN as input
+add_linuxrc() {
+       cat >> "$s"
+}
+
 # Checks if busybox has support for APPLET(s)
 # Exits from geninitrd if the support is not present.
 #
@@ -800,8 +806,7 @@
 
 firmware_install_module_pre() {
        local module="$1"
-       local linuxrc="$2"
-       local firmware_files="$3"
+       local firmware_files="$2"
 
        debug "Adding Firmwares ($firmware_files) to initrd for module $module"
        inst_d /proc
@@ -823,17 +828,19 @@
                inst /lib/firmware/$firmware /lib/firmware/$firmware
        done
 
-       echo "mount -t proc none /proc" >> "$linuxrc"
-       echo "mount -t sysfs none /sys" >> "$linuxrc"
-       echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" 
>> "$linuxrc"
+       echo "mount -t proc none /proc" | add_linuxrc
+       echo "mount -t sysfs none /sys" | add_linuxrc
+       echo "echo -n "/lib/firmware/firmware.sh" > /proc/sys/kernel/hotplug" | 
add_linuxrc
 }
 
 firmware_install_module_post() {
        local module="$1"
-       local linuxrc="$2"
-       local firmware_files="$3"
-       echo "umount /sys" >> "$linuxrc"
-       echo "umount /proc" >> "$linuxrc"
+       local firmware_files="$2"
+
+       add_linuxrc <<-'EOF'
+               umount /sys
+               umount /proc
+       EOF
 }
 
 modules_install() {
@@ -850,7 +857,6 @@
 
 modules_add_linuxrc() {
        local modules="$1"
-       local linuxrc="$2"
        local mod
 
        for mod in $modules; do
@@ -879,14 +885,14 @@
                fi
 
                if [ -n "$firmware_var" ]; then
-                       firmware_install_module_pre "$module" "$linuxrc" 
"$firmware_var"
+                       firmware_install_module_pre "$module" "$firmware_var"
                fi
-               echo "$insmod /lib/modules/$kernel/$MODULE2 $options" >> 
"$linuxrc"
+               echo "$insmod /lib/modules/$kernel/$MODULE2 $options" | 
add_linuxrc
                if [ -n "${sleep_var}" ]; then
-                       echo "usleep $sleep_var" >> "$linuxrc"
+                       echo "usleep $sleep_var" | add_linuxrc
                fi
                if [ -n "$firmware_var" ]; then
-                       firmware_install_module_post "$module" "$linuxrc" 
"$firmware_var"
+                       firmware_install_module_post "$module" "$firmware_var"
                fi
        done
 }
@@ -1228,6 +1234,8 @@
 inst_d /dev/pts /dev/shm
 
 s="$RCFILE"
+> "$s"
+chmod 755 "$s"
 ln -s /linuxrc $MNTIMAGE/init
 
 inst /bin/initrd-busybox /bin/initrd-busybox
@@ -1238,18 +1246,26 @@
        inst "$INSMOD" /bin/insmod.static
 fi
 
-cat > "$s" <<'EOF'
-#! /bin/sh
-mount -t proc none /proc
-CMDLINE="$(cat /proc/cmdline)"
-if [[ "$CMDLINE" = *debuginitrd* ]]; then
-       set -x
-fi
-umount /proc
+add_linuxrc <<-'EOF'
+       #!/bin/sh
+       mount -t proc none /proc
+       export CMDLINE="$(cat /proc/cmdline)"
+       if [ "$(awk '/debuginitrd/ { print "yes" }' /proc/cmdline)" = "yes" ]; 
then
+               export DEBUGINITRD=yes
+               debugshell() {
+                       : 'Entering debug shell. Type Ctrl-D to exit it.'
+                       sh
+               }
+               set -x
+       else
+               debugshell() {
+                       :
+               }
+       fi
+       umount /proc
 EOF
-chmod 755 "$s"
 
-modules_add_linuxrc "$MODULES" "$s"
+modules_add_linuxrc "$MODULES"
 
 # TODO: rewrite for busybox
 #if [ -n "$loopDev" ]; then
@@ -1279,24 +1295,22 @@
        else
                inst /usr/sbin/resume /bin/resume
        fi
-       echo "resume" >> "$s"
+       echo "resume" | add_linuxrc
 }
 
 initrd_gen_tuxonice() {
        inst_d /sys /proc
-cat << 'EOF' >> "$s"
-mount -t proc none /proc
-mount -t sysfs none /sys
-if [ "$(awk ' /resume2=/  { print "yes"; } ' /proc/cmdline)" = "yes" ]; then
-EOF
-cat << EOF >> "$s"
-               [ -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
+       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
@@ -1316,15 +1330,15 @@
        tmpfs_dev=yes
 
        busybox_applet mount mknod mkdir
-       cat >> "$s" <<-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
+       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
 }
 
 initrd_gen_udev() {
@@ -1340,7 +1354,7 @@
 
        if is_yes "$USE_UDEV"; then
                initrd_gen_tmpfs_dev
-               cat >> "$s" <<- 'EOF'
+               add_linuxrc <<-'EOF'
                        mount -t proc none /proc
                        mount -t sysfs none /sys
                        : 'Starting udev'
@@ -1349,14 +1363,14 @@
                if is_yes "$PROBESTATICMODS"; then
                        inst /sbin/initrd-udevtrigger /sbin/udevtrigger
                        inst /sbin/initrd-udevsettle /sbin/udevsettle
-                       cat >> "$s" <<-'EOF'
+                       add_linuxrc <<-'EOF'
                                /sbin/udevtrigger
                                /sbin/udevsettle
                        EOF
                fi
 
                busybox_applet killall
-               cat >> "$s" <<-'EOF'
+               add_linuxrc     <<-'EOF'
                        killall udevd
                        umount /proc
                        umount /sys
@@ -1378,26 +1392,28 @@
                inst /var/lib/multipath/bindings /var/lib/multipath
        fi
 
-       cat <<-'EOF' >> "$s"
-       mount -t proc none /proc
-       mount -t sysfs none /sys
-EOF
+       add_linuxrc <<-'EOF'
+               mount -t proc none /proc
+               mount -t sysfs none /sys
+       EOF
 
        initrd_gen_tmpfs_dev
        initrd_gen_devices
 
-       echo "export multipath_id=$MULTIPATH_ID" >> "$s"
-       cat <<-'EOF' >> "$s"
-       /sbin/multipath -v 0 $multipath_id
-
-       for a in /dev/mapper/*; do
-               [ $a = /dev/mapper/control ] && continue
-               /sbin/kpartx -a $a
-       done
+       echo "export multipath_id=$MULTIPATH_ID" | add_linuxrc
+       add_linuxrc <<-'EOF'
+               /sbin/multipath -v 0 $multipath_id
+
+               for a in /dev/mapper/*; do
+                       [ $a = /dev/mapper/control ] && continue
+                       /sbin/kpartx -a $a
+               done
 
-       umount /sys
-       umount /proc
-EOF
+               debugshell
+
+               umount /sys
+               umount /proc
+       EOF
 }
 
 initrd_gen_dmraid() {
@@ -1410,21 +1426,24 @@
 
        inst_d /sbin /sys
        inst /sbin/dmraid-initrd /sbin/dmraid
-       cat <<-EOF >> "$s"
-       mount -t proc none /proc
-       mount -t sysfs none /sys
-EOF
+       add_linuxrc <<-EOF
+               mount -t proc none /proc
+               mount -t sysfs none /sys
+       EOF
 
        initrd_gen_devices
 
-       cat <<-EOF >> "$s"
-       # 2 secs was enough for my system to initialize. but really this is 
udev issue?
-       usleep 2000000
-       : 'Activating Device-Mapper RAID(s)'
-       /sbin/dmraid -ay -i
-       umount /sys
-       umount /proc
-EOF
+       add_linuxrc <<-EOF
+               # 2 secs was enough for my system to initialize. but really 
this is udev issue?
+               usleep 2000000
+               : 'Activating Device-Mapper RAID(s)'
+               /sbin/dmraid -ay -i
+
+               debugshell
+
+               umount /sys
+               umount /proc
+       EOF
 }
 
 # Generates /dev nodes based on /proc/partitions information.
@@ -1436,16 +1455,19 @@
        fi
        proc_partitions=yes
 
-       cat <<-'EOF' >> "$s"
-       : 'Making device nodes'
-       # ignore first two lines, header, empty line and process rest
-       cat /proc/partitions | (read b; read b; while read major minor blocks 
dev rest; do
-               node=/dev/$dev
-               mkdir -p ${node%/*}
-               mknod $node b $major $minor
-       done
-       )
-EOF
+       add_linuxrc <<-'EOF'
+               : 'Making device nodes'
+               cat /proc/partitions | (
+                       # ignore first two lines, header, empty line and 
process rest
+                       read b; read b
+
+                       while read major minor blocks dev rest; do
+                               node=/dev/$dev
+                               mkdir -p ${node%/*}
+                               mknod $node b $major $minor
+                       done
+               )
+       EOF
 }
 
 initrd_gen_md() {
@@ -1488,7 +1510,7 @@
                done
        done
 
-       echo "mdassemble" >> "$s"
+       echo "mdassemble" | add_linuxrc
 
        # needed to determine md-version
        if [ "$do_md0" -eq 1 ]; then
@@ -1503,45 +1525,49 @@
        mknod "$MNTIMAGE/dev/urandom" c 1 9
        mkdir "$MNTIMAGE/newroot"
        mkdir "$MNTIMAGE/proc"
-       echo "ifconfig lo 127.0.0.1 up" >> "$s"
-       echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" >> "$s"
-       echo "ifconfig eth0 0.0.0.0 up" >> "$s"
-       echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" >> "$s"
-       cat << 'EOF' > "$MNTIMAGE/bin/setdhcp"
-#!/bin/sh
-[ "$1" != "bound" ] && exit
-[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
-[ -n "$subnet" ] && NETMASK="netmask $subnet"
-ifconfig $interface $ip $BROADCAST $NETMASK up
-if [ -n "$router" ]; then
-       for r in $router; do
-               route add default gw $r dev $interface
-       done
-fi
+       echo "ifconfig lo 127.0.0.1 up" | add_linuxrc
+       echo "route add -net 127.0.0.0 netmask 255.0.0.0 lo" | add_linuxrc
+       echo "ifconfig eth0 0.0.0.0 up" | add_linuxrc
+       echo "udhcpc -i eth0 -f -q -s /bin/setdhcp" | add_linuxrc
+
+       cat <<-'EOF' > "$MNTIMAGE/bin/setdhcp"
+               #!/bin/sh
+               [ "$1" != "bound" ] && exit
+               [ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
+               [ -n "$subnet" ] && NETMASK="netmask $subnet"
+               ifconfig $interface $ip $BROADCAST $NETMASK up
+               if [ -n "$router" ]; then
+                       for r in $router; do
+                               route add default gw $r dev $interface
+                       done
+               fi
 
-mount -t proc none /proc
-for o in $(cat /proc/cmdline); do
-       case $o in
-       nfsroot=*)
-               rootpath=${o#nfsroot=}
-               ;;
-       esac
-done
-umount /proc
+               mount -t proc none /proc
+               for o in $(cat /proc/cmdline); do
+                       case $o in
+                       nfsroot=*)
+                               rootpath=${o#nfsroot=}
+                               ;;
+                       esac
+               done
+               umount /proc
+
+               if [ -n "$rootpath" ]; then
+                       mount -n -t nfs -o 
ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath /newroot
+               else
+                       echo "Missing rootpath in what DHCP server sent to us. 
Failing..."
+                       echo "All seen variables are listed below:"
+                       set
+               fi
+       EOF
 
-if [ -n "$rootpath" ]; then
-       mount -n -t nfs -o ro,nolock,posix,tcp,wsize=8192,rsize=8192 $rootpath 
/newroot
-else
-       echo "Missing rootpath in what DHCP server sent to us. Failing..."
-       echo "All seen variables are listed below:"
-       set
-fi
-EOF
        chmod 755 "$MNTIMAGE/bin/setdhcp"
-       echo "cd /newroot" >> "$s"
-       echo "pivot_root . initrd" >> "$s"
-       echo "[ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < 
dev/console > dev/console 2>&1" >> "$s"
-       echo "exec /usr/sbin/chroot . /sbin/init -i < dev/console > dev/console 
2>&1" >> "$s"
+       add_linuxrc <<-'EOF'
+               cd /newroot
+               pivot_root . initrd
+               [ -x /sbin/chroot ] && exec /sbin/chroot . /sbin/init -i < 
dev/console > dev/console 2>&1
+               exec /usr/sbin/chroot . /sbin/init -i < dev/console > 
dev/console 2>&1
+       EOF
 }
 
 initrd_gen_lvm() {
@@ -1563,16 +1589,20 @@
                        inst $device /dev
                done
        fi
-       echo "mount -t proc none /proc" >> "$s"
-       echo "mount -t tmpfs none /tmp" >> "$s"
+       add_linuxrc <<-'EOF'
+               mount -t proc none /proc
+               mount -t tmpfs none /tmp
+       EOF
        if [ "$LVMTOOLSVERSION" = "1" ]; then
-               echo "lvm vgscan -T" >> "$s"
-               echo "lvm vgchange -T -a y $VGVOLUME" >> "$s"
-               echo "umount /tmp" >> "$s"
-               # fail to umount
-               echo "umount /proc" >> "$s"
+               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" >> "$s"
+               echo "cat /etc/lvm.conf > /tmp/lvm.conf" | add_linuxrc
                echo "global {" > "$MNTIMAGE/etc/lvm.conf"
                echo "  locking_type = 0" >> "$MNTIMAGE/etc/lvm.conf"
                echo "  locking_dir = \"/tmp\"" >> "$MNTIMAGE/etc/lvm.conf"
@@ -1601,50 +1631,49 @@
 
                initrd_gen_devices
 
-               cat <<-EOF >> "$s"
-
-               # disable noise from lvm accessing devices that aren't ready.
-               printk=\$(cat /proc/sys/kernel/printk)
-               echo 0 > /proc/sys/kernel/printk
-
-               export LVM_SYSTEM_DIR=/tmp
-               : 'Scanning for Volume Groups'
-               lvm vgscan --mknodes --ignorelockingfailure 2>/dev/null
-
-               : 'Activating Volume Groups'
-               lvm vgchange --ignorelockingfailure -a y $VGVOLUME 2>/dev/null
-
-               echo "\$printk" > /proc/sys/kernel/printk
-
-               # Find out major/minor
-               majmin="\$(lvm lvdisplay --ignorelockingfailure -c $rootdev 
2>/dev/null)"
-               majmin="\${majmin#*/}"
-               majmin="\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}"
-               major="\${majmin%:*}"
-               minor="\${majmin#*:}"
-               # Pass it to kernel
-               val=\$((256 * \$major + \$minor))
-               echo \$val > /proc/sys/kernel/real-root-dev
-               umount /tmp
-               umount /proc
-EOF
+               add_linuxrc <<-EOF
+                       # disable noise from lvm accessing devices that aren't 
ready.
+                       printk=\$(cat /proc/sys/kernel/printk)
+                       echo 0 > /proc/sys/kernel/printk
+
+                       export LVM_SYSTEM_DIR=/tmp
+                       : 'Scanning for Volume Groups'
+                       lvm vgscan --mknodes --ignorelockingfailure 2>/dev/null
+
+                       : 'Activating Volume Groups'
+                       lvm vgchange --ignorelockingfailure -a y $VGVOLUME 
2>/dev/null
+
+                       echo "\$printk" > /proc/sys/kernel/printk
+
+                       # Find out major/minor
+                       majmin="\$(lvm lvdisplay --ignorelockingfailure -c 
$rootdev 2>/dev/null)"
+                       majmin="\${majmin#*/}"
+                       majmin="\${majmin#*:*:*:*:*:*:*:*:*:*:*:*}"
+                       major="\${majmin%:*}"
+                       minor="\${majmin#*:}"
+                       # Pass it to kernel
+                       val=\$((256 * \$major + \$minor))
+                       echo \$val > /proc/sys/kernel/real-root-dev
<<diff output has been trimmed to 500 lines, 165 line(s) remained.>>
_______________________________________________
pld-cvs-commit mailing list
[email protected]
http://lists.pld-linux.org/mailman/listinfo/pld-cvs-commit

Reply via email to