seb view seb @ rev 24

Add a CGI web frontend
author Christophe Lincoln <pankso@slitaz.org>
date Sun Mar 12 04:47:04 2017 +0100 (2017-03-12)
parents e7d8e4a84d42
children cb51498760ab
line source
1 #!/bin/sh
2 #
3 # SEB - SliTaz Embedded Builder - Back to the roots with SliTaz in
4 # only a few MB. Please read the README file for more information.
5 #
6 # Copyright (C) 2017 SliTaz GNU/Linux - BSD License
7 # Author: Christophe Lincoln <pankso@slitaz.org>
8 #
9 . /lib/libtaz.sh
10 unset status get_cols separator colorize boldify
12 # --iso --work --linux can be set in config file or from cmdline
13 [ -f "build.conf" ] && . build.conf
15 [ "$work" ] || work="$(pwd)"
16 [ "$iso" ] || iso="seb-os-$(date '+%Y%m%d').iso"
17 [ "$linux" ] || linux="/boot/vmlinuz-$(uname -r)"
18 [ "$port" ] || port="8940"
20 rootfs="$work/rootfs"
21 rootiso="$work/rootiso"
22 sebfs="$work/sebfs"
23 sebpkgs="$work/sebpkgs"
24 cache="$work/cache"
25 vdisk="$work/sebhome.img"
26 qemu_opts="-m 512"
28 tools="/usr/share/seb/tools"
29 initfs="/usr/share/seb/initfs"
30 packages="/usr/share/seb/packages"
32 # Working from source tree
33 [ -d "tools" ] && tools="$(pwd)/tools"
34 [ -d "initfs" ] && initfs="$(pwd)/initfs"
35 [ -d "packages" ] && packages="$(pwd)/packages"
37 # Source libseb.sh (overwrite libtaz.sh function)
38 if [ -f "$PWD/libseb.sh" ]; then
39 libseb="$PWD/libseb.sh"
40 . ${libseb}
41 else
42 if ! . /lib/libseb.sh; then
43 echo "Can't source any: libseb.sh"; exit 1
44 fi
45 libseb="/lib/libseb.sh"
46 fi
47 debug "work=$work"
49 #
50 # Functions
51 #
53 help() {
54 cat << EOT
56 $(colorize 35 "SliTaz Embedded Builder")
58 $(boldify "Usage:") $(basename $0) [command] [--opts]
59 $(boldify "Manual:") man seb
61 $(boldify "Commands:")
62 -h help Display this short built-in help
63 -i init Create base files to customize
64 -b build Generate a distribution (initramfs & iso)
65 -p packages Handle packages: --list --add
66 -g geniso Re-generate the ISO image
67 -c clean Remove all SEB generated files
68 -l lsfs List all files in rootfs (-type f)
69 -v vdisk Create, mount or unmount a virtual disk
70 -e emu Emulate ISO image with Qemu
71 -w web Start seb graphical web interface
72 env Print current seb environment
74 $(boldify "Options:")
75 --work= Path to build directory
76 --iso= Specify SliTaz Embedded ISO image name
77 --linux= Path to a custom Linux kernel
78 --port= Httpd port for the web interface
79 --all Clean all files including sebfs
80 --emu Emulate ISO image after build
81 --check Check a virtual disk image
83 EOT
84 }
86 # Initial files which can be modified via sebfs/
87 init() {
88 mkdir -p ${sebfs} ${sebpkgs}
89 cp -rf ${initfs}/* ${sebfs}
90 }
92 geniso() {
93 echo -n "Generating ISO image: ${iso}"
94 cd ${work}
95 genisoimage -R -o ${iso} \
96 -b boot/isolinux/isolinux.bin \
97 -c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
98 -V "SliTaz Embedded" -input-charset iso8859-1 \
99 -boot-info-table rootiso 2> /dev/null; check
100 echo -n "Checking ISO image size..."
101 info 035 $(du -mhs $iso | awk '{print $1}')
102 }
104 emulate() {
105 if [ -x "/usr/bin/qemu" ]; then
106 echo "qemu $qemu_opts -cdrom $1"
107 if [ -f "$vdisk" ]; then
108 hda="-hda $vdisk"
109 umount ${vdisk} 2>/dev/null
110 fi
111 qemu ${qemu_opts} ${hda} -cdrom ${1}
112 else
113 echo $(echo "Please install:") $(boldify qemu); exit 0
114 fi
115 }
117 # Install files in the rootfs with same path as on the build host
118 # Usage: install_files "/file/1" "/lib/lib.so*" "/file/N"
119 install_files() {
120 for file in ${@} ; do
121 path=$(dirname $file)
122 if [ ! -d "${rootfs}${path}" ]; then
123 mkdir -p ${rootfs}${path}
124 fi
125 echo -n "Installing: $file"
126 cp -a ${file} ${rootfs}${path}; check
127 done
128 }
130 # Populate /dev + 'mdev -s' on boot
131 create_dev_files() {
132 echo -n "Populating: /dev"
133 mkdir -p ${rootfs}/dev/pts ${rootfs}/dev/shm
134 cd ${rootfs}/dev
135 mknod -m 0666 null c 1 3
136 mknod -m 0622 console c 5 1
137 mknod -m 0666 tty c 5 0
138 for i in 0 1 2; do
139 mknod -m 0666 tty$i c 4 $i
140 done; check
141 }
143 # Configuration files for /etc not generated by init to keep initfs/sebfs
144 # filesystems minimal
145 create_etc_files() {
146 echo -n "Creating config files in: /etc"
147 (echo "127.0.0.1 localhost seb" > ${rootfs}/etc/hosts
148 echo "localnet 127.0.0.1" > ${rootfs}/etc/networks
149 echo "order hosts,bind" > ${rootfs}/etc/host.conf
150 echo "multi on" >> ${rootfs}/etc/host.conf
151 # Users & passwd
152 echo "root:x:0:0:root:/root:/bin/sh" > ${rootfs}/etc/passwd
153 echo "root::13525:0:99999:7:::" > ${rootfs}/etc/shadow
154 cat > ${rootfs}/etc/group << EOT
155 root:x:0:
156 www:x:80:
157 EOT
158 cat > ${rootfs}/etc/gshadow << EOT
159 root:*::
160 www:!::
161 EOT
162 chmod 640 ${rootfs}/etc/*shadow)
163 cat > ${rootfs}/etc/nsswitch.conf << EOT
164 # /etc/nsswitch.conf: GNU Name Service Switch config.
165 #
167 passwd: files
168 group: files
169 shadow: files
171 hosts: files dns
172 networks: files
173 EOT
174 check
175 mkdir -p ${rootfs}/etc/daemons
176 }
178 build() {
179 title "Starting SliTaz Embedded builder"
181 # Build environment
182 rm -rf ${rootfs} ${rootiso}
183 mkdir -p ${rootfs} ${rootiso}/boot/isolinux
185 # FSH Tree
186 echo -n "Populating: filesystem"
187 for d in bin dev etc lib root run home proc media sbin sys \
188 usr/bin usr/sbin usr/share var/log var/cache var/lib var/run
189 do
190 mkdir -p ${rootfs}/${d}
191 done
192 install -d -m 1777 ${rootfs}/tmp; check
194 # /etc + /dev
195 create_etc_files
196 create_dev_files
198 # Initial sebfs from initfs: files can be modified
199 [ ! -d "$sebfs" ] && init
201 # GNU libc before chroot /bin/busybox --install + name resolution
202 for lib in /lib/libm[-.]* /lib/libc[-.]*; do
203 echo -n "Installing: $lib"
204 cp -a ${lib} ${rootfs}/lib && check
205 done
206 install_files "/lib/ld-*" "/lib/libnss_dns*" "/lib/libnss_file*" \
207 "/lib/libresolv*" "/lib/libpthread*" "/lib/libdl*"
209 # Busybox applets
210 echo -n "Installing: busybox"
211 cp -a /bin/busybox ${rootfs}/bin
212 chroot ${rootfs} /bin/busybox --install -s; check
214 # Busybox configs
215 echo -n "Installing: busybox configs"
216 cp -r /usr/share/udhcpc ${rootfs}/usr/share
217 cp -f /etc/udhcpd.conf ${rootfs}/etc
218 cp -f /etc/httpd.conf ${rootfs}/etc
219 check
221 # Busybox keymap
222 echo -n "Dumping : keymap"
223 mkdir -p ${rootfs}/usr/share/kmap
224 dumpkmap > ${rootfs}/usr/share/kmap/default; check
226 # Kilo editor (20K) with syntax highlight and search (Thanks Paul :-)
227 echo -n "$(colorize 034 'Installing: kilo text editor')"
228 cp -a ${tools}/kilo ${rootfs}/usr/bin; check
230 # Ncursesw && dialog for sebos and additional tools
231 install_files "/lib/libncursesw.so*" "/lib/libtinfo.so*"
232 mkdir -p ${rootfs}/usr/share/terminfo/l
233 cp /usr/share/terminfo/l/linux ${rootfs}/usr/share/terminfo/l
234 install_files "/usr/bin/dialog" "/etc/dialogrc"
236 # /lib/libseb.sh & sebos config tool
237 echo -n "Installing: /lib/libseb.sh"
238 cp ${libseb} ${rootfs}/lib; check
239 echo -n "$(colorize 034 'Installing: sebos config tool')"
240 cp ${tools}/sebos ${rootfs}/usr/bin; check
242 # httphelper.sh for amazing CGI/Shell functions
243 mkdir -p ${rootfs}/usr/lib/slitaz
244 cp /usr/lib/slitaz/httphelper.sh ${rootfs}/usr/lib/slitaz
246 # Packages TODO: handle deps
247 touch ${rootfs}/var/lib/packages
248 for pkg in $(ls $sebpkgs); do
249 echo -n $(colorize 035 "Installing package:"); info 036 "$pkg"
250 . ${sebpkgs}/${pkg}
251 seb_install; echo "$pkg|$desc" >> ${rootfs}/var/lib/packages
252 done
254 # Custom files NOW
255 if [ -d "$sebfs" ]; then
256 echo -n "Copying custom files from sebfs..."
257 cp -rf ${sebfs}/* ${rootfs}; check
258 fi
260 # COPYING
261 mkdir -p ${rootfs}/usr/share/licenses
262 cat > ${rootfs}/usr/share/licenses/COPYING << EOT
263 Copyright (c) 2007-$(date '+%Y') SliTaz GNU/Linux
265 SliTaz is free software; you can redistribute it and/or modify it under
266 the terms of the GNU General Public License as published by the Free
267 Software Foundation; either version 3 of the License, or (at your option)
268 any later version.
270 SliTaz is distributed in the hope that it will be useful, but WITHOUT ANY
271 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
272 FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
274 EOT
276 # Build date
277 cat >> ${rootfs}/etc/seb.conf << EOT
279 # Seb-OS Build date
280 build_date="$(date '+%Y%m%d')"
282 # Seb-OS/SliTaz release string
283 seb_os_release="$(cat /etc/slitaz-release)"
285 EOT
287 # Security check
288 chown -R 0.0 ${rootfs}
289 chmod 0600 ${rootfs}/etc/busybox.conf
291 # Rootfs archive: lzma e -si -so
292 echo -n "$(colorize 034 'Creating the initramfs...')"
293 cd ${rootfs}
294 find . -print | cpio -o -H newc | xz -9 --format=lzma \
295 > ${rootiso}/boot/rootfs.xz 2>/dev/null
296 check
298 # Linux Kernel
299 echo -n "Copying the Linux kernel..."
300 cp ${linux} ${rootiso}/boot/bzImage
301 info 035 "$(du -mh $linux | awk '{print $1}')"
303 # Bootloader
304 echo -n "Copying the bootloader (isolinux)..."
305 cp ${tools}/isolinux.bin ${rootiso}/boot/isolinux; check
307 echo -n "Creating bootloader configs..."
308 cat > ${rootiso}/boot/isolinux/isolinux.cfg << EOF
309 display display.txt
310 default seb
311 label seb
312 kernel /boot/bzImage
313 append initrd=/boot/rootfs.xz rw root=/dev/null rdinit=/sbin/init
314 implicit 0
315 prompt 1
316 timeout 40
317 EOF
318 cat > ${rootiso}/boot/isolinux/display.txt << EOF
320 ____ _ _ _ _ ___ _
321 / ___| \ | | | | | / / | (_)_ __ _ ___ __
322 | | _| \| | | | |/ /| | | | '_ \| | | \ \/ /
323 | |_| | |\ | |_| / / | |___| | | | | |_| |> <
324 \____|_| \_|\___/_/ |_____|_|_| |_|\__,_/_/\_\
326 SliTaz Embedded OS - Press <ENTER> to boot
327 www.slitaz.org
330 EOF
331 check
333 echo -n "Checking rootfs size..."
334 info 035 $(du -mhs $rootfs | awk '{print $1}')
335 echo -n "Installed files in rootfs..."
336 info 036 $(find ${rootfs} -type f | wc -l)
338 # ISO image
339 geniso; footer
340 }
342 # Handle seb packages
343 packages_handler() {
345 # List available packages
346 if [ ! "$1" ]; then
347 title "Seb packages"
348 for pkg in $(ls ${packages}); do
349 . ${packages}/${pkg}
350 echo -n "$(colorize 036 $pkg)"; indent 20 "$desc"
351 unset desc deps
352 done
353 footer && exit 0
354 fi
356 # Add package(s)
357 if [ "$add" ]; then
358 for pkg in ${@}; do
359 case "$pkg" in
360 --*) continue ;;
361 *)
362 if [ -f "$packages/$pkg" ]; then
363 echo -n "Adding package: $pkg"
364 mkdir -p ${sebpkgs}
365 cp -f ${packages}/${pkg} ${sebpkgs}; check
366 else
367 echo "Can't find package: $package/$pkg"
368 fi ;;
369 esac
370 done
371 fi
372 }
374 # Handle vdisk: create, check, mount, umount
375 vdisk_hanler() {
376 title "SEB Virtual disk"
377 vsize=60
378 root=${vdisk%.img}
380 # Info or create
381 if [ -f "$vdisk" ]; then
382 echo -n "Virtual disk: $vdisk"
383 info 035 "$(du -mhs $vdisk | awk '{print $1}')"
384 else
385 echo "Creating virtual disk image..."
386 dd if=/dev/zero of=${vdisk} bs=1M count=${vsize}
387 colorize 034 "Creating ext3 filesystem..."
388 mkfs.ext3 -L "SebOShome" ${vdisk}
389 fi
391 # Check
392 if [ "$check" ]; then
393 echo "Umounting vdisk before: e2fsck -p"
394 umount ${vdisk} >/dev/null
395 e2fsck -p ${vdisk}
396 fi
398 # Action: mount/unmount
399 if ! mount | grep -q "^$vdisk"; then
400 echo -n "Mounting virtual disk..."; mkdir -p ${root}
401 mount -o loop -t ext3 ${vdisk} ${root}; status
402 else
403 echo -n "Unmounting virtual disk..."
404 umount ${vdisk}; status; sleep 1
405 fi; footer
406 }
408 #
409 # Commands
410 #
412 case "$1" in
414 -i|init)
415 rootfs="$sebfs"
416 echo -n "Creating files in: ${rootfs}"
417 init; check ;;
419 -b|build)
420 check_root
421 build
422 [ "$emu" ] && emulate ${work}/${iso} ;;
424 -g|geniso)
425 check_root
426 geniso ;;
428 -c|clean)
429 check_root
430 echo -n "Cleaning: ${work}"
431 [ "$all" ] && rm -rf ${sebfs} ${cache}
432 rm -rf ${rootfs}* ${rootiso} ${work}/*.iso
433 check ;;
435 -p|package*)
436 shift
437 packages_handler "$@" ;;
439 -v|vdisk)
440 vdisk_hanler ;;
442 -l|lsfs)
443 title "Listing: $rootfs"
444 cd ${rootfs}; find . -type f | sed s'/^.//'g
445 footer "Rootfs files: $(find . -type f | wc -l)" ;;
447 -e|emu)
448 title "Emulating: $iso"
449 emulate ${work}/${iso}
450 footer ;;
452 -w|web)
453 pid=$(ps | grep 8940 | grep -v grep | awk '{print $1}')
454 if [ "$stop" ] && [ "$pid" ]; then
455 echo -n "Stopping Web interface... pid:"
456 kill ${pid}; info 035 "$pid"; exit 0
457 fi
458 # Need the cgi script in server root
459 if [ ! -f "$work/seb.cgi" ]; then
460 cp ${tools}/web/seb.cgi .
461 fi
462 if [ "$pid" ]; then
463 echo -n "Web interface is already running with pid:"
464 info 035 "$pid"
465 else
466 echo -n "Starting web interface on port:"; info 035 "$port"
467 httpd -p ${port} -u 0:0 -h ${work} \
468 -c ${tools}/web/httpd.conf
469 fi
470 tazweb --notoolbar http://localhost:${port}/ & ;;
472 env)
473 title "SEB environment"
474 cat << EOT
475 work=$work
476 rootfs=$rootfs
477 rootiso=$rootiso
478 sebfs=$sebfs
479 sebpkgs=$sebpkgs
480 cache=$cache
481 vdisk=$vdisk
482 tools=$tools
483 initfs=$initfs
484 packages=$packages
485 iso=$iso
486 linux=$linux
487 EOT
488 footer ;;
490 -t|testsuite)
491 # Development/debug purpose
492 if [ -x "./libseb.sh" ]; then
493 ./libseb.sh
494 else
495 /lib/libseb.sh
496 fi ;;
498 *) help ;;
500 esac && exit 0