fixed bug with not stopping portmapper (additional killall -9)
[grml-terminalserver.git] / grml-terminalserver
1 #!/bin/sh
2 # Filename:      terminalserver
3 # Purpose:       Program to do something
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: Sat Aug 06 14:14:40 CEST 2005
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
31 ###
32 ### __FUNCTIONS
33 ###
34
35 function printUsage
36 {
37   cat <<EOT
38 Usage: "$PROG_NAME__" [OPTIONS] <command>
39
40 $PROG_NAME__ is the config program for the terminalserver
41 comming with grml.
42
43 COMMANDS:
44    help             This help text
45    start <service>  Start services
46    stop <service>   Stop services
47    clean            Stop all + remove config and boot files from services
48    <default>        interactive
49
50 SERVICES:
51   tftp        Tftp daemon
52   dhcp        Dhcp daemon
53   nfs         All necessary nfs daemons
54   <>          ALL services
55   
56 OPTIONS:
57    -v         verbose (show what is going on, v++)
58    -h         this help text
59
60 EOT
61 }
62
63 function killPortmapper
64 {
65     /etc/init.d/portmap stop >/dev/null 2>&1
66     killall -9 portmap
67 }
68
69 # DHCP SERVICE {{{
70 function createDhcpConf
71 {
72   execute "mv -fb \"$DHCPD_CONFIG_FILE_\" \"$DHCPD_CONFIG_FILE_.old\"" eprint &>/dev/null
73
74   execute "source $TEMPLATE_CONFIG_DIR_/dhcpd_config" die
75 }
76
77 function removeDhcpConf
78 {
79   rm -f "$DHCPD_CONFIG_FILE_"
80 }
81
82
83 function stopDhcp
84 {
85   start-stop-daemon --stop --quiet --pidfile "$DHCPD_PID_"
86   rm -f $DHCPD_PID_
87   rm -f /var/lib/dhcp3/dhcpd.leases* 2>/dev/null    #FIXME
88   touch /var/lib/dhcp3/dhcpd.leases
89 }
90 function startDhcp
91 {
92   local conf_file_="$DHCPD_CONFIG_FILE_"
93   
94   test -f $DHCPD_BIN_ || die "could not find dhcpd \"$DHCPD_BIN_\""
95   start-stop-daemon --start --quiet --pidfile "$DHCPD_PID_" \
96     --exec "$DHCPD_BIN_" -- -cf "$conf_file_" -q "$INTERFACE_" || warn "problems starting dhcpd"
97 }
98 function runDhcp
99 {
100   isExistent "$DHCPD_CONFIG_FILE_" || \
101     warn "no config for dhcpd: \"$DHCPD_CONFIG_FILE_\" => not starting dhcpd" || return 1
102
103   stopDhcp
104   sleep 1
105   startDhcp
106 }
107 # }}}
108
109 # TFTP SERVICE {{{
110 function removeTftpConf
111 {
112   rm -rf $TFTPD_DATA_DIR_/*
113 }
114 function createTftpConf
115 {
116   removeTftpConf
117   
118   execute "mkdir $TFTPD_DATA_DIR_/pxelinux.cfg" die
119   execute "install -m 644 /usr/lib/syslinux/pxelinux.0 $TFTPD_DATA_DIR_" die
120   execute "install -m 644 $PATH_/minirt26.gz $TFTPD_DATA_DIR_" die
121   execute "install -m 644 $KERNEL_IMAGE_ $TFTPD_DATA_DIR_/linux26" die
122   execute "install -m 644 $MEMTEST_IMAGE_ $TFTPD_DATA_DIR_/memtest" die
123   execute "install -m 644 $PXE_BOOT_MSG_ $TFTPD_DATA_DIR_" die
124   execute "install -m 644 $PXE_BOOT_LOGO_ $TFTPD_DATA_DIR_" die
125
126   execute "source $TEMPLATE_CONFIG_DIR_/grub-pxelinux_config" die
127 }
128
129 function stopTftp
130 {
131   start-stop-daemon --stop --quiet --name "${TFTPD_BIN_##*/}"
132 }
133 function startTftp
134 {
135   test -f $TFTPD_BIN_ || die "could not find \"$TFTPD_BIN_\""
136   start-stop-daemon --start --quiet --exec "$TFTPD_BIN_" -- -l -a "$IP_" -s "$TFTPD_DATA_DIR_" || \
137     warn "problems starting tftpd server"
138 }
139 function runTftp
140 {
141   stopTftp
142   sleep 1
143   startTftp
144 }
145 # }}}
146
147
148 # NFS  {{{
149 function startNfs
150 {
151   /etc/init.d/portmap start
152   /etc/init.d/nfs-common start
153   # FIXME /etc/init.d/nfs-kernel-server start
154   $USR_SHARE_/nfs-kernel-server start
155
156   # FIXME Silly "/etc/init.d/nfs-kernel-server start"
157   # FIXME #246904 (init script does not start if no exports in /etc/exports)
158   execute "exportfs -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
159   #execute "echo -e \"\n$MOUNT_POINT_   $NETWORK_/$NETMASK_(ro,no_root_squash,async)\" >> /etc/exports" warn
160 }
161 function stopNfs
162 {
163   execute "exportfs -u -o ro,no_root_squash,async,nohide $NETWORK_/$NETMASK_:$MOUNT_POINT_" warn
164   if [[ `exportfs |wc -l` > 0 ]]; then
165     dprint "There are other exports, not stopping NFS serivces"
166   else
167     /etc/init.d/nfs-kernel-server stop >/dev/null 2>&1
168     /etc/init.d/nfs-common stop >/dev/null 2>&1
169     killPortmapper
170   fi
171 }
172 # }}}
173
174 function allreadyConfigured
175 {
176   isExistent "$CONF_FILE_" dprint || return 1
177   return 0
178 }
179
180 function createConfig
181 {
182   # FIXME
183   execute "sed -i 's/^ALL/\#ALL/' /etc/hosts.deny" warn
184   execute "sed -i 's/^ALL : ALL@ALL : DENY/ALL : ALL@ALL : ALLOW/' /etc/hosts.allow" warn
185
186   createTftpConf
187   createDhcpConf
188 }
189
190 function actionStart
191 {
192   createConfig
193   
194   echo -n "Starting tftpd..."
195   runTftp
196   echo "done"
197   echo -n "Starting dhcpd..."
198   runDhcp
199   echo "done"
200   echo -n "Starting nfs..."
201   startNfs
202   echo "done"
203 }
204
205 function actionStop
206 {
207   stopTftp
208   stopDhcp
209   stopNfs
210   notice "Terminal-server stopped"
211 }
212
213 function actionClean
214 {
215   actionStop
216   removeTftpConf
217   removeDhcpConf
218   stopNfs
219 }
220
221 # SERVICES {{{
222 function serviceStart
223 {
224   local service_="$1"   # service to start, if nothing => all
225
226   case "$service_" in
227     "") actionStart ;;
228     tftp) runTftp ;;
229     dhcp) runDhcp ;;
230     nfs) startNfs ;;
231     *) warn "Service $service_ not available" ;;
232   esac
233 }
234
235 function serviceStop
236 {
237   local service_="$1"   # service to stop, if nothing => all
238
239   case "$service_" in
240     "") actionStop ;;
241     tftp) stopTftp ;;
242     dhcp) stopDhcp ;;
243     nfs) stopNfs ;;
244     *) warn "Service $service_ not available" ;;
245   esac
246 }
247 # }}}
248
249 ###
250 ### __MAIN
251 ###
252
253 while getopts "i:hv" opt; do
254   case "$opt" in
255     h) printUsage; exit ;;
256     v) let verbose_=$verbose_+1 ;;
257     ?) printUsage; exit 64 ;;
258   esac
259 done
260 shift $(($OPTIND - 1))  # set ARGV to the first not parsed commandline parameter
261 setVerbose $verbose_
262
263 case "$1" in
264   help)   printUsage; exit 0 ;;
265 esac
266
267 checkRoot die "You have to be root to use this program"
268 disableSyslog
269
270 isExistent $DEFAULT_CONFIG_ die
271 . $DEFAULT_CONFIG_
272 . $CONFIG_
273 # used config vars:
274 # MOUNT_POINT_
275 # KERNEL_IMAGE_
276 # MEMTEST_IMAGE_
277 # PXE_BOOT_MSG_
278 # PXE_BOOT_LOGO_
279 if [[ $MOUNT_POINT_ == "" || $KERNEL_IMAGE_ == "" || $MEMTEST_IMAGE_ == "" || \
280   $PXE_BOOT_MSG_ == "" || $PXE_BOOT_MSG_ == "" ]]; then
281   warn "MOUNT_POINT_=\"$MOUNT_POINT_\" \
282 KERNEL_IMAGE_=\"$KERNEL_IMAGE_\" \
283 MEMTEST_IMAGE_=\"$MEMTEST_IMAGE_\"
284 PXE_BOOT_MSG_=\"$PXE_BOOT_MSG_\"
285 PXE_BOOT_LOGO_=\"$PXE_BOOT_LOGO_\""
286   die "False configuration, please update $CONFIG_"
287 fi
288   
289 case "$1" in
290   clean) actionClean; exit 0 ;;
291 esac
292
293
294 while true; do
295   isExistent "$CONF_FILE_" warn "sorry configfile \"$CONF_FILE_\" not found"
296   if [ $? -eq 1 ]; then
297     $CONFIG_PROG_ && continue
298     $DIALOG_ --clear --backtitle "$BACK_TITLE_" --title "Question" --yesno \
299       "grml-terminalserver-config returned an error, do you want to quit now?" 5 75 && exit 1
300   else
301     break
302   fi  
303 done
304 source $CONF_FILE_
305
306 # check for necessary files
307 check_necessary_files_='no'
308 if [ "$1" == 'start' ]; then
309   case "$2" in
310     tftp|"") check_necessary_files_='yes' ;;
311   esac
312 fi
313 if [ $check_necessary_files_ == 'yes' ]; then
314   # test for files absolutly necessary for grml-terminalserver and created from -config
315   problem_=0
316   for i in $PATH_/minirt26.gz; do
317     isExistent $i warn || problem_=1
318   done
319   if [ $problem_ -eq 1 ]; then
320     die 'Some necessary files are missing, please rerun grml-terminalserver-config, or copy the files manually'
321   fi
322 fi
323
324
325 case "$1" in
326   start) serviceStart "$2" ;;
327   stop) serviceStop "$2" ;;
328   "")  actionStart ;;
329   *)  printUsage ;;
330 esac
331
332 # END OF FILE
333 ################################################################################
334 # vim:foldmethod=marker