tazpanel view boot.cgi @ rev 456

boot.cgi: ISO mine: "table"izing input form.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Mon Apr 13 01:44:08 2015 +0300 (2015-04-13)
parents 1bc6783d2671
children 6bd26fb55b06
line source
1 #!/bin/sh
2 #
3 # Boot CGI script - All what happens before login (grub, rcS, slim)
4 #
5 # Copyright (C) 2011-2015 SliTaz GNU/Linux - BSD License
6 #
9 # Common functions from libtazpanel and source main boot config file.
11 . lib/libtazpanel
12 . /etc/rcS.conf
13 get_config
14 header
16 TITLE=$(_ 'TazPanel - Boot')
19 # Print last 40 lines of given file with "more" link
21 loghead() {
22 case $2 in
23 htmlize) tail -n40 $1 | htmlize;;
24 *) tail -n40;;
25 esac
26 [ $(wc -l < $1) -gt 40 ] && cat <<EOT
27 <hr/><a data-icon="view" href="index.cgi?file=$1">$(_ 'Show more...')</a>
28 EOT
29 }
32 #
33 # Commands
34 #
36 case " $(GET) " in
37 *\ syslog\ *)
38 logtype="$(GET syslog)"
39 [ "${logtype:-syslog}" == "syslog" ] && logtype=messages
40 xhtml_header
41 [ -w /etc/syslog.conf ] && cat <<EOT
42 <a href="index.cgi?file=/etc/syslog.conf&amp;action=edit" data-img="conf"></a>syslog.conf
43 EOT
44 cat <<EOT
45 <h2>$(_ 'System logs')</h2>
47 <ul id="tabs">
48 EOT
49 for i in $(sed '/var\/log/!d;s|.*/log/||' /etc/syslog.conf); do
50 unset act
51 [ "$i" == "$logtype" ] && act=' class="active"'
52 cat <<EOT
53 <li$act><a href="?syslog=$i" title="$(sed "/$i$/!d;s/[\t ].*//" /etc/syslog.conf)">$i</a></li>
54 EOT
55 done
56 cat <<EOT
57 </ul>
59 <section>
60 <div>
61 <pre>$(syntax_highlighter kernel < /var/log/$logtype | \
62 loghead /var/log/$logtype)</pre>
63 </div>
64 </section>
65 EOT
66 ;;
67 *\ log\ *)
68 unset actboot actslim actxlog actkernel colors
69 case "$(GET log)" in
70 boot)
71 actboot=' class="active"'
72 output="$(filter_taztools_msgs < /var/log/boot.log)"
73 colors=' class="term log"';;
74 slim)
75 actslim=' class="active"'
76 output="$(loghead /var/log/slim.log htmlize)" ;;
77 xlog)
78 actxlog=' class="active"'
79 output="$(syntax_highlighter xlog < /var/log/Xorg.0.log | loghead /var/log/Xorg.0.log)" ;;
80 *)
81 actkernel=' class="active"'
82 output="$(syntax_highlighter kernel < /var/log/dmesg.log | loghead /var/log/dmesg.log)" ;;
83 esac
84 xhtml_header
85 cat <<EOT
86 <h2>$(_ 'Boot log files')</h2>
88 <ul id="tabs">
89 <li$actkernel><a href="?log=kernel">$(_ 'Kernel messages')</a></li>
90 <li$actboot ><a href="?log=boot" >$(_ 'Boot scripts' )</a></li>
91 <li$actxlog ><a href="?log=xlog" >$(_ 'X server' )</a></li>
92 <li$actslim ><a href="?log=slim" >$(_ 'X session' )</a></li>
93 </ul>
95 <section>
96 <div>
97 <pre$colors>$output</pre>
98 </div>
99 </section>
100 EOT
101 ;;
104 *\ daemons\ *)
105 #
106 # Everything until user login
107 #
108 # Start and stop a daemon.
109 # (I think we don't need a 'restart' since 2 clicks and you are done)
110 . /etc/rcS.conf
111 xhtml_header
113 cat <<EOT
114 <h2>$(_ 'Manage daemons')</h2>
116 <p>$(_ 'Check, start and stop daemons on SliTaz')</p>
117 EOT
118 daemon=$(GET daemons)
119 case "$daemon" in
120 start=*)
121 sleep 1
122 /etc/init.d/${daemon#start=} start | log ;;
123 stop=*)
124 /etc/init.d/${daemon#stop=} stop | log ;;
125 pid=*)
126 echo "<pre>"
127 ps ww | sed 1q
128 for i in $(echo ${daemon#pid=} | sed 's/%20/ /g'); do
129 ps ww | sed "/^ $i /!d"
130 done
131 echo "</pre>" ;;
132 esac
134 # Daemon list
135 cat <<EOT
136 <section>
137 <table class="zebra wide daemons">
138 <thead>
139 <tr>
140 <td>$(_ 'Name')</td>
141 <td>$(_ 'Description')</td>
142 <td>$(_ 'Configuration')</td>
143 <td>$(_ 'Status')</td>
144 <td>$(_ 'Action')</td>
145 <td>$(_ 'PID')</td>
146 </tr>
147 </thead>
148 <tbody>
149 EOT
150 cd /etc/init.d
151 list="$(ls | sed -e /.sh/d -e /rc./d -e /RE/d -e /daemon/d -e /firewall/d)"
152 for name in $list; do
153 unset pkg pid status SHORT_DESC boot cfg
154 echo '<tr>'
155 # Name
156 echo "<td>$name</td>"
157 # First check if daemon is started at boottime
158 [ echo "RUN_DAEMONS" | fgrep $name ] && boot="on boot"
159 # Standard SliTaz busybox daemons and firewall
160 echo -n "<td>"
161 grep -qi "^${name}_OPTIONS=" /etc/daemons.conf && cfg="options|$cfg"
162 for i in /etc/slitaz /etc /etc/$name ; do
163 [ -s $i/$name.conf ] && cfg="edit::$i/$name.conf|$cfg"
164 done
165 [ -n "$(which $name)" ] && cfg="man|help|$cfg"
166 case "$name" in
167 firewall)
168 _ 'SliTaz Firewall with iptable rules' ;;
169 httpd)
170 _ 'Small and fast web server with CGI support' ;;
171 ntpd)
172 _ 'Network time protocol daemon' ;;
173 ftpd)
174 cfg="man|help|edit::/etc/inetd.conf"
175 _ 'Anonymous FTP server' ;;
176 udhcpd)
177 _ 'Busybox DHCP server' ;;
178 syslogd|klogd)
179 _ 'Linux Kernel log daemon' ;;
180 crond)
181 # FIXME crontab
182 _ 'Execute scheduled commands' ;;
183 dnsd)
184 cfg="man|help|edit|options::-d"
185 _ 'Small static DNS server daemon' ;;
186 tftpd)
187 cfg="man|help|edit::/etc/inetd.conf"
188 _ 'Transfer a file on tftp request' ;;
189 inetd)
190 _ 'Listen for network connections and launch programs' ;;
191 zcip)
192 cfg="man|help|edit:Script:/etc/zcip.script|options::eth0 /etc/zcip.script"
193 _ 'Manage a ZeroConf IPv4 link-local address' ;;
194 *)
195 # Description from receipt
196 [ -d "$LOCALSTATE/installed/$name" ] && pkg=$name
197 [ -d "$LOCALSTATE/installed/${name%d}" ] && pkg=${name%d}
198 [ -d "$LOCALSTATE/installed/${name}-pam" ] && pkg=${name}-pam
199 if [ "$pkg" ]; then
200 unset SHORT_DESC TAZPANEL_DAEMON
201 #FIXME $PKGS_DB
202 . $LOCALSTATE/installed/$pkg/receipt
203 echo -n "$SHORT_DESC"
204 cfg="${TAZPANEL_DAEMON:-$cfg|web::$WEB_SITE}"
205 else
206 echo -n "----"
207 fi ;;
208 esac
209 echo "</td>"
210 # Attempt to get daemon status
211 pidfile=$(find /var/run -name *$name*.pid)
212 [ "$pidfile" ] && pid=$(cat $pidfile)
213 # Dbus
214 [ -f /var/run/${name}/pid ] && pid=$(cat /var/run/${name}/pid)
215 # Apache
216 [ "$name" = "apache" ] && pid=$(cat /var/run/$name/httpd.pid)
217 # Pidof works for many daemons
218 [ "$pid" ] || pid=$(pidof $name)
220 echo -n "<td style='white-space: nowrap'>"
221 if [ -n "$cfg" ]; then
222 IFS="|"
223 for i in $cfg ; do
224 IFS=":"
225 set -- $i
226 case "$1" in
227 edit)
228 cat <<EOT
229 <a href="index.cgi?file=${3:-/etc/$name.conf}&amp;action=edit" title="${2:-$name Configuration}" data-img="conf"></a>
230 EOT
231 ;;
232 options)
233 key=$(echo -n $name | tr [a-z] [A-Z])_OPTIONS
234 cat <<EOT
235 <a href="index.cgi?file=/etc/daemons.conf&amp;action=setvar&amp;var=$key&amp;default=$3" title="${2:-$key}" data-img="opt"></a>
236 EOT
237 ;;
238 man)
239 cat <<EOT
240 <a href="index.cgi?exec=man ${3:-$name}&amp;back=boot.cgi%3Fdaemons" title="${2:-$name Manual}" data-img="man"></a>
241 EOT
242 ;;
243 help)
244 help='--help'
245 case $name in
246 cupsd|dropbear|gpm|slim|wpa_supplicant) help='-h'
247 esac
248 cat <<EOT
249 <a href="index.cgi?exec=$(which ${3:-$name}) $help&amp;back=boot.cgi%3Fdaemons" title="${2:-$name Help}" data-img="help"></a>
250 EOT
251 ;;
252 web) cat <<EOT
253 <a href="${i#$1:$2:}" title="${2:-$name website:} ${i#$1:$2:}" target="_blank" data-img="web"></a>
254 EOT
255 ;;
256 esac
257 done
258 fi
259 echo "</td>"
260 if [ "$pid" ]; then
261 cat <<EOT
262 <td><span title="$(_ 'Started')" data-img="on"></span></td>
263 <td><a href="?daemons=stop=$name" title="$(_ 'Stop')" data-img="stop"></a></td>
264 <td>
265 EOT
266 for i in $pid; do
267 cat <<EOT
268 <a href="?daemons=pid=$i">$i</a>
269 EOT
270 done
271 else
272 cat <<EOT
273 <td><span title="$(_ 'Stopped')" data-img="off"></span></td>
274 <td><a href="?daemons=start=$name" title="$(_ 'Start')" data-img="start"></a></td>
275 <td>-----
276 EOT
277 fi
278 echo '</td></tr>'
279 done
280 echo '</thead></table></section>' ;;
283 *\ grub\ *)
284 GRUBMENU="/boot/grub/menu.lst"
285 if [ "$(GET splash)" ]; then
286 default=$(GET default)
287 timeout=$(GET timeout)
288 splash=$(GET splash)
289 sed -i \
290 -e s"|default .*|default $default # new|" \
291 -e s"|timeout .*|timeout $timeout|" \
292 -e s"|splashimage=.*|splashimage=$splash|" \
293 $GRUBMENU
294 fi
295 default=$(cat $GRUBMENU | grep ^default | cut -d' ' -f2)
296 timeout=$(cat $GRUBMENU | grep ^timeout | cut -d' ' -f2)
297 splash=$(cat $GRUBMENU | grep ^splashimage | cut -d' ' -f2)
298 xhtml_header
299 cat <<EOT
300 <h2>$(_ 'GRUB Boot loader')</h2>
302 <p>$(_ 'The first application started when the computer powers on')</p>
304 <form class="wide">
305 <section>
306 <div>
307 <input type="hidden" name="grub"/>
308 <table>
309 <tr><td>$(_ 'Default entry:')</td>
310 <td><input type="text" name="default" value="${default##*=}"/></td></tr>
311 <tr><td>$(_ 'Timeout:')</td>
312 <td><input type="text" name="timeout" value="${timeout##*=}"/></td></tr>
313 <tr><td>$(_ 'Splash image:')</td>
314 <td><input type="text" name="splash" value="${splash##*=}" size="40"/></td></tr>
315 </table>
316 </div>
317 <footer>
318 <button type="submit" data-icon="ok">$(_ 'Change')</button>
319 </footer>
320 </section>
321 </form>
323 <form action="index.cgi">
324 <input type="hidden" name="file" value="$GRUBMENU"/>
325 <button data-icon="text">$(_ 'View or edit menu.lst')</button>
326 </form>
329 <section>
330 <header>$(_ 'Boot entries')</header>
331 <div>
332 EOT
335 menu=$(tail -q -n +$(grep -n ^title $GRUBMENU | head -n1 | cut -d: -f1) $GRUBMENU | \
336 sed -e "s|^$||g" | \
337 sed -e "s|^title|</pre></div>\n</section>\n\n<section>\n\t<header>$(_ 'Entry') #</header>\n<div><pre style=\"white-space:pre-wrap\">\0|g" | \
338 sed '/^[ \t]*$/d' | \
339 tail -q -n +2)"</pre>"
341 entry='-1'
342 echo "$menu" | while read line
343 do
344 if [ -n "$(echo $line | grep '#</header>')" ]; then
345 entry=$(($entry + 1))
346 fi
347 echo $line | sed "s|#</header>|$entry</header>|"
348 done
350 echo '</section>'
353 # Here we could check if an entry for gpxe is present if not
354 # display a form to add it.
355 [ -f "/boot/gpxe" ] && cat <<EOT
356 <section>
357 <header>gPXE</header>
358 <div>$(_ 'Web boot is available with gPXE')</div>
359 </section>
360 EOT
361 ;;
364 *\ iso\ *)
365 xhtml_header
366 iso=$(POST iso); [ -s "$iso" ] || unset iso
367 action=$(POST action); [ "$action" ] || action=$(GET action)
368 workdir=$(POST workdir)
369 [ -d $workdir ] || workdir=$(dirname $workdir)
370 [ -w $workdir -a "$workdir" ] || workdir=/tmp
372 echo "<h2>$(_ 'ISO mine')</h2>"
374 [ "$iso" ] || msg err 'Invalid ISO image.'
376 if [ "$iso" -a "$action" -a "$action" != "nop" ]; then
377 case "$action" in
378 install*) dev=$(POST instdev) ;;
379 *) dev=$(POST usbkeydev) ;;
380 esac
381 cd $workdir
382 cat <<EOT
383 <section>
384 <pre>
385 $(taziso $iso $action $dev 2>&1)
386 </pre>
387 </section>
388 EOT
389 fi
390 cat <<EOT
391 <section>
392 <form method="post" action="?iso" class="wide">
393 EOT
394 cat <<EOT
395 <table>
396 <tr><td>ISO image file full path
397 <span data-img="info" title="set /dev/cdrom for a physical CD-ROM"></span>
398 </td>
399 <td><input type="text" name="iso" value="$iso" size="50"/></td></tr>
400 <tr><td>Working directory</td>
401 <td><input type="text" name="workdir" value="$workdir"/></td></tr>
402 <tr><td>Windows partition</td>
403 <td><select name="instdev">
404 <option value="/dev/null">Choose a partition (optional)</option>
405 EOT
406 blkid | grep -iE "(msdos|vfat|ntfs)" | \
407 sed 's|^/dev/\(.*\):.*LABEL="\([^"]*\).*|\1 "\2"|' | \
408 while read dev label; do
409 echo -n "<option value=\"/dev/$dev\">/dev/$dev $label "
410 echo "$(($(cat /sys/block/${dev:0:3}/$dev/size)/2048))MB</option>"
411 done
412 cat <<EOT
413 </select></td></tr>
414 <tr><td>USB key device</td>
415 <td><select name="usbkeydev">
416 <option value="/dev/null">Choose a USB key (optional)</option>
417 EOT
418 grep -l 1 /sys/block/*/removable | \
419 sed 's|/sys/block/\(.*\)/removable|\1|' | while read dev; do
420 grep -qs 1 /sys/block/$DEV/ro && continue
421 echo -n "<option value=\"/dev/$dev\">/dev/$dev "
422 echo "$(($(cat /sys/block/$dev/size)/2048))MB $(cat \
423 /sys/block/$i/device/model 2>/dev/null)</option>"
424 done
425 cat <<EOT
426 </select></td></tr>
427 </table>
428 <footer>
429 EOT
431 if [ "$iso" ]; then
432 cat <<EOT
433 <select name="action">
434 <option value="nop">Choose an action</option>
435 $(taziso $iso list | sed -e \
436 's/"\(.*\)"[\t ]*"\(.*\)"/<option value="\1\">\2<\/option>/' -e \
437 "s|value=\"$action\"|& selected|")
438 </select>
439 EOT
440 elif [ "$action" ]; then
441 cat <<EOT
442 <input type="hidden" name="action" value="$action" />
443 EOT
444 fi
446 cat <<EOT
447 <button data-icon="cd" name="mine">Mine</button>
448 </footer>
449 </form>
450 </section>
451 EOT
452 ;;
455 *)
456 #
457 # Default content with summary
458 #
459 . /etc/rcS.conf
460 xhtml_header
461 cat <<EOT
462 <h2>$(_ 'Boot &amp; Start services')</h2>
464 <p>$(_ 'Everything that happens before user login')</p>
466 <form>
467 <button name="log" data-icon="logs" >$(_ 'Boot logs')</button>
468 <button name="syslog" data-icon="logs" >$(_ 'System logs')</button>
469 <button name="daemons" data-icon="daemons" data-root>$(_ 'Manage daemons')</button>
470 EOT
471 [ "$REMOTE_USER" == "root" -a -x /usr/bin/taziso ] && cat <<EOT
472 <button name="iso" data-icon="cd" >$(_ 'ISO mine')</button>
473 EOT
474 [ -w /boot/grub/menu.lst ] && cat <<EOT
475 <button name="grub" data-icon="grub" >$(_ 'Boot loader')</button>
476 EOT
477 cat <<EOT
478 </form>
481 <section>
482 <header>$(_ 'Configuration files')</header>
483 <form action="index.cgi" class="wide">
484 <table>
485 <tr><td>$(_ 'Main configuration file:') <b>rcS.conf</b></td>
486 <td><button name="file" value="/etc/rcS.conf" data-icon="view">$(_ 'View')</button></td></tr>
487 <tr><td>$(_ 'Login manager settings:') <b>slim.conf</b></td>
488 <td><button name="file" value="/etc/slim.conf" data-icon="view">$(_ 'View')</button></td></tr>
489 </table>
490 </form>
491 </section>
494 <section style="overflow-x: auto">
495 <header>$(_ 'Kernel cmdline')</header>
496 <pre>$(cat /proc/cmdline)</pre>
497 </section>
500 <section>
501 <header>
502 $(_ 'Local startup commands')
503 <form action="index.cgi">
504 <input type="hidden" name="file" value="/etc/init.d/local.sh"/>
505 EOT
506 [ -w /etc/init.d/local.sh ] && cat <<EOT
507 <button name="action" value="edit" data-icon="edit">$(_ 'Edit')</button>
508 EOT
509 cat <<EOT
510 </form>
511 </header>
512 <pre>$(cat /etc/init.d/local.sh | syntax_highlighter sh)</pre>
513 </section>
514 EOT
515 ;;
516 esac
518 xhtml_footer
519 exit 0