From 580bb5417c7f6e09fd046ed2d92aeec519373624 Mon Sep 17 00:00:00 2001 From: Philippe Seewer Date: Tue, 16 Jun 2009 14:24:38 +0200 Subject: [PATCH] netroot and others: Style changes Multiline conditionals ( && { ... } ) should use if, function declarations go to the top of the file, add/update comments and remove some unnecessary clutter. --- modules.d/40network/dhclient-script | 75 +++++++------ modules.d/40network/ifup | 119 +++++++++----------- modules.d/40network/netroot | 8 +- modules.d/95iscsi/iscsiroot | 165 +++++++++++++++------------- modules.d/95nfs/nfsroot | 29 +++-- 5 files changed, 195 insertions(+), 201 deletions(-) diff --git a/modules.d/40network/dhclient-script b/modules.d/40network/dhclient-script index 633ad262..3a255de4 100755 --- a/modules.d/40network/dhclient-script +++ b/modules.d/40network/dhclient-script @@ -4,15 +4,6 @@ LOG=/tmp/dhclient.$$.log ERR=/tmp/network.$$.err -PATH=$PATH:/sbin:/usr/sbin - -. /lib/dracut-lib - -getarg rdnetdebug && { - exec >/tmp/dhclient.$interface.$$.out - exec 2>>/tmp/dhclient.$interface.$$.out - set -x -} log_err() { # avoid the need for cat on the image @@ -22,51 +13,63 @@ log_err() { while read line; do echo " $line"; done < $LOG >> $ERR } -# Catch unlikely initial errors -trap 'echo Errors preparing to configure $netif > $ERR; exit 0' EXIT - -netif=$interface -ip=$new_ip_address -mtu=$new_interface_mtu -mask=$new_subnet_mask -bcast=$new_broadcast_address -gw=${new_routers%%,*} -domain=$new_domain_name -search=$new_domain_search -namesrv=$new_domain_name_servers -hostname=$new_host_name - -[ -f /tmp/net.$interface.override ] && . /tmp/net.$interface.override - -# save the offending command and let udev move on if we have an error -trap 'log_err; exit 0' EXIT - run() { CMD="$@" "$@" >> $LOG 2>&1 } setup_interface() { - [ -n "$mtu" ] && { + ip=$new_ip_address + mtu=$new_interface_mtu + mask=$new_subnet_mask + bcast=$new_broadcast_address + gw=${new_routers%%,*} + domain=$new_domain_name + search=$new_domain_search + namesrv=$new_domain_name_servers + hostname=$new_host_name + + [ -f /tmp/net.$netif.override ] && . /tmp/net.$netif.override + + if [ -n "$mtu" ] ; then run ip link set $netif down run ip link set $netif mtu $mtu run ip link set $netif up - } + fi run ip addr add $ip${mask:+/$mask} ${bcast:+broadcast $bcast} dev $netif [ -n "$gw" ] && run ip route add default via $gw - [ -n "${search}${domain}" -a -n "$namesrv" ] && { + if [ -n "${search}${domain}" -a -n "$namesrv" ] ; then echo search $search $domain > /etc/resolv.conf for s in $namesrv; do echo nameserver $s >> /etc/resolv.conf done - } - [ -e /tmp/hostname.set ] || { + fi + if [ ! -e /tmp/hostname.set ] ; then [ -n "$hostname" ] && mknod /tmp/hostname.set p && run hostname $hostname - } + fi : } +PATH=$PATH:/sbin:/usr/sbin + +. /lib/dracut-lib + +if getarg rdnetdebug ; then + exec >/tmp/dhclient.$interface.$$.out + exec 2>>/tmp/dhclient.$interface.$$.out + set -x +fi + +# Huh? Interface configured? +[ -f "/tmp/net.$netif.up" ] && exit 0 + +# save offending commands and let udev move on if we have an error +trap 'log_err; exit 0' EXIT + +# We already need a set netif here +netif=$interface + case $reason in PREINIT) run ip link set $netif up @@ -75,8 +78,8 @@ case $reason in setup_interface set | while read line; do [ "${line#new_}" = "$line" ] && continue - echo "$line" >>/tmp/dhclient.$netif.dhcpopts - done + echo "$line" + done >/tmp/dhclient.$netif.dhcpopts >/tmp/net.$netif.up echo online > /sys/class/net/$netif/uevent ;; *) ;; diff --git a/modules.d/40network/ifup b/modules.d/40network/ifup index 6aa81d01..a1ad01ec 100755 --- a/modules.d/40network/ifup +++ b/modules.d/40network/ifup @@ -1,34 +1,5 @@ #!/bin/sh -PATH=$PATH:/sbin:/usr/sbin - -. /lib/dracut-lib - -getarg rdnetdebug && { - exec >/tmp/ifup.$1.$$.out - exec 2>>/tmp/ifup.$1.$$.out - set -x -} - -netif=$1 - -# bail immediately if the interface is already up -# or we don't need the network -[ -f "/tmp/net.$netif.up" ] && exit 0 -[ -f "/tmp/root.info" ] || exit 0 -. /tmp/root.info -[ -z "$netroot" ] && exit 0 - -# loopback is always handled the same way -[ "$netif" = "lo" ] && { - ip link set lo up - ip addr add 127.0.0.1/8 dev lo - >/tmp/net.$netif.up - exit 0 -} - -# XXX need error handling like dhclient-script - die() { echo $netif: "$@" 1>&2 exit 1 @@ -66,51 +37,63 @@ do_dhcp() { dhclient -1 -q -cf /etc/dhclient.conf -pf /tmp/dhclient.$netif.pid -lf /tmp/dhclient.$netif.lease $netif } -ip_to_var() { - local v=${1}: - set -- - while [ -n "$v" ]; do - set -- "$@" "${v%%:*}" - v=${v#*:} - done +PATH=$PATH:/sbin:/usr/sbin - unset ip srv gw mask hostname dev autoconf - case $# in - 0) autoconf=off ;; - 1) autoconf=$1 ;; - 2) dev=$1; autoconf=$2 ;; - *) ip=$1; srv=$2; gw=$3; mask=$4; hostname=$5; dev=$6; autoconf=$7 - case $autoconf in - ''|none|off) [ -n "$ip" ] && autoconf=static ;; - esac - esac - [ -n "$dev" ] || dev=$netif - [ -n "$autoconf" ] || autoconf=off -} +. /lib/dracut-lib +if getarg rdnetdebug ; then + exec >/tmp/ifup.$1.$$.out + exec 2>>/tmp/ifup.$1.$$.out + set -x +fi + +# Huh? No $1? +[ -z "$1" ] && exit 1 + +# $netif reads easier than $1 +netif=$1 + +# bail immediately if the interface is already up +# or we don't need the network +[ -f "/tmp/net.$netif.up" ] && exit 0 +[ -f "/tmp/root.info" ] || exit 0 +. /tmp/root.info +[ -z "$netroot" ] && exit 0 + +# loopback is always handled the same way +if [ "$netif" = "lo" ] ; then + ip link set lo up + ip addr add 127.0.0.1/8 dev lo + >/tmp/net.$netif.up + exit 0 +fi + +# XXX need error handling like dhclient-script + +# No ip lines default to dhcp ip=$(getarg ip) -if [ -z "$ip" ]; then - do_dhcp; -else - # spin through the kernel command line, looking for ip= lines - [ "$CMDLINE" ] || read CMDLINE /tmp/net.$netif.override + # Store config for later use + for i in ip srv gw mask hostname; do + eval '[ "$'$i'" ] && echo '$i'="$'$i'"' + done > /tmp/net.$netif.override - case $autoconf in - static) do_static ;; - dhcp|on|any) do_dhcp ;; - bootp|rarp|both) die "autoconfig type $autoconf is not supported" ;; - ''|none|off) ;; + case $autoconf in + static) do_static ;; + dhcp|on|any) do_dhcp ;; + bootp|rarp|both) die "autoconfig type $autoconf is not supported" ;; + ''|none|off) ;; esac break done diff --git a/modules.d/40network/netroot b/modules.d/40network/netroot index e57be483..10a50c7b 100755 --- a/modules.d/40network/netroot +++ b/modules.d/40network/netroot @@ -2,11 +2,11 @@ . /lib/dracut-lib -getarg rdnetdebug && { +if getarg rdnetdebug ; then exec >/tmp/netroot.$1.$$.out exec 2>>/tmp/netroot.$1.$$.out set -x -} +fi # Huh? Empty $1? [ -z "$1" ] && exit 1 @@ -16,12 +16,12 @@ getarg rdnetdebug && { # Only try to configure from one network interface at a time # -[ "$NETROOT_LOCKED" ] || { +if [ -z "$NETROOT_LOCKED" ] ; then NETROOT_LOCKED=true export NETROOT_LOCKED exec flock -xo /tmp/netroot.lock -c "$0 $*" exit 1 -} +fi # There's no sense in doing something if no (net)root info is available # or root is already there diff --git a/modules.d/95iscsi/iscsiroot b/modules.d/95iscsi/iscsiroot index 277c1290..f848abac 100755 --- a/modules.d/95iscsi/iscsiroot +++ b/modules.d/95iscsi/iscsiroot @@ -1,4 +1,10 @@ #!/bin/sh +# +# This implementation is incomplete: Discovery mode is not implemented and +# the argument handling doesn't follow currently agreed formats. This is mainly +# because rfc4173 does not say anything about iscsi_initiator but open-iscsi's +# iscsistart needs this. +# . /lib/dracut-lib @@ -12,11 +18,6 @@ if getarg rdnetdebug; then set -x fi -# read static conf settings -for conf in conf/conf.d/*; do - [ -f ${conf} ] && . ${conf} -done - # Huh? Empty $1? [ -z "$1" ] && exit 1 @@ -31,6 +32,38 @@ done netif="$1" root="$2" +# read static conf settings +for conf in conf/conf.d/*; do + [ -f ${conf} ] && . ${conf} +done + +modprobe iscsi_tcp +modprobe crc32c + +if getarg iscsi_firmware ; then + iscsistart -b + exit 0 +fi + +# override conf settings by command line options +arg=$(getarg iscsi_initiator) +[ -n "$arg" ] && iscsi_initiator=$arg +arg=$(getarg iscsi_target_name) +[ -n "$arg" ] && iscsi_target_name=$arg +arg=$(getarg iscsi_target_ip) +[ -n "$arg" ] && iscsi_target_ip=$arg +arg=$(getarg iscsi_target_port) +[ -n "$arg" ] && iscsi_target_port=$arg +arg=$(getarg iscsi_target_group) +[ -n "$arg" ] && iscsi_target_group=$arg +arg=$(getarg iscsi_username) +[ -n "$arg" ] && iscsi_username=$arg +arg=$(getarg iscsi_password) +[ -n "$arg" ] && iscsi_password=$arg +arg=$(getarg iscsi_in_username) +[ -n "$arg" ] && iscsi_in_username=$arg +arg=$(getarg iscsi_in_password) +[ -n "$arg" ] && iscsi_in_password=$arg if [ $root = ${root#iscsi:} ]; then iroot=$(getarg iscsiroot) @@ -38,81 +71,59 @@ else iroot=${root#iscsi:} fi -if getarg iscsi_firmware >/dev/null; then - modprobe iscsi_tcp - modprobe crc32c - iscsistart -b -else - # override conf settings by command line options - arg=$(getarg iscsi_initiator) - [ -n "$arg" ] && iscsi_initiator=$arg - arg=$(getarg iscsi_initiator) - [ -n "$arg" ] && iscsi_target_name=$arg - arg=$(getarg iscsi_target_ip) - [ -n "$arg" ] && iscsi_target_ip=$arg - arg=$(getarg iscsi_target_port) - [ -n "$arg" ] && iscsi_target_port=$arg - arg=$(getarg iscsi_target_group) - [ -n "$arg" ] && iscsi_target_group=$arg - arg=$(getarg iscsi_username) - [ -n "$arg" ] && iscsi_username=$arg - arg=$(getarg iscsi_password) - [ -n "$arg" ] && iscsi_password=$arg - arg=$(getarg iscsi_in_username) - [ -n "$arg" ] && iscsi_in_username=$arg - arg=$(getarg iscsi_in_password) - [ -n "$arg" ] && iscsi_in_password=$arg +# override conf/commandline options by dhcp root_path +# FIXME this assumes that all values have been provided +OLDIFS="$IFS" +IFS=: +set $iroot +iscsi_target_ip=$1; shift +iscsi_protocol=$1; shift # ignored +iscsi_target_port=$1; shift +iscsi_lun=$1; shift +iscsi_target_name=$* +IFS="$OLDIFS" +# XXX is this needed? +getarg ro && iscsirw=ro +getarg rw && iscsirw=rw +fsopts=${fsopts+$fsopts,}${iscsirw} - # override conf/commandline options by dhcp root_path - OLDIFS="$IFS" - IFS=: - set $iroot - iscsi_target_ip=$1; shift - iscsi_protocol=$1; shift # ignored - iscsi_target_port=$1; shift - iscsi_lun=$1; shift - iscsi_target_name=$* - IFS="$OLDIFS" +if [ -z $iscsi_initiator ]; then + # XXX Where are these from? + [ -f /etc/initiatorname.iscsi ] && . /etc/initiatorname.iscsi + [ -f /etc/iscsi/initiatorname.iscsi ] && . /etc/iscsi/initiatorname.iscsi + iscsi_initiator=$InitiatorName - getarg ro && iscsirw=ro - getarg rw && iscsirw=rw - fsopts=${fsopts+$fsopts,}${iscsirw} - - modprobe iscsi_tcp - modprobe crc32c - - if [ -z $iscsi_initiator ]; then - [ -f /etc/initiatorname.iscsi ] && . /etc/initiatorname.iscsi - [ -f /etc/iscsi/initiatorname.iscsi ] && . /etc/iscsi/initiatorname.iscsi - iscsi_initiator=$InitiatorName - fi - - if [ -z $iscsi_target_port ]; then - iscsi_target_port=3260 - fi - - if [ -z $iscsi_target_group ]; then - iscsi_target_group=1 - fi - - if [ -z $iscsi_initiator ]; then - iscsi_initiator=$(iscsi-iname) - fi - - echo "InitiatorName='$iscsi_initiator'" > /dev/.initiatorname.iscsi - - # FIXME $iscsi_lun?? $iscsi_protocol?? - - iscsistart -i $iscsi_initiator -t $iscsi_target_name \ - -g $iscsi_target_group -a $iscsi_target_ip \ - -p $iscsi_target_port \ - ${iscsi_username+-u $iscsi_username} \ - ${iscsi_password+-w $iscsi_password} \ - ${iscsi_in_username+-U $iscsi_in_username} \ - ${iscsi_in_password+-W $iscsi_in_password} - # now we have a root filesystem somewhere in /dev/sda* - # let the normal block handler handle root= + # XXX rfc3720 says 'SCSI Initiator Name: The iSCSI Initiator Name specifies + # the worldwide unique name of the initiator.' Could we use hostname/ip + # if missing? fi +if [ -z $iscsi_target_port ]; then + iscsi_target_port=3260 +fi + +if [ -z $iscsi_target_group ]; then + iscsi_target_group=1 +fi + +if [ -z $iscsi_initiator ]; then + # XXX is this correct? + iscsi_initiator=$(iscsi-iname) +fi + +echo "InitiatorName='$iscsi_initiator'" > /dev/.initiatorname.iscsi + +# FIXME $iscsi_lun?? $iscsi_protocol?? + +iscsistart -i $iscsi_initiator -t $iscsi_target_name \ + -g $iscsi_target_group -a $iscsi_target_ip \ + -p $iscsi_target_port \ + ${iscsi_username+-u $iscsi_username} \ + ${iscsi_password+-w $iscsi_password} \ + ${iscsi_in_username+-U $iscsi_in_username} \ + ${iscsi_in_password+-W $iscsi_in_password} + +# now we have a root filesystem somewhere in /dev/sda* +# let the normal block handler handle root= exit 0 diff --git a/modules.d/95nfs/nfsroot b/modules.d/95nfs/nfsroot index ec094b31..feeea9ef 100755 --- a/modules.d/95nfs/nfsroot +++ b/modules.d/95nfs/nfsroot @@ -6,11 +6,11 @@ PATH=$PATH:/sbin:/usr/sbin # XXX needs error handling like ifup/dhclient-script -getarg rdnetdebug && { +if getarg rdnetdebug ; then exec > /tmp/nfsroot.$1.$$.out exec 2>> /tmp/nfsroot.$1.$$.out set -x -} +fi # Huh? Empty $1? [ -z "$1" ] && exit 1 @@ -45,7 +45,7 @@ if [ "${nfspath#*%s}" != "$nfspath" ]; then nfspath=${nfspath%%%s*}$node${nfspath#*%s} fi -# look through the flags and see if any are overridden by the command line +# Look through the flags and see if any are overridden by the command line # Append a , so we know we terminate flags=${flags}, while [ -n "$flags" ]; do @@ -65,6 +65,7 @@ while [ -n "$flags" ]; do nfsflags=${nfsflags+$nfsflags,}$f done +# Override rw/ro if set on cmdline getarg ro && nfsrw=ro getarg rw && nfsrw=rw nfsflags=${nfsflags+$nfsflags,}${nfsrw} @@ -73,21 +74,16 @@ nfsflags=${nfsflags+$nfsflags,}${nfsrw} incol2 /proc/filesystems nfs || modprobe nfs || exit 1 incol2 /proc/filesystems nfs4 || modprobe nfs || exit 1 -# XXX don't forget to move /var/lib/nfs/rpc_pipefs to new / - # Start rpcbind or rpcbind -# XXX occasionally saw 'rpcbind: fork failed: No such device' -- why? +# FIXME occasionally saw 'rpcbind: fork failed: No such device' -- why? [ -x /sbin/portmap ] && [ -z "$(pidof portmap)" ] && portmap [ -x /sbin/rpcbind ] && [ -z "$(pidof rpcbind)" ] && rpcbind -# XXX should I do rpc.idmapd here, or wait and start in the new root -# XXX waiting assumes root can read everything it needs right up until -# XXX we start it... - -# XXX really, want to retry in a loop I think, but not here... - if [ "$nfsver" = "nfs4" ]; then - [ -n "$(pidof rpc.statd)" ] || rpc.statd + # Start rpc.statd as mount won't let us use locks on a NFSv4 + # filesystem without talking to it. NFSv4 does locks internally, + # rpc.lockd isn't needed + [ -z "$(pidof rpc.statd)" ] && rpc.statd # XXX really needed? Do we need non-root users before we start it in # XXX the real root image? @@ -95,14 +91,15 @@ if [ "$nfsver" = "nfs4" ]; then rpc.idmapd fi - # NFSv4 does locks internally + # XXX Should we loop here? exec mount -t nfs4 -o${nfsflags}${nfslock+,$nfslock} \ $nfsserver:$nfspath $NEWROOT fi # NFSv{2,3} doesn't support using locks as it requires a helper to transfer # the rpcbind state to the new root -# [ -z "$nfslock" -o "$nfslock" = "lock" ] && - echo "Locks unsupported on NFSv{2,3}, using nolock" 1>&2 + echo "Warning: Locks unsupported on NFSv{2,3}, using nolock" 1>&2 + +# XXX Should we loop here? exec mount -t nfs -onolock,$nfsflags $nfsserver:$nfspath $NEWROOT