summaryrefslogblamecommitdiffstats
path: root/src/initramfs/scripts/etc/functions
blob: 417ff03be7194dfcbf9caae429401f343cd50114 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                            
                                         
 

                                                                         
 


                                                                         
                                                                      
 
                                                                       
                                                                            
 
                                                                             
                                                                   
                                                                       
                         


                                                                
 


                                                          
 


                                                                
 
 



                      

                                                                             
                                                            
          
                

                                          
                                                      
                     
                                                    
                                                            

                                                    
                                                            
    
                                              
         
                                                                
                                    






                                                           

  
                                                                             

                                        

                                       

                                                                      
                                                                             

                                                                  
                                                                        
 
                                                                             
                                                                       
                                               

              






              







                                                     






                                                                             
                                                    
                                                                       
                                      
                              




                       
              
                   

                                        


                                                 







                               

                                                              



                         
                                                                             

















                                                                          





                                                                               

                                                                   
                                               

                                                                
                                                                                           


                                                  

           





                                                                             

    

                                                                             
                                                                           
                                                                         







                                                       
                                                                             






                                                                        
                                            









                                                                             
                                                                        


                                                                        

               
               

          
                                                                            
                                


















                                                                       


                                                                    






                                                                     
            


                                                             


          
                             



            
 
                                                                             
                      
          
 
              









                                                                         
                                                                            

                                                                        
                                                              
                                                          

 

                                                                             
                                                                      
               
           
                                                                  
            
                     
                                                             




                                                      
                                                            



          
 



                                                          
             

                        
                             

                      


                                                                
             
    


                                      
                                      
    

                                   
                                                                                

               
                                                              
                                                          
                                    
                 
      
                            
                                                                                
           

 
























                                                                                
                                                       









                                                                                
            
                                                                     
                                                                     
             

                                       
                     
                                                           


                                                                     

                                                                          

                     

                                                                    
     
       
                         








                                                                             

     
                                   
                                                      

       
    
                                                                      

                                                                     

                                                           

                                                                         

                                                                         
                                    

                                                            

                       

                                                                          

           
                                     
                                                                       

        
      
                                                                          
                                                                      
                      
  

                                                                
                                                                             
                                                                         

 

                                                                             

                                                                            
            
                                           
                                         

                                                                          
      

 
                                                                             
                                                                         


                                 
                      



                                                                          

                                                                          
                                              

                       

                                                                        

       
                                                                              
                                                                       







                                                                      

                                                                               




                                                                             
                                
             

                                    

 
                                                                             
                                                                          
              

                                           

 
                                                                             




                                                         

                               
                         
              
                       




                                     
                                                                             
                                                                           

                                                                          


                                              

                                      




                                          
                                       

 


                                                                             
               

                        



                                                                     
                 
                                                          
                                                      
                                    
                                                              
                                                              
                   
                                 
                          

 
                                                                             





                                                                      
                                                                         
                                                                
                                    

                                                                         
                                                                        

                                                                
      






                                                                        
  

 
                                                                             


                                                         
                                       
                                     
                                                   
                                
                                                
                            
      




                                                                             

                                                                             
                 
            




                            
                      


                                   
                  
























                              


                     
                      






                           



























                                                      
                      










                           
                      










                                                      

                      









                           
                      









                        
                      










                        
                      









                           
                      









                                 
                      









                           
                      










                           
                      









                           
                      










                           
                      










                            
                      











                          
                      










                           
                      






                           
                                        

                
                      











                         
                      




















                                
 
 
                                                                             



                                                                                
               

                                             

                                      




















                                                                         
                                              











                                                
                                             
                                                        

                                                                       
                                                                                
                                            

                                                                          


                                                                                     
        
                                       
      
      
 
 

                                                                             

                                                                      
 


                                                                           

 



                                 

                                
 
 


                  
 

                  
 
 

                                             
 
 

                  
 
 

                      
 
 

                                  
 
 

                
 


                    
 
 

                                                    
 
 
                                                  


                     



                                                           



                              
                                                         
                 
 
 



                                   

                       
 
 



                                                       



                                                             
                  
               
 
 



                       
                           


             



                                            
# Copyright (c) 2003..2006 - RZ Uni Freiburg
# Copyright (c) 2006..2013 - OpenSLX GmbH
#
# This program/file is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your feedback to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org
#
# Common functions file for the configuration of linux diskless clients
# (included by init, hwautocfg, servconfig, ... within OpenSLX initialramfs)

#############################################################################
# set of empty dummy functions (loaded before real functions in the
# distro specific files - to have no undeclared function in init, serv-
# config, hwautocfg, ...)

# distro specific funtion called at the beginning of init script
preinit () {
:
}
# distro specific funtion called at the end of init script
postinit () {
:
}
# distro specific general function called from servconfig script
config_distro () {
:
}
# setup netconsole
config_netconsole () {
:
}

#############################################################################
# produce error message and if $2 is empty run (debug) shell
error () {
local e_msg="$1"
# set LOGFILE if not defined
[ -z "${LOGFILE}" ] && LOGFILE="/dev/null"
# if nonfatal error else fatal error message and shell
if [ -n "$2" ] ; then
  [ "$DEBUGLEVEL" -ge 1 -a "$DEBUGLEVEL" != 8 ] && \
    echo -e "${error_msg}${e_msg}${error_nfe}" >> ${LOGFILE}
  [ "$DEBUGLEVEL" -gt 1 -a "$DEBUGLEVEL" != 8 ] && \
    echo -e "${error_msg}${e_msg}${error_nfe}"
  [ "$DEBUGLEVEL" -gt 2 -a "$DEBUGLEVEL" != 8 ] && usleep 10
else
  echo -e "${error_msg}${e_msg}${error_shell}"
  /bin/sh
  echo -n "Reboot now? [y] (nothing happens here at the moment)"
  exec < /dev/console > /dev/console
  #input="y"
  #read input
  #[ -z "$input" -o "$input" = "y" -o "$input" = "Y" ] && {
  #  sleep 4
  #  [ -f /proc/sysrq-trigger ] || mount -t proc none /proc
  #  echo "b" > /proc/sysrq-trigger
  #}
fi
}
#############################################################################
# system uptime for performance analysis
sysup () {
uptime=$(sed -e "s, .*,," /proc/uptime)
echo "${uptime} s."
# if start times are kept, a summary of runtimes would be possible too
}
#############################################################################
# (re)generate dynamic linked libraries cache from /etc/ld.so.conf
ldcfg () {
[ -n "${ldsc}" ] && echo -e "$init_ldcfg" && ldconfig /tmp/ld.so.cache &
}
#############################################################################
# URI interpreter $1 variable to interprete, $2 type of token to return
# prot: tftp, http, nbd, ... server: IP or FQDN
uri_token () {
local var=$1
local prot
local rest
local server
local path
local port
local tmpval
local query=""
# first check if URI starts with prot:// - URI scheme
if strinstr "://" "$1" ; then
  prot=${var%://*}
  rest=${var#*://}
else
  rest="$var"
  prot=""
fi
# remove temporary '::' from path, since it conflicts with port specification
# ticket #232
if strinstr "::" "$rest" ; then
  rest=$(echo $rest | sed -e 's,::,§§§§§,g')
elif strinstr "\:\:" "$rest" ; then
  rest=$(echo $rest | sed -e 's,\:\:,§§§§§,g')
fi
# get the server IP or name and port - URI authority
# you get the path or other token without leading "/" - add it later if
# needed (fixme: port might be broken)
if strinstr ":" "$rest" ; then
  server="${rest%:*}"
  rest="${rest#*:}"
  path="${rest#*/}"
  port="${rest%/$path}"
else
  tmpval=$rest
  rest="${rest#*/}"
  server="$(echo $tmpval|sed 's,/.*,,')"
  path="${tmpval#${server}}"
  port=""
fi
# get path and query components - URI path, query
if strinstr "\?" "$rest" ; then
  path="${path%\?*}"
  query="${rest#*\?}"
fi
# return the requested token
case "$2" in
  prot) echo "$prot" ;;
  server) echo "$server" ;;
  # add '::' again to path, remove duplicate "//"
  path) echo "$path" | sed -e 's,§§§§§,\:\:,g;s,//,/,' ;;
  port) echo "$port" ;;
  query) echo "$query" ;;
esac
}
#############################################################################
# mounter for different file sources
fsmount () {
local proto=$1
local server=$2
local srcpath=$3
local target=$4
local mntopt=$5
local transport
testmkd ${target}
case "${proto}" in
  *nbd)
    # TODO: to be filled in ...
  ;;
  lbdev)
    # we expect the stuff on toplevel directory, filesystem type should be
    # autodetected here ... (vmimgserv is blockdev here)
    vmbdev=/dev/${server}
    waitfor ${vmbdev} 20000
    mount -o ro ${vmbdev} ${target} || error "$scfg_evmlm" nonfatal
  ;;
  nfs)
    # we expect nfs mounts here ... check that nfs module is loaded before
    cat /proc/filesystems | grep -q -E "[[:space:]]nfs" || \
      { modprobe ${MODPRV} nfs 2>/dev/null || error "$error_nfsmod" nonfatal; }
    # nfs4 seems not supported yet by busybox mount (to be checked)
    #for nfsproto in nfs4 nfs; do
      for transport in tcp tcp tcp udp fail; do
        [ $transport = "fail" ] && { error "$init_nfs" nonfatal;
           noimg=yes; break; }
         mount -n -t nfs -o ${mntopt},nolock,rsize=262144,wsize=262144,proto=${transport} \
           ${server}:${srcpath} ${target} && break
      done
    #done
  ;;
  smb|cifs)
    # cifs mounts might be problematic if accessed with superuser id
    mount -n -o ${mntopt},user=guest,guest,file_mode=0755,dir_mode=0755 \
      -t ${proto} //${server}${srcpath} ${target}
    #d_mkrlscript entry boot.slx "/opt/openslx/uclib-rootfs/bin/mount -n \
    #-o ${mntopt},guest,user=guest,file_mode=0755,dir_mode=0755 -t ${proto} \
    #//${server}${srcpath} ${target#/mnt}"
  ;;
esac
}
#############################################################################
# replace @@@serverip@@@ with real serverip. Allows the usage of a variable
# for the server ip address (e.g. in machine-setup or kernel commandline)
checkip ()
{
if [ "@@@serverip@@@" = "$1" -a -n "$serverip" ] ; then
  echo $serverip
else
  echo $1
fi
}
#############################################################################
# replacement for which command to find executables inside stage4 rootfs
binfinder()
{
local program="$1"
local s4path
local notfound=1
for s4path in bin sbin usr/bin usr/sbin usr/local/bin \
              usr/local/sbin usr/bin/X11; do
  if [ -f "/mnt/$s4path/$program" ] && \
     [ -x "/mnt/$s4path/$program" ]; then
    printf '%s\n' "/$s4path/$program"
    notfound=0
    break
  fi
done
return $notfound
}
#############################################################################
# disk formatter and mounter. some proper error message output should be
# added. It uses programs invoked from stage 4 root filesystem. First
# argument is blockdev or file to be formatted, second mountpoint if not
# /mnt/tmp (default)
diskfm () {
local target=$1
local mntpnt=$2
local fs
local path
[ -f /lib/ld-linux.so.2 ] || ln -s /mnt/lib/ld-linux.so.2 /lib/ld-linux.so.2
for fs in xfs reiserfs ext2 ; do
  if strinfile "$fs" /proc/filesystems || modprobe ${MODPRV} $fs ; then
    unset $found
    for path in /sbin /bin /usr/sbin /usr/bin ; do
      if test -x /mnt/$path/mkfs.$fs ; then
        found=yes
        case mkfs.$fs in
          mkfs.xfs)
            fopt="-f"
            mopt="-o noexec"
          ;;
          mkfs.ext2)
            fopt="-Fq"
            mopt="-o nocheck,noexec"
          ;;
          mkfs.reiserfs)
            fopt="-f"
            mopt="-o noexec"
          ;;
        esac
        mount --bind /dev /mnt/dev
        chroot /mnt mkfs.$fs $fopt $target >/dev/null 2>&1 #|| error
        umount /mnt/dev
        if [ -z $mntpnt ] ; then
          umount /mnt/tmp 2>/dev/null
          if mount -t $fs -n $mopt $target /mnt/tmp 2>/dev/null; then
            return 0
          else
            mount -n -t tmpfs none /mnt/tmp
          fi
        else
          testmkd $mntpnt
          mount -t $fs -n -o loop $target $mntpnt 2>/dev/null
          return 0
        fi
      fi
    done
    [ -z $found ] && continue
  else break
  fi
done
}

#############################################################################
# dhcp client function
rundhcp ()
{
local vci="$1"
# ensure the interface is up - might produce conflicts - nbd could fail!!
# fixme: really needed?
#  [ -n "$noipyet" ] && ip link set dev $nwif up
[ -f /lib/modules/${KERNEL}/kernel/net/packet/af_packet.ko ] && \
  modprobe ${MODPRV} af_packet   
echo "Starting udhcpc for IP configuration"
mkdir /var/lib/dhcp >/dev/null 2>&1
[ -n $vci ] && vci="-V $vci"
testmkd /usr/share/udhcpc
ln -sf /bin/dhcpmkconfig /usr/share/udhcpc/default.script
# see for options.c file in udhcp subdir of busybox for accepted "-O option"
# TODO: look into udhcpc code to find out how "search" OPT is called now
#udhcpc -O search -O nissrv -O nisdomain -t 8 -q $vci \
udhcpc -O domain -O nissrv -O nisdomain -O wpad -t 8 -q $vci \
  -s /usr/share/udhcpc/default.script -i $nwif 2>/dev/null
}

#############################################################################
# function for retrieving configuration file (machine-setup) via tftp from a
# predefined server or given source (file=tftp-server:/path via kernel
# command line)
unpack () {
# $1 is  config file name to get, $2 IP of server to get file from
local dst=$1
if [ -s $dst ] ; then
  # fixme: handle different types of packaging (gzip/bzip2)??
  if ! tar -xpzf $dst 2> /tmp/ConfTGZ-tar-error ; then
     cat /tmp/ConfTGZ-tar-error
     error "$unpack_ConfTGZ" nonfatal
     rm /tmp/ConfTGZ-tar-error
  fi
  [ "$DEBUGLEVEL" -le 2 -o "$DEBUGLEVEL" -eq 8  ] && rm $dst
  return 0
else
  return 1
fi
}

# tftp wrapper
# usage tftp_get <path> <tftpdserver> [count]
# count is optional - default is 3 - use -1 for indefinit 
tftp_get () {
  local file="$1"
  local file_server="$2"
  local download_successful=0
  local countdown="$3"

  if [ -z "$1" -o -z "$2" ]; then
    [ $DEBUGLEVEL -ge 1 ] && \
      echo "[tftp_get] Usage: tftp_get <path> <server> [count]" 
    return 1;
  fi

  [ "$countdown" = "" ] && countdown=3

  until [ $download_successful -eq 1 ]
  do
    if [ "$countdown" = "0" ]; then
      [ $DEBUGLEVEL -ge 1 ] && \
        echo "[tftp_get] download of \"$file\" from \"$file_server\" ... failed"
      return 0;
    fi
    tftp -g -r "/$file" -l /tmp/$(basename $file) $file_server
    [ -s /tmp/$(basename $file) ] && download_successful=1
    countdown=$(expr $countdown - 1)
    usleep 200000
  done
  [ $DEBUGLEVEL -ge 1 ] && \
    echo "[tftp_get] download of \"$file\" from \"$file_server\" ... successful"
  return 0;
}


# wget wrapper
# usage wget_get <path> <ftp/httpserver> [count]
# count is optional - default is 3 - use -1 for indefinit 
wget_get () {
  local file="$1"
  local file_server="$2"
  local download_successful=0
  local countdown="$3"

  if [ -z "$1" -o -z "$2" ]; then
    [ $DEBUGLEVEL -ge 1 ] && \
      echo "[wget_get] Usage: wget_get <path> <server> [count]" 
    return 1;
  fi

  [ "$countdown" = "" ] && countdown=3

  until [ $download_successful -eq 1 ]
  do
    if [ "$countdown" = "0" ]; then
      [ $DEBUGLEVEL -ge 1 ] && \
        echo "[wget_get] download of \"$file\" from \"$file_server\" ... failed"
      return 0;
    fi
    wget -q $file_server$file -O /tmp/$(basename $file)
    [ -s /tmp/$(basename $file) ] && download_successful=1
    countdown=$(expr $countdown - 1)
    usleep 200000
  done
  [ $DEBUGLEVEL -ge 1 ] && \
    echo "[wget_get] download of \"$file\" from \"$file_server\" ... successful"
  return 0;
}


fileget () {
# normally tftp would be used, alternatively use wget for ftp or http
# if local device file is specified - mount and unmount after copying
local cfgfile
[ "x$fileprot" = "x" ] && fileprot=tftp
if [ "x$filepath" != "x" ] ; then
  cfgfile=${filepath}
  [ "x$fileserv" = "x" ] && fileserv=$(checkip ${serverip})
  # wait for dns if "fileserv" is a name and not lbd device
  [ "$fileprot" != "lbd" ] && \
    echo ${fileserv} | grep -qi [a-z] && waitfor /tmp/dhcp-done 10000
  [ $DEBUGLEVEL -ge 1 ] && echo "fileget -  fileprot:$fileprot, filepath:\
$filepath, fileserv:$fileserv" >>$LOGFILE
  case "$fileprot" in
   ftp|http)
     wget_get $cfgfile $fileprot://$fileserv \
      && { unpack /tmp/$(basename $cfgfile) && break; } 2>>$LOGFILE 
   ;;
   lbd)
     local ldev=$fileserv
     echo "Waiting for configuration file ${cfgfile} ...."
     [ $DEBUGLEVEL -ge 1 ] && echo "fileget -  fileprot:$fileprot, filepath:\
$filepath, fileserv:$fileserv" >>$LOGFILE
     waitfor /mnt/${cfgfile} 10000
     if [ -f /mnt/${cfgfile} ]; then
       unpack /mnt/$cfgfile
     else
       error "$init_errlfg"
     fi
   ;;
   *)
     tftp_get $cfgfile $fileserv  \
       && unpack /tmp/$(basename $cfgfile) 2>>$LOGFILE
   ;;
   esac
else
  # predefined value for OpenSLX environment; it is expected that this
  # directory is just below the tftpboot (path to which the daemon is
  # restricted to)
  filepath="client-config"
  [ "x$fileserv" = "x" ] && fileserv=$(checkip ${serverip})
  [ $DEBUGLEVEL -ge 1 ] && echo "fileget - fileprot:$fileprot, filepath:\
$filepath, fileserv:$fileserv" >>$LOGFILE
  # try to get configuration files successively; start with distro client
  # and try last distro default ...
  mac=$(echo $macaddr|sed "s/:/-/g")
  for cfgfile in ${filepath}/${SYSTEM_NAME}/01-$mac.tgz \
                 ${filepath}/${SYSTEM_NAME}/default.tgz ; do
    case "$fileprot" in
      ftp|http)
        wget $fileprot://$fileserv/$cfgfile -O /tmp/$(basename $cfgfile) \
          2>>$LOGFILE && { unpack /tmp/$(basename $cfgfile) && break; }
      ;;
      tftp)
        tftp_get $cfgfile $fileserv \
          2>>$LOGFILE && { unpack /tmp/$(basename $cfgfile) && break; }
      ;;
    esac
  done
  echo -e "\n## Configuration via fileget from ${fileprot}://${fileserv}/\
${cfgfile}\n# Hierarchy is distro client and as last distro/default" \
    >>/tmp/confviafile
fi
cat /initramfs/machine-setup >>/tmp/confviafile 2>/dev/null || \
  error "$nomachsetup"
echo "fileget via $fileprot from $fileserv/$cfgfile finished" >/tmp/file-done
[ $DEBUGLEVEL -ge 1 ] && echo "fileget from $cfgfile finished" >>$LOGFILE
}

#############################################################################
# function for creating directories after testing of their existance avoids
# to recreate directories in union mounts. If second and/or third parameters
# are given check for permission patterns
testmkd () {
  test -d $1 || mkdir -p $1 >/dev/null 2>&1
  [ $# -ge 2 ] && for i in "$2" "$3" ; do
    echo "$i" | grep -q -E "^[0-9]{3,4}$" && chmod "$i" $1 >/dev/null 2>&1
    echo "$i" | grep -q -E ":." && chown "$i" $1 >/dev/null 2>&1
  done
}

#############################################################################
# function for writing a syslog-ng.conf file. First parameter is absolute
# destination (incl. /mnt prefix)
# sysngwriter $dest $start_syslog
sysngwriter () {
local mysyslogngcfg=$1
# logging servers might be specified in $log_servers (from e.g. dhcp)
# fixme!? only first logserver from dhcp variable is used
[ -n "$log_servers" ] && start_syslog="syslog://${log_servers% *}:514/udp"
echo -e "# File written by $0 within InitRamFS\n\
source src {\n\tfile(\"/proc/kmsg\");\n\
\tunix-dgram(\"/dev/log\");\n\tinternal();\n};\ndestination console_all {\
\n\tfile(\"/dev/tty10\");\n};" >$mysyslogngcfg
case "$start_syslog" in
  yes|Yes|YES)
    echo -e "\nlog {\n\tsource(src);\n\tdestination(console_all);\n};" \
      >>$mysyslogngcfg
  ;;
  file)
    echo -e "destination allmessages {\n\tfile(\"/var/log/allmessages\");\n};\
\nlog {\n\tsource(src); destination(allmessages);\n};" >>$mysyslogngcfg
  ;;
  *)
    syslogsrv=$(uri_token $start_syslog server)
    syslogport=$(uri_token $start_syslog port)
    syslogprot=$(uri_token $start_syslog path)
    [ -z ${syslogport} ] && syslogport=514
    [ -z ${syslogprot} ] && syslogprot=udp
    echo -e "destination loghost {\n\t${syslogprot}(\"${syslogsrv}\" \
port(${syslogport}));\n};\nlog {\n\tsource(src);\n\tdestination(loghost);\n};"\
      >>$mysyslogngcfg
  ;;
esac
}

#############################################################################
# simple string in string search
strinstr () {
  case "$2" in *$1*) return 0;; esac
  return 1
}

#############################################################################
# simple string in file search, for the future grep should be used instead
strinfile () {
  case "$(cat $2)" in *$1*) return 0;; esac
  return 1
}

#############################################################################
# wait for a file to appear and stop after maxwait counts
waitfor () {
local file=$1
local maxwait=$2
local count=0
[ -z $file ] && return 0
[ -z $maxwait ] && maxwait=5000
while [ ! -e $file ] ; do
  usleep 1000 
  count=$(($count + 1))
  [ $count -gt $maxwait ] && return 1
done
return 0
}

#############################################################################
# ldconfig is needed if rootfilesystem is composed of several parts. Search
# for ldconfig and execute it (we expect ldconfig not to be a dynamically 
# linked tool)
# check that /mnt/etc/ld.so.conf is never lost
ldconfig () {
local cachefile="$1"
for ldcfg in /mnt/sbin/ldconfig.real \
             /mnt/sbin/ldconfig \
             /mnt/bin/ldconfig \
             /mnt/usr/sbin/ldconfig; do
  test -x $ldcfg && {
    $ldcfg -r /mnt -C $cachefile; break; }
done
echo "finished at $(sysup)" >/tmp/ldcfg
}

#############################################################################
# base passwd/shadow, the standard user present in every system. All other
# system users should be generated within the service function
basepasswd () {
# hack (see Ticket #206)
root_pw="$1"
# strip every line with userid between 500 and 99999 from the passwd
# file
sed '/^[a-zA-Z0-9]*:[a-zA-Z0-9]*:[1-9][0-9]\{3,4\}:/d;/^+:*/d;/^+$/d;
  /^[a-zA-Z0-9]*:[a-zA-Z0-9]*:[5-9][0-9]\{2\}:/d' /mnt/etc/passwd \
  >/tmp/newpasswd
# and add user nobody again (is there a more elegant way?)
sed -n -e '/nobody/p' /mnt/etc/passwd >>/tmp/newpasswd
# create the shadow from passwd file
echo -e "root:"${root_pw}":12958:0:10000::::" >/mnt/etc/shadow
sed 's/:.*/:!:13078:0:99999:7:::/;/^root.*/d' /tmp/newpasswd \
  >>/mnt/etc/shadow
mv /tmp/newpasswd /mnt/etc/passwd
chmod 0640 /mnt/etc/shadow
}

#############################################################################
# wait for the completion of configuration file (machine-setup). It is
# composed from different sources
cfgcomplete () {
waitfor /tmp/dhcp-done 10000 || error "$error_errdcfg" nonfatal
waitfor /tmp/file-done 10000 || error "$error_errfcfg" nonfatal
waitfor /tmp/ldap-done 10000 || error "$error_errlcfg" nonfatal
# concatenate the different files now into the central config file, order
# matters - ldap (not implemented yet) data has highest priority
if ! test -f /tmp/cfgcomplete ; then
  echo "## Stage3 configuration via initial/early /etc/initramfs-setup" \
    >/etc/ext-initramfs-setup
  for config in /etc/initramfs-setup /tmp/confviadhcp /tmp/confviafile \
  	        /tmp/confvialdap
    do test -f $config && cat $config >>/etc/ext-initramfs-setup
  done
# check again and replace @@@serverip@@@ and produce a final version for
# stage4 (no much use, just debugging)
sed "s,@@@serverip@@@,$serverip," -i /etc/ext-initramfs-setup
. /etc/ext-initramfs-setup
cp /etc/ext-initramfs-setup /mnt/etc/machine-setup
mv /etc/ext-initramfs-setup /etc/initramfs-setup
echo "config completed" >/tmp/cfgcomplete
fi
}

#############################################################################
# execute all shell scripts in the given init-hook folder
runinithook () {
local hook=$1
echo "[openslx] run: $hook" > /dev/kmsg
if [ -d /etc/init-hooks/$hook ]; then
  for hook_script in /etc/init-hooks/$hook/*.sh; do
    if [ -e $hook_script ]; then
      /bin/init-wrapper $hook_script $DEBUGLEVEL
      . /tmp/env/wrapper.env
    fi
  done
fi
}

#############################################################################
# localization simply derived from $language variable set in machine-setup or
# other sources - mostly taken from knoppix
localization () {
country="$1"
CONSOLE_FONT="lat9w-16.psfu"
case "$country" in
  # German version
  de*)
    COUNTRY="de"
    LANG="de_DE.UTF-8"
    KEYTABLE="de-latin1-nodeadkeys"
    XKEYBOARD="de"
    KDEKEYBOARD="de"
    CHARSET="utf8"
    KDEKEYBOARDS="us,fr"
    TZ="Europe/Berlin"
  ;;
  # Belgian version
  be*)
    COUNTRY="be"
    LANG="C"
    KEYTABLE="be2-latin1"
    XKEYBOARD="be"
    KDEKEYBOARD="be"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="us,de,fr"
    TZ="Europe/Brussels"
  ;;
  # Bulgarian version
  bg*)
    COUNTRY="bg"
    LANG="bg_BG"
    KEYTABLE="bg"
    XKEYBOARD="bg"
    KDEKEYBOARD="bg"
    CHARSET="microsoft-cp1251"
    KDEKEYBOARDS="us,de,fr"
    TZ="Europe/Sofia"
  ;;
  # Brasilian version
  br*)
    COUNTRY="br"
    LANG="pt_BR.UTF-8"
    KEYTABLE="br"
    XKEYBOARD="br"
    KDEKEYBOARD="br"
    CHARSET="utf8"
    KDEKEYBOARDS="us,es,pt"
    TZ="America/Sao_Paulo"
  ;;
  # Switzerland (basically de with some modifications)
  ch)
    LANGUAGE="de"
    COUNTRY="ch"
    LANG="de_CH"
    KEYTABLE="sg-latin1"
    XKEYBOARD="de_CH"
    KDEKEYBOARD="de_CH"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="de,us,fr"
    TZ="Europe/Zurich"
  ;;
  # Simplified Chinese
  cn)
    COUNTRY="cn"
    LANG="zh_CN.GB2312"
    KEYTABLE="us"
    XKEYBOARD="us"
    KDEKEYBOARD="us"
    CHARSET="gb2312.1980-0"
    KDEKEYBOARDS="us,de,fr"
    XMODIFIERS="@im=Chinput"
    TZ="Asia/Shanghai"
  ;;
  # Czechoslovakia
  cs|cz)
    LANGUAGE="cs"
    COUNTRY="cs"
    LANG="cs_CZ.UTF-8"
    KEYTABLE="cz-lat2"
    XKEYBOARD="cs"
    KDEKEYBOARD="cz"
    CHARSET="iso8859-2"
    KDEKEYBOARDS="us,de,fr"
    TZ="Europe/Prague"
    CONSOLE_FONT="iso02g"
  ;;
  # Denmark
  dk|da)
    COUNTRY="dk"
    LANG="da_DK.UTF-8"
    # Workaround: "dk" broken in gettext, use da:da_DK
    LANGUAGE="da:da_DK"
    KEYTABLE="dk"
    XKEYBOARD="dk"
    KDEKEYBOARD="dk"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="dk,de,us,fr"
    TZ="Europe/Copenhagen"
  ;;
  es)
  # Spain
    COUNTRY="es.UTF-8"
    LANG="es_ES"
    KEYTABLE="es"
    XKEYBOARD="es"
    KDEKEYBOARD="es"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="de,us,fr"
    TZ="Europe/Madrid"
  ;;
  # Finland
  fi)
    COUNTRY="fi"
    LANG="fi_FI.UTF-8"
    KEYTABLE="fi"
    XKEYBOARD="fi"
    KDEKEYBOARD="fi"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="us"
    TZ="Europe/Helsinki"
  ;;
  # France
  fr*)
    COUNTRY="fr"
    LANG="fr_FR.UTF-8"
    KEYTABLE="fr"
    XKEYBOARD="fr"
    KDEKEYBOARD="fr"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="de,us"
    TZ="Europe/Paris"
  ;;
  he|il)
  # Hebrew version
    LANGUAGE="he"
    COUNTRY="il"
    LANG="he_IL.UTF-8"
    KEYTABLE="us"
    XKEYBOARD="us"
    KDEKEYBOARD="il"
    CHARSET="iso8859-8"
    KDEKEYBOARDS="us,fr,de"
    TZ="Asia/Jerusalem"
  ;;
  # Ireland
  ie)
    COUNTRY="ie"
    LANG="en_IE.UTF-8"
    KEYTABLE="uk"
    XKEYBOARD="uk"
    KDEKEYBOARD="gb"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="us,de,es,fr,it"
    TZ="Europe/Dublin"
  ;;
  # Italy
  it)
    COUNTRY="it"
    LANG="it_IT.UTF-8"
    KEYTABLE="it"
    XKEYBOARD="it"
    KDEKEYBOARD="it"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="fr,us,de"
    TZ="Europe/Rome"
  ;;
  # Japan
  ja)
    COUNTRY="jp"
    LANG="ja_JP.UTF-8"
    LANGUAGE="ja"
    KEYTABLE="us"
    XKEYBOARD="us"
    KDEKEYBOARD="us"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="fr,us,de"
    TZ="Asia/Tokyo"
  ;;
  # The Netherlands
  nl)
    COUNTRY="nl"
    LANG="nl_NL.UTF-8"
    KEYTABLE="us"
    XKEYBOARD="us"
    KDEKEYBOARD="en_US"
    CHARSET="iso8859-15"
    KDEKEYBOARDS="nl,de,fr"
    TZ="Europe/Amsterdam"
  ;;
  # Poland
  pl)
    COUNTRY="pl"
    LANG="pl_PL.UTF-8"
    KEYTABLE="pl"
    XKEYBOARD="pl"
    KDEKEYBOARD="pl"
    CHARSET="iso8859-2"
    KDEKEYBOARDS="de,us,fr"
    TZ="Europe/Warsaw"
    CONSOLE_FONT="iso02g"
  ;;
  # Russia
  ru)
    COUNTRY="ru"
    LANG="ru_RU.UTF-8"
    KEYTABLE="ru"
    XKEYBOARD="ru"
    KDEKEYBOARD="ru"
    CHARSET="koi8-r"
    CONSOLE_FONT="Cyr_a8x16"
    KDEKEYBOARDS="de,us,fr"
    TZ="Europe/Moscow"
  ;;
  # Slovakia
  sk)
    COUNTRY="sk"
    LANG="sk_SK.UTF-8"
    KEYTABLE="sk-qwerty"
    XKEYBOARD="sk"
    KDEKEYBOARD="sk"
    CHARSET="iso8859-2"
    KDEKEYBOARDS="us,de"
    TZ="Europe/Bratislava"
    CONSOLE_FONT="iso02g"
  ;;
  # Slovenia
  sl)
    LANGUAGE="sl"
    COUNTRY="si"
    LANG="sl_SI.UTF-8"
    KEYTABLE="slovene"
    XKEYBOARD="sl"
    KDEKEYBOARD="si"
    CHARSET="iso8859-2"
    KDEKEYBOARDS="us,de"
    TZ="Europe/Ljubljana"
    CONSOLE_FONT="iso02g"
  ;;
  tr)
# Turkish version (guessed)
    COUNTRY="tr"
    LANG="tr_TR.UTF-8"
    KEYTABLE="tr_q-latin5"
    XKEYBOARD="tr"
    KDEKEYBOARD="tr"
    CHARSET="iso8859-9"
    KDEKEYBOARDS="us,de,fr"
    TZ="Europe/Istanbul"
  ;;
  # Taiwan - Traditional Chinese version
  tw)
    COUNTRY="tw"
    LANG="zh_TW.UTF-8"
    LANGUAGE="zh_TW.Big5"
    KEYTABLE="us"
    XKEYBOARD="us"
    KDEKEYBOARD="us"
    CHARSET="iso8859-1"
    KDEKEYBOARDS="us"
    XMODIFIERS="@im=xcin"
    TZ="Asia/Taipei"
  ;;
  # Great Britian
  uk)
    COUNTRY="uk"
    LANG="en_GB.UTF-8"
    LANGUAGE="en"
    KEYTABLE="uk"
    XKEYBOARD="uk"
    KDEKEYBOARD="gb"
    CHARSET="iso8859-1"
    KDEKEYBOARDS="us"
    TZ="Europe/London"
  ;;
  # US and default configuration
  *)
    LANGUAGE="us"
    COUNTRY="us"
    LANG="C"
    KEYTABLE="us"
    XKEYBOARD="us"
    KDEKEYBOARD="us"
    CHARSET="iso8859-1"
    KDEKEYBOARDS="de,fr"
    TZ="America/New_York"
  ;;
esac
}

#############################################################################
# This function gets an uri or a comma separated list of uris as parameter.
# It will then try to mount these uris and add them to the union at /
include_in_fsroot_union () {
  union_id=0 # used to have guarantee differing names for the unions mount point
  union_type=$1
  for ROOTFS in $(echo $2 |sed 's/,/ /g'); do
    union_id=$(($union_id + 1))
    srvproto=$(uri_token $ROOTFS prot)
    case $srvproto in
      nfs)
        # activate kernel nfs if not present, nfsroot consists now of two
        # different parts
        root_path=$(uri_token $ROOTFS path)
        nfsserver=$(uri_token $ROOTFS server)
        mkdir -p /mnt/tmp/${root_path}_${union_id}
        for proto in tcp udp fail; do
          [ $proto = "fail" ] && { error "$scfg_nfs"; break; }
          mount -n -t nfs -o ro,nolock,$proto $nfsserver:$root_path \
            /mnt/tmp/${root_path}_${union_id} && break
        done
      ;;
      *nbd)
	echo "Not working yet"
        nbdmod=$srvproto
        # get settings for nbd-client, filesystem equals to path in URI
        # notation
        nbdhost=$(uri_token $ROOTFS server)
        nbdport=$(uri_token $ROOTFS port)
        nbdrfst=$(uri_token $ROOTFS path)
        echo -e "(D)NBD $ROOTFS parsed to $srvproto - $nbdhost - \
          $nbdport - $nbdrfst" >> /tmp/testlog
        #FIXME: do mount
      ;;
      aoe)
        echo "Not implemented yet"
      ;;
      iscsi)
        echo "Not implemented yet"
        #iscsiserver=$(uri_token $ROOTFS server)
        #iscsiport=$(uri_token $ROOTFS port)
        #iscsitarget=$(uri_token $ROOTFS path)
      ;;
    esac
    if [ "X$union_type" == "XUnionFS" ]; then
      [ "$DEBUGLEVEL" -gt 2 -a "$DEBUGLEVEL" != 8 ] && \
        echo "Using unionctl to mount ${root_path}_${union_id} (type: \
          $union_type)"
      unionctl /mnt/ --add --after 1 --mode ro /mnt/tmp/${root_path}_${union_id}
    elif [ "X$union_type" == "XAUFS" ]; then
      # unionctl for aufs is a sh script needing tools not included in our
      # initramfs
      [ "$DEBUGLEVEL" -gt 2 -a "$DEBUGLEVEL" != 8 ] && \
        echo "Using aufs-mount to mount ${root_path}_${union_id} (type: $union_type)"
      mount -n -o remount,add:1:/mnt/tmp/${root_path}_${union_id}=ro none /mnt
    else
      error ${init_loadunions} nonfatal
    fi
  done
}

#############################################################################
# dummy functions - avoid undefined functions in servconfig (functions are
# normally overwritten by settings within distro-functions) - a file 
# generated by mkdxsinitrd from <distro>/functions-default & ~-version

# setup initial boot scripts (for most standard distributions, gentoo is to
# be handled differently)
initial_boot () {
:
}
# function for udev configuration
config_udev () {
:
}
# function for ntp configuration
config_ntp () {
:
}
# function for afs
config_afs () {
:
}
# function for atd
config_atd () {
:
}
# function for configuration of cron services
config_cron () {
:
}
# syslog service
config_syslog () {
:
}
# secure shell service
config_sshd () {
:
}
# snmp agent for remote monitoring
config_snmp () {
:
}
# consolefont
consolefont () {
:
}
# acpi and powersave
config_acpi () {
:
}
# configure hal, dbus, resmgr and services like that
config_dreshal () {
:
}
# configure automounter, simple NFSv3 imports only
config_automount () {
:
}
# configure print services / start requested printer daemon
config_printer () {
:
}
# configure bluetooth services
config_bt () {
:
}
# create a compliant runlevel script, needed for boot.slx
d_mkrlscript () {
:
}
# start name service caching daemon
config_nscd () {
:
}
# configure automounter
config_automount () {
:
}
# configure samba service (not winbind, but nmbd, smbd)
config_samba () {
:
}
# start portmapper (needed at least for nfs and nis services)
config_portmap () {
:
}
# start nis/ypbind
config_nis () {
:
}
# configure nfsv4 stuff
config_nfsv4 () {
:
}
# configure keyboard layout
keytable () {
:
}
# set up localization like keytable, console
dlocale () {
:
}