seb view seb @ rev 30

Add a new default kernel with some networking support
author Christophe Lincoln <pankso@slitaz.org>
date Mon Mar 20 23:15:15 2017 +0100 (2017-03-20)
parents 6a760d83d976
children f6fd796b9a46
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 --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
84 EOT
85 }
87 # Initial files which can be modified via sebfs/
88 init() {
89 mkdir -p ${sebfs} ${sebpkgs}
90 cp -rf ${initfs}/* ${sebfs}
91 }
93 geniso() {
94 echo -n "Generating ISO image: ${iso}"
95 cd ${work}
96 genisoimage -R -o ${iso} \
97 -b boot/isolinux/isolinux.bin \
98 -c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
99 -V "SliTaz Embedded" -input-charset iso8859-1 \
100 -boot-info-table rootiso 2> /dev/null; check
101 echo -n "Checking ISO image size..."
102 info 035 $(du -mhs $iso | awk '{print $1}')
103 }
105 emulate() {
106 if [ -x "/usr/bin/qemu" ]; then
107 echo "qemu $qemu_opts -cdrom $1"
108 if [ -f "$vdisk" ]; then
109 hda="-hda $vdisk"
110 umount ${vdisk} 2>/dev/null
111 fi
112 qemu ${qemu_opts} ${hda} -cdrom ${1}
113 else
114 echo $(echo "Please install:") $(boldify qemu); exit 0
115 fi
116 }
118 # Install files in the rootfs with same path as on the build host
119 # Usage: install_files "/file/1" "/lib/lib.so*" "/file/N"
120 install_files() {
121 for file in ${@} ; do
122 path=$(dirname $file)
123 if [ ! -d "${rootfs}${path}" ]; then
124 mkdir -p ${rootfs}${path}
125 fi
126 echo -n "Installing: $file"
127 cp -a ${file} ${rootfs}${path}; check
128 done
129 }
131 # Populate /dev + 'mdev -s' on boot
132 create_dev_files() {
133 echo -n "Populating: /dev"
134 mkdir -p ${rootfs}/dev/pts ${rootfs}/dev/shm
135 cd ${rootfs}/dev
136 mknod -m 0666 null c 1 3
137 mknod -m 0622 console c 5 1
138 mknod -m 0666 tty c 5 0
139 for i in 0 1 2; do
140 mknod -m 0666 tty$i c 4 $i
141 done; check
142 }
144 # Configuration files for /etc not generated by init to keep initfs/sebfs
145 # filesystems minimal
146 create_etc_files() {
147 echo -n "Creating config files in: /etc"
148 (echo "127.0.0.1 localhost seb" > ${rootfs}/etc/hosts
149 echo "localnet 127.0.0.1" > ${rootfs}/etc/networks
150 echo "order hosts,bind" > ${rootfs}/etc/host.conf
151 echo "multi on" >> ${rootfs}/etc/host.conf
152 # Users & passwd
153 echo "root:x:0:0:root:/root:/bin/sh" > ${rootfs}/etc/passwd
154 echo "root::13525:0:99999:7:::" > ${rootfs}/etc/shadow
155 cat > ${rootfs}/etc/group << EOT
156 root:x:0:
157 www:x:80:
158 EOT
159 cat > ${rootfs}/etc/gshadow << EOT
160 root:*::
161 www:!::
162 EOT
163 chmod 640 ${rootfs}/etc/*shadow)
164 cat > ${rootfs}/etc/nsswitch.conf << EOT
165 # /etc/nsswitch.conf: GNU Name Service Switch config.
166 #
168 passwd: files
169 group: files
170 shadow: files
172 hosts: files dns
173 networks: files
174 EOT
175 check
176 mkdir -p ${rootfs}/etc/daemons
177 }
179 build() {
180 title "Starting SliTaz Embedded builder"
182 # Build environment
183 rm -rf ${rootfs} ${rootiso}
184 mkdir -p ${rootfs} ${rootiso}/boot/isolinux
186 # FSH Tree
187 echo -n "Populating: filesystem"
188 for d in bin dev etc lib root run home proc media sbin sys \
189 usr/bin usr/sbin usr/share var/log var/cache var/lib var/run
190 do
191 mkdir -p ${rootfs}/${d}
192 done
193 install -d -m 1777 ${rootfs}/tmp; check
195 # /etc + /dev
196 create_etc_files
197 create_dev_files
199 # Initial sebfs from initfs: files can be modified
200 [ ! -d "$sebfs" ] && init
202 # GNU libc before chroot /bin/busybox --install + name resolution
203 for lib in /lib/libm[-.]* /lib/libc[-.]*; do
204 echo -n "Installing: $lib"
205 cp -a ${lib} ${rootfs}/lib && check
206 done
207 install_files "/lib/ld-*" "/lib/libnss_dns*" "/lib/libnss_file*" \
208 "/lib/libresolv*" "/lib/libpthread*" "/lib/libdl*"
210 # Busybox applets
211 echo -n "Installing: busybox"
212 cp -a /bin/busybox ${rootfs}/bin
213 chroot ${rootfs} /bin/busybox --install -s; check
215 # Busybox configs
216 echo -n "Installing: busybox configs"
217 cp -r /usr/share/udhcpc ${rootfs}/usr/share
218 cp -f /etc/udhcpd.conf ${rootfs}/etc
219 cp -f /etc/httpd.conf ${rootfs}/etc
220 check
222 # Busybox keymap
223 echo -n "Dumping : keymap"
224 mkdir -p ${rootfs}/usr/share/kmap
225 dumpkmap > ${rootfs}/usr/share/kmap/default; check
227 # Kilo editor (20K) with syntax highlight and search (Thanks Paul :-)
228 echo -n "$(colorize 033 'Installing: kilo text editor')"
229 cp -a ${tools}/kilo ${rootfs}/usr/bin; check
231 # Ncursesw && dialog for sebos and additional tools
232 install_files "/lib/libncursesw.so*" "/lib/libtinfo.so*"
233 mkdir -p ${rootfs}/usr/share/terminfo/l
234 cp /usr/share/terminfo/l/linux ${rootfs}/usr/share/terminfo/l
235 install_files "/usr/bin/dialog" "/etc/dialogrc"
237 # /lib/libseb.sh & sebos config tool
238 echo -n "Installing: /lib/libseb.sh"
239 cp ${libseb} ${rootfs}/lib; check
240 echo -n "$(colorize 033 'Installing: sebos config tool')"
241 cp ${tools}/sebos ${rootfs}/usr/bin; check
243 # httphelper.sh for amazing CGI/Shell functions
244 mkdir -p ${rootfs}/usr/lib/slitaz
245 cp /usr/lib/slitaz/httphelper.sh ${rootfs}/usr/lib/slitaz
247 # Packages TODO: handle deps
248 touch ${rootfs}/var/lib/packages
249 for pkg in $(ls $sebpkgs); do
250 echo -n $(colorize 035 "Installing package:"); info 036 "$pkg"
251 . ${sebpkgs}/${pkg}
252 seb_install; echo "$pkg|$desc" >> ${rootfs}/var/lib/packages
253 done
255 # Custom files NOW
256 if [ -d "$sebfs" ]; then
257 echo -n "Copying custom files from sebfs..."
258 cp -rf ${sebfs}/* ${rootfs}; check
259 fi
261 # COPYING
262 mkdir -p ${rootfs}/usr/share/licenses
263 cat > ${rootfs}/usr/share/licenses/COPYING << EOT
264 Copyright (c) 2007-$(date '+%Y') SliTaz GNU/Linux
266 SliTaz is free software; you can redistribute it and/or modify it under
267 the terms of the GNU General Public License as published by the Free
268 Software Foundation; either version 3 of the License, or (at your option)
269 any later version.
271 SliTaz is distributed in the hope that it will be useful, but WITHOUT ANY
272 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
273 FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
275 EOT
277 # Build date
278 cat >> ${rootfs}/etc/seb.conf << EOT
280 # Seb-OS Build date
281 build_date="$(date '+%Y%m%d')"
283 # Seb-OS/SliTaz release string
284 seb_os_release="$(cat /etc/slitaz-release)"
286 EOT
288 # Security check
289 chown -R 0.0 ${rootfs}
290 chmod 0600 ${rootfs}/etc/busybox.conf
292 # Rootfs archive: lzma e -si -so
293 echo -n "$(colorize 033 'Creating the initramfs...')"
294 cd ${rootfs}
295 find . -print | cpio -o -H newc | xz -9 --format=lzma \
296 > ${rootiso}/boot/rootfs.xz 2>/dev/null
297 check
299 # Linux Kernel
300 echo -n "Copying the Linux kernel..."
301 cp ${linux} ${rootiso}/boot/bzImage
302 info 035 "$(du -mh $linux | awk '{print $1}')"
304 # Bootloader
305 echo -n "Copying the bootloader (isolinux)..."
306 cp ${tools}/isolinux.bin ${rootiso}/boot/isolinux; check
308 echo -n "Creating bootloader configs..."
309 cat > ${rootiso}/boot/isolinux/isolinux.cfg << EOF
310 display display.txt
311 default seb
312 label seb
313 kernel /boot/bzImage
314 append initrd=/boot/rootfs.xz rw root=/dev/null rdinit=/sbin/init
315 implicit 0
316 prompt 1
317 timeout 40
318 EOF
319 cat > ${rootiso}/boot/isolinux/display.txt << EOF
321 ____ _ _ _ _ ___ _
322 / ___| \ | | | | | / / | (_)_ __ _ ___ __
323 | | _| \| | | | |/ /| | | | '_ \| | | \ \/ /
324 | |_| | |\ | |_| / / | |___| | | | | |_| |> <
325 \____|_| \_|\___/_/ |_____|_|_| |_|\__,_/_/\_\
327 SliTaz Embedded OS - Press <ENTER> to boot
328 www.slitaz.org
331 EOF
332 check
334 echo -n "Checking rootfs size..."
335 info 035 $(du -mhs $rootfs | awk '{print $1}')
336 echo -n "Installed files in rootfs..."
337 info 036 $(find ${rootfs} -type f | wc -l)
339 # ISO image
340 geniso; footer
341 }
343 # Handle seb packages
344 packages_handler() {
346 # List available packages
347 if [ ! "$1" ]; then
348 title "Seb packages"
349 for pkg in $(ls ${packages}); do
350 . ${packages}/${pkg}
351 echo -n "$(colorize 036 $pkg)"; indent 20 "$desc"
352 unset desc deps
353 done
354 footer && exit 0
355 fi
357 # Add package(s)
358 if [ "$add" ]; then
359 for pkg in ${@}; do
360 case "$pkg" in
361 --*) continue ;;
362 *)
363 if [ -f "$packages/$pkg" ]; then
364 echo -n "Adding package: $pkg"
365 mkdir -p ${sebpkgs}
366 cp -f ${packages}/${pkg} ${sebpkgs}; check
367 else
368 echo "Can't find package: $package/$pkg"
369 fi ;;
370 esac
371 done
372 fi
373 }
375 # Handle vdisk: create, check, mount, umount
376 vdisk_hanler() {
377 title "SEB Virtual disk"
378 vsize=60
379 root=${vdisk%.img}
381 # Info or create
382 if [ -f "$vdisk" ]; then
383 echo -n "Virtual disk: $vdisk"
384 info 035 "$(du -mhs $vdisk | awk '{print $1}')"
385 else
386 echo "Creating virtual disk image..."
387 dd if=/dev/zero of=${vdisk} bs=1M count=${vsize}
388 colorize 033 "Creating ext3 filesystem..."
389 mkfs.ext3 -L "SebOShome" ${vdisk}
390 fi
392 # Check
393 if [ "$check" ]; then
394 echo "Umounting vdisk before: e2fsck -p"
395 umount ${vdisk} >/dev/null
396 e2fsck -p ${vdisk}
397 fi
399 # Action: mount/unmount
400 if ! mount | grep -q "^$vdisk"; then
401 echo -n "Mounting virtual disk..."; mkdir -p ${root}
402 mount -o loop -t ext3 ${vdisk} ${root}; status
403 else
404 echo -n "Unmounting virtual disk..."
405 umount ${vdisk}; status; sleep 1
406 fi; footer
407 }
409 #
410 # Commands
411 #
413 case "$1" in
415 -i|init)
416 rootfs="$sebfs"
417 echo -n "Creating files in: ${rootfs}"
418 init; check ;;
420 -b|build)
421 check_root
422 build
423 [ "$emu" ] && emulate ${work}/${iso} ;;
425 -g|geniso)
426 check_root
427 geniso ;;
429 -c|clean)
430 check_root
431 echo -n "Cleaning: ${work}"
432 [ "$all" ] && rm -rf ${sebfs} ${cache}
433 rm -rf ${rootfs}* ${rootiso} ${work}/*.iso
434 check ;;
436 -p|package*)
437 shift
438 packages_handler "$@" ;;
440 -v|vdisk)
441 vdisk_hanler ;;
443 -l|lsfs)
444 title "Listing: $rootfs"
445 cd ${rootfs}; find . -type f | sed s'/^.//'g
446 footer "Rootfs files: $(find . -type f | wc -l)" ;;
448 -e|emu)
449 title "Emulating: $iso"
450 emulate ${work}/${iso}
451 footer ;;
453 -w|web)
454 pid=$(ps | grep $port | grep -v grep | awk '{print $1}')
455 if [ "$stop" ] && [ "$pid" ]; then
456 echo -n "Stopping Web interface... pid:"
457 kill ${pid}; info 035 "$pid"; exit 0
458 fi
460 # Need the last cgi script in server root
461 cp -f ${tools}/web/seb.cgi .
463 if [ "$pid" ]; then
464 echo -n "Web interface is already running with pid:"
465 info 035 "$pid"
466 else
467 echo -n "Starting web interface on port:"; info 035 "$port"
468 httpd -p ${port} -u 0:0 -h ${work} \
469 -c ${tools}/web/httpd.conf
470 fi
471 tazweb --small --raw http://localhost:${port}/ & ;;
473 env)
474 title "SEB environment"
475 cat << EOT
476 work=$work
477 rootfs=$rootfs
478 rootiso=$rootiso
479 sebfs=$sebfs
480 sebpkgs=$sebpkgs
481 cache=$cache
482 vdisk=$vdisk
483 tools=$tools
484 initfs=$initfs
485 packages=$packages
486 iso=$iso
487 linux=$linux
488 EOT
489 footer ;;
491 -t|testsuite)
492 # Development/debug purpose
493 if [ -x "./libseb.sh" ]; then
494 ./libseb.sh
495 else
496 /lib/libseb.sh
497 fi ;;
499 *) help ;;
501 esac && exit 0