seb view seb @ rev 23

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