system-config-init (5628B)
1 #!/usr/bin/env pthbs-build 2 #+busybox.539513a18a06a21e4660004fea30f3658959c5c7f54488b66b5fee7120b0c27c 3 #+busybox-findutils.3ba95afbbde5f39d6a3c0b17d82ff2e5350904cf19dd40acdf1c60ed126d3bda 4 #+busybox-awk.4ac003d165540d0084cd08c1386f136343851ef344ac5ff844769b058049d859 5 #+s6.f8253c2abb58178ecb00dce8c7ddf1f2a9442f92cc684a89bac43678e8b032ef 6 #+s6-rc.b34650e8572f705c4e28a97fc20a025ea567d94209d26f9d62eb4e5f50c88f25 7 #+s6-portable-utils.a7bb8fee72684768df2a17ffad5f24a7f7fa19b13596e954490f3ff8b6a00e51 8 #+s6-linux-init.5e20ff1210efb17f191e49fff731969a575bfa0395835de4b686b0722b69ad6f 9 #+execline.bbdee27ba351e082ed15132d873419815cb0bc19523a36af84571eab05c0e836 10 #+system-config-rc.d7f41da7298770784df5f59c77b4e74547debd2bf7a453dadd774945a7a6c1bc 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=/versions/$pthbs_package 31 dest=${pthbs_destdir%/}${prefix} 32 cd '.' 33 34 35 s6rcdb=/versions/system-config-rc.d7f41da7298770784df5f59c77b4e74547debd2bf7a453dadd774945a7a6c1bc/config/s6-rc-db 36 37 # Generate init (${dest} must not exist but parent dir does) 38 mkdir -p "$pthbs_destdir//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/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