| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Build Alpine Linux packages: baseline, OSH as /bin/sh, OSH as /bin/bash
|
| 4 | # See regtest/aports.md
|
| 5 | #
|
| 6 | # Usage:
|
| 7 | # regtest/aports-run.sh <function name>
|
| 8 | #
|
| 9 | # Common usage:
|
| 10 | #
|
| 11 | # export APORTS_EPOCH=2025-08-04-foo # optional override
|
| 12 | # $0 build-many-shards shard{0..16} # build all 17 shards in 2 configs
|
| 13 | #
|
| 14 | # Also useful:
|
| 15 | #
|
| 16 | # $0 fetch-packages fetch $pkg_filter $a_repo # alpine repo is 'main' or 'community'
|
| 17 | #
|
| 18 | # $0 fetch-packages fetch 100,300p # packages 100-300
|
| 19 | # $0 fetch-packages fetch '.*' # all packages
|
| 20 | #
|
| 21 | # Look for results in _tmp/aports-build/
|
| 22 | #
|
| 23 | # Build many packages:
|
| 24 | #
|
| 25 | # $0 build-packages-overlayfs osh-as-sh shard9 community
|
| 26 | # $0 build-packages-overlayfs osh-as-sh shardA # main is default $a_repo
|
| 27 | #
|
| 28 | # Build a single package:
|
| 29 | #
|
| 30 | # $0 build-package-overlayfs osh-as-sh userspace-rcu
|
| 31 | # $0 build-package-overlayfs osh-as-sh xterm community # community repo
|
| 32 | #
|
| 33 | # Drop into a shell:
|
| 34 | # INTERACTIVE=1 $0 build-package-overlayfs osh-as-sh userspace-rcu
|
| 35 | #
|
| 36 | # PKG_FILTER
|
| 37 | # shard[0-9]+ - shard3 is packages 301 to 400
|
| 38 | # [0-9]+ - 42 means build the first 42 packages
|
| 39 | # [0-9]+,[0-9]+p - 100,300p packages 100 to 300 (sed syntax)
|
| 40 | # ALL - all packages
|
| 41 | # .* - egrep pattern matching all packages
|
| 42 | # curl - egrep pattern matching 'curl'
|
| 43 | #
|
| 44 | # Preview packages:
|
| 45 | #
|
| 46 | # $0 package-dirs shard9 community
|
| 47 |
|
| 48 | : ${LIB_OSH=stdlib/osh}
|
| 49 | source $LIB_OSH/bash-strict.sh
|
| 50 | source $LIB_OSH/task-five.sh
|
| 51 |
|
| 52 | source regtest/aports-common.sh
|
| 53 |
|
| 54 | #
|
| 55 | # Config
|
| 56 | #
|
| 57 |
|
| 58 | show-config() {
|
| 59 | enter-rootfs sh -c '
|
| 60 | ls -l /bin/sh /bin/ash /bin/bash
|
| 61 | '
|
| 62 | }
|
| 63 |
|
| 64 | save-default-config() {
|
| 65 | enter-rootfs sh -c '
|
| 66 | set -x
|
| 67 | dest=/bin/bash.ORIG
|
| 68 | cp /bin/bash $dest
|
| 69 | '
|
| 70 | show-config
|
| 71 | }
|
| 72 |
|
| 73 |
|
| 74 | set-baseline() {
|
| 75 | # ensure we have the default config
|
| 76 | enter-rootfs sh -c '
|
| 77 | set -x
|
| 78 | ln -s -f /bin/busybox /bin/sh
|
| 79 | ln -s -f /bin/busybox /bin/ash
|
| 80 | cp /bin/bash.ORIG /bin/bash
|
| 81 | '
|
| 82 | show-config
|
| 83 | }
|
| 84 |
|
| 85 | set-osh-as-X() {
|
| 86 | local x=$1
|
| 87 |
|
| 88 | enter-rootfs sh -c '
|
| 89 | x=$1
|
| 90 | set -x
|
| 91 | if ! test -f /usr/local/bin/oils-for-unix; then
|
| 92 | echo "Build Oils first"
|
| 93 | exit
|
| 94 | fi
|
| 95 | ln -s -f /usr/local/bin/oils-for-unix /bin/$x
|
| 96 | ' dummy0 "$x"
|
| 97 | show-config
|
| 98 | }
|
| 99 |
|
| 100 | set-osh-as-sh() {
|
| 101 | set-osh-as-X sh
|
| 102 | }
|
| 103 |
|
| 104 | set-osh-as-ash() {
|
| 105 | set-osh-as-X ash
|
| 106 | }
|
| 107 |
|
| 108 | set-osh-as-bash() {
|
| 109 | set-osh-as-X bash
|
| 110 | }
|
| 111 |
|
| 112 | #
|
| 113 | # Run
|
| 114 | #
|
| 115 |
|
| 116 | package-dirs() {
|
| 117 | # lz gives 5 packages: some fail at baseline
|
| 118 | # lzip: a single fast package
|
| 119 | # mpfr4: OSH bug, and big log
|
| 120 | # yash: make sure it doesn't hang
|
| 121 | local package_filter=${1:-'lz|mpfr|yash'}
|
| 122 | local a_repo=${2:-main} # or 'community'
|
| 123 |
|
| 124 | local -a prefix
|
| 125 |
|
| 126 | if [[ $package_filter = 'ALL' ]]; then
|
| 127 | prefix=( cat )
|
| 128 |
|
| 129 | # 100 means 0 to 100
|
| 130 | elif [[ $package_filter =~ ^[0-9]+$ ]]; then
|
| 131 | prefix=( head -n $package_filter )
|
| 132 |
|
| 133 | # 100,300p means lines 100 to 300
|
| 134 | elif [[ $package_filter =~ ^[0-9]+,[0-9]+p$ ]]; then
|
| 135 | prefix=( sed -n $package_filter )
|
| 136 |
|
| 137 | elif [[ $package_filter =~ ^shard([0-9]+)$ ]]; then
|
| 138 | # shards of 100 packages
|
| 139 |
|
| 140 | local shard_num=${BASH_REMATCH[1]}
|
| 141 | #echo shard=$shard_num
|
| 142 |
|
| 143 | local range
|
| 144 | # shard 0 is 0-99
|
| 145 | # shard 9 is 900 to 999
|
| 146 | # shard 10 is 1000 to 1099
|
| 147 | case $shard_num in
|
| 148 | # sed doesn't like 000,099
|
| 149 | 0) range='1,100p' ;;
|
| 150 | *) range="${shard_num}01,$(( shard_num + 1))00p" ;;
|
| 151 | esac
|
| 152 |
|
| 153 | prefix=( sed -n "$range" )
|
| 154 |
|
| 155 | # shardA, shardB For testing the combined report
|
| 156 | elif [[ $package_filter =~ ^shard([A-Z]+)$ ]]; then
|
| 157 | local shard_name=${BASH_REMATCH[1]}
|
| 158 | case $a_repo in
|
| 159 | main)
|
| 160 | case $shard_name in
|
| 161 | A) package_filter='^gzip' ;; # failure
|
| 162 | B) package_filter='^xz' ;; # failure
|
| 163 | C) package_filter='^lz' ;; # 3 packages
|
| 164 | D) package_filter='^jq$' ;; # produces autotools test-suite.log
|
| 165 | P) package_filter='^xz$|^shorewall' ;; # patches
|
| 166 | *) package_filter='^perl-http-daemon' ;; # test out perl
|
| 167 | esac
|
| 168 | ;;
|
| 169 | community)
|
| 170 | case $shard_name in
|
| 171 | A) package_filter='^py3-zulip' ;; # one Python package
|
| 172 | B) package_filter='^xterm' ;; # one C package
|
| 173 | C) package_filter='^shfmt' ;; # one Go package
|
| 174 | D) package_filter='^shellspec' ;; # OSH disagreement because of 'var'
|
| 175 | *) package_filter='^shell' ;; # a bunch of packages
|
| 176 | esac
|
| 177 | ;;
|
| 178 | *)
|
| 179 | die "Invalid a_repo $a_repo"
|
| 180 | ;;
|
| 181 | esac
|
| 182 |
|
| 183 | prefix=( egrep "$package_filter" )
|
| 184 |
|
| 185 | elif [[ $package_filter =~ ^disagree-(.*)+$ ]]; then
|
| 186 | local filename=${BASH_REMATCH[1]}
|
| 187 | # A file of EXACT package names, not patterns
|
| 188 | # See copy-disagree
|
| 189 | local package_file="_tmp/$package_filter.txt"
|
| 190 | comm -1 -2 <(sort $package_file) <(sort _tmp/apk-${a_repo}-manifest.txt)
|
| 191 | return
|
| 192 |
|
| 193 | else
|
| 194 | prefix=( egrep "$package_filter" )
|
| 195 |
|
| 196 | fi
|
| 197 |
|
| 198 | "${prefix[@]}" _tmp/apk-${a_repo}-manifest.txt
|
| 199 | }
|
| 200 |
|
| 201 | copy-disagree() {
|
| 202 | ### Determine what to run
|
| 203 |
|
| 204 | local epoch=${1:-2025-09-18-bash}
|
| 205 | cp -v \
|
| 206 | _tmp/aports-report/$epoch/disagree-packages.txt \
|
| 207 | _tmp/disagree-$epoch.txt
|
| 208 | }
|
| 209 |
|
| 210 | do-packages() {
|
| 211 | ### Download sources - abuild puts it in /var/cahe/distfiles
|
| 212 | local action=${1:-fetch}
|
| 213 | local package_filter=${2:-}
|
| 214 | local a_repo=${3:-main}
|
| 215 | # flags to pass to the inner shell
|
| 216 | local sh_flags=${4:-'-e -u'} # -u to disable -e
|
| 217 |
|
| 218 | # 6 seconds for 10 packages
|
| 219 | # There are ~1600 packages
|
| 220 | # So if there are 20 shards, each shard could have 10?
|
| 221 |
|
| 222 | local -a package_dirs
|
| 223 | package_dirs=( $(package-dirs "$package_filter" "$a_repo") )
|
| 224 |
|
| 225 | echo "${dirs[@]}"
|
| 226 | #return
|
| 227 |
|
| 228 | time enter-rootfs-user sh $sh_flags -c '
|
| 229 |
|
| 230 | action=$1
|
| 231 | a_repo=$2
|
| 232 | shift 2
|
| 233 | for dir in "$@"; do
|
| 234 | time abuild -r -C aports/$a_repo/$dir "$action"
|
| 235 | done
|
| 236 | ' dummy0 "$action" "$a_repo" "${package_dirs[@]}"
|
| 237 | }
|
| 238 |
|
| 239 | fetch-packages() {
|
| 240 | local package_filter=${1:-}
|
| 241 | local a_repo=${2:-main}
|
| 242 |
|
| 243 | # -u means we don't pass -e (and it's non-empty)
|
| 244 | do-packages fetch "$package_filter" "$a_repo" '-u'
|
| 245 | }
|
| 246 |
|
| 247 | banner() {
|
| 248 | echo
|
| 249 | echo "=== $@"
|
| 250 | echo
|
| 251 | }
|
| 252 |
|
| 253 | build-package-overlayfs() {
|
| 254 | local config=${1:-baseline}
|
| 255 | local pkg=${2:-lua5.4}
|
| 256 | local a_repo=${3:-main}
|
| 257 |
|
| 258 | # baseline stack:
|
| 259 | # _chroot/aports-build
|
| 260 | # _chroot/package-upper/baseline/gzip # upper dir / layer dir
|
| 261 | #
|
| 262 | # osh-as-sh stack:
|
| 263 | # _chroot/aports-build
|
| 264 | # _chroot/osh-as-sh.overlay/layer # this has the symlink
|
| 265 | # _chroot/package-upper/osh-as-sh/gzip # upper dir / layer dir
|
| 266 |
|
| 267 | local merged=_chroot/package.overlay/merged
|
| 268 | local work=_chroot/package.overlay/work
|
| 269 |
|
| 270 | local layer_dir=_chroot/package-layers/$config/$pkg
|
| 271 | mkdir -p $layer_dir
|
| 272 |
|
| 273 | local overlay_opts
|
| 274 | case $config in
|
| 275 | baseline)
|
| 276 | overlay_opts="lowerdir=$CHROOT_DIR,upperdir=$layer_dir,workdir=$work"
|
| 277 | ;;
|
| 278 | osh-as-sh)
|
| 279 | local osh_as_sh=_chroot/osh-as-sh.overlay/layer
|
| 280 | overlay_opts="lowerdir=$osh_as_sh:$CHROOT_DIR,upperdir=$layer_dir,workdir=$work"
|
| 281 | ;;
|
| 282 | *)
|
| 283 | die "Invalid config $config"
|
| 284 | ;;
|
| 285 | esac
|
| 286 |
|
| 287 | sudo mount \
|
| 288 | -t overlay \
|
| 289 | aports-package \
|
| 290 | -o "$overlay_opts" \
|
| 291 | $merged
|
| 292 |
|
| 293 | $merged/enter-chroot -u udu sh -c '
|
| 294 | cd oils
|
| 295 |
|
| 296 | # show the effect of the overlay
|
| 297 | #ls -l /bin/sh
|
| 298 |
|
| 299 | regtest/aports-guest.sh build-one-package "$@"
|
| 300 | ' dummy0 "$pkg" "$a_repo"
|
| 301 |
|
| 302 | if test -n "$INTERACTIVE"; then
|
| 303 | echo "Starting interactive shell in overlayfs environment for package $a_repo/$pkg"
|
| 304 | echo "Rebuild: abuild -f -r -C ~/aports/$a_repo/$pkg"
|
| 305 | echo " Help: abuild -h"
|
| 306 | # If the last command in the child shell exited non-zero then ctrl-d/exit
|
| 307 | # will report that error code to the parent. If we don't ignore that error
|
| 308 | # we will exit early and leave the package overlay mounted.
|
| 309 | set +o errexit
|
| 310 | $merged/enter-chroot -u udu
|
| 311 | set -o errexit
|
| 312 | fi
|
| 313 |
|
| 314 | unmount-loop $merged
|
| 315 | }
|
| 316 |
|
| 317 | build-many-packages-overlayfs() {
|
| 318 | local package_filter=${1:-}
|
| 319 | local config=${2:-baseline}
|
| 320 | local a_repo=${3:-main}
|
| 321 |
|
| 322 | local -a package_dirs
|
| 323 | package_dirs=( $(package-dirs "$package_filter" "$a_repo") )
|
| 324 |
|
| 325 | banner "Building ${#package_dirs[@]} packages (filter=$package_filter a_repo=$a_repo)"
|
| 326 |
|
| 327 | for pkg in "${package_dirs[@]}"; do
|
| 328 | build-package-overlayfs "$config" "$pkg" "$a_repo"
|
| 329 | done
|
| 330 | }
|
| 331 |
|
| 332 | clean-host-and-guest() {
|
| 333 | # host dir _tmp/aports-build
|
| 334 | rm -r -f -v $BASE_DIR
|
| 335 | }
|
| 336 |
|
| 337 | clean-guest() {
|
| 338 | # clean guest chroot
|
| 339 | sudo rm -r -f -v $CHROOT_HOME_DIR/oils/_tmp
|
| 340 | }
|
| 341 |
|
| 342 | readonly -a CONFIGS=( baseline osh-as-sh )
|
| 343 |
|
| 344 | APORTS_EPOCH="${APORTS_EPOCH:-}"
|
| 345 | # default epoch
|
| 346 | if test -z "$APORTS_EPOCH"; then
|
| 347 | APORTS_EPOCH=$(date '+%Y-%m-%d')
|
| 348 | fi
|
| 349 |
|
| 350 | _build-many-configs-overlayfs() {
|
| 351 | local package_filter=${1:-}
|
| 352 | local epoch=${2:-$APORTS_EPOCH}
|
| 353 | local a_repo=${3:-main}
|
| 354 |
|
| 355 | if test -z "$package_filter"; then
|
| 356 | die "Package filter is required (e.g. shard3, ALL)"
|
| 357 | fi
|
| 358 |
|
| 359 | clean-guest
|
| 360 |
|
| 361 | # See note about /etc/sudoers.d at top of file
|
| 362 |
|
| 363 | local dest_dir="$BASE_DIR/$epoch/$package_filter" # e.g. shard10
|
| 364 |
|
| 365 | for config in "${CONFIGS[@]}"; do
|
| 366 | banner "$epoch: Using config $config"
|
| 367 |
|
| 368 | build-many-packages-overlayfs "$package_filter" "$config" "$a_repo"
|
| 369 | done
|
| 370 | }
|
| 371 |
|
| 372 | remove-shard-files() {
|
| 373 | local shard_dir=${1:-_chroot/shardC}
|
| 374 |
|
| 375 | log "Removing big files in shard $shard_dir"
|
| 376 |
|
| 377 | # For all packages packages, for baseline and osh-as-sh, clean up the aports source dir
|
| 378 | # For linux, clang, etc. it becomes MANY GIGABYTES
|
| 379 | #
|
| 380 | # 2025-09-12: ignore errors from rm; I think there was a race condition -
|
| 381 | # processes could still be running and creating files
|
| 382 | #
|
| 383 | # rm: cannot remove '_chroot/shard6/baseline/llvm19/home/udu/aports/main/llvm19/src/llvm-project-19.1.7.src/build/lib': Directory not empty
|
| 384 | # real 1041m46.464s
|
| 385 |
|
| 386 | sudo rm -r -f $shard_dir/*/*/home/udu/aports/ || true
|
| 387 | }
|
| 388 |
|
| 389 | build-many-shards-overlayfs() {
|
| 390 | sudo -k
|
| 391 |
|
| 392 | local a_repo=${A_REPO:-main} # env var like $APORTS_EPOCH
|
| 393 |
|
| 394 | # Clean up old runs
|
| 395 | sudo rm -r -f _chroot/shard* _chroot/disagree*
|
| 396 |
|
| 397 | banner "$APORTS_EPOCH $a_repo: building shards: $*"
|
| 398 |
|
| 399 | time for shard_name in "$@"; do
|
| 400 | _build-many-configs-overlayfs "$shard_name" "$APORTS_EPOCH" "$a_repo"
|
| 401 |
|
| 402 | # Move to _chroot/shard10, etc.
|
| 403 | mv -v --no-target-directory _chroot/package-layers _chroot/$shard_name
|
| 404 |
|
| 405 | make-shard-tree $shard_name $a_repo
|
| 406 |
|
| 407 | remove-shard-files _chroot/$shard_name
|
| 408 | done
|
| 409 | }
|
| 410 |
|
| 411 | make-shard-tree() {
|
| 412 | ### Put outputs in rsync-able format, for a SINGLE shard
|
| 413 |
|
| 414 | # The dire structure is like this:
|
| 415 | #
|
| 416 | # _tmp/aports-build/
|
| 417 | # 2025-09-10-overlayfs/
|
| 418 | # shard0/
|
| 419 | # baseline/
|
| 420 | # apk.txt
|
| 421 | # tasks.tsv
|
| 422 | # log/
|
| 423 | # gzip.log.txt
|
| 424 | # xz.log.txt
|
| 425 | # test-suite/ # autotools dir
|
| 426 | # gzip/
|
| 427 | # test-suite.log.txt
|
| 428 | # osh-as-sh/
|
| 429 | # apk.txt
|
| 430 | # tasks.tsv
|
| 431 | # log/
|
| 432 | # gzip.log.txt
|
| 433 | # xz.log.txt
|
| 434 | # test-suite/
|
| 435 | # gzip/
|
| 436 | # test-suite.log.txt
|
| 437 | # shard1/
|
| 438 | # ...
|
| 439 | # shard16/
|
| 440 | # ...
|
| 441 |
|
| 442 | local shard_name=$1
|
| 443 | local a_repo=${2:-main}
|
| 444 | local epoch=${3:-$APORTS_EPOCH}
|
| 445 |
|
| 446 | local shard_dir=_chroot/$shard_name
|
| 447 |
|
| 448 | for config in baseline osh-as-sh; do
|
| 449 | local dest_dir=$BASE_DIR/$epoch/$shard_name/$config
|
| 450 | mkdir -p $dest_dir
|
| 451 | #ls -l $shard_dir/$config
|
| 452 |
|
| 453 | time python3 devtools/tsv_concat.py \
|
| 454 | $shard_dir/$config/*/home/udu/oils/_tmp/aports-guest/*.task.tsv > $dest_dir/tasks.tsv
|
| 455 |
|
| 456 | # Allowed to fail if zero .apk are built
|
| 457 | time md5sum $shard_dir/$config/*/home/udu/packages/$a_repo/x86_64/*.apk > $dest_dir/apk.txt \
|
| 458 | || true
|
| 459 |
|
| 460 | abridge-logs2 $shard_dir/$config $dest_dir
|
| 461 |
|
| 462 | done
|
| 463 | }
|
| 464 |
|
| 465 | abridge-logs2() {
|
| 466 | local config_src_dir=${1:-_chroot/shardD/osh-as-sh}
|
| 467 | local dest_dir=${2:-$BASE_DIR/shardD/osh-as-sh}
|
| 468 |
|
| 469 | local log_dest_dir=$dest_dir/log
|
| 470 | local test_suite_dest_dir=$dest_dir/test-suite
|
| 471 | mkdir -p $log_dest_dir $test_suite_dest_dir
|
| 472 |
|
| 473 | local threshold=$(( 500 * 1000 )) # 500 KB
|
| 474 |
|
| 475 | # this assumes the build process doesn't create *.log.txt
|
| 476 | # test-suite.log is the name used by the autotools test runner - we want to save those too
|
| 477 | # ignore permission errors with || true
|
| 478 | { find $config_src_dir -name '*.log.txt' -a -printf '%s\t%P\n' || true; } |
|
| 479 | while read -r size path; do
|
| 480 | local src=$config_src_dir/$path
|
| 481 | # Remove text until last slash (shortest match)
|
| 482 | # like $(basename $path) but in bash, for speed
|
| 483 | local filename=${path##*/}
|
| 484 | local dest=$log_dest_dir/$filename
|
| 485 |
|
| 486 | if test "$size" -lt "$threshold"; then
|
| 487 | cp -v $src $dest
|
| 488 | else
|
| 489 | { echo "*** This log is abridged to its last 1000 lines:"
|
| 490 | echo
|
| 491 | tail -n 1000 $src
|
| 492 | } > $dest
|
| 493 | fi
|
| 494 | done
|
| 495 |
|
| 496 | { find $config_src_dir -name 'test-suite.log' -a -printf '%P\n' || true; } |
|
| 497 | while read -r path; do
|
| 498 | local src=$config_src_dir/$path
|
| 499 |
|
| 500 | # Remove text after the first slash (shortest match)
|
| 501 | local package_name=${path%%/*}
|
| 502 | local dest=$test_suite_dest_dir/$package_name/test-suite.log.txt
|
| 503 |
|
| 504 | mkdir -p "$(dirname $dest)"
|
| 505 | cp -v --no-target-directory $src $dest
|
| 506 | done
|
| 507 |
|
| 508 | # 500K threshold: 76 MB
|
| 509 | du --si -s $log_dest_dir
|
| 510 | }
|
| 511 |
|
| 512 | compare-speed() {
|
| 513 | ### reusing the chroot reuses is a LITTLE faster, but not a lot
|
| 514 |
|
| 515 | # single chroot
|
| 516 | build-many-shards shardC
|
| 517 |
|
| 518 | # 3 chroots + overlayfs mounts
|
| 519 | build-many-shards2 shardC
|
| 520 | }
|
| 521 |
|
| 522 | task-five "$@"
|