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