mrrl

Minimal Reliable Reproducible Linux
git clone https://ccx.te2000.cz/git/mrrl
Log | Files | Refs | Submodules | README

system-config-init (5664B)


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