pthbs

Packaging Through Hashed Build Scripts
git clone https://ccx.te2000.cz/git/pthbs
Log | Files | Refs | Submodules | README

core-system-init (4846B)


      1 #!/usr/bin/env pthbs-build
      2 #+busybox.62e4b1201b20ef0faf051b0e9b3ad1d3fbf4d498b8a0910ce4845a9017323f62
      3 #+s6.be82395a9f4e8e0e08dc9900d7b4959e46db9d13d68355a6c5afebaf9237beb4
      4 #+s6-rc.89bacaaa86979b436e879c3e8fecb8a0a65998401423b8df358344e9da462c76
      5 #+s6-portable-utils.973a62b4cfd3a1a9caa04edd1f0e5ce3d48b4fae15342fbebbd70c866e7f1a79
      6 #+s6-linux-init.e61bfeb8577f8d8a68e5e18e878a60e4c959dd23628d625cb59c39342a20a4a2
      7 #+execline.09706e0446392c1dc9f3d83c47704aadb8ec15c063317d139d0143cf59349b0f
      8 #+core-system-rc.6498ea08413878c03ec39818bb30ba875befe5495376a530592d920c2ec71400
      9 
     10 s6rcdb=/home/ccx/versions/core-system-rc.6498ea08413878c03ec39818bb30ba875befe5495376a530592d920c2ec71400/s6-rc-db
     11 prefix=/home/ccx/versions/$pthbs_package
     12 pkgdir="$pthbs_destdir/$prefix"
     13 
     14 q() {
     15 	"s6-quote" "$@"
     16 }
     17 
     18 qx() {
     19 	exe=$(realpath "$(which "$1")")
     20 	name=$(basename "$1")
     21 	if ! test -x "$exe"; then
     22 		printf 'Error: executable not found: %s\n' "$1"
     23 	fi
     24 	mkdir -p "$pkgdir/deps/command"
     25 	ln -sf $exe "$pkgdir/deps/command/$name"
     26 	"s6-quote" "$prefix/deps/command/$name"
     27 }
     28 
     29 # Generate init ($pkgdir must not exist but parent dir does)
     30 mkdir -p "$pthbs_destdir//home/ccx/versions"
     31 s6-linux-init-maker \
     32 	-p "/command:/usr/local/bzr/all/sbin:/usr/local/bzr/all/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
     33 	-G "$(q "prefix/deps/command/env") TERM=linux $(q "prefix/deps/command/getty") 38400 tty9" \
     34 	-t 1 \
     35 	-c "$prefix" \
     36 	"$pkgdir"
     37 
     38 
     39 # Link deps that are passed to s6-l-i-m
     40 qx env
     41 qx getty
     42 
     43 # Link execlineb - can't quote shebang
     44 qx execlineb
     45 el=$prefix/deps/command/$name
     46 
     47 # Rewrite dynamically generated scripts
     48 s6-cat >"$pkgdir/scripts/rc.init" <<EOF || exit $?
     49 #!$el -S1
     50 $(qx export) TERM "linux"
     51 $(qx foreground) { $(qx s6-echo) "* Starting s6-rc from "$(q "$s6rcdb") }
     52 $(qx if) { $(qx s6-rc-init) -d -c $(q "$s6rcdb") /run/service }
     53 $prefix/scripts/runlevel \$1
     54 EOF
     55 
     56 s6-cat >"$pkgdir/scripts/rc.shutdown" <<EOF || exit $?
     57 #!$el -P
     58 
     59 ### Things to do before hardware halt/reboot/poweroff.
     60 ### Ideally, it should be a single call to the service manager,
     61 ### telling it to bring all the services down.
     62 
     63 $(qx redirfd) -w 1 /dev/console
     64 $(qx redirfd) -w 2 /dev/console
     65 $(qx s6-rc) -v2 -bDa change
     66 EOF
     67 
     68 s6-cat >"$pkgdir/scripts/runlevel" <<EOF || exit $?
     69 #!$el -S1
     70 export TERM "linux"
     71 
     72 # The requested runlevel is in "\$1" but currently ignored.
     73 
     74 # Run the service manager.
     75 # Call it twice because the first time may spuriously "timeout"
     76 # due to the clock jumping forward.
     77 
     78 $(qx ifelse) -X {
     79 	$(qx s6-rc) -v 2 -t 600000 -- change ok-all-but-tty 
     80 } {
     81 	# no timeout, start ttys properly
     82 	$(qx foreground) { $(qx s6-echo) "* System started normally (bundle ok-all-but-tty)" }
     83 	$(qx foreground) { sleep 0.3 }
     84 	$(qx foreground) { $(qx s6-svc) -d /run/service/console-log-tail }
     85 	$(qx s6-rc) -v 2 -t 600000 -- change ok-all
     86 }
     87 $(qx s6-echo) "* System startup failure: retry"
     88 # try again without tty and then with
     89 $(qx foreground) { $(qx s6-rc) -v 2 -t 600000 -- change ok-all-but-tty }
     90 $(qx foreground) { $(qx sleep) 0.1 }
     91 $(qx foreground) { $(qx s6-svc) -d /run/service/console-log-tail }
     92 $(qx s6-rc) -v 2 -t 600000 -- change ok-all
     93 EOF
     94 
     95 s6-mkdir "$pkgdir/run-image/service/console-log-tail" || exit $?
     96 s6-cat >"$pkgdir/run-image/service/console-log-tail/run" <<EOF || exit $?
     97 #!$el -P
     98 $(qx redirfd) -w 1 /dev/console
     99 $(qx fdmove) -c 2 1
    100 $(qx pipeline) -w { $(qx s6-tai64nlocal) }
    101 $(qx tail) -F -n +1 /run/uncaught-logs/current
    102 EOF
    103 s6-chmod 755 "$pkgdir/run-image/service/console-log-tail/run" exit $?
    104 
    105 
    106 cd "$pthbs_destdir/home/ccx/versions/$pthbs_package"
    107 find -type d -o -print | awk -F/ '
    108 BEGIN {
    109 	x["./bin/init"]=1
    110 	x["./bin/halt"]=1
    111 	x["./bin/poweroff"]=1
    112 	x["./bin/reboot"]=1}
    113 
    114 function r1(s) {
    115 	sub("^[.]/[^/]*", ".", s)
    116 	return s
    117 }
    118 function s1(repl, s) {
    119 	sub("^[.]/[^/]*", "./"repl, s)
    120 	return s
    121 }
    122 function link(src) {
    123 	x[$0]=0
    124 	printf "%s\t%s\n", $0, src
    125 	printf "genlinks >>%s\t%s<<\n", $0, src >>"/dev/stderr"
    126 }
    127 $1!="."{exit 1}
    128 
    129 
    130 $2 == "command" { link($0); next }
    131 $2 == "bin" { link(s1("command", $0)); next }
    132 
    133 $2 == "library.so" { link($0); next }
    134 $2 == "library" { link($0); next }
    135 $2 == "lib" && $NF ~ /\.l?a$/ { link(s1("library", $0)); next }
    136 $2 == "lib" && $NF ~ /\.so(|\..*)$/ { link(s1("library.so", $0)); next }
    137 
    138 $2 == "share" && $3 ~ /^(info|man|doc|icons|terminfo)$/ { link(r1($0)); next }
    139 
    140 $2 == "man" { link($0); next }
    141 $2 == "info" { link($0); next }
    142 $2 == "doc" { link($0); next }
    143 $2 == "icons" { link($0); next }
    144 $2 == "terminfo" { link($0); next }
    145 
    146 $2 == "include" { link($0); next }
    147 
    148 { printf "genlinks ##%s## skipped\n", $0 >>"/dev/stderr" }
    149 
    150 END {
    151 	for(fname in x) { printf "DEBUG: x[\"%s\"]=\"%s\"\n", fname, x[fname] >"/dev/stderr" }
    152 	for(fname in x) {
    153 		if(x[fname]) {
    154 			printf "ERROR: missing expected file \"%s\"\n", fname >"/dev/stderr"
    155 			exit 3
    156 		}
    157 	}
    158 }' >.install-links.new
    159 mv .install-links.new .install-links