fix error to not only use a running kernel
[grml-terminalserver.git] / grml-terminalserver-config
1 #!/bin/sh
2 # Filename:      terminalserver-config
3 # Purpose:       configuration program for grml-terminalserver
4 # Authors:       grml-team (grml.org), (c) Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Don Nov 16 14:39:51 CET 2006 [mika]
8 ################################################################################
9
10
11 ###
12 ### __INCLUDES
13 ###
14 . /etc/grml/sh-lib
15 #. /etc/grml/sysexits-sh
16
17
18
19 ###
20 ### __VARIABLES
21 ###
22
23 verbose_=0
24
25 # this file holds all variable definitions
26 SHARED_PROG_VARS_="/usr/share/grml-terminalserver/shared_prog_vars"
27 isExistent $SHARED_PROG_VARS_ die
28 . $SHARED_PROG_VARS_
29
30 # variables used in the config file for grml-terminalserver
31 INTERFACE_=""
32 IP_=""
33 NETMASK_=""
34 GW_=""
35 NAMESERVERS_=""
36 IPRANGE_FROM_=""
37 IPRANGE_TO_=""
38 NETWORK_=""
39 OPTIONS_=""
40 BOOT_ARGS_=""
41 NAT_INTERFACE_="none"
42
43
44 ###
45 ### __FUNCTIONS
46 ###
47
48 function printUsage
49 {
50   cat <<EOT
51 Usage: "$PROG_NAME__" [OPTIONS] <command>
52
53 $PROG_NAME__ is the config program for the terminalserver coming with grml.
54
55 COMMANDS:
56
57    help             This help text
58    interactive      Interactive Configuration of the grml-terminalserver
59    grubConf         Configure grub and create boot-image (for non-PXE NICs)
60    grubConfWrite    Configure grub and write image to floppy disk
61    grubWrite        Write compiled grub-image to floppy disk
62    grubMultiWrite   batchwrite grub-image to floppy disk
63    initrd           Only create the initrd
64    clean            Remove all configfiles created during user configuration
65    <default>        interactive mode
66
67 OPTIONS:
68    -v               verbose (show what is going on, v++)
69    -h               this help text
70
71 EOT
72 }
73
74
75 function writeConfig
76 {
77   local date_=""
78
79   if [ -f $CONF_FILE_ ]; then
80     mv -fb $CONF_FILE_ ${CONF_FILE_}-old
81   fi
82
83   date_=`date`
84   cat <<EOT > $CONF_FILE_
85 # GRML TERMINAL-SERVER CONFIG
86 # created on "$date_"
87 INTERFACE_="$INTERFACE_"
88 IP_="$IP_"
89 NETWORK_="$NETWORK_"
90 NETMASK_="$NETMASK_"
91 GW_="$GW_"
92 NAMESERVERS_="$NAMESERVERS_"
93 IPRANGE_FROM_="$IPRANGE_FROM_"
94 IPRANGE_TO_="$IPRANGE_TO_"
95 OPTIONS_="$OPTIONS_"
96 BOOT_ARGS_="$BOOT_ARGS_"
97 NAT_INTERFACE_="$NAT_INTERFACE_"
98
99 EOT
100   notice "config successfully safed to \"$CONF_FILE_\""
101 }
102
103
104 # AUTOMATIC CONFIGURATION  {{{
105 function checkParamArg
106 {
107   local param_name="$1"
108   local arg="$2"
109
110   #eval "echo $`echo $test`"
111   echo $arg |grep "^[-|+]" &>/dev/null || return
112
113   die "Argument from $param_name looks like another parameter \"$arg\"" 1
114 }
115
116 function actionAutoconf
117 {
118   checkParamArg "-i" "$interface_"
119 }
120 # }}}
121
122
123 # INITRD {{{
124
125 function actionMkInitrd
126 {
127   echo -n "Creating initrd $PATH_/minirt26.gz: "
128   isExistent "$PATH_/minirt26.gz" && echo && echo "$PATH_/minirt26.gz exists already, skipping initrd creation" && return 0
129   mkInitrd
130   echo done
131 }
132
133 function mkInitrd
134 {
135   TMP_DIR_=`mktemp -td terminalserver_initrd.XXXXXX`
136   local i=''
137   local tmp_loopname=''
138
139   # copying original initrd into $INITRD
140   execute "rm -r $INITRD_" 2>/dev/null
141   execute "cp $ORIGINAL_INITRD_ $TMP_DIR_/minirt26.gz" warn || return 1
142   execute "mkdir -p $INITRD_" warn || return 1
143   execute "mkdir -p $PATH_/mini-root.orig" warn || return 1
144   execute "gunzip $TMP_DIR_/minirt26.gz" warn || return 1
145   tmp_loopname=`findNextFreeLoop die`
146   execute "mount -o loop=$tmp_loopname $TMP_DIR_/minirt26 $PATH_/mini-root.orig " warn || return 1
147   execute "cp -a $PATH_/mini-root.orig/* $INITRD_" warn || return 1
148   execute "umount $PATH_/mini-root.orig" warn || return 1
149   losetup -d $tmp_loopname &>/dev/null
150   tmp_loopname=''
151   execute "rmdir $PATH_/mini-root.orig" warn || return 1
152   execute "rm $TMP_DIR_/minirt26" warn || return 1
153
154   # implanting my initrd changes into the original initrd
155
156   # copy programs, check if there are already links with this name to busybox
157   for i in $USR_SHARE_/timeout $USR_SHARE_/discover* $USR_SHARE_/udhcpc $USR_SHARE_/udhcp-config.sh $USR_SHARE_/cdir $USR_SHARE_/rdir /usr/bin/ldd ;do
158     tmp_name_="${i##*/}"
159     isNotExistent "$INITRD_/static/$tmp_name_" eprint || execute "rm $INITRD_/static/$tmp_name_"
160     cp $i "$INITRD_/static/$tmp_name_"
161   done
162   cp $USR_SHARE_/linuxrc $INITRD_/
163   mkdir -p $INITRD_/mylib
164   mkdir -p $INITRD_/myusr/i386-uclibc-linux/lib
165   for i in libdl libc libuClibc ld-uClibc; do
166     cp -a $UCLIBC_PATH_/$i*.so* $INITRD_/myusr/i386-uclibc-linux/lib
167   done
168
169   # copy data files for discover (discover-data)
170   mkdir -p $INITRD_/mylib/discover
171   cp /lib/discover/list.xml /lib/discover/pci-*.xml $INITRD_/mylib/discover/
172
173   #
174   # which modules should i put into the ramdisk
175   #
176
177   # get cards detected by discover
178   if [ -f /lib/discover/pci.lst ] ; then
179     isNotExistent $CARDS_DETECTED_BY_DISCOVER && awk '/ethernet/{print $3}' /lib/discover/pci.lst |sort |uniq > $CARDS_DETECTED_BY_DISCOVER
180   else # newer discover does not provide /lib/discover/pci.lst, get a list of all network drivers therefore
181     isNotExistent $CARDS_DETECTED_BY_DISCOVER && \
182     # find *all* network drivers, but do not include wlan/pcmcia/... related ones
183     # blacklist: proteon and depca as they seem to cause problems with udevsettle
184     find /lib/modules/${KERNEL_VERSION_}/kernel/drivers/net/ -name \*.ko | \
185       grep -v 'wireless\|wan\|hamradio\|wlan\|ppp\|irda\|pcmcia\|depca\|proteon' | \
186         sed 's#.*./## ; s#\.ko##' | sort | uniq > $CARDS_DETECTED_BY_DISCOVER
187     # find the ones supported by discover
188     # for mod in `grep "name'>.*</data>" /lib/discover/pci-device.xml | sed "s#.*name'>## ; s#</data>##" | sort -u`; do
189     #     find /lib/modules/${KERNEL_VERSION_}/kernel/drivers/net -name ${mod}.ko | sed 's#.*./## ; s#\.ko##' >> $CARDS_DETECTED_BY_DISCOVER
190     # done
191   fi
192
193   local modules="`cat $CARDS_DETECTED_BY_DISCOVER |xargs` af_packet"
194   local modules_dep=""
195
196   # get paths of modules + paths of all dependent modules
197   echo -n "" >"$TMP_"
198   for i in $modules; do
199     tmp_=`awk -F: '{if($1~/'"$i".ko'/) {print $0}}' $MODULES_PATH_/$KERNEL_VERSION_/modules.dep`
200     echo "${tmp_%%:*}"
201     # FIXME ugly sed hack :(
202     echo "${tmp_#*:}" | xargs -n1 echo | sed 's/://'
203   done \
204   | sort | uniq | while read module relax; do
205     if [ -n "$module" ]; then
206       echo "$module" >> $TMP_
207     fi
208   done
209
210   # copy modules + dependend modules into ramdisk
211   local mod_path_="$INITRD_/mylib/modules/$KERNEL_VERSION_"
212   local tmp_dst_path_="$mod_path_/kernel"
213   mkdir -p $tmp_dst_path_
214   cat $TMP_ |sort |uniq |while read module; do
215     local tmp_path=${module#*/kernel/}
216     tmp_path=$tmp_dst_path_/${tmp_path%/*.ko}
217     local module_path=$tmp_path/${module##/*/}
218
219     isNotExistent "$tmp_path" dprint && mkdir -p "$tmp_path"
220     isNotExistent "$module_path" dprint && cp "$MODULES_PATH_ROOT_DIFF_/$module" "$module_path"
221   done
222
223   # copying additional modules
224   for i in fs/nfs/nfs.ko net/sunrpc/sunrpc.ko fs/lockd/lockd.ko net/packet/af_packet.ko; do
225     local tmp_path="$tmp_dst_path_/${i%/*}"
226     mkdir -p $tmp_path
227     cp $MODULES_PATH_/$KERNEL_VERSION_/kernel/$i "$tmp_path"
228   done
229
230   # generate modules.dep
231   cp $MODULES_PATH_/$KERNEL_VERSION_/modules.dep $mod_path_
232   #grep "^\/lib\/modules\/$KERNEL_VERSION_\/kernel\/" $MODULES_PATH_/$KERNEL_VERSION_/modules.dep |\
233   #  sed "s/\/lib\/modules\/$KERNEL_VERSION_\/kernel\//\/modules\//g" > $mod_path_/modules.dep
234
235   # put everything into the new initrd
236   local tmp_size=`du -s $INITRD_ |awk '{print $1}'`   # in kB
237   let tmp_size=$tmp_size+1000
238   if (( $tmp_size >= 16000 )); then
239     warn "Your initrd is $tmp_size kByte large => TOO BIG (should be <= 16000kB)"
240     warn "Please remove a few modules from $CARDS_DETECTED_BY_DISCOVER or edit $INITRD_ manually"
241     return 1
242   fi
243
244   execute "dd if=/dev/zero of=$TMP_DIR_/minirt26 bs=${tmp_size}k count=1 &>/dev/null" warn || \
245     warn "could not create filesystem image"
246
247   tmp_loopname=`findNextFreeLoop die`
248   execute "losetup $tmp_loopname $TMP_DIR_/minirt26" die
249   execute "mke2fs -L \"GRML NETINIT\" -b 1024 -N 8192 -O none -F -q -m 0 $tmp_loopname" warn
250
251   execute "mkdir $PATH_/minirt26_mountp" warn
252   execute "mount $tmp_loopname $PATH_/minirt26_mountp" warn
253   execute "cp -a $INITRD_/* $PATH_/minirt26_mountp" warn
254   execute "umount $PATH_/minirt26_mountp" warn
255   execute "losetup -d $tmp_loopname &>/dev/null" warn
256   execute "rmdir $PATH_/minirt26_mountp" warn
257   execute "gzip -9 $TMP_DIR_/minirt26" warn
258   execute "rm -r $INITRD_" warn
259   execute "mv $TMP_DIR_/minirt26.gz $PATH_"
260   execute "rm -fr $TMP_DIR_"
261 }
262 # }}}
263
264
265 # INTERACTIVE CONFIGURATION  {{{
266
267 function actionInteractive
268 {
269   local i=""
270
271   dprint "running in interactive mode"
272
273   local card_title_="Choose network device connected to client network"
274   local card_message_="Available network devices:"
275   local iprange_title_="IP Address range for clients"
276 local iprange_message_="
277 Please enter the desired IP-Range of addresses that should be allocated by clients, separated by a single space.
278
279 Example:
280           192.168.0.101 192.168.0.200
281
282 for addresses from 192.168.0.101 to (and including) 192.168.0.200.
283
284 "
285   local runconfig_title_="Networkcard config"
286   local runconfig_message_="Would you like to configure your interfaces now?"
287   local grub_title_="Grub configuration"
288   local grub_message_="Do you have any NON-PXE network cards you would like to boot from?"
289
290   # on witch interfaces should we listen
291   local netdevices_="$(cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}')"
292   local device_list_=""
293   for INTERFACE_ in $netdevices_; do device_list_="$device_list_ ${INTERFACE_} Networkcard_${INTERFACE_##eth}"; done
294     echo -n "" >"$TMP_"
295     $DIALOG_ --backtitle "$BACK_TITLE_" --title "$card_title_" --menu "$card_message_" \
296       0 0 18 $device_list_ 2>"$TMP_" || warn "could not get network-interface"
297   INTERFACE_="$(<$TMP_)" ; echo -n "" >"$TMP_"
298
299   while true; do
300     IP_=`netGetIp "$INTERFACE_" warn`
301     NETMASK_=`netGetNetmask "$INTERFACE_" warn`
302     netValidIp "$IP_" warn && break
303     $DIALOG_ --backtitle "$BACK_TITLE_" --title "$runconfig_title_" --yesno "$runconfig_message_" 18 45 && \
304       netcardconfig || die "Could not get interface" $?
305   done
306
307   IPRANGE_FROM_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMin/{print $2}'`
308   IPRANGE_TO_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMax/{print $2}'`
309   NETWORK_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/Network:/{print $2}'`
310   NETWORK_=${NETWORK_%/*}
311   local iprange_=""
312   while [ -z "$IPRANGE_FROM_" -o -z "$IPRANGE_TO_" -o -z "$iprange_" ]; do
313     iprange_="$IPRANGE_FROM_ $IPRANGE_TO_"
314     echo -n "" >"$TMP_"
315     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$iprange_title_ ($INTERFACE_=$IP_/$NETMASK_)" \
316       --inputbox "$iprange_message_" 18 75 "$iprange_" 2>"$TMP_" || die "problems getting network range" $?
317
318     iprange_="$(<$TMP_)"
319     IPRANGE_FROM_="${iprange_%% *}"
320     IPRANGE_TO_="${iprange_##* }"
321
322     for i in "$IPRANGE_FROM_" "$IPRANGE_TO_"; do
323       netValidIp "$i" warn || iprange_=""
324     done
325   done
326
327   NAMESERVERS_=`netGetNameservers warn`
328   GW_=`netGetDefaultGateway warn`
329   GW_DEV_=`/sbin/ip route get "$GW_" | awk '{ print $3; exit; }'`
330   if [ "$GW_DEV_" != "$INTERFACE_" ] && [ "$GW_DEV_" != "" ]; then
331     # GW_DEV_ of server is not the same device as the one serviced by dhcpd
332     # so it doesn't make sense to provide the GW_ address to the clients
333     local do_nat_="YES"
334     local do_nat_title_="Network Address Translation"
335     local do_nat_message_="
336 Do you want to set up NAT so that clients booting from this
337 grml-terminalserver can use this machine also as gateway to
338 the internet?"
339
340     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$do_nat_title_" --yesno "$do_nat_message_" 15 75 || \
341       do_nat_="NO"
342     if [ "$do_nat_" = "YES" ]; then
343       # user wants NAT, we give the clients the server address as
344       # gateway as well
345       GW_="$IP_"
346       NAT_INTERFACE_="$GW_DEV_"
347     else
348       # no NAT, no sensible gateway
349       GW_=
350       NAT_INTERFACE_="none"
351     fi
352   fi
353
354
355   # grub
356   echo -n "" >"$TMP_"
357   local grub_write_="YES"
358   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$grub_title_" --yesno "$grub_message_" 5 75 && \
359     grubConfig || grub_write_="NO"
360
361
362   # get options
363   #local OPT_IPTABLES_="yes"
364   #local OPT_SSH_="yes"
365   #local OPT_DISTCC_="yes"
366   #local OPT_SQUID_=""
367
368   local OPTIONS_TITLE_="Options"
369   local OPTIONS_MESSAGE_="Please give the appropriate options you want the clients to use:
370
371 grml2hd   - Make a non-interactive remote installation
372
373
374
375 "
376 #  local OPTIONS_MESSAGE_="Please give the appropriate options you want the clients to use:
377 #
378 #iptables  - Only the server should be able to access the clients
379 #ssh       - A ssh-key will be created on the server and distributed to the clients
380 #distcc    - You want to use the clients as compile-farm (ssh options recommned)
381 #
382 #"
383
384   local OPT_IPTABLES_DESC_="Start iptables on the clients"
385   local OPT_SSH_DESC_="Start ssh on the clients"
386   local OPT_DISTCC_DESC_="Start distcc on the clients"
387   local OPT_GRML2HD_DESC_="Remote install grml on the network clients"
388   # dialog options (enable if implemented)
389   #iptables "$OPT_IPTABLES_DESC_" off \
390   #ssh "$OPT_SSH_DESC_" off \
391   #distcc "$OPT_DISTCC_DESC_" off \
392   echo -n "" >"$TMP_"
393   $DIALOG_ --clear --separate-output --backtitle "$BACK_TITLE_" --title "$OPTIONS_TITLE_" --checklist "$OPTIONS_MESSAGE_" 25 80 10 \
394   grml2hd "$OPT_GRML2HD_DESC_" off \
395     2>$TMP_ || die "could not get terminalserver options" $?
396   while read tmp_option_; do
397     OPTIONS_="$OPTIONS_ $tmp_option_"
398   done <$TMP_
399
400   # parse options
401   for i in $OPTIONS_; do
402     case "$i" in
403       grml2hd)  optGrml2Hd || return 1 ;;
404     esac
405   done
406
407   echo -n "" >"$TMP_"
408   local OPTIONS_BOOTARG_MESSAGE_="Here you can add additional boot arguments for the clients seperated by spaces:
409
410 Quite usefull examples:
411
412 ssh=<pw>              - Start ssh server and set password of user grml to pw
413 services=<1,2,3>      - Execute /etc/init.d/{1,2,3} start
414 console=ttyS0,9600n8  - Initialise seriell console
415 secure                - Boot only restricted clients (without root permissions)
416 startx                - Boot into X (should be used with secure)
417
418
419 "
420   $DIALOG_ --clear --no-collapse --backtitle "$BACK_TITLE_" --title "$OPTIONS_TITLE_" --inputbox "$OPTIONS_BOOTARG_MESSAGE_" 0 0\
421     2>$TMP_ || die "problems getting additional boot arguments"
422   BOOT_ARGS_="$BOOT_ARGS_ $(<$TMP_)"
423
424   writeConfig
425   if [ $grub_write_ == "YES" ]; then
426     grubWrite
427   fi
428   actionMkInitrd
429   notice "GRML terminalserver successfully configured"
430 }
431 # }}}
432
433 # OPTIONS GETTING DIALOG {{{
434 function optGrml2Hd
435 {
436   local GRML2HD_TITLE_='Grml2hd options dialog'
437   local tmp_=''
438   local options_='BOOT_IMAGE=grml2hd'
439
440   # get partition to install grml2hd on
441   OPTIONS_PARTITION_MSG_='Please give me the target partition where to install grml'
442   PARTITION_TITLE_='Partition selection'
443   echo -n "" >"$TMP_"
444   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$PARTITION_TITLE_" --inputbox \
445     "$OPTIONS_PARTITION_MSG_" 0 75 '/dev/hda1' 2>$TMP_ || die "problems getting partition"
446   tmp_="partition=$(<$TMP_)"
447   options_="$options_ $tmp_"
448
449   # get filesystem type
450   OPTION_FS_TYPE_='Please give me the filesystem type'
451   FS_TITLE_='Filesystem selection'
452   echo -n "" >"$TMP_"
453   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$FS_TITLE_" --inputbox \
454   "$OPTION_FS_TYPE_" 0 75 'ext3' 2>$TMP_ || die "problems getting filesystem type"
455   tmp_="filesystem=$(<$TMP_)"
456   options_="$options_ $tmp_"
457
458   # get where to save mbr
459   OPTION_MBR_='Please give me the location where to save the mbr'
460   MBR_TITLE_='Select location of mbr'
461   echo -n "" >"$TMP_"
462   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$MBR_TITLE_" --inputbox \
463     "$OPTION_MBR_" 0 75 '/dev/hda' 2>$TMP_ || die "problems getting location where to write mbr"
464   tmp_="mbr=$(<$TMP_)"
465   options_="$options_ $tmp_"
466
467   # get first user
468   OPTION_USER_='Who should be the first user on the system'
469   USER_TITLE_='User selection'
470   echo -n "" >"$TMP_"
471   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$USER_TITLE_" --inputbox \
472     "$OPTION_USER_" 0 75 'grml' 2>$TMP_ || die "problems getting first user of system"
473   tmp_="user=$(<$TMP_)"
474   options_="$options_ $tmp_"
475
476   BOOT_ARGS_="$options_"
477 }
478 #}}}
479
480 # GRUB CONFIG  {{{
481 function grubConfig
482 {
483   local tmp_=`mktemp -td terminalserver_grub.XXXXXX` || warn "could not create tmp file for grubConfig"
484   if [ -z "$tmp_" ]; then return 1; fi
485
486   grubConfigWork $tmp_
487   local ret_=$?
488   execute "rm -rf $tmp_" warn
489
490   return $ret_
491 }
492
493 function grubConfigWork
494 {
495   local tmp_="$1"
496
497   local grub_title_="Grub configuration"
498   local grub_nic_message_="
499 Give me the types of network cards you would want to boot from.
500 If you networkcard is already listed press <OK>!
501
502 WARNING: DON't add all NIC's, grub could possibly stop working!!"
503   local nics_=""
504
505   while read module_ desc_ mode_; do
506     nics_="$nics_ $module_ $desc_ $mode_"
507   done < $GRUB_NIC_CONF_
508
509   echo -n "" >$TMP_
510   local cmd_line_=""
511   $DIALOG_ --clear --separate-output --backtitle "$BACK_TITLE_" --title "$grub_title_" --checklist \
512     "$grub_nic_message_" 10 70 0 $nics_ 2>$TMP_
513   local ret_=$?
514   if [[ $ret_ -ne 0 || `wc -l <$TMP_` -eq 0 ]]; then
515     echo $ret_
516     warn "you should give me your specified NICs but didn't => DEFAULT will be used"
517     awk '/ on$/{print $1}' $GRUB_NIC_CONF_ >$TMP_
518   fi
519   while read module_; do
520     cmd_line_="$cmd_line_ --enable-$module_"
521   done < $TMP_
522
523   echo "\"$cmd_line_\""
524
525   execute "tar xzf $GRUB_SOURCE_ -C $tmp_" warn || return 1
526   execute "cd $tmp_/grub*" warn || return 1
527 cat >preset-menu <<EOT
528 # Set up the serial terminal, first of all.
529 #serial --unit=0 --speed=19200
530 #terminal --timeout=0 serial
531
532 # Initialize the network.
533 #dhcp --with-configfile
534 #bootp --with-configfile
535 dhcp
536 default 0
537 timeout 0
538 title Load config from Tftp server
539   configfile (nd)/menu.lst
540 EOT
541   #execute "./configure --enable-diskless --enable-preset-menu=./preset-menu $cmd_line_" warn || read
542   execute "CC=gcc-3.3 ./configure --enable-preset-menu=./preset-menu $cmd_line_" warn || read
543   execute "make -j2 CC=gcc-3.3" warn || read #return 1
544
545   local st1_="stage1/stage1"
546   local st2_="stage2/stage2"
547   isExistent $st1_ warn || return 1
548   isExistent $st2_ warn || return 1
549
550   execute "mv -fb $PATH_/grub.img $PATH_/grub.img.old"
551   execute "cat $st1_ $st2_ > $PATH_/grub.img" warn || return 1
552   return 0
553 }
554
555 function grubWrite
556 {
557   local grub_title_="Grub configuration"
558   $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$grub_title_" --yesno \
559     "Do you want to write your grub image to /dev/fd0" 5 75 || return 1
560
561   execute "dd if=$PATH_/grub.img of=/dev/fd0" warn || return 0
562   return 1
563 }
564
565 function grubMultiWrite
566 {
567   local target_=${1:-'/dev/fd0'}
568
569   isExistent "$PATH_/grub.img" die "you must give me an image or run \"$PROG_NAME__ grubconf\""
570   isExistent "$target_" die "$target_ is not a valied"
571   while true; do
572     echo -n "Please insert disk into $target_ and press <ENTER> (STRG-C for end)"
573     read
574     dd if=$PATH_/grub.img of="$target_"
575   done
576 }
577 # }}}
578
579
580 function removeTmpFiles
581 {
582   execute "rm -f $TMP_" warn
583 }
584
585 function actionClean
586 {
587   for i in dhcpd.conf grub.img minirt26.gz; do
588     execute "rm -f $PATH_/$i*"
589   done
590
591   for i in $CARDS_DETECTED_BY_DISCOVER $CONF_FILE_; do
592     execute "rm -f $i"
593   done
594
595 }
596
597
598 ###
599 ### __MAIN
600 ###
601
602 while getopts "i:hv" opt; do
603   case "$opt" in
604     i) interface_=$OPTARG ;;
605     h) printUsage; exit ;;
606     v) let verbose_=$verbose_+1 ;;
607     ?) printUsage; exit 64 ;;
608   esac
609 done
610 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
611 setVerbose $verbose_
612
613 case "$1" in
614   help)   printUsage; exit 0 ;;
615 esac
616
617 checkRoot die 'You have to be root to use this program'
618 disableSyslog
619
620 execute "mkdir -p $PATH_" die
621
622 TMP_=`mktemp -t grml-terminalserver-config.XXXXXX` || die "Could not create tmpfile" $?
623 setExitFunction 'removeTmpFiles'
624
625
626 . $DEFAULT_CONFIG_
627 . $CONFIG_
628 # used config vars:
629 # MODULES_PATH_
630 # MODULES_PATH_ROOT_DIFF_
631 # KERNEL_VERSION_
632 # ORIGINAL_INITRD_
633 if [[ $MODULES_PATH_ == "" || $KERNEL_VERSION_ == "" || $ORIGINAL_INITRD_ == "" ]]; then
634   warn "MODULES_PATH_=\"$MODULES_PATH_\" \
635 KERNEL_VERSION_=\"$KERNEL_VERSION_\" \
636 ORIGINAL_INITRD_=\"$ORIGINAL_INITRD_\""
637   die "False configuration, please update $CONFIG_"
638 fi
639
640
641 case "$1" in
642   interactive)  actionInteractive ;;
643   grubConf)   grubConfig ;;
644   grubConfWrite)  grubConfig && grubWrite ;;
645   grubWrite)    grubWrite ;;
646   grubMultiWrite)   grubMultiWrite "$2" ;;
647   initrd) actionMkInitrd ;;
648   clean)  actionClean ;;
649   *)    actionInteractive ;;
650 esac
651
652 removeTmpFiles
653 # END OF FILE
654 ################################################################################
655 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2