summaryrefslogtreecommitdiffstats
path: root/initramfs/mkdxsinitrd
blob: 64c244e58ba4126b0a0ce2a0d819add53d44d02c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#!/bin/bash
# This program 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
#
# Main script for generating the InitRamFS of OpenSLX clients, to constitute
# the Stage3 boot stadium

# debug level of script itself
DEBUG=1
# debug level of the initramfs (to trigger the addition of specific
# binaries and kernel modules)
IRFSDEBUG=1
COMETCEXCL="XF86Config*\nissue*\nmtab*\nfstab*\n"
COMDIRINDXS="/tmp/scratch /var/lib/nobody"

# distro specific settings read from configuration file
# D_SYSCONFDIR - system wide configuration settings in /etc - sysconfig in
# SuSE and RedHat, default in Debian and Ubuntu, conf.d in Gentoo
# D_ETCEXCL - list of files, wildcards to be excluded from /etc when using
# bind mounts
# D_DIRINBINDMNT - lists of directories to be created in bind mounted rw
# part of the client filesystem
# D_RODIRSINRW - ReadOnly Directories in RW part of filesystem to save on
# TempFS usage
# D_DIRINDXS - directories in client filesystem which should be present
# anyhow
# D_BINDMNT - any code which is specific in bind mount environment
# D_RCDIRS - runlevel directories relative to /etc
# D_HWMODTOIGNORE -

# just ensure that variables are set and the user gets an idea if
# something fails
D_SYSCONFDIR="<set in ~/distro-config/distro file>"
D_ETCEXCL="<set in ~/distro-config/distro file>"
D_BINDMPTS="<set in ~/distro-config/distro file>"
D_DIRINBINDMNT="<set in ~/distro-config/distro file>"
D_RODIRSINRW="<set in ~/distro-config/distro file>"
D_DIRINDXS="<set in ~/distro-config/distro file>"
D_BINDMNT="<set in ~/distro-config/distro file>"
D_INITDIR="<set in ~/distro-config/distro file>"
D_INITBOOTD="<set in ~/distro-config/distro file>"
D_RCDIRS="<set in ~/distro-config/distro file>"
D_RCDIRSINRW="<set in ~/distro-config/distro file>"
D_INITSCRIPTS="<set in ~/distro-config/distro file>"
D_XF86CONFFILE="<set in ~/distro-config/distro file>"
D_XORGBIN="<set in ~/distro-config/distro file>"
D_XFONTPATH="<set in ~/distro-config/distro file>"
D_DEFAULTCOUNTRY="<set in ~/distro-config/distro file>"
# read in the global settings
SLX_CONFIG_PATH=@@@SLX_CONFIG_PATH@@@
SLX_SHARE_PATH=@@@SLX_BASE_PATH@@@/share
[ -f $SLX_SHARE_PATH/settings.default ] && \
   . $SLX_SHARE_PATH/settings.default
[ -f $SLX_CONFIG_PATH/settings ] && \
   . $SLX_CONFIG_PATH/settings

#########################################################################
# End of global variable declaration, nothing needed to be changed at
# default below that line

testmkd () {
  test -d $1 || mkdir -p $1 >/dev/null 2>&1
}

usage()
{
  echo -e "mkdxsinitrd [-C] [-dh] [-k kernel version] [-i path/file] \
[-f module(s)] [-n module(s)] [-m module(s)] -r path [-s theme]\n\
[-c initramfs-setup] -S system_name"
  echo "OPTIONEN"
  echo "  -C create special initramfs for CD/DVD syslinux demo mode"
  echo "  -c location of initramfs configuration file (initramfs-setup)"
  echo "  -d use ISC dhclient instead of udhcpcd of busybox"
  echo "  -h show usage information"
  echo "  -k kernel version"
  echo "  -i path/file optional information where to write initramfs file"
  echo "  -f list of blockdev/filesystem modules"
  echo "  -n list of network adaptor modules"
  echo "  -m list of misc modules not covered by -f or -n"
  echo "  -p list of active plugins"
  echo "  -r path to stage1 root (vendor OS, e.g. /var/opt/openslx/stage1/suse-..."
  echo "  -S name of the system for which the initramfs is built"
  echo "  -s theme (for splash)"
  echo "  -t tpm support (client security)"
  echo "  -w enable WLAN support. Put WLAN kernel drivers to the network module"
  echo "        list"
  exit 0
}

# check needed things for installation
precheck() {
  # check if running as root
  if [ "`id -u`" != "0" ]; then
    echo -e "\nYou don't have the needed permission. Please rerun as root user!\n"
    exit 1
  fi
  # Check for correct settings of SLX-Variables
  if [ "x" == "x$SLX_BASE_PATH" ]; then
    if [ "x$SLX_CONFIG_PATH" == "x@@@SLX_CONFIG_PATH@@@" ]; then
       echo "Please make sure you install the Open SLX Software properly,"
       echo "i.e. install it using the provided Makefile. Thanks."
    else
       echo "Essential configuration is missing. Please verify"
       echo "existence and correctness of $SLX_SHARE_PATH/settings.default"
    fi
    exit 1
  fi
}

# replaces which command to find executables inside ROOTDIR
binfinder()
{
local PROGRAM="$1"
local RET=1
for ELEMENT in bin sbin usr/bin usr/sbin usr/local/bin \
               usr/local/sbin usr/bin/X11; do
  if [ -f "${ROOTDIR}/$ELEMENT/$PROGRAM" ] && \
     [ -x "${ROOTDIR}/$ELEMENT/$PROGRAM" ]; then
    printf '%s\n' "${ROOTDIR}/$ELEMENT/$PROGRAM"
    RET=0
    break
  fi
done
return $RET
}

# copy some binary to given destination. Takes binary in $1 and
# destination in $2
cobi()
{
local DEST=$2
local FPTB=`binfinder $1`
if [ -z ${FPTB} ] ; then
  return 1;
elif [ -L ${FPTB} ] ; then
  # do not copy the link but the binary the link points to
  local LINKDEST=`ls -la ${FPTB} | sed -e "s,.* ,,"`
  case $LINKDEST in
    /*) FPTB=${ROOTDIR}${LINKDEST}
	;;
    *) ;;
  esac
  cp ${FPTB} ${INSTDIR}/${DEST}
else
  cp ${FPTB} ${INSTDIR}/${DEST}
fi
slxldd --root-path=${ROOTDIR} ${FPTB} >>${INSTDIR}/tmp/libraries
return 0
}

# replace copy
repco()
{
local FILENAME=$1
DATE=`date +%m-%d-%Y`
# do not forget to define all variables which should be replaced within
# the start and configuration scripts. All comment lines will be deleted.
if [ -d ${SLX_SHARE_PATH}/initramfs/${FILENAME} ] ; then
  testmkd ${INSTDIR}/${FILENAME}
else
  if file ${SLX_SHARE_PATH}/initramfs/${FILENAME}| grep "ELF" &>/dev/null ; then
    cp ${SLX_SHARE_PATH}/initramfs/${FILENAME} ${INSTDIR}/${FILENAME}
  elif [ -L ${SLX_SHARE_PATH}/initramfs/${FILENAME} ] ; then
    cp -a ${SLX_SHARE_PATH}/initramfs/${FILENAME} ${INSTDIR}/${FILENAME%/*}
  else
	if [ -z ${SLX_VERSION} ] ; then
		SLX_VERSION=$(slxversion)
	fi
    sed -e "s,@@@INSTDIR@@@,${INSTDIR},g;s,@@@KERNVER@@@,${KERNVER},g" \
        -e "s,@@@DISTRO_NAME@@@,${DISTRO_NAME},g" \
		-e "s,@@@DISTRO_VER@@@,${DISTRO_VER},g" \
		-e "s,@@@NETIF@@@,${NET_IF},g" \
        -e "s,@@@NWMODULES@@@,${NWMODULES},g" \
        -e "s,@@@COMDIRINDXS@@@,${COMDIRINDXS},g" \
        -e "s,@@@COMETCEXCL@@@,${COMETCEXCL},g" \
        -e "s,@@@DATE@@@,${DATE},g;/^#[^!].*/d" \
          ${SLX_SHARE_PATH}/initramfs/${FILENAME} >> ${INSTDIR}/${FILENAME}
     # ugly hack for creating a general slx config file instead of "patching"
     # the stage3 init
     echo -e "slxconf_date=${DATE}\nslxconf_kernver=${KERNVER}\n\
slxconf_listnwmod=\"${NWMODULES}\"\nslxconf_distro_name=${DISTRO_NAME}\n\
slxconf_distro_ver=${DISTRO_VER}\nslxconf_system_name=${SYSTEM_NAME}\n\
slxconf_slxver=\"${SLX_VERSION}\"" \ > ${INSTDIR}/etc/sysconfig/slxsystem.conf
     sed -e "s,^#!/bin/sh,#!/bin/ash," -i ${INSTDIR}/${FILENAME}
  fi
fi
}

copy_distro_stuff() {
  # copy first the default distro dependent configuration (variables) and
  # function files, then append to each the distro/version specific file
  # variables, functions set in default might that way be overwritten by
  # definitions in distro/version specific files
  cat ${SLX_SHARE_PATH}/distro-specs/$1/config-default >${INSTDIR}/etc/sysconfig/config
  cat ${SLX_SHARE_PATH}/distro-specs/$1/config-$2 >>${INSTDIR}/etc/sysconfig/config
  #[ -f $SLX_SHARE_PATH/settings.default ] && \
  #   cat $SLX_SHARE_PATH/settings.default >>${INSTDIR}/etc/sysconfig/config
  #[ -f $SLX_CONFIG_PATH/settings ] && \
  #   cat $SLX_CONFIG_PATH/settings >>${INSTDIR}/etc/sysconfig/config
  cat ${SLX_SHARE_PATH}/distro-specs/$1/functions-default >${INSTDIR}/etc/distro-functions
  cat ${SLX_SHARE_PATH}/distro-specs/$1/functions-$2 >>${INSTDIR}/etc/distro-functions
  # would be cool not to copy .svn here ...
  # fixme!! works only for etc directories. If dir exists then next copy places
  # file into the existing dir
  cp -a distro-specs/$1/files-default ${INSTDIR}/etc/sysconfig/files 2>/dev/null
  # ... so remove them afterwards ...
  find ${INSTDIR}/etc/sysconfig/files \
    -regex ".*/\..*" -exec rm -rf {} 2>/dev/null \;
}
# copy kernel modules even if links (first parameter is source, second
# destination and third source prefix)
modcp () {
if [ -L $1 ] ; then
  cp $3/$(readlink $1) $2
else
  cp $1 $2
fi
}
# This function makes the directory of this script to the present working
# directory
# Does also work when called by symbolic links (even for nested links).
goto_script_dir()
{
  [ $DEBUG -ge 2 ] && echo "Aufgerufen wurde $0, checking if it is a link"
  calleddetails=`ls -l $0` # get the file flags (e.g. lrwxrwxrwx)
  [ $DEBUG -ge 2 ] && echo $calleddetails

  calleddir=${0%/*}  # Strip the filename from path
  [ $DEBUG -ge 2 ] && echo "Wechsele ins Verzeichnis des aufgerufenen scripts/links ($calleddir)"
  cd $calleddir

  while index=`expr index "$calleddetails" "l"`;
        [ $index -eq 1 ]; do

    [ $DEBUG -ge 2 ] && echo "It is a link"
    target=`echo $calleddetails | awk '{print $NF}'`
    [ $DEBUG -ge 2 ] && echo "The target of the link is: $target"
    hasslash=`expr index "$target" "/"`
    if [ $hasslash -ne 0 ]; then
        targetpath=${target%/*} # extract the pathname
        [ $DEBUG -ge 2 ] && echo "Following link to $targetpath"
        cd $targetpath
    else
        [ $DEBUG -ge 2 ] && echo "It is in the same directory as the link"
    fi
    targetfile=`basename $target`
    [ $DEBUG -ge 2 ] && echo "The target file of the link is: $targetfile"
    calleddetails=`ls -l $targetfile` # get the file flags (e.g. lrwxrwxrwx)
    [ $DEBUG -ge 2 ] && echo $calleddetails
  done
  [ $DEBUG -ge 2 ] && pwd

}

# theming
# add a bootsplash image to the initial ramdisk
# add a displaymanager theme to use
add_theme() {

theme="${theme}"
themes_dir="${SLX_SHARE_PATH}/themes/openslx"
if [ -d "${SLX_SHARE_PATH}/themes/${theme}" ]; then
  themes_dir="${SLX_SHARE_PATH}/themes/${theme}"
fi
echo -e "\n\tUsing ${themes_dir} for themes."

echo -e "\tBootsplash theme found:\c"
if [ -d "${SLX_SHARE_PATH}/themes/${theme}/bootsplash" ]; then
  echo -e "\t\tyes"
  cp -a ${SLX_SHARE_PATH}/splashy/* ${INSTDIR}/bin
  mkdir -p ${INSTDIR}/etc/splashy
  cp -a ${themes_dir}/bootsplash/* ${INSTDIR}/etc/splashy
  any_themes=1
else echo -e "\tno"
fi

echo -e "\tDisplaymanager theme found:\c"
if [ -d "${SLX_SHARE_PATH}/themes/${theme}/displaymanager" ]; then
  echo -e "\tyes"
  cp -a ${themes_dir}/displaymanager ${INSTDIR}/usr/share/themes
  any_themes=1
else echo -e "\tno"
fi

if [ -z ${any_themes} ]; then
  echo -e "Neither bootsplash nor displaymanager theme found in"
  echo -e "\t${themes_dir}"
fi

echo
}


#########################################################################
# End of function declaration

while getopts :Cdhk:c:i:r:o:s:f:n:m:p:S:tu:wI:a: a ; do
  case $a in
    \:|\?)
    case $OPTARG in
      c)  echo "-c requires location of initramfs-setup file";;
      k)  echo "-k requires kernel version parameter";;
      i)  echo "-i requires initramfs path parameter";;
      f)  echo "-f requires file system module (list) parameter";;
      n)  echo "-n requires network module (list) parameter";;
      m)  echo "-m requires misc module (list) parameter";;
      p)  echo "-p requires plugins (list) parameter";;
      r)  echo "-r requires root dir parameter";;
      s)  echo "-s may require theme name";;
      I)  echo "-I requires network interface parameter";;
      S)  echo "-S requires the system name";;
      V)  echo "-V requires an executable to run inside linuxrc";;
      *)  echo "Unknown option: -$OPTARG"
          echo "Try mkinitrd -h";;
    esac
    exit 1
    ;;
    C)  cdboot=1;;
    c)  CLCONFFILE=$OPTARG;;
    d)  use_dhclient=1;;
    k)  KERNVER=$OPTARG;;
    i)  INITRD_PATH=$OPTARG;;
    f)  FSMODULES=$OPTARG;;
    n)  NWMODULES=$OPTARG;;
    m)  MISCMODULES=$OPTARG;;
    p)  PLUGINS=$OPTARG;;
    r)  ROOTDIR=$OPTARG;;
    s)  theme=$OPTARG;;
    S)  SYSTEM_NAME=$OPTARG;;
    t)  use_tpm=1;;
    w)  enable_wlan=1;;
    d)  IRFSDEBUG=$OPTARG;;
    l)  local_init_script="$OPTARG";; # use for pre/postinit.local?
    h)  usage;;
  esac
done
shift $(expr $OPTIND - 1)


#########################################################################
# End of parameter, argument interpretation

# Check for superuser id
precheck
#Change to the directory this script is located in
goto_script_dir

if [ -z "$ROOTDIR" ] ; then
  echo "No ROOTDIR given - please use the -r flag! Exiting..."
  exit 1
fi
if [ -z "$SYSTEM_NAME" -a -z "$cdboot" ] ; then
  echo "No system name given - please use the -S flag! Exiting..."
  exit 1
fi
# set temporary setup directory, clean it and setup basic directory
# structure
if [ -z "$INSTDIR" ] ; then
  INSTDIR=/tmp/slx-tmpramfs
fi
rm -rf ${INSTDIR}
mkdir -p ${INSTDIR}/{dev,proc,tmp,mnt,root,bin,sys,lib}
mkdir -p ${INSTDIR}/var/{lib,run} ${INSTDIR}/var/lib/nfs/state
ln -s /bin ${INSTDIR}/sbin
mkdir -p ${INSTDIR}/usr/share
# needed for KNOPPIX hwsetup tool not to be confused with ${D_SYSCONFDIR}
mkdir -p ${INSTDIR}/etc/sysconfig

# slx init or cdboot init 
if [ -z "$cdboot" ] ; then
  # set DISTRO_NAME & DISTRO_VER from ROOTDIR:
  VENDOR_OS=$(basename $ROOTDIR)
  DISTRO_NAME=${VENDOR_OS%%-*}
	# select distro type
  DISTRO_VER=${VENDOR_OS#*-}
	# drop distro type, keep from version onwards
  DISTRO_VER=${DISTRO_VER%%-*}
	# remove any optional stuff after version
  DISTRO_VER=${DISTRO_VER%%_*}
	# remove any architecture specification after version
  copy_distro_stuff ${DISTRO_NAME} ${DISTRO_VER}

  # initial ramdisk scripts: init, functions, servconfig, hwautocfg, ...
  # copy and replace variable names
  for dirs in `find ${SLX_SHARE_PATH}/initramfs/* ! -regex ".*/\..*"` ; do
    repco ${dirs##*initramfs/}
  done
  chmod 755 ${INSTDIR}/init \
    ${INSTDIR}/bin/{servconfig,hwautocfg,xconfig,dhcpmkconfig}
else
  cp ${SLX_SHARE_PATH}/cdboot/init ${INSTDIR}/init
  chmod 755 ${INSTDIR}/init
fi

# quickhack architecture detection of target system
if [ -d ${ROOTDIR}/lib64 ] ; then
  ARCH=x86_64
else
  ARCH=i586
fi

# copy the proper busybox, ddcprobe, 915resolution depending on architecture 
# (i586 for 32bit and x86_64 for 64bit)
# fixme: At the moment the busybox does not support ext2 filesystem tools
cp ${SLX_SHARE_PATH}/busybox/busybox.${ARCH} ${INSTDIR}/bin/busybox
cp ${SLX_SHARE_PATH}/ramfstools/ddcprobe.${ARCH} ${INSTDIR}/bin/ddcprobe
cp ${SLX_SHARE_PATH}/ramfstools/915resolution.${ARCH} ${INSTDIR}/bin/915resolution
slxldd --root-path=${ROOTDIR} ${INSTDIR}/bin/busybox >>${INSTDIR}/tmp/libraries
for bbins in [ ar arping ash bunzip2 cat chmod chown chroot cp cpio cut \
    date dd df dmesg du echo env expr fdisk free grep gunzip hwclock \
    insmod id ip kill killall ln ls lsmod mdev mkdir \
    mknod mkswap modprobe mount mv nice ping printf ps rdate \
    rm rmmod sed sleep sort swapoff swapon switch_root tar test tftp \
    time touch tr udhcpc umount uptime usleep vconfig vi wget zcat zcip; do
  ln -fs /bin/busybox ${INSTDIR}/bin/$bbins
done
# fake the sh link in busybox environment
echo -e "#!/bin/ash\n/bin/ash \$@" > ${INSTDIR}/bin/sh
chmod a+x ${INSTDIR}/bin/sh

# binaries useful for debugging purposes
if [ $IRFSDEBUG -gt 2 ] ; then
  for bbins in \
    strace
  do
    cobi ${bbins} bin || echo "Program ${bbins} not found"
  done
fi

# slx init or cdboot init 
if [ -z "$cdboot" ] ; then
  # one of the dhcp clients (do we really need any other dhcp client?)
  if [ -n "${use_dhclient}" ] ; then
    if binfinder dhclient ; then
      cobi dhclient bin
      if [ "$DISTRO_NAME" = "ubuntu" ] ; then
        #Dhclient auf Ubu 6.10 streikt ohne libnss_compat
        echo "/lib/libnss_compat.so.2" >> ${INSTDIR}/tmp/libraries
        echo "/lib/libnsl.so.1" >> ${INSTDIR}/tmp/libraries
      fi
    else
      echo "dhclient is not available"
    fi
  fi

  # if nbd or dnbd is used as clients root filesystem
  if [ -n "${FSMODULES}" ] ; then
  for i in ${FSMODULES}; do
    case "$i" in
      dnbd)
        cobi dnbd-client bin
      ;;
      nbd)
        cobi nbd-client bin
      ;;
    esac
  done
  fi
  # if tpm should be used within InitRamFS ...
  if [ -n "${use_tpm}" ] ; then
    MISCMODULES="${MISCMODULES} tpm"
    # copy the other stuff
  fi
  # if unionfs
  cobi unionctl bin &>/dev/null || \
    echo "Program unionctl not found; could be ignored ..."

  # if cowloop
  cobi cowdev bin &>/dev/null || \
    echo "Program cowdev not found; could be ignored ..."

  # if iscsi (or just with modules)??
  # cobi iscsiadm iscsid bin &>/dev/null || \
  #  echo "Program iscsid not found; could be ignored ..."

  # distro specific additional stuff
  case "${DISTRO_NAME}" in
    debian*)
      cp ${ROOTDIR}/lib/libnss_compat.so.2 ${INSTDIR}/lib;;
  esac
  # add libpci.so to the libraries, as we are going to copy it manually:
  slxldd --root-path=${ROOTDIR} ${SLX_SHARE_PATH}/initramfs/lib/libpci.so.2 \
    >>${INSTDIR}/tmp/libraries
else
  # fast kernel switch tool
  cobi kexec bin
fi
  
# if wlan should be supported in stage3
if [ -n "${enable_wlan}" ] ; then
  cobi iwconfig bin
fi
  
# if tpm should be used within InitRamFS ...
if [ -n "${use_tpm}" ] ; then
  MISCMODULES="${MISCMODULES} tpm"

  ########## platform-independent stuff ##########
  SLX_TPM_PATH="$SLX_SHARE_PATH/tpm"
  if [ ! -d "$SLX_TPM_PATH" ] ; then
        echo "ERROR: can't find platform-independent tpm-files: $SLX_TPM_PATH"
        exit 1
  fi

  # copy files with directory-hierarchy
  for TPM_FILE in $(find ${SLX_TPM_PATH} -type f) ; do
    path=$(dirname $TPM_FILE | sed "s,$SLX_TPM_PATH,,")
    mkdir -p ${INSTDIR}/$path
    cp $TPM_FILE ${INSTDIR}/$path
  done
  ################################################

  ########### platform-dependent stuff ###########
  # TPM_BINPATH contains path to platform-dependent stuff
  TPM_BINPATH="${ROOTDIR}/usr/local/share/tpm"
  if [ ! -d "$TPM_BINPATH" ] ; then
        echo "ERROR: can't find platform-dependent tpm-files: $TPM_BINPATH"
        exit 1
  fi
  # copy platform-dependent directory structure and files
  for TPM_FILE in $(find ${TPM_BINPATH} -type f) ; do
    path=$(dirname $TPM_FILE | sed "s,${ROOTDIR},,")
    mkdir -p ${INSTDIR}/$path
    cp $TPM_FILE ${INSTDIR}/$path
  done
  ################################################

  # include the shared libraries required for various binaries
  ### echo "adding shared libs:"
  SHLIBS="$(grep -v '^#' ${SLX_TPM_PATH}/etc/libdeps)"
  for lib in $SHLIBS ; do
    ### cp ${ROOTDIR}/$lib ${INSTDIR}/lib/ && echo $lib
	echo ${ROOTDIR}/$lib >>${INSTDIR}/tmp/libraries
  done
  
  # trousers and tpm-tools
  cobi tcsd bin
  TPM_TOOLS="tpm_sealdata tpm_changeownerauth tpm_clear
                tpm_restrictpubek tpm_selftest tpm_setactive
                tpm_setclearable tpm_setenable tpm_setownable
                tpm_setpresence tpm_takeownership tpm_version
                tpm_createek tpm_getpubek tpm_unseal"
  for tool in $TPM_TOOLS ; do
	  cobi $tool bin
  done
  # ssh stuff
  cobi ssh bin
  cobi scp bin

  # just debugging tools, can be safely removed...
  cobi bash bin
  cp ${ROOTDIR}/usr/bin/ldd /${INSTDIR}/bin
  cobi strace bin
fi

# if unionfs
cobi unionctl bin &>/dev/null || \
  echo "Program unionctl not found; could be ignored ..."

# if cowloop
cobi cowdev bin &>/dev/null || \
  echo "Program cowdev not found; could be ignored ..."

# if iscsi (or just with modules)??
# cobi iscsiadm iscsid bin &>/dev/null || \
#  echo "Program iscsid not found; could be ignored ..."

# distro specific additional stuff
case "${DISTRO_NAME}" in
  debian*)
    cp ${ROOTDIR}/lib/libnss_compat.so.2 ${INSTDIR}/lib;;
esac

#### end tpm-stuff ####

# now copy all libraries that have been determined to be required:
# first we handle all 64-bit libs...
for lib in $(fgrep /lib64/ ${INSTDIR}/tmp/libraries 2>/dev/null|sort -u); do
  if ! [ -d ${INSTDIR}/lib64 ] ; then
    mkdir ${INSTDIR}/lib64
  fi
  cp ${lib} ${INSTDIR}/lib64
done
# ...now copy 32-bit libs:
for lib in $(fgrep -v /lib64/ ${INSTDIR}/tmp/libraries 2>/dev/null|sort -u); do
  cp ${lib} ${INSTDIR}/lib
done
rm -f ${INSTDIR}/tmp/libraries

# add kernel modules and dependency files
if [ -z "$KERNVER" ] ; then
  KERNVER=$(ls -l ${ROOTDIR}/boot/vmlinuz | grep vmlinuz | sed "s/.*vmlinuz-//")
fi
if [ -z "$KERNVER" ] ; then
  KERNVER=$(ls -l ${ROOTDIR}/vmlinuz | grep vmlinuz | sed "s/.*vmlinuz-//")
fi
# if no vmlinuz found, go for vmlinuz-x.yz.uv...
if [ -z "$KERNVER" ] ; then
  KERNVER=$(ls -lt ${ROOTDIR}/boot/vmlinuz* | grep vmlinuz | \
    sed -n "1,1s/.*vmlinuz-//p")
  echo "No vmlinuz-link found. Using kernel version "$KERNVER
fi

if [ -z "$KERNVER" ] ; then
  echo "No kernel version set or detected - please use the -k flag! Exiting..."
  exit 1
fi
if [ -z "$INITRD_PATH" ] ; then
  INITRD_PATH="/tmp/slxinitramfs.gz"
fi
if [ -z "$NWMODULES" ] ; then
    #NWMODULES="forcedeth e1000 e100 tg3 via-rhine r8169 pcnet32 8139too 8139cp"
    NWMODULES="forcedeth e1000 e100 tg3 3c59x via-rhine r8169 pcnet32 b44 8139too"
fi
# slx init or cdboot init 
if [ -z "$cdboot" ] ; then
  # base filesystem modules
  if [ -z "$FSMODULES" ] ; then
    FSMODULES="nbd squashfs nfs"
  fi
  # put all needed modules into initial ramdisk
  mkdir -p ${INSTDIR}/lib/modules/${KERNVER}
  for module in af_packet unix ${NWMODULES} ${FSMODULES} ${MISCMODULES} \
    ${DEBUGMODULES} usbcore hid usbhid uhci-hcd ohci-hcd; do
    mod=( $(cd /${ROOTDIR}; find "lib/modules/${KERNVER}" -name ${module}.ko \
      2>/dev/null) )

    mod_nr=${#mod[*]}
    declare -i counter=0
    while [ ${counter} -lt ${mod_nr} ]; do
      mpath=${INSTDIR}/${mod[$counter]%/*}
      mkdir -p ${mpath}
      # specific function which can follow a link
      modcp ${ROOTDIR}/${mod[$counter]} ${mpath} ${ROOTDIR}
      # fixmee!! selection of dependent modules could be handled a little bit
      # more clever
      rdirprefix="${ROOTDIR}/lib/modules/${KERNVER}/kernel"
      ddirprefix="${INSTDIR}/lib/modules/${KERNVER}/kernel"
      case "$module" in
        e100|8139too|via-rhine|sis900|epic100|sundance|8139cp|eepro100|pcnet32)
          cp ${rdirprefix}/drivers/net/mii.ko \
             ${ddirprefix}/drivers/net &>/dev/null
        ;;
        # WLAN module block
        #ipw3945|...)
        # cp ${rdirprefix}/net/wireless ...
        #;;
        ath_pci)
          for wlanmodule in ath_rate_sample ath_hal wlan wlan_scan_sta ; do
            mod=$(cd /${ROOTDIR}; find "lib/modules/${KERNVER}" \
              -name ${wlanmodule}.ko 2>/dev/null)
            mkdir -p ${INSTDIR}/${mod%/*}
            # specific function which can follow a link
            modcp ${ROOTDIR}/$mod ${INSTDIR}/${mod%/*} ${ROOTDIR}
          done
        ;;
        # for unsupported WLAN devices
        ndiswrapper)
          cp -a ${ROOTDIR}/etc/ndiswrapper ${INSTDIR}/etc
        ;;
        nfs)
          # create directory structure for modules needed and copy them
          # to the proper place (as expected by modprobe/modules.dep)
          mkdir -p ${ddirprefix}/net/sunrpc
          mkdir -p ${ddirprefix}/fs/lockd ${ddirprefix}/fs/nfs_common
          cp ${rdirprefix}/net/sunrpc/sunrpc.ko \
            ${ddirprefix}/net/sunrpc &>/dev/null
          cp ${rdirprefix}/fs/lockd/lockd.ko \
            ${ddirprefix}/fs/lockd &>/dev/null
          cp ${rdirprefix}/fs/nfs_common/nfs_acl.ko \
	    ${ddirprefix}/fs/nfs_common &>/dev/null
        ;;
        ext3)
          # needed for ext2 support
          mkdir -p ${ddirprefix}/fs/jbd
          cp ${rdirprefix}/fs/jbd/jbd.ko \
            ${ddirprefix}/fs/jbd
        ;;
        netloop)
          # needed for xen stuff
          cobi brctl bin &>/dev/null || \
            echo "Program brctl not found; could be ignored ..."
          mkdir -p ${ddirprefix}/net/bridge
          cp ${rdirprefix}/net/bridge/bridge.ko \
            ${ddirprefix}/net/bridge &>/dev/null
        ;;
        iscsi_tcp)
          # iscsi modules
          # cobi cobi iscsiadm iscsid bin &>/dev/null
          #mkdir -p ${ddirprefix}/drivers/scsi
          #for i in scsi_mod scsi_transport_iscsi libiscsi iscsi_tcp; do
          #  cp ${rdirprefix}/drivers/scsi/$i.ko
          #    ${ddirprefix}/drivers/scsi
          #done
        ;;
        tpm)
          # complete the tpm modules
          for mod in tpm_atmel tpm_bios tpm_nsc tpm_infineon tpm_tis ; do
            cp ${rdirprefix}/drivers/char/tpm/$mod.ko \
              ${ddirprefix}/drivers/char/tpm/
          done
        ;;
        sata_sil|sata_nv|sata_via)
          # serial ata local disk support
          mkdir -p ${ddirprefix}/drivers/scsi ${ddirprefix}/drivers/ata
          cp ${rdirprefix}/drivers/scsi/{scsi,sd}_mod.ko \
            ${ddirprefix}/drivers/scsi
          cp ${rdirprefix}/drivers/ata/libata.ko ${ddirprefix}/drivers/ata
        ;;
        vesafb)
          # vesa framebuffer support
          mkdir -p ${ddirprefix}/drivers/video/console
          for i in bitblit fbcon font mdacon softcursor tileblit; do
            cp ${rdirprefix}/drivers/video/console/$i.ko \
              ${ddirprefix}/drivers/video/console
          done
        ;;
      esac
      counter=${counter}+1
    done
  done
  # copy user-supplied preinit files. The client specific one overwrites the
  # system default one. postinit.local may be part of fileget tgz (in stage3)
  for cfg in default/initramfs/preinit.local \
           ${SYSTEM_NAME}/initramfs/preinit.local \
           default/initramfs/postinit.local \
           ${SYSTEM_NAME}/initramfs/postinit.local ; do
    test -f ${SLX_PRIVATE_PATH}/config/$cfg && \
      cp ${SLX_PRIVATE_PATH}/config/$cfg ${INSTDIR}/bin
  done
  # copy init-hooks from all plugins which are going to be executed in stage3
  mkdir -p ${INSTDIR}/etc/init-hooks
  PLUGINS=$(echo ${PLUGINS} | sed -e "s/,/ /g")
  for plugin in $PLUGINS ; do
    test -d ${SLX_BASE_PATH}/lib/plugins/$plugin/init-hooks && \
      cp -r ${SLX_BASE_PATH}/lib/plugins/$plugin/init-hooks/* \
            ${INSTDIR}/etc/init-hooks/
  done
  # custom hardware configuration files pcitable.local and Cards.local
  # fixme!! New source for these files (instead of templates)!
  if [ -f ${SLX_SHARE_PATH}/templates/pcitable.local ] ; then
    cat ${SLX_SHARE_PATH}/templates/pcitable.local > \
      ${INSTDIR}/usr/share/hwdata/pcitable.new
    cat ${INSTDIR}/usr/share/hwdata/pcitable >>\
      ${INSTDIR}/usr/share/hwdata/pcitable.new
    mv ${INSTDIR}/usr/share/hwdata/pcitable.new \
      ${INSTDIR}/usr/share/hwdata/pcitable
  fi
  if [ -f ${SLX_SHARE_PATH}/templates/Cards.local ] ; then
    cat ${SLX_SHARE_PATH}/templates/Cards.local > \
      ${INSTDIR}/usr/share/hwdata/Cards.new
    cat ${INSTDIR}/usr/share/hwdata/Cards >>\
      ${INSTDIR}/usr/share/hwdata/Cards.new
    mv ${INSTDIR}/usr/share/hwdata/Cards.new \
      ${INSTDIR}/usr/share/hwdata/Cards
  fi
else
  # just copy the several ethernet drivers
  mkdir -p ${INSTDIR}/lib/modules/${KERNVER}/kernel/drivers/net
  for module in af_packet unix ${NWMODULES} mii ; do
    mod=$(cd /${ROOTDIR}; find "lib/modules/${KERNVER}" -name ${module}.ko \
2>/dev/null)
    mpath=${INSTDIR}/${mod%/*}
    mkdir -p ${mpath}
    # specific function which can follow a link
    modcp ${ROOTDIR}/$mod ${mpath} ${ROOTDIR}
  done
fi
  
cp ${ROOTDIR}/lib/modules/${KERNVER}/modules.* \
  ${INSTDIR}/lib/modules/${KERNVER}

# add the common default client configuration file - just to ensure to
# have all major variables defined. 
echo "## common settings for clients sharing an InitRamFS" \
  > ${INSTDIR}/etc/initramfs-setup
if [ -n "${CLCONFFILE}" ] ; then
  cat ${CLCONFFILE} >>${INSTDIR}/etc/initramfs-setup
  echo "Using ${CLCONFFILE}"
elif [ -n "$cdboot" ] ; then
  echo -e "ramfs_nicmods=\"${NWMODULES}\"\nramfs_kernver=\"${KERNVER}\"" \
    >>${INSTDIR}/etc/initramfs-setup
fi
echo "## end of common initramfs-setup" >>${INSTDIR}/etc/initramfs-setup

#########################################################################
# End of file copy procedures

[ -n "${theme}" ] && add_theme

# finally create the compressed initial ramdisk cpio archive
pushd . > /dev/null 2>&1
  cd ${INSTDIR}
  find . ! -name "*~" | cpio -H newc --create | gzip -9 > $INITRD_PATH
popd > /dev/null 2>&1