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: Son Jän 20 12:12:44 CET 2008 [mika]
8 ################################################################################
15 #. /etc/grml/sysexits-sh
25 # this file holds all variable definitions
26 SHARED_PROG_VARS_="/usr/share/grml-terminalserver/shared_prog_vars"
27 isExistent $SHARED_PROG_VARS_ die
30 # variables used in the config file for grml-terminalserver
51 Usage: "$PROG_NAME__" [OPTIONS] <command>
53 $PROG_NAME__ is the config program for the terminalserver coming with grml.
58 interactive Interactive Configuration of the grml-terminalserver
59 grubConf <file> Configure grub and create boot-image (for non-PXE NICs)
60 Read modules for grub from file if given.
61 grubConfWrite Configure grub and write image to floppy disk
62 grubWrite Write compiled grub-image to floppy disk
63 grubMultiWrite batchwrite grub-image to floppy disk
64 initrd Only create the initrd
65 clean Remove all configfiles created during user configuration
66 <default> interactive mode
69 -v verbose (show what is going on, v++)
80 if [ -f $CONF_FILE_ ]; then
81 mv -fb $CONF_FILE_ ${CONF_FILE_}-old
85 cat <<EOT > $CONF_FILE_
86 # GRML TERMINAL-SERVER CONFIG
88 INTERFACE_="$INTERFACE_"
93 NAMESERVERS_="$NAMESERVERS_"
94 IPRANGE_FROM_="$IPRANGE_FROM_"
95 IPRANGE_TO_="$IPRANGE_TO_"
97 BOOT_ARGS_="$BOOT_ARGS_"
98 NAT_INTERFACE_="$NAT_INTERFACE_"
101 notice "config successfully safed to \"$CONF_FILE_\""
105 # AUTOMATIC CONFIGURATION {{{
106 function checkParamArg
108 local param_name="$1"
111 #eval "echo $`echo $test`"
112 echo $arg |grep "^[-|+]" &>/dev/null || return
114 die "Argument from $param_name looks like another parameter \"$arg\"" 1
117 function actionAutoconf
119 checkParamArg "-i" "$interface_"
125 function actionMkInitrd
127 echo -n "Creating initrd $PATH_/minirt26.gz: "
128 if isExistent "$PATH_/minirt26.gz" ; then
130 echo "$PATH_/minirt26.gz exists already, skipping initrd creation"
134 if [ -e '/live/cow' ]; then
145 local cfg_="/etc/initramfs-tools/initramfs.conf"
146 local tmp_="`mktemp -t terminalserver__initramfsbk.XXXXXX`"
148 sed -i 's/^MODULES=.*/MODULES=netboot/' $cfg_
149 update-initramfs -u -t 1>/dev/null
152 local initrd_="/boot/initrd.img-$KERNEL_VERSION_"
153 mv $initrd_ $PATH_/minirt26.gz
154 mv ${initrd_}.bak $initrd_
160 TMP_DIR_=`mktemp -td terminalserver_initrd.XXXXXX`
162 local tmp_loopname=''
164 # copying original initrd into $INITRD
165 execute "rm -r $INITRD_" 2>/dev/null
166 execute "cp $ORIGINAL_INITRD_ $TMP_DIR_/minirt26.gz" warn || return 1
167 execute "mkdir -p $INITRD_" warn || return 1
168 execute "mkdir -p $PATH_/mini-root.orig" warn || return 1
169 execute "gunzip $TMP_DIR_/minirt26.gz" warn || return 1
170 tmp_loopname=`findNextFreeLoop die`
171 execute "mount -o loop=$tmp_loopname $TMP_DIR_/minirt26 $PATH_/mini-root.orig " warn || return 1
172 execute "cp -a $PATH_/mini-root.orig/* $INITRD_" warn || return 1
173 execute "umount $PATH_/mini-root.orig" warn || return 1
174 losetup -d $tmp_loopname &>/dev/null
176 execute "rmdir $PATH_/mini-root.orig" warn || return 1
177 execute "rm $TMP_DIR_/minirt26" warn || return 1
179 # implanting my initrd changes into the original initrd
181 # copy programs, check if there are already links with this name to busybox
182 for i in $USR_SHARE_/timeout $USR_SHARE_/udhcp-config.sh $USR_SHARE_/cdir $USR_SHARE_/rdir; do
184 isNotExistent "$INITRD_/static/$tmp_name_" eprint || execute "rm $INITRD_/static/$tmp_name_"
185 cp $i "$INITRD_/static/$tmp_name_"
187 cp $USR_SHARE_/linuxrc $INITRD_/
188 mkdir -p $INITRD_/mylib
189 mkdir -p $INITRD_/myusr
192 # which modules should i put into the ramdisk
195 # find *all* network drivers, but do not include wlan/pcmcia/... related ones
196 # blacklist: proteon and depca as they seem to cause problems with udevsettle
197 find ${MODULES_PATH_}/${KERNEL_VERSION_}/kernel/drivers/net/ -name \*.ko | \
198 grep -v 'wireless\|wan\|hamradio\|wlan\|ppp\|irda\|pcmcia\|depca\|proteon' | \
199 sed 's#.*./## ; s#\.ko##' | sort | uniq > $CARDS_DETECTED_BY_DISCOVER
201 local modules="`cat $CARDS_DETECTED_BY_DISCOVER |xargs` af_packet"
204 # get paths of modules + paths of all dependent modules
206 for i in $modules; do
207 tmp_=`awk -F: '{if($1~/'"$i".ko'/) {print $0}}' $MODULES_PATH_/$KERNEL_VERSION_/modules.dep`
209 # FIXME ugly sed hack :(
210 echo "${tmp_#*:}" | xargs -n1 echo | sed 's/://'
212 | sort | uniq | while read module relax; do
213 if [ -n "$module" ]; then
214 echo "$module" >> $TMP_
218 # copy modules + dependend modules into ramdisk
219 local mod_path_="$INITRD_/mylib/modules/$KERNEL_VERSION_"
220 local tmp_dst_path_="$mod_path_/kernel"
221 mkdir -p $tmp_dst_path_
222 cat $TMP_ |sort |uniq |while read module; do
223 local tmp_path=${module#*/kernel/}
224 tmp_path=$tmp_dst_path_/${tmp_path%/*.ko}
225 local module_path=$tmp_path/${module##/*/}
227 isNotExistent "$tmp_path" dprint && mkdir -p "$tmp_path"
228 isNotExistent "$module_path" dprint && cp "$MODULES_PATH_ROOT_DIFF_/$module" "$module_path"
231 # copying additional modules
232 for i in fs/nfs/nfs.ko net/sunrpc/sunrpc.ko fs/lockd/lockd.ko net/packet/af_packet.ko; do
233 local tmp_path="$tmp_dst_path_/${i%/*}"
235 cp $MODULES_PATH_/$KERNEL_VERSION_/kernel/$i "$tmp_path"
239 cp $MODULES_PATH_/$KERNEL_VERSION_/modules.dep $mod_path_
240 cp $MODULES_PATH_/$KERNEL_VERSION_/modules.alias $mod_path_
241 #grep "^\/lib\/modules\/$KERNEL_VERSION_\/kernel\/" $MODULES_PATH_/$KERNEL_VERSION_/modules.dep |\
242 # sed "s/\/lib\/modules\/$KERNEL_VERSION_\/kernel\//\/modules\//g" > $mod_path_/modules.dep
244 # put everything into the new initrd
245 local tmp_size=`du -s $INITRD_ |awk '{print $1}'` # in kB
246 let tmp_size=$tmp_size+1000
248 if (( $tmp_size >= $max_size )); then
249 warn "Your initrd is $tmp_size kByte large => TOO BIG (should be <= ${max_size}kB)"
250 warn "Please remove a few modules from $CARDS_DETECTED_BY_DISCOVER or edit $INITRD_ manually"
254 execute "dd if=/dev/zero of=$TMP_DIR_/minirt26 bs=${tmp_size}k count=1 &>/dev/null" warn || \
255 warn "could not create filesystem image"
257 tmp_loopname=`findNextFreeLoop die`
258 execute "losetup $tmp_loopname $TMP_DIR_/minirt26" die
259 execute "mke2fs -L \"GRML NETINIT\" -b 1024 -N 8192 -O none -F -q -m 0 $tmp_loopname" warn
261 execute "mkdir $PATH_/minirt26_mountp" warn
262 execute "mount $tmp_loopname $PATH_/minirt26_mountp" warn
263 execute "cp -a $INITRD_/* $PATH_/minirt26_mountp" warn
264 execute "umount $PATH_/minirt26_mountp" warn
265 execute "losetup -d $tmp_loopname &>/dev/null" warn
266 execute "rmdir $PATH_/minirt26_mountp" warn
267 execute "gzip -9 $TMP_DIR_/minirt26" warn
268 execute "rm -r $INITRD_" warn
269 execute "mv $TMP_DIR_/minirt26.gz $PATH_"
270 execute "rm -fr $TMP_DIR_"
275 # INTERACTIVE CONFIGURATION {{{
277 function actionInteractive
281 dprint "running in interactive mode"
283 local card_title_="Choose network device connected to client network"
284 local card_message_="Available network devices:"
285 local iprange_title_="IP Address range for clients"
286 local iprange_message_="
287 Please enter the desired IP-Range of addresses that should be allocated by clients, separated by a single space.
290 192.168.0.101 192.168.0.200
292 for addresses from 192.168.0.101 to (and including) 192.168.0.200.
295 local runconfig_title_="Networkcard config"
296 local runconfig_message_="Would you like to configure your interfaces now?"
297 local grub_title_="Grub configuration"
298 local grub_message_="Do you have any NON-PXE network cards you would like to boot from?"
300 # on witch interfaces should we listen
301 local netdevices_="$(grep -ve 'lo:' -ve 'Inter-|' -ve 'face |bytes' /proc/net/dev | awk -F: '{print $1}')"
302 local device_list_=""
303 for INTERFACE_ in $netdevices_; do device_list_="$device_list_ ${INTERFACE_} Networkcard_${INTERFACE_##eth}"; done
305 $DIALOG_ --backtitle "$BACK_TITLE_" --title "$card_title_" --menu "$card_message_" \
306 0 0 18 $device_list_ 2>"$TMP_" || warn "could not get network-interface"
307 INTERFACE_="$(<$TMP_)" ; echo -n "" >"$TMP_"
310 IP_=`netGetIp "$INTERFACE_" warn`
311 NETMASK_=`netGetNetmask "$INTERFACE_" warn`
312 netValidIp "$IP_" warn && break
313 $DIALOG_ --backtitle "$BACK_TITLE_" --title "$runconfig_title_" --yesno "$runconfig_message_" 18 45 && \
314 netcardconfig || die "Could not get interface" $?
317 IPRANGE_FROM_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMin/{print $2}'`
318 IPRANGE_TO_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/HostMax/{print $2}'`
319 NETWORK_=`execute "ipcalc -nb $IP_/$NETMASK_" warn |awk '/Network:/{print $2}'`
320 NETWORK_=${NETWORK_%/*}
322 while [ -z "$IPRANGE_FROM_" -o -z "$IPRANGE_TO_" -o -z "$iprange_" ]; do
323 iprange_="$IPRANGE_FROM_ $IPRANGE_TO_"
325 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$iprange_title_ ($INTERFACE_=$IP_/$NETMASK_)" \
326 --inputbox "$iprange_message_" 18 75 "$iprange_" 2>"$TMP_" || die "problems getting network range" $?
329 IPRANGE_FROM_="${iprange_%% *}"
330 IPRANGE_TO_="${iprange_##* }"
332 for i in "$IPRANGE_FROM_" "$IPRANGE_TO_"; do
333 netValidIp "$i" warn || iprange_=""
337 NAMESERVERS_=`netGetNameservers warn`
338 GW_=`netGetDefaultGateway warn`
339 GW_DEV_=`/sbin/ip route get "$GW_" | awk '{ print $3; exit; }'`
340 if [ "$GW_DEV_" != "$INTERFACE_" ] && [ "$GW_DEV_" != "" ]; then
341 # GW_DEV_ of server is not the same device as the one serviced by dhcpd
342 # so it doesn't make sense to provide the GW_ address to the clients
344 local do_nat_title_="Network Address Translation"
345 local do_nat_message_="
346 Do you want to set up NAT so that clients booting from this
347 grml-terminalserver can use this machine also as gateway to
350 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$do_nat_title_" --yesno "$do_nat_message_" 15 75 || \
352 if [ "$do_nat_" = "YES" ]; then
353 # user wants NAT, we give the clients the server address as
356 NAT_INTERFACE_="$GW_DEV_"
358 # no NAT, no sensible gateway
367 local grub_write_="YES"
368 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$grub_title_" --yesno "$grub_message_" 5 75 && \
369 grubConfig || grub_write_="NO"
373 #local OPT_IPTABLES_="yes"
374 #local OPT_SSH_="yes"
375 #local OPT_DISTCC_="yes"
378 local OPTIONS_TITLE_="Options"
379 local OPTIONS_MESSAGE_="Please give the appropriate options you want the clients to use:
381 grml2hd - Make a non-interactive remote installation
386 # local OPTIONS_MESSAGE_="Please give the appropriate options you want the clients to use:
388 #iptables - Only the server should be able to access the clients
389 #ssh - A ssh-key will be created on the server and distributed to the clients
390 #distcc - You want to use the clients as compile-farm (ssh options recommned)
394 local OPT_IPTABLES_DESC_="Start iptables on the clients"
395 local OPT_SSH_DESC_="Start ssh on the clients"
396 local OPT_DISTCC_DESC_="Start distcc on the clients"
397 local OPT_GRML2HD_DESC_="Remote install grml on the network clients"
398 # dialog options (enable if implemented)
399 #iptables "$OPT_IPTABLES_DESC_" off \
400 #ssh "$OPT_SSH_DESC_" off \
401 #distcc "$OPT_DISTCC_DESC_" off \
403 $DIALOG_ --clear --separate-output --backtitle "$BACK_TITLE_" --title "$OPTIONS_TITLE_" --checklist "$OPTIONS_MESSAGE_" 25 80 10 \
404 grml2hd "$OPT_GRML2HD_DESC_" off \
405 2>$TMP_ || die "could not get terminalserver options" $?
406 while read tmp_option_; do
407 OPTIONS_="$OPTIONS_ $tmp_option_"
411 for i in $OPTIONS_; do
413 grml2hd) optGrml2Hd || return 1 ;;
418 local OPTIONS_BOOTARG_MESSAGE_="Here you can add additional boot arguments for the clients seperated by spaces:
420 Quite usefull examples:
422 ssh=<pw> - Start ssh server and set password of user grml to pw
423 services=<1,2,3> - Execute /etc/init.d/{1,2,3} start
424 console=ttyS0,9600n8 - Initialise serial console
429 $DIALOG_ --clear --no-collapse --backtitle "$BACK_TITLE_" --title "$OPTIONS_TITLE_" --inputbox "$OPTIONS_BOOTARG_MESSAGE_" 0 0\
430 2>$TMP_ || die "problems getting additional boot arguments"
431 BOOT_ARGS_="$BOOT_ARGS_ $(<$TMP_)"
434 if [ $grub_write_ == "YES" ]; then
438 notice "GRML terminalserver successfully configured"
442 # OPTIONS GETTING DIALOG {{{
445 local GRML2HD_TITLE_='Grml2hd options dialog'
447 local options_='BOOT_IMAGE=grml2hd'
449 # get partition to install grml2hd on
450 OPTIONS_PARTITION_MSG_='Please give me the target partition where to install grml'
451 PARTITION_TITLE_='Partition selection'
453 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$PARTITION_TITLE_" --inputbox \
454 "$OPTIONS_PARTITION_MSG_" 0 75 '/dev/hda1' 2>$TMP_ || die "problems getting partition"
455 tmp_="partition=$(<$TMP_)"
456 options_="$options_ $tmp_"
458 # get filesystem type
459 OPTION_FS_TYPE_='Please give me the filesystem type'
460 FS_TITLE_='Filesystem selection'
462 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$FS_TITLE_" --inputbox \
463 "$OPTION_FS_TYPE_" 0 75 'ext3' 2>$TMP_ || die "problems getting filesystem type"
464 tmp_="filesystem=$(<$TMP_)"
465 options_="$options_ $tmp_"
467 # get where to save mbr
468 OPTION_MBR_='Please give me the location where to save the mbr'
469 MBR_TITLE_='Select location of mbr'
471 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$MBR_TITLE_" --inputbox \
472 "$OPTION_MBR_" 0 75 '/dev/hda' 2>$TMP_ || die "problems getting location where to write mbr"
474 options_="$options_ $tmp_"
477 OPTION_USER_='Who should be the first user on the system'
478 USER_TITLE_='User selection'
480 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$USER_TITLE_" --inputbox \
481 "$OPTION_USER_" 0 75 'grml' 2>$TMP_ || die "problems getting first user of system"
482 tmp_="user=$(<$TMP_)"
483 options_="$options_ $tmp_"
485 BOOT_ARGS_="$options_"
492 local tmp_=`mktemp -td terminalserver_grub.XXXXXX` || warn "could not create tmp file for grubConfig"
493 if [ -z "$tmp_" ]; then return 1; fi
495 grubConfigWork "$tmp_" "$1"
497 execute "rm -rf $tmp_" warn
502 function grubConfigWork
506 local grub_title_="Grub configuration"
507 local grub_nic_message_="
508 Give me the types of network cards you would want to boot from.
509 If you networkcard is already listed press <OK>!
511 WARNING: DON't add all NIC's, grub could possibly stop working!!"
514 if [[ $2 == "" ]]; then
515 while read module_ desc_ mode_; do
516 nics_="$nics_ $module_ $desc_ $mode_"
517 done < $GRUB_NIC_CONF_
521 $DIALOG_ --clear --separate-output --backtitle "$BACK_TITLE_" --title "$grub_title_" --checklist \
522 "$grub_nic_message_" 10 70 0 $nics_ 2>$TMP_
524 if [[ $ret_ -ne 0 || `wc -l <$TMP_` -eq 0 ]]; then
526 warn "you should give me your specified NICs but didn't => DEFAULT will be used"
527 awk '/ on$/{print $1}' $GRUB_NIC_CONF_ >$TMP_
532 while read module_; do
533 cmd_line_="$cmd_line_ --enable-$module_"
536 echo "\"$cmd_line_\""
538 execute "tar xzf $GRUB_SOURCE_ -C $tmp_" warn || return 1
539 execute "cd $tmp_/grub*" warn || return 1
540 cat >preset-menu <<EOT
541 # Set up the serial terminal, first of all.
542 #serial --unit=0 --speed=19200
543 #terminal --timeout=0 serial
545 # Initialize the network.
546 #dhcp --with-configfile
547 #bootp --with-configfile
551 title Load config from Tftp server
552 configfile (nd)/menu.lst
556 test -r /lib64 && export CC="$CC -m32"
558 CC="$CC" execute "./configure --enable-preset-menu=./preset-menu $cmd_line_" warn || read
559 CC="$CC" execute "make -j2" warn || read #return 1
561 local st1_="stage1/stage1"
562 local st2_="stage2/stage2"
563 isExistent $st1_ warn || return 1
564 isExistent $st2_ warn || return 1
566 if [ -r "$PATH_/grub.img" ] ; then
567 execute "mv -fb $PATH_/grub.img $PATH_/grub.img.old"
570 execute "cat $st1_ $st2_ > $PATH_/grub.img" warn || return 1
577 local grub_title_="Grub configuration"
578 $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "$grub_title_" --yesno \
579 "Do you want to write your grub image to /dev/fd0" 5 75 || return 1
581 execute "dd if=$PATH_/grub.img of=/dev/fd0" warn || return 0
585 function grubMultiWrite
587 local target_=${1:-'/dev/fd0'}
589 isExistent "$PATH_/grub.img" die "you must give me an image or run \"$PROG_NAME__ grubconf\""
590 isExistent "$target_" die "$target_ is not a valied"
592 echo -n "Please insert disk into $target_ and press <ENTER> (STRG-C for end)"
594 dd if=$PATH_/grub.img of="$target_"
600 function removeTmpFiles
602 execute "rm -f $TMP_" warn
607 for i in dhcpd.conf grub.img minirt26.gz; do
608 execute "rm -f $PATH_/$i*"
611 for i in $CARDS_DETECTED_BY_DISCOVER $CONF_FILE_; do
622 while getopts "i:hv" opt; do
624 i) interface_=$OPTARG ;;
625 h) printUsage; exit ;;
626 v) let verbose_=$verbose_+1 ;;
627 ?) printUsage; exit 64 ;;
630 shift $(($OPTIND - 1)) # set ARGV to the first not parsed commandline parameter
634 help) printUsage; exit 0 ;;
637 checkRoot die 'You have to be root to use this program'
640 execute "mkdir -p $PATH_" die
642 TMP_=`mktemp -t grml-terminalserver-config.XXXXXX` || die "Could not create tmpfile" $?
643 setExitFunction 'removeTmpFiles'
650 # MODULES_PATH_ROOT_DIFF_
653 if [[ $MODULES_PATH_ == "" || $KERNEL_VERSION_ == "" || $ORIGINAL_INITRD_ == "" ]]; then
654 warn "MODULES_PATH_=\"$MODULES_PATH_\" \
655 KERNEL_VERSION_=\"$KERNEL_VERSION_\" \
656 ORIGINAL_INITRD_=\"$ORIGINAL_INITRD_\""
657 die "False configuration, please update $CONFIG_"
662 interactive) actionInteractive ;;
663 grubConf) grubConfig "$2" ;;
664 grubConfWrite) grubConfig && grubWrite ;;
665 grubWrite) grubWrite ;;
666 grubMultiWrite) grubMultiWrite "$2" ;;
667 initrd) actionMkInitrd ;;
668 clean) actionClean ;;
669 *) actionInteractive ;;
674 ################################################################################
675 # vim:foldmethod=marker tabstop=2 expandtab shiftwidth=2