tazpanel view boot.cgi @ rev 491

boot.cgi: fix usb key list
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue May 12 16:54:32 2015 +0200 (2015-05-12)
parents 2874c858a6b2
children e6181f0521c8
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
42 cat <<EOT
43 <section>
44 <header>
45 $(_ 'System logs')
46 EOT
48 [ -w /etc/syslog.conf ] && cat <<EOT
49 <form action="index.cgi">
50 <input type="hidden" name="file" value="/etc/syslog.conf"/>
51 <button name="action" value="edit" data-icon="edit">syslog.conf</button>
52 </form>
53 EOT
55 cat <<EOT
56 </header>
58 <ul id="tabs">
59 EOT
60 for i in $(sed '/var\/log/!d;s|.*/log/||' /etc/syslog.conf); do
61 unset act
62 [ "$i" == "$logtype" ] && act=' class="active"'
63 cat <<EOT
64 <li$act><a href="?syslog=$i" title="$(sed "/$i$/!d;s/[\t ].*//" /etc/syslog.conf)">$i</a></li>
65 EOT
66 done
67 cat <<EOT
68 </ul>
70 <pre style="overflow-x: auto">$(syntax_highlighter kernel < /var/log/$logtype | \
71 loghead /var/log/$logtype)</pre>
72 </section>
73 EOT
74 ;;
77 *\ log\ *)
78 unset actboot actslim actxlog actkernel colors
79 case "$(GET log)" in
80 boot)
81 actboot=' class="active"'
82 output="$(filter_taztools_msgs < /var/log/boot.log)"
83 colors=' class="term log"';;
84 slim)
85 actslim=' class="active"'
86 output="$(loghead /var/log/slim.log htmlize)" ;;
87 xlog)
88 actxlog=' class="active"'
89 output="$(syntax_highlighter xlog < /var/log/Xorg.0.log | loghead /var/log/Xorg.0.log)" ;;
90 *)
91 actkernel=' class="active"'
92 output="$(syntax_highlighter kernel < /var/log/dmesg.log | loghead /var/log/dmesg.log)" ;;
93 esac
94 xhtml_header
95 cat <<EOT
96 <h2>$(_ 'Boot log files')</h2>
98 <ul id="tabs">
99 <li$actkernel><a href="?log=kernel">$(_ 'Kernel messages')</a></li>
100 <li$actboot ><a href="?log=boot" >$(_ 'Boot scripts' )</a></li>
101 <li$actxlog ><a href="?log=xlog" >$(_ 'X server' )</a></li>
102 <li$actslim ><a href="?log=slim" >$(_ 'X session' )</a></li>
103 </ul>
105 <section>
106 <div>
107 <pre$colors style="overflow-x: auto">$output</pre>
108 </div>
109 </section>
110 EOT
111 ;;
114 *\ daemons\ *)
115 #
116 # Everything until user login
117 #
118 # Start and stop a daemon.
119 # (I think we don't need a 'restart' since 2 clicks and you are done)
120 . /etc/rcS.conf
121 xhtml_header
123 cat <<EOT
124 <h2>$(_ 'Manage daemons')</h2>
126 <p>$(_ 'Check, start and stop daemons on SliTaz')</p>
127 EOT
128 daemon=$(GET daemons)
129 case "$daemon" in
130 start=*)
131 sleep 1
132 /etc/init.d/${daemon#start=} start | log ;;
133 stop=*)
134 /etc/init.d/${daemon#stop=} stop | log ;;
135 pid=*)
136 echo "<pre>"
137 ps ww | sed 1q
138 for i in $(echo ${daemon#pid=} | sed 's/%20/ /g'); do
139 ps ww | sed "/^ $i /!d"
140 done
141 echo "</pre>" ;;
142 esac
144 # Daemon list
145 cat <<EOT
146 <section>
147 <table class="zebra wide daemons">
148 <thead>
149 <tr>
150 <td>$(_ 'Name')</td>
151 <td>$(_ 'Description')</td>
152 <td>$(_ 'Configuration')</td>
153 <td>$(_ 'Status')</td>
154 <td>$(_ 'Action')</td>
155 <td>$(_ 'PID')</td>
156 </tr>
157 </thead>
158 <tbody>
159 EOT
160 cd /etc/init.d
161 list="$(ls | sed -e /.sh/d -e /rc./d -e /RE/d -e /daemon/d -e /firewall/d)"
162 for name in $list; do
163 unset pkg pid status SHORT_DESC boot cfg
164 echo '<tr>'
165 # Name
166 echo "<td>$name</td>"
167 # First check if daemon is started at boottime
168 [ echo "RUN_DAEMONS" | fgrep $name ] && boot="on boot"
169 # Standard SliTaz busybox daemons and firewall
170 echo -n "<td>"
171 grep -qi "^${name}_OPTIONS=" /etc/daemons.conf && cfg="options|$cfg"
172 for i in /etc/slitaz /etc /etc/$name ; do
173 [ -s $i/$name.conf ] && cfg="edit::$i/$name.conf|$cfg"
174 done
175 [ -n "$(which $name)" ] && cfg="man|help|$cfg"
176 case "$name" in
177 firewall)
178 _ 'SliTaz Firewall with iptable rules' ;;
179 httpd)
180 _ 'Small and fast web server with CGI support' ;;
181 ntpd)
182 cfg="man|help|edit::/etc/ntp.conf|options"
183 _ 'Network time protocol daemon' ;;
184 ftpd)
185 cfg="man|help|edit::/etc/inetd.conf"
186 _ 'Anonymous FTP server' ;;
187 udhcpd)
188 cfg="man|help|edit|options"
189 _ 'Busybox DHCP server' ;;
190 syslogd|klogd)
191 cfg="man|help|edit::/etc/syslog.conf|options"
192 _ 'Linux Kernel log daemon' ;;
193 crond)
194 # FIXME crontab
195 _ 'Execute scheduled commands' ;;
196 dnsd)
197 cfg="man|help|edit|options::-d"
198 _ 'Small static DNS server daemon' ;;
199 tftpd)
200 cfg="man|help|edit::/etc/inetd.conf|options"
201 _ 'Transfer a file on tftp request' ;;
202 lpd)
203 cfg="man|help|options"
204 _ 'Printer daemon' ;;
205 inetd)
206 _ 'Listen for network connections and launch programs' ;;
207 zcip)
208 cfg="man|help|edit:Script:/etc/zcip.script|options::eth0 /etc/zcip.script"
209 _ 'Manage a ZeroConf IPv4 link-local address' ;;
210 *)
211 # Description from receipt
212 [ -d "$LOCALSTATE/installed/$name" ] && pkg=$name
213 [ -d "$LOCALSTATE/installed/${name%d}" ] && pkg=${name%d}
214 [ -d "$LOCALSTATE/installed/${name}-pam" ] && pkg=${name}-pam
215 if [ "$pkg" ]; then
216 unset SHORT_DESC TAZPANEL_DAEMON
217 #FIXME $PKGS_DB
218 . $LOCALSTATE/installed/$pkg/receipt
219 echo -n "$SHORT_DESC"
220 cfg="${TAZPANEL_DAEMON:-$cfg|web::$WEB_SITE}"
221 else
222 echo -n "----"
223 fi ;;
224 esac
225 echo "</td>"
226 # Attempt to get daemon status
227 pidfile=$(find /var/run -name *$name*.pid)
228 [ "$pidfile" ] && pid=$(cat $pidfile)
229 # Dbus
230 [ -f /var/run/${name}/pid ] && pid=$(cat /var/run/${name}/pid)
231 # Apache
232 [ "$name" = "apache" ] && pid=$(cat /var/run/$name/httpd.pid)
233 # Pidof works for many daemons
234 [ "$pid" ] || pid=$(pidof $name)
236 echo -n "<td style='white-space: nowrap'>"
237 if [ -n "$cfg" ]; then
238 IFS="|"
239 for i in $cfg ; do
240 IFS=":"
241 set -- $i
242 case "$1" in
243 edit)
244 cat <<EOT
245 <a href="index.cgi?file=${3:-/etc/$name.conf}&amp;action=edit" title="${2:-$name Configuration} in ${3:-/etc/$name.conf}" data-img="conf"></a>
246 EOT
247 ;;
248 options)
249 key=$(echo -n $name | tr [a-z] [A-Z])_OPTIONS
250 cat <<EOT
251 <a href="index.cgi?file=/etc/daemons.conf&amp;action=setvar&amp;var=$key&amp;default=$3" title="${2:-$key}" data-img="opt"></a>
252 EOT
253 ;;
254 man)
255 cat <<EOT
256 <a href="index.cgi?exec=man ${3:-$name}&amp;back=boot.cgi%3Fdaemons" title="${2:-$name Manual}" data-img="man"></a>
257 EOT
258 ;;
259 help)
260 help='--help'
261 case $name in
262 cupsd|dropbear|gpm|slim|wpa_supplicant) help='-h'
263 esac
264 cat <<EOT
265 <a href="index.cgi?exec=$(which ${3:-$name}) $help&amp;back=boot.cgi%3Fdaemons" title="${2:-$name Help}" data-img="help"></a>
266 EOT
267 ;;
268 web) cat <<EOT
269 <a href="${i#$1:$2:}" title="${2:-$name website:} ${i#$1:$2:}" target="_blank" data-img="web"></a>
270 EOT
271 ;;
272 esac
273 done
274 fi
275 echo "</td>"
276 if [ "$pid" ]; then
277 cat <<EOT
278 <td><span title="$(_ 'Started')" data-img="on"></span></td>
279 <td><a href="?daemons=stop=$name" title="$(_ 'Stop')" data-img="stop"></a></td>
280 <td>
281 EOT
282 for i in $pid; do
283 cat <<EOT
284 <a href="?daemons=pid=$i">$i</a>
285 EOT
286 done
287 else
288 cat <<EOT
289 <td><span title="$(_ 'Stopped')" data-img="off"></span></td>
290 <td><a href="?daemons=start=$name" title="$(_ 'Start')" data-img="start"></a></td>
291 <td>-----
292 EOT
293 fi
294 echo '</td></tr>'
295 done
296 echo '</thead></table></section>' ;;
299 *\ grub\ *)
300 GRUBMENU="/boot/grub/menu.lst"
301 if [ "$(GET splash)" ]; then
302 default=$(GET default)
303 timeout=$(GET timeout)
304 splash=$(GET splash)
305 sed -i \
306 -e s"|default .*|default $default # new|" \
307 -e s"|timeout .*|timeout $timeout|" \
308 -e s"|splashimage=.*|splashimage=$splash|" \
309 $GRUBMENU
310 fi
311 default=$(cat $GRUBMENU | grep ^default | cut -d' ' -f2)
312 timeout=$(cat $GRUBMENU | grep ^timeout | cut -d' ' -f2)
313 splash=$(cat $GRUBMENU | grep ^splashimage | cut -d' ' -f2)
314 xhtml_header
315 cat <<EOT
316 <h2>$(_ 'GRUB Boot loader')</h2>
318 <p>$(_ 'The first application started when the computer powers on')</p>
320 <form class="wide">
321 <section>
322 <div>
323 <input type="hidden" name="grub"/>
324 <table>
325 <tr><td>$(_ 'Default entry:')</td>
326 <td><input type="text" name="default" value="${default##*=}"/></td></tr>
327 <tr><td>$(_ 'Timeout:')</td>
328 <td><input type="text" name="timeout" value="${timeout##*=}"/></td></tr>
329 <tr><td>$(_ 'Splash image:')</td>
330 <td><input type="text" name="splash" value="${splash##*=}" size="40"/></td></tr>
331 </table>
332 </div>
333 <footer>
334 <button type="submit" data-icon="ok">$(_ 'Change')</button>
335 </footer>
336 </section>
337 </form>
339 <form action="index.cgi">
340 <input type="hidden" name="file" value="$GRUBMENU"/>
341 <button data-icon="text">$(_ 'View or edit menu.lst')</button>
342 </form>
345 <section>
346 <header>$(_ 'Boot entries')</header>
347 <div>
348 EOT
351 menu=$(tail -q -n +$(grep -n ^title $GRUBMENU | head -n1 | cut -d: -f1) $GRUBMENU | \
352 sed -e "s|^$||g" | \
353 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" | \
354 sed '/^[ \t]*$/d' | \
355 tail -q -n +2)"</pre>"
357 entry='-1'
358 echo "$menu" | while read line
359 do
360 if [ -n "$(echo $line | grep '#</header>')" ]; then
361 entry=$(($entry + 1))
362 fi
363 echo $line | sed "s|#</header>|$entry</header>|"
364 done
366 echo '</section>'
369 # Here we could check if an entry for gpxe is present if not
370 # display a form to add it.
371 [ -f "/boot/gpxe" ] && cat <<EOT
372 <section>
373 <header>gPXE</header>
374 <div>$(_ 'Web boot is available with gPXE')</div>
375 </section>
376 EOT
377 ;;
380 *\ iso\ *)
381 xhtml_header
382 iso=$(POST iso); [ -s "$iso" ] || unset iso
383 action=$(POST action); [ "$action" ] || action=$(GET action)
384 workdir=$(POST workdir)
385 [ -d $workdir ] || workdir=$(dirname $workdir)
386 [ -w $workdir -a "$workdir" ] || workdir=/tmp
388 echo "<h2>$(_ 'ISO mine')</h2>"
390 [ "$iso" ] || msg err 'Invalid ISO image.'
392 if [ "$iso" -a "$action" -a "$action" != "nop" ]; then
393 case "$action" in
394 install*) dev=$(POST instdev) ;;
395 *) dev=$(POST usbkeydev) ;;
396 esac
397 cd $workdir
398 cat <<EOT
399 <section>
400 <pre>
401 $(taziso $iso $action $dev 2>&1)
402 </pre>
403 </section>
404 EOT
405 fi
406 cat <<EOT
407 <section>
408 <form method="post" action="?iso" class="wide">
409 EOT
410 cat <<EOT
411 <table>
412 <tr><td>ISO image file full path
413 <span data-img="info" title="set /dev/cdrom for a physical CD-ROM"></span>
414 </td>
415 <td><input type="text" name="iso" value="$iso" size="50"/></td></tr>
416 <tr><td>Working directory</td>
417 <td><input type="text" name="workdir" value="$workdir" size="50"/></td></tr>
418 <tr><td>Target partition
419 <span data-img="info" title="For hard disk installation only. Will create /slitaz tree and keep other files. No partitioning and no formatting."></span>
420 </td>
421 <td><select name="instdev">
422 <option value="/dev/null">Choose a partition (optional)</option>
423 EOT
424 blkid | grep -iE "(msdos|vfat|ntfs|ext[234]|xfs|btrfs)" | \
425 sed -e 's|[A-Z]*ID="[^"]*"||g;s| SEC[^ ]*||;s|LABEL=||;s|:||' \
426 -e 's|TYPE="\([^"]*\)"|\1|;s|/dev/||' | \
427 while read dev label type; do
428 echo -n "<option value=\"/dev/$dev\">/dev/$dev $label $type "
429 echo "$(blk2h < /sys/block/${dev:0:3}/$dev/size)</option>"
430 done
431 cat <<EOT
432 </select></td></tr>
433 <tr><td>USB key device
434 <span data-img="info" title="For USB boot key only. Will erase the full device."></span>
435 </td>
436 <td><select name="usbkeydev">
437 <option value="/dev/null">Choose a USB key (optional)</option>
438 EOT
439 grep -l 1 /sys/block/*/removable | \
440 sed 's|/sys/block/\(.*\)/removable|\1|' | while read dev; do
441 grep -qs 1 /sys/block/$DEV/ro && continue
442 [ -d /sys/block/$DEV/device/scsi_disk ] || continue
443 echo -n "<option value=\"/dev/$dev\">/dev/$dev "
444 echo "$(blk2h < /sys/block/$dev/size) $(cat \
445 /sys/block/$i/device/model 2>/dev/null)</option>"
446 done
447 cat <<EOT
448 </select></td></tr>
449 </table>
450 <footer>
451 EOT
453 if [ "$iso" ]; then
454 cat <<EOT
455 <select name="action">
456 <option value="nop">Choose an action</option>
457 $(taziso $iso list | sed -e \
458 's/"\(.*\)"[\t ]*"\(.*\)"/<option value="\1\">\2<\/option>/' -e \
459 "s|value=\"$action\"|& selected|")
460 </select>
461 EOT
462 elif [ "$action" ]; then
463 cat <<EOT
464 <input type="hidden" name="action" value="$action" />
465 EOT
466 fi
468 cat <<EOT
469 <button data-icon="cd" name="mine">Mine</button>
470 </footer>
471 </form>
472 </section>
473 EOT
474 ;;
477 *)
478 #
479 # Default content with summary
480 #
481 . /etc/rcS.conf
482 xhtml_header
483 cat <<EOT
484 <h2>$(_ 'Boot &amp; Start services')</h2>
486 <p>$(_ 'Everything that happens before user login')</p>
488 <form>
489 <button name="log" data-icon="logs" >$(_ 'Boot logs')</button>
490 <button name="syslog" data-icon="logs" >$(_ 'System logs')</button>
491 <button name="daemons" data-icon="daemons" data-root>$(_ 'Manage daemons')</button>
492 EOT
493 [ "$REMOTE_USER" == "root" -a -x /usr/bin/taziso ] && cat <<EOT
494 <button name="iso" data-icon="cd" >$(_ 'ISO mine')</button>
495 EOT
496 [ -w /boot/grub/menu.lst ] && cat <<EOT
497 <button name="grub" data-icon="grub" >$(_ 'Boot loader')</button>
498 EOT
499 cat <<EOT
500 </form>
503 <section>
504 <header>$(_ 'Configuration files')</header>
505 <form action="index.cgi" class="wide">
506 <table>
507 <tr><td>$(_ 'Main configuration file:') <b>rcS.conf</b></td>
508 <td><button name="file" value="/etc/rcS.conf" data-icon="view">$(_ 'View')</button></td></tr>
509 <tr><td>$(_ 'Login manager settings:') <b>slim.conf</b></td>
510 <td><button name="file" value="/etc/slim.conf" data-icon="view">$(_ 'View')</button></td></tr>
511 </table>
512 </form>
513 </section>
516 <section style="overflow-x: auto">
517 <header>$(_ 'Kernel cmdline')</header>
518 <pre>$(cat /proc/cmdline)</pre>
519 </section>
522 <section>
523 <header>
524 $(_ 'Local startup commands')
525 <form action="index.cgi">
526 <input type="hidden" name="file" value="/etc/init.d/local.sh"/>
527 EOT
528 [ -w /etc/init.d/local.sh ] && cat <<EOT
529 <button name="action" value="edit" data-icon="edit">$(_ 'Edit')</button>
530 EOT
531 cat <<EOT
532 </form>
533 </header>
534 <pre><code class="language-bash">$(cat /etc/init.d/local.sh | htmlize)</code></pre>
535 </section>
536 EOT
537 ;;
538 esac
540 xhtml_footer
541 exit 0