seb view seb @ rev 7

Add seb builder utility
author Christophe Lincoln <pankso@slitaz.org>
date Mon Mar 06 16:42:09 2017 +0100 (2017-03-06)
parents
children a4fa34b33e9e
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]
59 $(boldify "Commands:")
60 -h help Display this short built-in help
61 -i init Creat base files to customize
62 -b build Generate a distribution (initramfs & iso)
63 -p packages Handle packages: --list --add
64 -g geniso Re-generate the ISO image
65 -c clean Remove all SEB generated files
66 -l lsfs List all files in rootfs (-type f)
67 -v vdisk Create, mount or unmount a virtual disk
68 -e emu Emulate ISO image with Qemu
69 env Print current seb environment
71 $(boldify "Options:")
72 --work= Path to build directory
73 --iso= Specify SliTaz Embedded ISO image name
74 --linux= Path to a custom Linux kernel
75 --all Clean all files including sebfs
76 --emu Emulate ISO image after build
77 --check Check a virtual disk image
79 EOT
80 }
82 # Initial files who can be modified via sebfs/
83 init() {
84 mkdir -p ${sebfs} ${sebpkgs}
85 cp -rf ${initfs}/* ${sebfs}
86 }
88 geniso() {
89 echo -n "Generating ISO image: ${iso}"
90 cd ${work}
91 genisoimage -R -o ${iso} \
92 -b boot/isolinux/isolinux.bin \
93 -c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
94 -V "SliTaz Embedded" -input-charset iso8859-1 \
95 -boot-info-table rootiso 2> /dev/null; check
96 echo -n "Checking ISO image size..."
97 info 035 $(du -mhs $iso | awk '{print $1}')
98 }
100 emulate() {
101 if [ -x "/usr/bin/qemu" ]; then
102 echo "qemu $qemu_opts -cdrom $1"
103 if [ -f "$vdisk" ]; then
104 hda="-hda $vdisk"
105 umount ${vdisk} 2>/dev/null
106 fi
107 qemu ${qemu_opts} ${hda} -cdrom ${1}
108 else
109 echo $(echo "Please install:") $(boldify qemu); exit 0
110 fi
111 }
113 # Install files in the rootfs with same path than on the build host
114 # Usage: install_files "/file/1" "/lib/lib.so*" "/file/N"
115 install_files() {
116 for file in ${@} ; do
117 path=$(dirname $file)
118 echo -n "Installing: $file"
119 cp -a ${file} ${rootfs}${path} && check
120 done
121 }
123 # Populate /dev + 'mdev -s' on boot
124 create_dev_files() {
125 echo -n "Populating: /dev"
126 mkdir -p ${rootfs}/dev/pts ${rootfs}/dev/shm
127 cd ${rootfs}/dev
128 mknod -m 0666 null c 1 3
129 mknod -m 0622 console c 5 1
130 mknod -m 0666 tty c 5 0
131 for i in 0 1 2; do
132 mknod -m 0666 tty$i c 4 $i
133 done; check
134 }
136 # Configuration files for /etc not generate by init to keep initfs/sebfs
137 # filesytems minimals
138 create_etc_files() {
139 echo -n "Creating config files in: /etc"
140 (echo "127.0.0.1 localhost seb" > ${rootfs}/etc/hosts
141 echo "localnet 127.0.0.1" > ${rootfs}/etc/networks
142 echo "order hosts,bind" > ${rootfs}/etc/host.conf
143 echo "multi on" >> ${rootfs}/etc/host.conf
144 # Users & passwd
145 echo "root:x:0:0:root:/root:/bin/sh" > ${rootfs}/etc/passwd
146 echo "root::13525:0:99999:7:::" > ${rootfs}/etc/shadow
147 cat > ${rootfs}/etc/group << EOT
148 root:x:0:
149 www:x:80:
150 EOT
151 cat > ${rootfs}/etc/gshadow << EOT
152 root:*::
153 www:!::
154 EOT
155 chmod 640 ${rootfs}/etc/*shadow)
156 cat > ${rootfs}/etc/nsswitch.conf << EOT
157 # /etc/nsswitch.conf: GNU Name Service Switch config.
158 #
160 passwd: files
161 group: files
162 shadow: files
164 hosts: files dns
165 networks: files
166 EOT
167 check
168 }
170 build() {
171 title "Starting SliTaz Embedded builder"
173 # Build environment
174 rm -rf ${rootfs} ${rootiso}
175 mkdir -p ${rootfs} ${rootiso}/boot/isolinux
177 # FSH Tree
178 echo -n "Populating: filesystem"
179 for d in bin dev etc lib root run home proc media sbin sys \
180 usr/bin usr/sbin usr/share var/log var/cache var/lib
181 do
182 mkdir -p ${rootfs}/${d}
183 done
184 install -d -m 1777 ${rootfs}/tmp; check
186 # /etc + /dev
187 create_etc_files
188 create_dev_files
190 # Initial sebfs from initfs: files can be modified
191 [ ! -d "$sebfs" ] && init
193 # GNU libc before chroot /bin/busybox --install + name resolution
194 for lib in /lib/libm[-.]* /lib/libc[-.]* /lib/ld-* /lib/libpthread*; do
195 echo -n "Installing: $lib"
196 cp -a ${lib} ${rootfs}/lib && check
197 done
198 install_files "/lib/libnss_dns*" "/lib/libnss_file*" "/lib/libresolv*"
200 # Busybox applets
201 action "Installing: busybox"
202 cp -a /bin/busybox ${rootfs}/bin
203 chroot ${rootfs} /bin/busybox --install -s; check
205 # Busybox configs
206 echo -n "Installing: busybox configs"
207 cp -r /usr/share/udhcpc ${rootfs}/usr/share
208 cp -f /etc/udhcpd.conf ${rootfs}/etc
209 cp -f /etc/httpd.conf ${rootfs}/etc
210 check
212 # Busybox keymap
213 echo -n "Dumping : keymap"
214 mkdir -p ${rootfs}/usr/share/kmap
215 dumpkmap > ${rootfs}/usr/share/kmap/default; check
217 # Kilo editor (20K) with syntax highlight and search (Thanks Paul :-)
218 action "Installing: kilo text editor"
219 cp -a ${tools}/kilo ${rootfs}/usr/bin; check
221 # Ncursesw && dialog for sebos and additional tools
222 install_files "/lib/libncursesw.so*" "/usr/bin/dialog"
223 mkdir -p ${rootfs}/usr/share/terminfo/l
224 cp /usr/share/terminfo/l/linux ${rootfs}/usr/share/terminfo/l
225 cp /etc/dialogrc ${rootfs}/etc
227 # /lib/libseb.sh & sebos config tool
228 echo -n "Installing: /lib/libseb.sh"
229 cp ${libseb} ${rootfs}/lib; check
230 action "Installing: sebos config tool"
231 cp ${tools}/sebos ${rootfs}/usr/bin; check
233 # httphelper.sh for amazing CGI/Shell functions
234 mkdir -p ${rootfs}/usr/lib/slitaz
235 cp /usr/lib/slitaz/httphelper.sh ${rootfs}/usr/lib/slitaz
237 # Packages TODO: handle deps
238 touch ${rootfs}/var/lib/packages
239 for pkg in $(ls $sebpkgs); do
240 echo -n $(colorize 035 "Installing package:"); info 036 "$pkg"
241 . ${packages}/${pkg}
242 seb_install; echo "$pkg|$desc" >> ${rootfs}/var/lib/packages
243 done
245 # Custom files NOW
246 if [ -d "$sebfs" ]; then
247 echo -n "Copying custom files from sebfs..."
248 cp -rf ${sebfs}/* ${rootfs}; check
249 fi
251 # COPYING
252 mkdir -p ${rootfs}/usr/share/licenses
253 cat > ${rootfs}/usr/share/licenses/COPYING << EOT
254 Copyright (c) 2007-$(date '+%Y') SliTaz GNU/Linux
256 SliTaz is free software; you can redistribute it and/or modify it under
257 the terms of the GNU General Public License as published by the Free
258 Software Foundation; either version 3 of the License, or (at your option)
259 any later version.
261 SliTaz is distributed in the hope that it will be useful, but WITHOUT ANY
262 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
263 FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
265 EOT
267 # Build date
268 cat >> ${rootfs}/etc/seb.conf << EOT
270 # Seb-OS Build date
271 build_date="$(date '+%Y%m%d')"
273 # Seb-OS/SliTaz release string
274 seb_os_release="$(cat /etc/slitaz-release)"
276 EOT
278 # Security check
279 chmod 0600 ${rootfs}/etc/busybox.conf
281 # Rootfs archive
282 action "Creating the initramfs..."
283 cd ${rootfs}
284 find . -print | cpio -o -H newc | lzma e -si -so \
285 > ${rootiso}/boot/rootfs.gz 2>/dev/null
286 check
288 # Linux Kernel
289 echo -n "Copying the Linux kernel..."
290 cp ${linux} ${rootiso}/boot/bzImage; check
292 # Bootloader
293 echo -n "Copying the bootloader (isolinux)..."
294 cp ${tools}/isolinux.bin ${rootiso}/boot/isolinux; check
296 echo -n "Creating bootloader configs..."
297 cat > ${rootiso}/boot/isolinux/isolinux.cfg << EOF
298 display display.txt
299 default seb
300 label seb
301 kernel /boot/bzImage
302 append initrd=/boot/rootfs.gz rw root=/dev/null rdinit=/sbin/init
303 implicit 0
304 prompt 1
305 timeout 40
306 EOF
307 cat > ${rootiso}/boot/isolinux/display.txt << EOF
309 ____ _ _ _ _ ___ _
310 / ___| \ | | | | | / / | (_)_ __ _ ___ __
311 | | _| \| | | | |/ /| | | | '_ \| | | \ \/ /
312 | |_| | |\ | |_| / / | |___| | | | | |_| |> <
313 \____|_| \_|\___/_/ |_____|_|_| |_|\__,_/_/\_\
315 SliTaz Embedded OS - Press <ENTER> to boot
316 www.slitaz.org
319 EOF
320 check
322 echo -n "Checking rootfs size..."
323 info 035 $(du -mhs $rootfs | awk '{print $1}')
324 echo -n "Installed files in rootfs..."
325 info 036 $(find ${rootfs} -type f | wc -l)
327 # ISO image
328 geniso; footer
329 }
331 # Handle seb packages
332 packages_handler() {
334 # List avalaible packages
335 if [ ! "$1" ]; then
336 title "Seb packages"
337 for pkg in $(ls ${packages}); do
338 . ${packages}/${pkg}
339 echo -n "$(colorize 036 $pkg)"; indent 20 "$desc"
340 unset desc deps
341 done
342 footer && exit 0
343 fi
345 # Add package(s)
346 if [ "$add" ]; then
347 for pkg in ${@}; do
348 case "$pkg" in
349 --*) continue ;;
350 *)
351 if [ -f "$packages/$pkg" ]; then
352 echo -n "Addind package: $pkg"
353 mkdir -p ${sebpkgs}
354 cp -f ${packages}/${pkg} ${sebpkgs}; check
355 else
356 echo "Can't find package: $package/$pkg"
357 fi ;;
358 esac
359 done
360 fi
361 }
363 # Handle vdisk: create, check, mount, umount
364 vdisk_hanler() {
365 title "SEB Virtual disk"
366 vsize=40960
367 root=${vdisk%.img}
369 # Info or create
370 if [ -f "$vdisk" ]; then
371 echo -n "Virtual disk: $vdisk"
372 info 035 "$(du -mhs $vdisk | awk '{print $1}')"
373 else
374 echo "Creating virtual disk image..."
375 dd if=/dev/zero of=${vdisk} bs=1k count=${vsize}
376 action "Creating ext3 filesystem..."; echo
377 mkfs.ext3 -L "SebOShome" ${vdisk}
378 fi
380 # Check
381 if [ "$check" ]; then
382 echo "Umounting vdisk before: e2fsck -p"
383 umount ${vdisk} >/dev/null
384 e2fsck -p ${vdisk}
385 fi
387 # Action: mount/unmount
388 if ! mount | grep -q "^$vdisk"; then
389 echo -n "Mounting virtual disk..."; mkdir -p ${root}
390 mount -o loop -t ext3 ${vdisk} ${root}; status
391 else
392 echo -n "Unmounting virtual disk..."
393 umount ${vdisk}; status; sleep 1
394 fi; footer
395 }
397 #
398 # Commands
399 #
401 case "$1" in
403 -i|init)
404 rootfs="$sebfs"
405 echo -n "Creating files in: ${rootfs}"
406 init; check ;;
408 -b|build)
409 check_root
410 build
411 [ "$emu" ] && emulate ${work}/${iso} ;;
413 -g|geniso)
414 check_root
415 geniso ;;
417 -c|clean)
418 check_root
419 echo -n "Cleaning: ${work}"
420 [ "$all" ] && rm -rf ${sebfs} ${cache}
421 rm -rf ${rootfs}* ${rootiso} ${work}/*.iso
422 check ;;
424 -p|package*)
425 shift
426 packages_handler "$@" ;;
428 -v|vdisk)
429 vdisk_hanler ;;
431 -l|lsfs)
432 title "Listing: $rootfs"
433 cd ${rootfs}; find . -type f | sed s'/^.//'g
434 footer "Rootfs files: $(find . -type f | wc -l)" ;;
436 emu)
437 title "Emulating: $iso"
438 emulate ${work}/${iso}
439 footer ;;
441 env)
442 title "SEB environment"
443 cat << EOT
444 work=$work
445 rootfs=$rootfs
446 rootiso=$rootiso
447 sebfs=$sebfs
448 sebpkgs=$sebpkgs
449 cache=$cache
450 vdisk=$vdisk
451 tools=$tools
452 initfs=$initfs
453 packages=$packages
454 iso=$iso
455 linux=$linux
456 EOT
457 footer ;;
459 -t|testsuite)
460 # Development purpose
461 ${tools}/libseb.sh ;;
463 *) help ;;
465 esac && exit 0