pthbs

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

system-config-init (3610B)


      1 {% extends "genlinks" %}
      2 {%- block script %}
      3 #+{{pkg_install_name("busybox")}}
      4 #+{{pkg_install_name("s6")}}
      5 #+{{pkg_install_name("s6-rc")}}
      6 #+{{pkg_install_name("s6-portable-utils")}}
      7 #+{{pkg_install_name("s6-linux-init")}}
      8 #+{{pkg_install_name("execline")}}
      9 #+{{pkg_install_name("system-config-rc")}}
     10 
     11 s6rcdb={{pkg_install_dir("system-config-rc")}}/config/s6-rc-db
     12 prefix={{versions}}/$pthbs_package
     13 pkgdir="$pthbs_destdir/$prefix"
     14 
     15 q() {
     16 	"s6-quote" "$@"
     17 }
     18 
     19 qx() {
     20 	exe=$(realpath "$(which "$1")")
     21 	name=$(basename "$1")
     22 	if ! test -x "$exe"; then
     23 		printf 'Error: executable not found: %s\n' "$1"
     24 	fi
     25 	mkdir -p "$pkgdir/deps/command"
     26 	ln -sf $exe "$pkgdir/deps/command/$name"
     27 	"s6-quote" "$prefix/deps/command/$name"
     28 }
     29 
     30 # Generate init ($pkgdir must not exist but parent dir does)
     31 mkdir -p "$pthbs_destdir/{{versions}}"
     32 s6-linux-init-maker \
     33 	-p "/run/current/command:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" \
     34 	-G "$(q "$prefix/deps/command/env") TERM=linux $(q "$prefix/deps/command/getty") 38400 tty9" \
     35 	-t 1 \
     36 	-N \
     37 	-c "$prefix" \
     38 	"$pkgdir"
     39 
     40 
     41 # Link deps that are passed to s6-l-i-m
     42 qx env
     43 qx getty
     44 
     45 # Link execlineb - can't quote shebang
     46 qx execlineb
     47 el=$prefix/deps/command/$name
     48 
     49 # Rewrite bin/init as it fails finding it's own s6-linux-init
     50 s6-cat >"$pkgdir/bin/init.new" <<EOF || exit $?
     51 #!$el -S0
     52 $(qx pthbs-enter)
     53 $(qx s6-envdir) -I /run/current/env
     54 EOF
     55 s6-cat >>"$pkgdir/bin/init.new" <"$pkgdir/bin/init"
     56 chmod +x "$pkgdir/bin/init.new"
     57 mv -v "$pkgdir/bin/init.new" "$pkgdir/bin/init"
     58 
     59 # Rewrite dynamically generated scripts
     60 s6-cat >"$pkgdir/scripts/rc.init" <<EOF || exit $?
     61 #!$el -S1
     62 $(qx export) TERM "linux"
     63 $(qx foreground) { $(qx s6-echo) "* Starting s6-rc from "$(q "$s6rcdb") }
     64 $(qx if) { $(qx s6-rc-init) -d -c $(q "$s6rcdb") /run/service }
     65 $prefix/scripts/runlevel \$1
     66 EOF
     67 
     68 s6-cat >"$pkgdir/scripts/rc.shutdown" <<EOF || exit $?
     69 #!$el -P
     70 
     71 ### Things to do before hardware halt/reboot/poweroff.
     72 ### Ideally, it should be a single call to the service manager,
     73 ### telling it to bring all the services down.
     74 
     75 $(qx redirfd) -w 1 /dev/console
     76 $(qx redirfd) -w 2 /dev/console
     77 $(qx s6-rc) -v2 -bDa change
     78 EOF
     79 
     80 s6-cat >"$pkgdir/scripts/runlevel" <<EOF || exit $?
     81 #!$el -S1
     82 export TERM "linux"
     83 
     84 # The requested runlevel is in "\$1" but currently ignored.
     85 
     86 # Run the service manager.
     87 # Call it twice because the first time may spuriously "timeout"
     88 # due to the clock jumping forward.
     89 
     90 $(qx ifelse) -X {
     91 	$(qx s6-rc) -v 2 -t 600000 -- change ok-all-but-tty 
     92 } {
     93 	# no timeout, start ttys properly
     94 	$(qx foreground) { $(qx s6-echo) "* System started normally (bundle ok-all-but-tty)" }
     95 	$(qx foreground) { sleep 0.3 }
     96 	$(qx foreground) { $(qx s6-svc) -d /run/service/console-log-tail }
     97 	$(qx s6-rc) -v 2 -t 600000 -- change ok-all
     98 }
     99 $(qx foreground) { $(qx s6-echo) "* System startup failure: retry" }
    100 # try again without tty and then with
    101 $(qx foreground) { $(qx s6-rc) -v 2 -t 600000 -- change ok-all-but-tty }
    102 $(qx foreground) { $(qx sleep) 0.1 }
    103 $(qx foreground) { $(qx s6-svc) -d /run/service/console-log-tail }
    104 $(qx s6-rc) -v 2 -t 600000 -- change ok-all
    105 EOF
    106 
    107 s6-mkdir "$pkgdir/run-image/service/console-log-tail" || exit $?
    108 s6-cat >"$pkgdir/run-image/service/console-log-tail/run" <<EOF || exit $?
    109 #!$el -P
    110 $(qx redirfd) -w 1 /dev/console
    111 $(qx fdmove) -c 2 1
    112 $(qx pipeline) -w { $(qx s6-tai64nlocal) }
    113 $(qx tail) -F -n +1 /run/uncaught-logs/current
    114 EOF
    115 s6-chmod 755 "$pkgdir/run-image/service/console-log-tail/run" exit $?
    116 
    117 {% endblock -%}
    118 {% block genlinks_begin %}
    119 	x["./bin/init"]=1
    120 	x["./bin/halt"]=1
    121 	x["./bin/poweroff"]=1
    122 	x["./bin/reboot"]=1
    123 {%- endblock %}