seb view seb @ rev 20

Tiny edits
author Paul Issott <paul@slitaz.org>
date Tue Mar 07 20:24:00 2017 +0000 (2017-03-07)
parents ff5c597e5d11
children 313cfea052a5
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[-.]* /lib/ld-* /lib/libpthread*; do
200 echo -n "Installing: $lib"
201 cp -a ${lib} ${rootfs}/lib && check
202 done
203 install_files "/lib/libnss_dns*" "/lib/libnss_file*" "/lib/libresolv*"
205 # Busybox applets
206 action "Installing: busybox"
207 cp -a /bin/busybox ${rootfs}/bin
208 chroot ${rootfs} /bin/busybox --install -s; check
210 # Busybox configs
211 echo -n "Installing: busybox configs"
212 cp -r /usr/share/udhcpc ${rootfs}/usr/share
213 cp -f /etc/udhcpd.conf ${rootfs}/etc
214 cp -f /etc/httpd.conf ${rootfs}/etc
215 check
217 # Busybox keymap
218 echo -n "Dumping : keymap"
219 mkdir -p ${rootfs}/usr/share/kmap
220 dumpkmap > ${rootfs}/usr/share/kmap/default; check
222 # Kilo editor (20K) with syntax highlight and search (Thanks Paul :-)
223 action "Installing: kilo text editor"
224 cp -a ${tools}/kilo ${rootfs}/usr/bin; check
226 # Ncursesw && dialog for sebos and additional tools
227 install_files "/lib/libncursesw.so*" "/lib/libtinfo.so*"
228 mkdir -p ${rootfs}/usr/share/terminfo/l
229 cp /usr/share/terminfo/l/linux ${rootfs}/usr/share/terminfo/l
230 install_files "/usr/bin/dialog" "/etc/dialogrc"
232 # /lib/libseb.sh & sebos config tool
233 echo -n "Installing: /lib/libseb.sh"
234 cp ${libseb} ${rootfs}/lib; check
235 action "Installing: sebos config tool"
236 cp ${tools}/sebos ${rootfs}/usr/bin; check
238 # httphelper.sh for amazing CGI/Shell functions
239 mkdir -p ${rootfs}/usr/lib/slitaz
240 cp /usr/lib/slitaz/httphelper.sh ${rootfs}/usr/lib/slitaz
242 # Packages TODO: handle deps
243 touch ${rootfs}/var/lib/packages
244 for pkg in $(ls $sebpkgs); do
245 echo -n $(colorize 035 "Installing package:"); info 036 "$pkg"
246 . ${sebpkgs}/${pkg}
247 seb_install; echo "$pkg|$desc" >> ${rootfs}/var/lib/packages
248 done
250 # Custom files NOW
251 if [ -d "$sebfs" ]; then
252 echo -n "Copying custom files from sebfs..."
253 cp -rf ${sebfs}/* ${rootfs}; check
254 fi
256 # COPYING
257 mkdir -p ${rootfs}/usr/share/licenses
258 cat > ${rootfs}/usr/share/licenses/COPYING << EOT
259 Copyright (c) 2007-$(date '+%Y') SliTaz GNU/Linux
261 SliTaz is free software; you can redistribute it and/or modify it under
262 the terms of the GNU General Public License as published by the Free
263 Software Foundation; either version 3 of the License, or (at your option)
264 any later version.
266 SliTaz is distributed in the hope that it will be useful, but WITHOUT ANY
267 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
268 FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
270 EOT
272 # Build date
273 cat >> ${rootfs}/etc/seb.conf << EOT
275 # Seb-OS Build date
276 build_date="$(date '+%Y%m%d')"
278 # Seb-OS/SliTaz release string
279 seb_os_release="$(cat /etc/slitaz-release)"
281 EOT
283 # Security check
284 chown -R 0.0 ${rootfs}
285 chmod 0600 ${rootfs}/etc/busybox.conf
287 # Rootfs archive
288 action "Creating the initramfs..."
289 cd ${rootfs}
290 find . -print | cpio -o -H newc | lzma e -si -so \
291 > ${rootiso}/boot/rootfs.gz 2>/dev/null
292 check
294 # Linux Kernel
295 echo -n "Copying the Linux kernel..."
296 cp ${linux} ${rootiso}/boot/bzImage; check
298 # Bootloader
299 echo -n "Copying the bootloader (isolinux)..."
300 cp ${tools}/isolinux.bin ${rootiso}/boot/isolinux; check
302 echo -n "Creating bootloader configs..."
303 cat > ${rootiso}/boot/isolinux/isolinux.cfg << EOF
304 display display.txt
305 default seb
306 label seb
307 kernel /boot/bzImage
308 append initrd=/boot/rootfs.gz rw root=/dev/null rdinit=/sbin/init loglevel=5
309 implicit 0
310 prompt 1
311 timeout 40
312 EOF
313 cat > ${rootiso}/boot/isolinux/display.txt << EOF
315 ____ _ _ _ _ ___ _
316 / ___| \ | | | | | / / | (_)_ __ _ ___ __
317 | | _| \| | | | |/ /| | | | '_ \| | | \ \/ /
318 | |_| | |\ | |_| / / | |___| | | | | |_| |> <
319 \____|_| \_|\___/_/ |_____|_|_| |_|\__,_/_/\_\
321 SliTaz Embedded OS - Press <ENTER> to boot
322 www.slitaz.org
325 EOF
326 check
328 echo -n "Checking rootfs size..."
329 info 035 $(du -mhs $rootfs | awk '{print $1}')
330 echo -n "Installed files in rootfs..."
331 info 036 $(find ${rootfs} -type f | wc -l)
333 # ISO image
334 geniso; footer
335 }
337 # Handle seb packages
338 packages_handler() {
340 # List available packages
341 if [ ! "$1" ]; then
342 title "Seb packages"
343 for pkg in $(ls ${packages}); do
344 . ${packages}/${pkg}
345 echo -n "$(colorize 036 $pkg)"; indent 20 "$desc"
346 unset desc deps
347 done
348 footer && exit 0
349 fi
351 # Add package(s)
352 if [ "$add" ]; then
353 for pkg in ${@}; do
354 case "$pkg" in
355 --*) continue ;;
356 *)
357 if [ -f "$packages/$pkg" ]; then
358 echo -n "Adding package: $pkg"
359 mkdir -p ${sebpkgs}
360 cp -f ${packages}/${pkg} ${sebpkgs}; check
361 else
362 echo "Can't find package: $package/$pkg"
363 fi ;;
364 esac
365 done
366 fi
367 }
369 # Handle vdisk: create, check, mount, umount
370 vdisk_hanler() {
371 title "SEB Virtual disk"
372 vsize=60
373 root=${vdisk%.img}
375 # Info or create
376 if [ -f "$vdisk" ]; then
377 echo -n "Virtual disk: $vdisk"
378 info 035 "$(du -mhs $vdisk | awk '{print $1}')"
379 else
380 echo "Creating virtual disk image..."
381 dd if=/dev/zero of=${vdisk} bs=1M count=${vsize}
382 action "Creating ext3 filesystem..."; echo
383 mkfs.ext3 -L "SebOShome" ${vdisk}
384 fi
386 # Check
387 if [ "$check" ]; then
388 echo "Umounting vdisk before: e2fsck -p"
389 umount ${vdisk} >/dev/null
390 e2fsck -p ${vdisk}
391 fi
393 # Action: mount/unmount
394 if ! mount | grep -q "^$vdisk"; then
395 echo -n "Mounting virtual disk..."; mkdir -p ${root}
396 mount -o loop -t ext3 ${vdisk} ${root}; status
397 else
398 echo -n "Unmounting virtual disk..."
399 umount ${vdisk}; status; sleep 1
400 fi; footer
401 }
403 #
404 # Commands
405 #
407 case "$1" in
409 -i|init)
410 rootfs="$sebfs"
411 echo -n "Creating files in: ${rootfs}"
412 init; check ;;
414 -b|build)
415 check_root
416 build
417 [ "$emu" ] && emulate ${work}/${iso} ;;
419 -g|geniso)
420 check_root
421 geniso ;;
423 -c|clean)
424 check_root
425 echo -n "Cleaning: ${work}"
426 [ "$all" ] && rm -rf ${sebfs} ${cache}
427 rm -rf ${rootfs}* ${rootiso} ${work}/*.iso
428 check ;;
430 -p|package*)
431 shift
432 packages_handler "$@" ;;
434 -v|vdisk)
435 vdisk_hanler ;;
437 -l|lsfs)
438 title "Listing: $rootfs"
439 cd ${rootfs}; find . -type f | sed s'/^.//'g
440 footer "Rootfs files: $(find . -type f | wc -l)" ;;
442 -e|emu)
443 title "Emulating: $iso"
444 emulate ${work}/${iso}
445 footer ;;
447 env)
448 title "SEB environment"
449 cat << EOT
450 work=$work
451 rootfs=$rootfs
452 rootiso=$rootiso
453 sebfs=$sebfs
454 sebpkgs=$sebpkgs
455 cache=$cache
456 vdisk=$vdisk
457 tools=$tools
458 initfs=$initfs
459 packages=$packages
460 iso=$iso
461 linux=$linux
462 EOT
463 footer ;;
465 -t|testsuite)
466 # Development purpose
467 ${tools}/libseb.sh ;;
469 *) help ;;
471 esac && exit 0