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