| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Junk drawer of debugging funcitons
|
| 4 | #
|
| 5 | # Usage:
|
| 6 | # regtest/aports-debug.sh <function name>
|
| 7 |
|
| 8 | : ${LIB_OSH=stdlib/osh}
|
| 9 | source $LIB_OSH/bash-strict.sh
|
| 10 | source $LIB_OSH/task-five.sh
|
| 11 |
|
| 12 | source regtest/aports-common.sh
|
| 13 |
|
| 14 | chroot-manifest() {
|
| 15 | local name=${1:-foo}
|
| 16 |
|
| 17 | # TODO: use this to help plan OCI layers
|
| 18 | # 251,904 files after a build of mpfr
|
| 19 |
|
| 20 | local out=_tmp/$name.manifest.txt
|
| 21 |
|
| 22 | # pipefail may fail
|
| 23 | set +o errexit
|
| 24 | sudo find $CHROOT_DIR \
|
| 25 | -name proc -a -prune -o \
|
| 26 | -type f -a -printf '%P %s\n' |
|
| 27 | sort | tee $out
|
| 28 |
|
| 29 | echo
|
| 30 | echo "Wrote $out"
|
| 31 | }
|
| 32 |
|
| 33 | show-chroot() {
|
| 34 | sudo tree $CHROOT_HOME_DIR/oils/_tmp
|
| 35 | }
|
| 36 |
|
| 37 | sizes() {
|
| 38 | set +o errexit
|
| 39 |
|
| 40 | # 312 MB
|
| 41 | sudo du --si -s $CHROOT_DIR
|
| 42 |
|
| 43 | # 29 MB after 80 source packages, that's not so much
|
| 44 |
|
| 45 | # getting up to 373 M though - worth sharding
|
| 46 | sudo du --si -s $CHROOT_DIR/var/cache
|
| 47 |
|
| 48 | sudo du --si -s $CHROOT_DIR/var/cache/distfiles
|
| 49 |
|
| 50 | # 110 MB just of logs
|
| 51 | # need to thin these out
|
| 52 | sudo du --si -s $CHROOT_HOME_DIR/oils/_tmp/
|
| 53 |
|
| 54 | sudo du --si -s $BASE_DIR/
|
| 55 | }
|
| 56 |
|
| 57 | filter-basename() {
|
| 58 | sed 's|.*/||g'
|
| 59 | }
|
| 60 |
|
| 61 | readonly C_BUG='cannot create executable|cannot compile programs|No working C compiler'
|
| 62 | readonly B_BUG='builddeps failed'
|
| 63 |
|
| 64 | grep-c-bug-2() {
|
| 65 | local epoch_dir=${1:-$REPORT_DIR/2025-08-04-rootbld}
|
| 66 | # 2025-08-07: shard{6,7,8,9} have errors. TODO: re-run them
|
| 67 |
|
| 68 | egrep "$C_BUG" $epoch_dir/*/baseline/log/* #| filter-basename
|
| 69 | }
|
| 70 |
|
| 71 | grep-phdr-bug-2() {
|
| 72 | local epoch_dir=${1:-$REPORT_DIR/2025-08-04-rootbld}
|
| 73 | # 2025-08-07: also shard{6,7,8,9} !
|
| 74 |
|
| 75 | egrep 'PHDR' $epoch_dir/*/baseline/log/* #| filter-basename
|
| 76 | }
|
| 77 |
|
| 78 | grep-b-bug-2() {
|
| 79 | local epoch_dir=${1:-$REPORT_DIR/2025-08-04-rootbld}
|
| 80 |
|
| 81 | egrep "$B_BUG" $epoch_dir/*/baseline/log/* #| filter-basename
|
| 82 | }
|
| 83 |
|
| 84 | grep-c-bug() {
|
| 85 | local epoch_dir=${1:-$REPORT_DIR/2025-08-04-rootbld}
|
| 86 |
|
| 87 | egrep -l "$C_BUG" $epoch_dir/*/baseline/log/* | filter-basename > _tmp/b.txt
|
| 88 | egrep -l "$C_BUG" $epoch_dir/*/osh-as-sh/log/* | filter-basename > _tmp/o.txt
|
| 89 |
|
| 90 | wc -l _tmp/{b,o}.txt
|
| 91 | diff -u _tmp/{b,o}.txt || true
|
| 92 | echo
|
| 93 | echo done
|
| 94 | }
|
| 95 |
|
| 96 | grep-b-bug() {
|
| 97 | local epoch_dir=${1:-$REPORT_DIR/2025-08-04-rootbld}
|
| 98 |
|
| 99 | egrep "$B_BUG" $epoch_dir/*/baseline/log/*
|
| 100 |
|
| 101 | egrep -l "$B_BUG" $epoch_dir/*/baseline/log/* | filter-basename > _tmp/b-b.txt
|
| 102 | egrep -l "$B_BUG" $epoch_dir/*/osh-as-sh/log/* | filter-basename > _tmp/b-o.txt
|
| 103 |
|
| 104 | wc -l _tmp/b-{b,o}.txt
|
| 105 | diff -u _tmp/b-{b,o}.txt || true
|
| 106 | echo
|
| 107 | echo done
|
| 108 | }
|
| 109 |
|
| 110 | update-build-server() {
|
| 111 | ssh -A he.oils.pub '
|
| 112 | set -x
|
| 113 | cd ~/git/oils-for-unix/oils
|
| 114 | git fetch
|
| 115 | git status
|
| 116 |
|
| 117 | cd ~/git/oils-for-unix/alpine-chroot-install
|
| 118 | git fetch
|
| 119 | git status
|
| 120 | '
|
| 121 | }
|
| 122 |
|
| 123 | bwrap-demo() {
|
| 124 | # chroot only
|
| 125 | enter-rootfs-user sh -c '
|
| 126 | whoami; pwd; ls -l /
|
| 127 | set -x
|
| 128 | cat /proc/sys/kernel/unprivileged_userns_clone
|
| 129 | cat /proc/sys/user/max_user_namespaces
|
| 130 | unshare --user echo "Namespaces work"
|
| 131 | '
|
| 132 |
|
| 133 | enter-rootfs-user sh -c 'bwrap ls -l /'
|
| 134 | }
|
| 135 |
|
| 136 | bwrap-debian-demo() {
|
| 137 | bwrap \
|
| 138 | --bind $CHROOT_DIR / \
|
| 139 | --proc /proc \
|
| 140 | --bind /proc/sys/kernel/overflowuid /proc/sys/kernel/overflowuid \
|
| 141 | --bind /proc/sys/kernel/overflowgid /proc/sys/kernel/overflowgid \
|
| 142 | --dev /dev \
|
| 143 | -- \
|
| 144 | sh
|
| 145 | #-c 'ls /; bwrap --proc /proc --dev /dev -- sh -c "nested bwrap"'
|
| 146 | }
|
| 147 |
|
| 148 | login-shell-demo() {
|
| 149 | # shopt -p login_shell is a bash extension
|
| 150 | # so is exec -a
|
| 151 | local sh=${1:-bash}
|
| 152 |
|
| 153 | local detect='echo dollar0=$0; shopt -p login_shell; true'
|
| 154 |
|
| 155 | echo EXEC
|
| 156 | ( exec -- $sh -c "$detect" )
|
| 157 | # invoke with different argv[0]
|
| 158 | ( exec -a "-$sh" -- $sh -c "$detect" )
|
| 159 | echo
|
| 160 |
|
| 161 | echo 'sh -l'
|
| 162 | $sh -c "$detect"
|
| 163 | # Hm it doesn't set dollar0 to -, but it still turns on the login shell.
|
| 164 | # Gah.
|
| 165 | $sh -l -c "$detect"
|
| 166 | echo
|
| 167 |
|
| 168 | echo 'sudo'
|
| 169 | sudo -- $sh -c "$detect"
|
| 170 | sudo --login $sh -c "$detect" # wait it's not set here?
|
| 171 | echo
|
| 172 |
|
| 173 | return
|
| 174 |
|
| 175 | # requires entering password - requires root
|
| 176 |
|
| 177 | echo 'su'
|
| 178 | su andy $sh -c "$detect"
|
| 179 | su -l andy $sh -c "$detect" # wait it's not set here?
|
| 180 | echo
|
| 181 |
|
| 182 | # Conclusion:
|
| 183 | #
|
| 184 | # There are FOUR ways to set a login shell
|
| 185 | #
|
| 186 | # - exec -a "-$sh" -- $sh
|
| 187 | # - this is a bash builtin, so external tools can't invoke it
|
| 188 | # - exec -a is also not POSIX; it's a bash thing
|
| 189 | # - sudo --login (aka sudo -i, which is confusing!)
|
| 190 | # - su -l
|
| 191 | # - requires password?
|
| 192 | # - bash -l
|
| 193 | # - not POSIX
|
| 194 | #
|
| 195 | # TODO: bubblewrap should have control over argv[0]?
|
| 196 | # We can also use the symlink trick: create /bin/-bash next to /bin/bash!
|
| 197 |
|
| 198 | # Debian also has: https://wiki.debian.org/Schroot
|
| 199 | }
|
| 200 |
|
| 201 | # CONJECTURE: login shell is for ONE TIME initialization of state that's INHERITED
|
| 202 | # - umask is inherited
|
| 203 | # - env vars are inherited
|
| 204 | #
|
| 205 | # Claude AI has a good argument against it:
|
| 206 | # - PS1 is not inherited
|
| 207 | # - shopt -s histappend is not inherited
|
| 208 | #
|
| 209 | # Examples of login shells
|
| 210 | # - ssh
|
| 211 | # - sudo -i and su -l
|
| 212 | # - but NOT tmux?
|
| 213 |
|
| 214 | # Good chat:
|
| 215 | # https://claude.ai/share/497778f4-fd11-4daf-9be6-9fe195e19df9
|
| 216 | #
|
| 217 | # "So then for /etc/profile for login shells, isn't it true that you should
|
| 218 | # ONLY add inherited state like $PATH and $LANG and umask? And then
|
| 219 | # everything else should be initialized every time you start a new shell,
|
| 220 | # whether it's login or not, like shopt -s histappend"
|
| 221 | #
|
| 222 | # Violating this principle causes:
|
| 223 | #
|
| 224 | # Problem 1: Missing features in non-login shells
|
| 225 | # tmux # No histappend - history gets clobbered
|
| 226 | # gnome-terminal # No aliases - productivity lost
|
| 227 | # Problem 2: Redundant work in login shells
|
| 228 | # ssh user@host # Sets PATH 5 times (once per sourced file)
|
| 229 | # Problem 3: Shell compatibility issues
|
| 230 | # /etc/profile with bashisms # Breaks when user uses dash, zsh, etc.
|
| 231 |
|
| 232 | # "Your understanding is exactly right: /etc/profile should be the "set it
|
| 233 | # once, inherit everywhere" file for process state, while interactive features
|
| 234 | # get set fresh in each shell instance."
|
| 235 | #
|
| 236 | # TODO:
|
| 237 | # - this should go in doc/interpreter-state.md
|
| 238 | #
|
| 239 | # POSIX rule:
|
| 240 | # - the file $ENV is sourced for non-interactive shells?
|
| 241 | # - bash only
|
| 242 | #
|
| 243 | # TODO: what are OSH rules? just ~/.config/oils/oshrc ? For interactive shells
|
| 244 | #
|
| 245 | # And then that sources /etc/profile?
|
| 246 |
|
| 247 | show-login-files() {
|
| 248 | # sets $PATH, $PAGER
|
| 249 | # sets umask - for file creation
|
| 250 | # set prompt
|
| 251 | # source .d files
|
| 252 | cat $CHROOT_DIR/etc/profile
|
| 253 | echo
|
| 254 |
|
| 255 | ls -l $CHROOT_DIR/etc/profile.d
|
| 256 | echo
|
| 257 |
|
| 258 | # 00-bashrc - source bashrc
|
| 259 | # locale - CHARSET LANG LC_COLLATE
|
| 260 |
|
| 261 | head $CHROOT_DIR/etc/profile.d/*
|
| 262 | echo
|
| 263 |
|
| 264 | # - ONLY for interactive shells
|
| 265 | echo '=== bashrc'
|
| 266 | cat $CHROOT_DIR/etc/bash/bashrc
|
| 267 |
|
| 268 | # more files
|
| 269 | #ls -l $CHROOT_DIR/etc/bash/*.sh
|
| 270 |
|
| 271 | #cat $CHROOT_DIR/home/udu/.profile
|
| 272 | }
|
| 273 |
|
| 274 | su-demo() {
|
| 275 | sh -c 'echo 1; "$@"' dummy0 printf '%s\n' 'a b' 'c d'
|
| 276 | # same
|
| 277 | sh -c 'echo 1; "$@"' -- printf '%s\n' 'a b' 'c d'
|
| 278 |
|
| 279 | # wtf man - su takes this over? this is right
|
| 280 | #su andy sh -c 'echo 1; "$@"' dummy0 printf '%s\n' 'a b' 'c d'
|
| 281 |
|
| 282 | # AH this is the right way to do it! Not with sh?
|
| 283 | su andy -- -c 'echo 1; "$@"' dummy0 printf '%s\n' 'a b' 'c d'
|
| 284 |
|
| 285 | #su andy sh -c 'echo 1; "$@"' -- printf '%s\n' 'a b' 'c d'
|
| 286 | }
|
| 287 |
|
| 288 | big-logs-1() {
|
| 289 | local dir=${1:-_tmp/aports-report/2025-09-10-overlayfs}
|
| 290 |
|
| 291 | find $dir -type f -size +1M
|
| 292 | return
|
| 293 |
|
| 294 | # one-off patch
|
| 295 | find $dir -type f -size +1M -a -exec bash -c 'file=$1; echo "truncated 2025-09-10" > $file' unused0 {} ';'
|
| 296 | }
|
| 297 |
|
| 298 | big-logs-2() {
|
| 299 | local truncate=${1:-}
|
| 300 |
|
| 301 | # one-off patch
|
| 302 | local -a prefix=( find _chroot/shard* -type f -a -name '*.log.txt' -a -size +1M )
|
| 303 |
|
| 304 | if test -z "$truncate"; then
|
| 305 | "${prefix[@]}"
|
| 306 | else
|
| 307 | sudo "${prefix[@]}" -a -print -exec bash -x -e -c 'file=$1; cp -v truncated.txt $file; stat -c "%s" $file' unused0 {} ';'
|
| 308 | fi
|
| 309 | }
|
| 310 |
|
| 311 | task-five "$@"
|
| 312 |
|
| 313 | task-five "$@"
|