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