seb view seb @ rev 32

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