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 85937e3de0b0
children 68f0c6c3b1d3
files seb
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/seb	Mon Mar 06 16:42:09 2017 +0100
     1.3 @@ -0,0 +1,465 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# SEB - SliTaz Embedded Builder - Back to the roots with SliTaz in
     1.7 +# only a few MB. Please read the README file for more information.
     1.8 +#
     1.9 +# Copyright (C) 2017 SliTaz GNU/Linux - BSD License
    1.10 +# Author: Christophe Lincoln <pankso@slitaz.org>
    1.11 +#
    1.12 +. /lib/libtaz.sh
    1.13 +unset status get_cols separator colorize boldify
    1.14 +
    1.15 +# --iso --work --linux can be set in config file or from cmdline
    1.16 +[ -f "build.conf" ] && . build.conf
    1.17 +
    1.18 +[ "$work" ]  || work="$(pwd)"
    1.19 +[ "$iso" ]   || iso="seb-os-$(date '+%Y%m%d').iso"
    1.20 +[ "$linux" ] || linux="/boot/vmlinuz-$(uname -r)"
    1.21 +
    1.22 +rootfs="$work/rootfs"
    1.23 +rootiso="$work/rootiso"
    1.24 +sebfs="$work/sebfs"
    1.25 +sebpkgs="$work/sebpkgs"
    1.26 +cache="$work/cache"
    1.27 +vdisk="$work/sebhome.img"
    1.28 +qemu_opts="-m 512"
    1.29 +
    1.30 +tools="/usr/share/seb/tools"
    1.31 +initfs="/usr/share/seb/initfs"
    1.32 +packages="/usr/share/seb/packages"
    1.33 +
    1.34 +# Working from source tree
    1.35 +[ -d "tools" ]  && tools="$(pwd)/tools"
    1.36 +[ -d "initfs" ] && initfs="$(pwd)/initfs"
    1.37 +[ -d "packages" ] && packages="$(pwd)/packages"
    1.38 +
    1.39 +# Source libseb.sh (overwrite libtaz.sh function)
    1.40 +if [ -f "$PWD/libseb.sh" ]; then
    1.41 +	libseb="$PWD/libseb.sh"
    1.42 +	. ${libseb}
    1.43 +else
    1.44 +	if ! . /lib/libseb.sh; then
    1.45 +		echo "Can't source any: libseb.sh"; exit 1
    1.46 +	fi
    1.47 +	libseb="/lib/libseb.sh"
    1.48 +fi
    1.49 +debug "work=$work"
    1.50 +
    1.51 +#
    1.52 +# Functions
    1.53 +#
    1.54 +
    1.55 +help() {
    1.56 +	cat << EOT
    1.57 +
    1.58 +$(colorize 35 "SliTaz Embedded Builder")
    1.59 +
    1.60 +$(boldify "Usage:") $(basename $0) [command] [--opts]
    1.61 +
    1.62 +$(boldify "Commands:")
    1.63 +  -h  help       Display this short built-in help
    1.64 +  -i  init       Creat base files to customize
    1.65 +  -b  build      Generate a distribution (initramfs & iso)
    1.66 +  -p  packages   Handle packages: --list --add
    1.67 +  -g  geniso     Re-generate the ISO image
    1.68 +  -c  clean      Remove all SEB generated files
    1.69 +  -l  lsfs       List all files in rootfs (-type f)
    1.70 +  -v  vdisk      Create, mount or unmount a virtual disk
    1.71 +  -e  emu        Emulate ISO image with Qemu
    1.72 +      env        Print current seb environment
    1.73 +
    1.74 +$(boldify "Options:")
    1.75 +  --work=        Path to build directory
    1.76 +  --iso=         Specify SliTaz Embedded ISO image name
    1.77 +  --linux=       Path to a custom Linux kernel
    1.78 +  --all          Clean all files including sebfs
    1.79 +  --emu          Emulate ISO image after build
    1.80 +  --check        Check a virtual disk image
    1.81 +
    1.82 +EOT
    1.83 +}
    1.84 +
    1.85 +# Initial files who can be modified via sebfs/
    1.86 +init() {
    1.87 +	mkdir -p ${sebfs} ${sebpkgs}
    1.88 +	cp -rf ${initfs}/* ${sebfs}
    1.89 +}
    1.90 +
    1.91 +geniso() {
    1.92 +	echo -n "Generating ISO image: ${iso}"
    1.93 +	cd ${work}
    1.94 +	genisoimage -R -o ${iso} \
    1.95 +		-b boot/isolinux/isolinux.bin \
    1.96 +		-c boot/isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
    1.97 +		-V "SliTaz Embedded" -input-charset iso8859-1 \
    1.98 +		-boot-info-table rootiso 2> /dev/null; check
    1.99 +	echo -n "Checking ISO image size..."
   1.100 +	info 035 $(du -mhs $iso | awk '{print $1}')
   1.101 +}
   1.102 +
   1.103 +emulate() {
   1.104 +	if [ -x "/usr/bin/qemu" ]; then
   1.105 +		echo "qemu $qemu_opts -cdrom $1"
   1.106 +		if [ -f "$vdisk" ]; then
   1.107 +			hda="-hda $vdisk"
   1.108 +			umount ${vdisk} 2>/dev/null
   1.109 +		fi
   1.110 +		qemu ${qemu_opts} ${hda} -cdrom ${1}
   1.111 +	else
   1.112 +		echo $(echo "Please install:") $(boldify qemu); exit 0
   1.113 +	fi
   1.114 +}
   1.115 +
   1.116 +# Install files in the rootfs with same path than on the build host
   1.117 +# Usage: install_files "/file/1" "/lib/lib.so*" "/file/N" 
   1.118 +install_files() {
   1.119 +	for file in ${@} ; do
   1.120 +		path=$(dirname $file)
   1.121 +		echo -n "Installing: $file"
   1.122 +		cp -a ${file} ${rootfs}${path} && check
   1.123 +	done
   1.124 +}
   1.125 +
   1.126 +# Populate /dev + 'mdev -s' on boot
   1.127 +create_dev_files() {	
   1.128 +	echo -n "Populating: /dev"
   1.129 +	mkdir -p ${rootfs}/dev/pts ${rootfs}/dev/shm
   1.130 +	cd ${rootfs}/dev
   1.131 +	mknod -m 0666 null c 1 3
   1.132 +	mknod -m 0622 console c 5 1
   1.133 +	mknod -m 0666 tty c 5 0
   1.134 +	for i in 0 1 2; do
   1.135 +		mknod -m 0666 tty$i c 4 $i
   1.136 +	done; check
   1.137 +}
   1.138 +
   1.139 +# Configuration files for /etc not generate by init to keep initfs/sebfs
   1.140 +# filesytems minimals
   1.141 +create_etc_files() {
   1.142 +	echo -n "Creating config files in: /etc"
   1.143 +	(echo "127.0.0.1      localhost seb" > ${rootfs}/etc/hosts
   1.144 +	echo "localnet    127.0.0.1" > ${rootfs}/etc/networks
   1.145 +	echo "order hosts,bind" > ${rootfs}/etc/host.conf
   1.146 +	echo "multi on" >> ${rootfs}/etc/host.conf
   1.147 +	# Users & passwd
   1.148 +	echo "root:x:0:0:root:/root:/bin/sh" > ${rootfs}/etc/passwd
   1.149 +	echo "root::13525:0:99999:7:::" > ${rootfs}/etc/shadow
   1.150 +	cat > ${rootfs}/etc/group << EOT
   1.151 +root:x:0:
   1.152 +www:x:80:
   1.153 +EOT
   1.154 +	cat > ${rootfs}/etc/gshadow << EOT
   1.155 +root:*::
   1.156 +www:!::
   1.157 +EOT
   1.158 +	chmod 640 ${rootfs}/etc/*shadow)
   1.159 +	cat > ${rootfs}/etc/nsswitch.conf << EOT
   1.160 +# /etc/nsswitch.conf: GNU Name Service Switch config.
   1.161 +#
   1.162 +
   1.163 +passwd:     files
   1.164 +group:      files
   1.165 +shadow:     files
   1.166 +
   1.167 +hosts:      files dns
   1.168 +networks:   files
   1.169 +EOT
   1.170 +	check
   1.171 +}
   1.172 +
   1.173 +build() {
   1.174 +	title "Starting SliTaz Embedded builder"
   1.175 +
   1.176 +	# Build environment
   1.177 +	rm -rf ${rootfs} ${rootiso}
   1.178 +	mkdir -p ${rootfs} ${rootiso}/boot/isolinux
   1.179 +
   1.180 +	# FSH Tree
   1.181 +	echo -n "Populating: filesystem"
   1.182 +	for d in bin dev etc lib root run home proc media sbin sys \
   1.183 +		usr/bin usr/sbin usr/share var/log var/cache var/lib
   1.184 +	do
   1.185 +		mkdir -p ${rootfs}/${d}
   1.186 +	done
   1.187 +	install -d -m 1777 ${rootfs}/tmp; check
   1.188 +	
   1.189 +	# /etc + /dev
   1.190 +	create_etc_files
   1.191 +	create_dev_files
   1.192 +
   1.193 +	# Initial sebfs from initfs: files can be modified
   1.194 +	[ ! -d "$sebfs" ] && init
   1.195 +
   1.196 +	# GNU libc before chroot /bin/busybox --install + name resolution
   1.197 +	for lib in /lib/libm[-.]* /lib/libc[-.]* /lib/ld-* /lib/libpthread*; do
   1.198 +		echo -n "Installing: $lib"
   1.199 +		cp -a ${lib} ${rootfs}/lib && check
   1.200 +	done
   1.201 +	install_files "/lib/libnss_dns*" "/lib/libnss_file*" "/lib/libresolv*"
   1.202 +
   1.203 +	# Busybox applets
   1.204 +	action "Installing: busybox"
   1.205 +	cp -a /bin/busybox ${rootfs}/bin
   1.206 +	chroot ${rootfs} /bin/busybox --install -s; check
   1.207 +
   1.208 +	# Busybox configs
   1.209 +	echo -n "Installing: busybox configs"
   1.210 +	cp -r /usr/share/udhcpc ${rootfs}/usr/share
   1.211 +	cp -f /etc/udhcpd.conf ${rootfs}/etc
   1.212 +	cp -f /etc/httpd.conf ${rootfs}/etc
   1.213 +	check
   1.214 +	
   1.215 +	# Busybox keymap
   1.216 +	echo -n "Dumping   : keymap"
   1.217 +	mkdir -p ${rootfs}/usr/share/kmap
   1.218 +	dumpkmap > ${rootfs}/usr/share/kmap/default; check
   1.219 +	
   1.220 +	# Kilo editor (20K) with syntax highlight and search (Thanks Paul :-)
   1.221 +	action "Installing: kilo text editor"
   1.222 +	cp -a ${tools}/kilo ${rootfs}/usr/bin; check
   1.223 +	
   1.224 +	# Ncursesw && dialog for sebos and additional tools
   1.225 +	install_files "/lib/libncursesw.so*" "/usr/bin/dialog"
   1.226 +	mkdir -p ${rootfs}/usr/share/terminfo/l
   1.227 +	cp /usr/share/terminfo/l/linux ${rootfs}/usr/share/terminfo/l
   1.228 +	cp /etc/dialogrc ${rootfs}/etc
   1.229 +	
   1.230 +	# /lib/libseb.sh & sebos config tool
   1.231 +	echo -n "Installing: /lib/libseb.sh"
   1.232 +	cp ${libseb} ${rootfs}/lib; check
   1.233 +	action "Installing: sebos config tool"
   1.234 +	cp ${tools}/sebos ${rootfs}/usr/bin; check
   1.235 +	
   1.236 +	# httphelper.sh for amazing CGI/Shell functions
   1.237 +	mkdir -p ${rootfs}/usr/lib/slitaz
   1.238 +	cp /usr/lib/slitaz/httphelper.sh ${rootfs}/usr/lib/slitaz
   1.239 +	
   1.240 +	# Packages TODO: handle deps
   1.241 +	touch ${rootfs}/var/lib/packages
   1.242 +	for pkg in $(ls $sebpkgs); do
   1.243 +		echo -n $(colorize 035 "Installing package:"); info 036 "$pkg"
   1.244 +		. ${packages}/${pkg}
   1.245 +		seb_install; echo "$pkg|$desc" >> ${rootfs}/var/lib/packages
   1.246 +	done
   1.247 +	
   1.248 +	# Custom files NOW
   1.249 +	if [ -d "$sebfs" ]; then
   1.250 +		echo -n "Copying custom files from sebfs..."
   1.251 +		cp -rf ${sebfs}/* ${rootfs}; check
   1.252 +	fi
   1.253 +
   1.254 +	# COPYING
   1.255 +	mkdir -p ${rootfs}/usr/share/licenses
   1.256 +	cat > ${rootfs}/usr/share/licenses/COPYING << EOT
   1.257 +Copyright (c)  2007-$(date '+%Y')  SliTaz GNU/Linux
   1.258 +
   1.259 +SliTaz is free software; you can redistribute it and/or modify it under
   1.260 +the terms of the GNU General Public License as published by the Free
   1.261 +Software Foundation; either version 3 of the License, or (at your option)
   1.262 +any later version.
   1.263 +
   1.264 +SliTaz is distributed in the hope that it will be useful, but WITHOUT ANY
   1.265 +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   1.266 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
   1.267 +
   1.268 +EOT
   1.269 +	
   1.270 +	# Build date
   1.271 +	cat >> ${rootfs}/etc/seb.conf << EOT
   1.272 +
   1.273 +# Seb-OS Build date
   1.274 +build_date="$(date '+%Y%m%d')"
   1.275 +
   1.276 +# Seb-OS/SliTaz release string
   1.277 +seb_os_release="$(cat /etc/slitaz-release)"
   1.278 +
   1.279 +EOT
   1.280 +	
   1.281 +	# Security check
   1.282 +	chmod 0600 ${rootfs}/etc/busybox.conf
   1.283 +
   1.284 +	# Rootfs archive
   1.285 +	action "Creating the initramfs..."
   1.286 +	cd ${rootfs}
   1.287 +	find . -print | cpio -o -H newc | lzma e -si -so \
   1.288 +		> ${rootiso}/boot/rootfs.gz 2>/dev/null
   1.289 +	check
   1.290 +
   1.291 +	# Linux Kernel
   1.292 +	echo -n "Copying the Linux kernel..."
   1.293 +	cp ${linux} ${rootiso}/boot/bzImage; check
   1.294 +
   1.295 +	# Bootloader
   1.296 +	echo -n "Copying the bootloader (isolinux)..."
   1.297 +	cp ${tools}/isolinux.bin ${rootiso}/boot/isolinux; check
   1.298 +
   1.299 +	echo -n "Creating bootloader configs..."
   1.300 +	cat > ${rootiso}/boot/isolinux/isolinux.cfg << EOF
   1.301 +display display.txt
   1.302 +default seb
   1.303 +label seb
   1.304 +	kernel /boot/bzImage
   1.305 +	append initrd=/boot/rootfs.gz rw root=/dev/null rdinit=/sbin/init
   1.306 +implicit 0
   1.307 +prompt 1
   1.308 +timeout 40
   1.309 +EOF
   1.310 +	cat > ${rootiso}/boot/isolinux/display.txt << EOF
   1.311 +
   1.312 +           ____ _   _ _   _   ___     _
   1.313 +          / ___| \ | | | | | / / |   (_)_ __  _   ___  __ 
   1.314 +         | |  _|  \| | | | |/ /| |   | | '_ \| | | \ \/ / 
   1.315 +         | |_| | |\  | |_| / / | |___| | | | | |_| |>  < 
   1.316 +          \____|_| \_|\___/_/  |_____|_|_| |_|\__,_/_/\_\ 
   1.317 +        
   1.318 +             SliTaz Embedded OS - Press <ENTER> to boot
   1.319 +                        www.slitaz.org
   1.320 +
   1.321 +
   1.322 +EOF
   1.323 +	check
   1.324 +	
   1.325 +	echo -n "Checking rootfs size..."
   1.326 +	info 035 $(du -mhs $rootfs | awk '{print $1}')
   1.327 +	echo -n "Installed files in rootfs..."
   1.328 +	info 036 $(find ${rootfs} -type f | wc -l)
   1.329 +	
   1.330 +	# ISO image
   1.331 +	geniso; footer
   1.332 +}
   1.333 +
   1.334 +# Handle seb packages
   1.335 +packages_handler() {
   1.336 +	
   1.337 +	# List avalaible packages
   1.338 +	if [ ! "$1" ]; then
   1.339 +		title "Seb packages"
   1.340 +		for pkg in $(ls ${packages}); do
   1.341 +			. ${packages}/${pkg}
   1.342 +			echo -n "$(colorize 036 $pkg)"; indent 20 "$desc"
   1.343 +			unset desc deps
   1.344 +		done
   1.345 +		footer && exit 0
   1.346 +	fi
   1.347 +	
   1.348 +	# Add package(s)
   1.349 +	if [ "$add" ]; then
   1.350 +		for pkg in ${@}; do
   1.351 +			case "$pkg" in
   1.352 +				--*) continue ;;
   1.353 +				*)
   1.354 +					if [ -f "$packages/$pkg" ]; then
   1.355 +						echo -n "Addind package: $pkg"
   1.356 +						mkdir -p ${sebpkgs}
   1.357 +						cp -f ${packages}/${pkg} ${sebpkgs}; check
   1.358 +					else
   1.359 +						echo "Can't find package: $package/$pkg"
   1.360 +					fi ;;
   1.361 +			esac
   1.362 +		done
   1.363 +	fi
   1.364 +}
   1.365 +
   1.366 +# Handle vdisk: create, check, mount, umount
   1.367 +vdisk_hanler() {
   1.368 +	title "SEB Virtual disk"
   1.369 +	vsize=40960
   1.370 +	root=${vdisk%.img}
   1.371 +	
   1.372 +	# Info or create
   1.373 +	if [ -f "$vdisk" ]; then
   1.374 +		echo -n "Virtual disk: $vdisk"
   1.375 +		info 035 "$(du -mhs $vdisk | awk '{print $1}')"
   1.376 +	else
   1.377 +		echo "Creating virtual disk image..."
   1.378 +		dd if=/dev/zero of=${vdisk} bs=1k count=${vsize}
   1.379 +		action "Creating ext3 filesystem..."; echo
   1.380 +		mkfs.ext3 -L "SebOShome" ${vdisk}
   1.381 +	fi
   1.382 +	
   1.383 +	# Check
   1.384 +	if [ "$check" ]; then
   1.385 +		echo "Umounting vdisk before: e2fsck -p"
   1.386 +		umount ${vdisk} >/dev/null
   1.387 +		e2fsck -p ${vdisk}
   1.388 +	fi
   1.389 +	
   1.390 +	# Action: mount/unmount
   1.391 +	if ! mount | grep -q "^$vdisk"; then
   1.392 +		echo -n "Mounting virtual disk..."; mkdir -p ${root} 
   1.393 +		mount -o loop -t ext3 ${vdisk} ${root}; status
   1.394 +	else
   1.395 +		echo -n "Unmounting virtual disk..."
   1.396 +		umount ${vdisk}; status; sleep 1
   1.397 +	fi; footer
   1.398 +}
   1.399 +
   1.400 +#
   1.401 +# Commands
   1.402 +#
   1.403 +
   1.404 +case "$1" in
   1.405 +
   1.406 +	-i|init)
   1.407 +		rootfs="$sebfs"
   1.408 +		echo -n "Creating files in: ${rootfs}"
   1.409 +		init; check ;;
   1.410 +
   1.411 +	-b|build)
   1.412 +		check_root
   1.413 +		build 
   1.414 +		[ "$emu" ] && emulate ${work}/${iso} ;;
   1.415 +
   1.416 +	-g|geniso)
   1.417 +		check_root
   1.418 +		geniso ;;
   1.419 +
   1.420 +	-c|clean)
   1.421 +		check_root
   1.422 +		echo -n "Cleaning: ${work}"
   1.423 +		[ "$all" ] && rm -rf ${sebfs} ${cache}
   1.424 +		rm -rf ${rootfs}* ${rootiso} ${work}/*.iso
   1.425 +		check ;;
   1.426 +	
   1.427 +	-p|package*)
   1.428 +		shift
   1.429 +		packages_handler "$@" ;;
   1.430 +	
   1.431 +	-v|vdisk)
   1.432 +		vdisk_hanler ;;
   1.433 +
   1.434 +	-l|lsfs)
   1.435 +		title "Listing: $rootfs"
   1.436 +		cd ${rootfs}; find . -type f | sed s'/^.//'g
   1.437 +		footer "Rootfs files: $(find . -type f | wc -l)" ;;
   1.438 +	
   1.439 +	emu)
   1.440 +		title "Emulating: $iso"
   1.441 +		emulate ${work}/${iso} 
   1.442 +		footer ;;
   1.443 +
   1.444 +	env)
   1.445 +		title "SEB environment"
   1.446 +		cat << EOT
   1.447 +work=$work
   1.448 +rootfs=$rootfs
   1.449 +rootiso=$rootiso
   1.450 +sebfs=$sebfs
   1.451 +sebpkgs=$sebpkgs
   1.452 +cache=$cache
   1.453 +vdisk=$vdisk
   1.454 +tools=$tools
   1.455 +initfs=$initfs
   1.456 +packages=$packages
   1.457 +iso=$iso
   1.458 +linux=$linux
   1.459 +EOT
   1.460 +		footer ;;
   1.461 +	
   1.462 +	-t|testsuite)
   1.463 +		# Development purpose
   1.464 +		${tools}/libseb.sh ;;
   1.465 +
   1.466 +	*) help ;;
   1.467 +
   1.468 +esac && exit 0