Repository navigation
Expand file tree
/
Copy pathbuild-bundle.sh
More file actions
executable file
·1592 lines (1468 loc) · 69.3 KB
/
Copy pathbuild-bundle.sh
File metadata and controls
executable file
·1592 lines (1468 loc) · 69.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
#
# Copyright (c) 2026, NOFire AI
# SPDX-License-Identifier: Apache-2.0
#
# Build the self-contained brig bundle for one architecture -- the single tarball
# install.sh fetches and unpacks. This script is where all the building happens.
#
# The tarball is a complete /var/lib/brig/data tree: brig and brigd (and brig-broker,
# from a brig release that carries it), cosign and oras, the
# private-containerd stack (containerd, ctr, runc, nerdctl, CNI), the microVM
# monitors, the urunc runtime, the guest kernel and initrd, plus every config
# file, systemd unit, wrapper and the uninstaller. Most components are upstream
# release artifacts, hvi among them. The kernel and the initrd come from the
# signed hull-assets bundle. urunc is built here from urunc-dev/urunc, because
# no urunc release carries what brig needs.
#
# scripts/build-bundle.sh --arch amd64 --version v0.1.0 --out dist
# scripts/build-bundle.sh --arch arm64 --version v0.1.0 --variant generic-boot
#
# --rootless adds the rootless path: rootlesskit, slirp4netns, nerdctl's two
# rootless launchers and brig-rootless-setup.sh. It is a separate artifact
# because a node install driven by root never uses any of it, and a user
# install cannot work without it.
#
# docker is required (the urunc build runs in a container). Building the arm64
# pieces on an amd64 runner (or vice versa) needs binfmt/qemu registered,
# because the static urunc binary is CGO and is built native inside a
# target-arch container (docker run --platform).
#
# Produces, under --out:
# brig-standalone-<version>-linux-<arch>.tar.gz (the install tarball)
# brig-standalone-<version>-linux-<arch>.pins.env (the version manifest)
# brig-standalone-<version>-linux-<arch>.boot-assets.sha256
# (the kernel and initrd digests; not
# for --variant stock)
#
# and with --rootless, the same files named brig-standalone-<version>-rootless-*.
set -eu
# Directory modes in the archive come from the builder's umask, so two hosts
# with different umasks produce different bytes from the same inputs. Pin it.
umask 022
SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_DIR="$(dirname "$SELF_DIR")"
ARCH=""
VERSION=""
OUT="$REPO_DIR/dist"
VARIANT="generic-boot"
URUNC_VERSION_STOCK="" # only for --variant stock: a released urunc tag
# Off by default: a node install driven by root never runs the rootless path,
# and shipping it costs ~39MB and four binaries that would go unused. --rootless
# builds the second artifact, which is the one a user install needs.
ROOTLESS=false
info() { echo "[build-bundle] $*" >&2; }
fatal() { echo "[build-bundle] ERROR: $*" >&2; exit 1; }
while [ $# -gt 0 ]; do
case "$1" in
--arch) ARCH="$2"; shift 2 ;;
--version) VERSION="$2"; shift 2 ;;
--out) OUT="$2"; shift 2 ;;
--variant) VARIANT="$2"; shift 2 ;;
--rootless) ROOTLESS=true; shift ;;
--urunc-version) URUNC_VERSION_STOCK="$2"; shift 2 ;;
--help|-h) awk 'NR > 1 && /^#/ { sub(/^# ?/, ""); print; next } NR > 1 { exit }' "$0"; exit 0 ;;
*) fatal "unknown argument '$1'" ;;
esac
done
[ -n "$ARCH" ] || fatal "--arch is required (amd64 or arm64)"
[ -n "$VERSION" ] || fatal "--version is required"
case "$ARCH" in
amd64) ARCH_UNAME=x86_64 ;;
arm64) ARCH_UNAME=aarch64 ;;
*) fatal "unsupported arch '$ARCH'" ;;
esac
case "$VARIANT" in
stock|generic-boot|introspection) ;;
*) fatal "unknown --variant '$VARIANT'" ;;
esac
for tool in curl tar sha256sum awk sed; do
command -v "$tool" >/dev/null 2>&1 || fatal "'$tool' is required"
done
# ---------------------------------------------------------------- pins
# build-bundle.sh is the build authority: bumping a version here is the only place
# it changes. install.sh does not build, so it carries none of these.
# The brig release this bundle carries. On Linux it is the brig a user gets,
# so it is pinned like every other component. Bump it with the brig release a
# bundle is cut for. BRIG_VERSION=latest still works for a local build.
BRIG_VERSION="${BRIG_VERSION:-v0.5.0}"
BRIG_REPO="${BRIG_REPO:-brig-sh/brig}"
# urunc has no release that carries what brig needs, so it is built from
# source, and the source is pinned to one commit. A branch tip
# moves with every push, so it is not a release input: two builds of one bundle
# version would carry different code. Moving a pin is a reviewed one-line
# change to a *_REF_DEFAULT below, plus its *_BRANCH_DEFAULT when the commit
# comes from another branch.
#
# *_BRANCH names the branch the pinned commit comes from. It is recorded in
# pins.env, and it is what gets built when the matching *_REF is set empty
# (URUNC_REF= scripts/build-bundle.sh ...): the tip, with a warning in the
# build log and *_PINNED=false in pins.env. An unset *_REF takes the pin
# below, which is why these use ${VAR-default} and not ${VAR:-default}.
#
# urunc: feat/unchanged_containers on top of urunc v0.8.1, plus the two
# urunit-agent exec fixes, urunc-dev/urunc#1059 and #1060. It also carries a
# commit that sizes a container boot's vCPUs from the container's CPU limit,
# one that boots arm64 guests on cloud-hypervisor's virtio console, one that
# passes image_type=raw on every --disk (urunc-dev/urunc#1117), which
# cloud-hypervisor v54.0 requires, and the hvi monitor with exec over its agent
# socket. On top of that, feat/linux-hvi-v050: urunit's single binary as
# the whole initrd, the overlay-block root (com.urunc.unikernel.rootfsMode),
# and stop signals handed to the guest init, which it lets power off when the
# agent ends the signal session. It writes urunit.conf as the NUL-terminated
# records of urunc-dev/urunc#1111. The stack sits on urunc main as of
# 89dfe8d, with the bind-mount fixes of urunc-dev/urunc#1150, and presses a
# cloud-hypervisor guest's power button when its init cannot take the signal.
URUNC_REPO="${URUNC_REPO:-urunc-dev/urunc}"
URUNC_BRANCH_DEFAULT=feat/linux-hvi-v050
URUNC_REF_DEFAULT=3c00b69ff9ac1b8bb260975cdc63d18a7043558b
URUNC_GO_IMAGE="${URUNC_GO_IMAGE:-golang:1.26.8}"
# urunit, the one binary of the initrd, is not built here: the initrd comes from
# hull-assets. pins.env records the urunit commit that urunc pins in
# packaging/container-initrd/urunit.pin.
# hvi: the monitor brig boots a generic container on. It is the
# brig-sh/hvi-vmm release hull ships on macOS, static against musl. The tag,
# its commit and the sha256 of each tarball describe one release, so a bump
# changes all four together. scripts/fetch-hvi.sh checks them.
HVI_VERSION=0.1.0-rc.2
HVI_COMMIT=2ed8f61dde8c20facd5f1a3668b170b07fb0eee1
HVI_SHA256_LINUX_AMD64=e62358d9abcf2c8999227ef7ba16c25b57e131467eb4df916bffb68578b9ff25
HVI_SHA256_LINUX_ARM64=3c11e3b1443fb9e39a652a0011ac74d548a7bd05674b6c4115ffbd5a0260e903
# A branch given without a ref would otherwise build the pinned commit and
# ignore the branch, so ask which one was meant.
if [ -n "${URUNC_BRANCH:-}" ] && [ "$URUNC_BRANCH" != "$URUNC_BRANCH_DEFAULT" ] && [ -z "${URUNC_REF+set}" ]; then
fatal "URUNC_BRANCH=$URUNC_BRANCH is set but URUNC_REF is not. Set URUNC_REF= to build that branch's tip, or URUNC_REF=<commit> to build one commit from it."
fi
if [ -n "${URUNIT_REF+set}" ] || [ -n "${URUNIT_BRANCH+set}" ] || [ -n "${URUNIT_REPO+set}" ]; then
fatal "urunit is no longer built here: URUNIT_REPO, URUNIT_REF and URUNIT_BRANCH do nothing. The initrd comes from hull-assets; move ASSETS_VERSION to a release built from the urunit you want."
fi
if [ -n "${HVI_REF+set}" ] || [ -n "${HVI_BRANCH+set}" ]; then
fatal "hvi is no longer built from source: HVI_REF and HVI_BRANCH do nothing. Move HVI_VERSION and its pins to another release instead."
fi
URUNC_BRANCH="${URUNC_BRANCH:-$URUNC_BRANCH_DEFAULT}"
URUNC_REF="${URUNC_REF-$URUNC_REF_DEFAULT}"
# The checkout compares HEAD with the ref, so a ref is a full SHA: a short one,
# a tag or a branch name would never match.
if [ -n "$URUNC_REF" ]; then
case "$URUNC_REF" in
*[!0-9a-f]*) fatal "URUNC_REF='$URUNC_REF' is not a commit: pin a full 40-character SHA" ;;
esac
[ "${#URUNC_REF}" -eq 40 ] || fatal "URUNC_REF='$URUNC_REF' is not a commit: pin a full 40-character SHA"
fi
# The monitors and virtiofsd come from one monitors-build release. The release
# is tagged vX.Y.Z and names its per-arch tarball after the monitor versions it
# carries. monitors-build publishes no checksum file, so each tarball is pinned
# by sha256 the way cosign is.
#
# cloud-hypervisor has to be v54.0 or later. Earlier releases offer the
# virtio-net offloads by default and write a guest's virtio-net header to the
# tap unchanged. That is the guest side of PwnEscape, the Linux host-kernel bug
# in deferred checksum completion (fixed upstream in 6785011f8b16). v54.0 turns
# the offloads off by default and drops a transmit chain whose header asks for
# an offload the guest did not negotiate.
MONITORS_RELEASE="${MONITORS_RELEASE:-v0.1.1}"
MONITORS_VERSION="${MONITORS_VERSION:-FC-v1.7.0_CLH-v54.0_S5-v0.12.1_VFS-v1.13.0_QM-v10.1.1-9a44e}"
MONITORS_SHA_LINUX_AMD64="66a33fbbcb1e125d7c80e99249b58580ff782eb6c1cb1710c8a52e470aa1a3e9"
MONITORS_SHA_LINUX_ARM64="93c1c63437efc3d0c935a6eb3f358d28cc773f8770e4f608109c91cbdfcac2ee"
CONTAINERD_VERSION="${CONTAINERD_VERSION:-v2.3.5}"
RUNC_VERSION="${RUNC_VERSION:-v1.5.1}"
NERDCTL_VERSION="${NERDCTL_VERSION:-v2.3.5}"
CNI_VERSION="${CNI_VERSION:-v1.9.1}"
# The rootless path. nerdctl's own tarball carries containerd-rootless.sh and
# its setuptool; these two are the pieces it shells out to and no other
# component pulls in.
ROOTLESSKIT_VERSION="${ROOTLESSKIT_VERSION:-v3.2.0}"
SLIRP4NETNS_VERSION="${SLIRP4NETNS_VERSION:-v1.3.5}"
ORAS_VERSION="${ORAS_VERSION:-v1.3.4}"
COSIGN_VERSION="${COSIGN_VERSION:-v3.1.3}"
COSIGN_SHA_LINUX_AMD64="4629c757b7618056f8ddd7e2625ae9fdd94c0372a65049520bc7d9df9efc7f71"
COSIGN_SHA_LINUX_ARM64="c5d324e091826b0d7a78eb16fef316450b4eb9aaec045611c08ba06f5e73220a"
# hvi comes from its own release; the rest come from the monitors release.
# stock leaves hvi out: it ships a released urunc, and no urunc release knows
# hvi yet.
if [ "$VARIANT" = "stock" ]; then
MONITORS="${MONITORS:-firecracker cloud-hypervisor solo5-hvt solo5-spt}"
else
MONITORS="${MONITORS:-hvi firecracker cloud-hypervisor solo5-hvt solo5-spt}"
fi
ASSETS_REGISTRY="${ASSETS_REGISTRY:-ghcr.io}"
ASSETS_REPO="${ASSETS_REPO:-nofireai/hull-assets}"
ASSETS_VERSION="${ASSETS_VERSION:-0.1.11}"
# Who signs hull-assets: the identity brig checks before it boots a kernel from
# there. The build checks the same one before it packs the kernel.
ASSETS_SIGNER_DEFAULT='^https://github\.com/NOFireAI/hull-assets/\.github/workflows/build-assets\.yml@refs/heads/main$'
ASSETS_SIGNER="${ASSETS_SIGNER:-$ASSETS_SIGNER_DEFAULT}"
ASSETS_ISSUER="${ASSETS_ISSUER:-https://token.actions.githubusercontent.com}"
VIRTIOFSD="${VIRTIOFSD:-true}"
# Fixed install layout, baked into the config files the bundle carries. install.sh
# unpacks the tree to exactly these paths, so they are not configurable there.
# k3s-style: the whole install lives under one root (/var/lib/brig) as two
# sibling trees -- the immutable bundle under data/ and mutable state under
# agent/ -- with the runtime socket in /run and the launchers on the host PATH.
# The layout is fixed at build time and baked into every wrapper, config and
# unit below. Root installs own /var/lib/brig; a bundle built for one user
# points these at that user's own directories, which is what makes an install
# without root possible.
PREFIX="${PREFIX:-/var/lib/brig/data}"
DATA_DIR="${DATA_DIR:-/var/lib/brig/agent}"
RUN_DIR="${RUN_DIR:-/run/brig}"
BIN_DIR="$PREFIX/bin"
ETC_DIR="$PREFIX/etc"
SHARE_DIR="$PREFIX/share"
LIBEXEC_DIR="$PREFIX/libexec"
CNI_DIR="$LIBEXEC_DIR/cni"
LOG_DIR="$DATA_DIR/log"
CONTAINERD_SOCK="$RUN_DIR/containerd.sock"
NAMESPACE=brig
POOL_NAME=brig-devpool
BRIDGE_NAME=brig0
BRIDGE_SUBNET=10.44.0.0/24
SERVICE_NAME=brig-containerd
POOL_SERVICE_NAME=brig-devpool
# The snapshotter brig is told to use by default. devmapper stays configured in
# containerd (below), so a user switches to it at runtime with no reinstall by
# exporting CONTAINERD_SNAPSHOTTER=devmapper before running brig.
SNAPSHOTTER=overlayfs
POOL_FS=ext2
POOL_BASE_IMAGE_SIZE=10GB
VIRTIOFSD_OPTIONS="--cache always --sandbox none"
BRIG_GROUP=brig
GITHUB="https://github.com"
NAME="brig-standalone-$VERSION-linux-$ARCH"
[ "$ROOTLESS" = true ] && NAME="brig-standalone-$VERSION-rootless-linux-$ARCH"
TMP_DIR="$(mktemp -d)"
# The urunc build runs in docker as root and leaves root-owned files
# (vendored deps, dist/, apt caches) under TMP_DIR. A non-root builder -- a CI
# runner -- cannot rm those, and a failing cleanup would fail the whole build
# after the tarball was already produced. So reclaim them via docker if a plain
# rm cannot, and never let cleanup change the exit status.
cleanup() {
[ -n "${TMP_DIR:-}" ] && [ -d "$TMP_DIR" ] || return 0
rm -rf "$TMP_DIR" 2>/dev/null && return 0
if command -v docker >/dev/null 2>&1; then
docker run --rm -v "$TMP_DIR":/t alpine:3.20 \
chown -R "$(id -u):$(id -g)" /t >/dev/null 2>&1 || true
rm -rf "$TMP_DIR" 2>/dev/null || true
fi
return 0
}
trap cleanup EXIT INT TERM
STAGE="$TMP_DIR/$NAME"
DL="$TMP_DIR/dl"
mkdir -p "$STAGE/bin" "$STAGE/libexec" "$STAGE/libexec/cni" "$STAGE/share/guest" "$DL" "$OUT"
# brig-ctl: drive the private stack by hand. Baked with the fixed layout.
write_brig_ctl() {
cat > "$STAGE/bin/brig-ctl" <<CTLENV
#!/bin/sh
# brig-ctl: run ctr, nerdctl and friends against the private brig stack.
set -eu
PREFIX="$PREFIX"
SERVICE_NAME="$SERVICE_NAME"
BRIG_ENV="$ETC_DIR/brig-env.sh"
ROOTLESS=$ROOTLESS
# brig-env.sh is the one place that works out what the caller's euid means:
# root drives the system stack, anyone else drives a rootless containerd whose
# socket is inside the rootlesskit namespace and at a different path entirely.
# Source it rather than answer that question again here. Repeating it is what
# broke: a rootless install got handed the root socket path, and every nerdctl
# and ctr call through this script failed on a socket that does not exist.
if [ -f "\$BRIG_ENV" ]; then
. "\$BRIG_ENV"
fi
# What the bundle was built with, used only where brig-env.sh left a gap.
CONTAINERD_ADDRESS="\${CONTAINERD_ADDRESS:-$CONTAINERD_SOCK}"
CONTAINERD_NAMESPACE="\${CONTAINERD_NAMESPACE:-$NAMESPACE}"
CONTAINERD_SNAPSHOTTER="\${CONTAINERD_SNAPSHOTTER:-$SNAPSHOTTER}"
NERDCTL_TOML="\${NERDCTL_TOML:-$ETC_DIR/nerdctl.toml}"
CNI_PATH="\${CNI_PATH:-$CNI_DIR}"
CTLENV
cat >> "$STAGE/bin/brig-ctl" <<'CTLBODY'
# brig-ctl carries the prefix it was built for, so a copy run straight out of
# an unpacked tarball drives whatever install sits at that path instead of the
# tree it was run from -- which silently repoints a live rootless setup.
# brig-uninstall.sh guards the same way. $0 without a slash came off PATH, and
# an unresolvable one is left alone rather than guessed at.
self="$0"
case "$self" in
*/*) ;;
*) self="$(command -v -- "$self" 2>/dev/null || echo "")" ;;
esac
if [ -n "$self" ]; then
self_prefix="$(cd "$(dirname -- "$self")/.." 2>/dev/null && pwd || echo "")"
if [ -n "$self_prefix" ] && [ "$self_prefix" != "$PREFIX" ]; then
echo "brig-ctl: this copy was built for $PREFIX but runs from $self_prefix." >&2
echo "brig-ctl: use $PREFIX/bin/brig-ctl, or install this tree first." >&2
exit 1
fi
fi
export CONTAINERD_ADDRESS CONTAINERD_NAMESPACE CONTAINERD_SNAPSHOTTER NERDCTL_TOML CNI_PATH
PATH="$PREFIX/bin:$PATH"
export PATH
# ctr stats --address as a file path before it dials, so the unix:// scheme
# brig-env.sh writes for nerdctl fails there on every install. A rootless
# install's socket exists only inside the rootlesskit namespace. nerdctl enters
# that namespace by itself and ctr does not, so ctr goes in through the nsenter
# of the setup tool nerdctl ships. Rootless means what it means in brig-env.sh:
# a rootless bundle and a caller who is not root.
run_ctr() {
if [ "$ROOTLESS" = true ] && [ "$(id -u)" -ne 0 ]; then
XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
export XDG_RUNTIME_DIR
if [ ! -f "$XDG_RUNTIME_DIR/containerd-rootless/child_pid" ]; then
echo "brig-ctl: no rootless containerd is running for $(id -un)." >&2
echo "brig-ctl: start it with: systemctl --user start $SERVICE_NAME.service" >&2
return 1
fi
"$PREFIX/bin/containerd-rootless-setuptool.sh" nsenter -- \
"$PREFIX/bin/ctr" --address "${CONTAINERD_ADDRESS#unix://}" "$@"
else
"$PREFIX/bin/ctr" --address "${CONTAINERD_ADDRESS#unix://}" "$@"
fi
}
cmd="${1:-help}"
[ $# -gt 0 ] && shift
case "$cmd" in
ctr)
run_ctr --namespace "$CONTAINERD_NAMESPACE" "$@" ;;
nerdctl)
exec "$PREFIX/bin/nerdctl" "$@" ;;
run)
exec "$PREFIX/bin/nerdctl" run --runtime io.containerd.urunc.v2 \
--snapshotter "$CONTAINERD_SNAPSHOTTER" "$@" ;;
env)
cat "$BRIG_ENV" ;;
version)
cat "$PREFIX/pins.env" ;;
status)
# A rootless install's daemon is a systemd --user unit, so asking the
# system manager about it answers "could not be found" on a stack that
# is running perfectly well.
if command -v systemctl >/dev/null 2>&1; then
if [ "$(id -u)" -eq 0 ]; then
systemctl --no-pager status "$SERVICE_NAME.service" || true
else
systemctl --user --no-pager status "$SERVICE_NAME.service" || true
fi
fi
echo
run_ctr plugin ls 2>/dev/null | awk 'NR==1 || /snapshotter/ || /urunc/' ;;
rootless)
if [ "$ROOTLESS" != true ]; then
echo "brig-ctl: this bundle was built without rootless support." >&2
echo "brig-ctl: install a brig-standalone-*-rootless-* bundle instead." >&2
exit 1
fi
exec "$PREFIX/bin/brig-rootless-setup.sh" "$@" ;;
uninstall)
exec "$PREFIX/bin/brig-uninstall.sh" "$@" ;;
help|--help|-h)
cat <<USAGE
brig-ctl <command> [args]
ctr [args] ctr against the private containerd
nerdctl [args] nerdctl against the private containerd
run <image> nerdctl run with the urunc runtime and snapshotter
rootless set this user up to run brig without sudo (rootless bundles)
env shell exports for driving the stack by hand
status service state and the relevant containerd plugins
version the bundled component versions
uninstall remove this install
USAGE
;;
*)
echo "unknown command '$cmd', try 'brig-ctl help'" >&2
exit 1 ;;
esac
CTLBODY
chmod 0755 "$STAGE/bin/brig-ctl"
}
# brig-uninstall.sh: static, reads the .install-stamp install.sh writes, and
# removes exactly what that install created.
# brig-rootless-setup.sh: give one user a containerd of their own, so brig runs
# without sudo. The system stack is root's -- $RUN_DIR is root-owned, and even
# with the socket group-readable a rootless nerdctl enters a user namespace
# that drops supplementary groups, so group membership never arrives. This
# writes the per-user half and starts it.
write_brig_rootless_setup() {
cat > "$STAGE/bin/brig-rootless-setup.sh" <<SETUPENV
#!/bin/sh
# brig-rootless-setup.sh -- set the invoking user up to run brig without sudo.
# Generated by build-bundle.sh. Baked with the fixed layout.
set -eu
PREFIX="$PREFIX"
NAMESPACE="$NAMESPACE"
SNAPSHOTTER="$SNAPSHOTTER"
CNI_DIR="$CNI_DIR"
ETC_DIR="$ETC_DIR"
SETUPENV
cat >> "$STAGE/bin/brig-rootless-setup.sh" <<'SETUPBODY'
say() { printf ' %-10s %s\n' "$1" "$2"; }
fatal() { echo "brig-rootless-setup: ERROR: $*" >&2; exit 1; }
owned_by_me() { [ "$(stat -c %u "$1" 2>/dev/null || echo -1)" = "$(id -u)" ]; }
# A rootless brig runs the monitor as the container image's user, and an image
# whose user is not root maps into this user's subuid range -- uid 501 in the
# image is 100500 on the host. A grant naming this user cannot reach that, and
# setfacl takes one uid at a time, so the range cannot be named either.
# --open-devices trades the per-user ACL for mode 0666 on these devices, which
# is what lets such an image run. See docs/rootless.md.
OPEN_DEVICES=false
for arg in "$@"; do
case "$arg" in
--open-devices) OPEN_DEVICES=true ;;
--help|-h) echo "usage: brig-rootless-setup.sh [--open-devices]"; exit 0 ;;
*) fatal "unknown argument '$arg'" ;;
esac
done
# Whether the device is open to every user of this host.
device_open() {
[ "$(stat -c %a "/dev/$1" 2>/dev/null || echo 0)" = 666 ]
}
# Whether this user can reach the device the way the monitor will. Group
# membership is not consulted: it does not survive the user namespace, so the
# kvm group that makes /dev/kvm openable at a login shell reaches nothing
# inside. The answer comes from the device, whichever rule file put it there.
device_granted() {
if [ "$OPEN_DEVICES" = true ]; then
device_open "$1"
else
getfacl -p "/dev/$1" 2>/dev/null | grep -q "^user:$(id -un):rw" || device_open "$1"
fi
}
[ "$(id -u)" -ne 0 ] || fatal "run this as the user who will run brig, not as root"
[ -d "$PREFIX" ] || fatal "no brig bundle at $PREFIX"
command -v systemctl >/dev/null 2>&1 || fatal "this needs a systemd user session"
XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
export XDG_RUNTIME_DIR
[ -d "$XDG_RUNTIME_DIR" ] || fatal "no $XDG_RUNTIME_DIR; log in as this user first"
CONF="${XDG_CONFIG_HOME:-$HOME/.config}/brig"
DATA="${XDG_DATA_HOME:-$HOME/.local/share}/brig"
UNIT="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/brig-containerd.service"
ROOTLESSKIT="$PREFIX/bin/rootlesskit"
ROOTLESS_SH="$PREFIX/bin/containerd-rootless.sh"
for f in "$ROOTLESSKIT" "$ROOTLESS_SH" "$PREFIX/bin/slirp4netns" "$PREFIX/bin/containerd"; do
[ -x "$f" ] || fatal "$f is missing from this bundle"
done
command -v newuidmap >/dev/null 2>&1 || fatal "newuidmap is missing (apt install uidmap)"
grep -q "^$(id -un):" /etc/subuid 2>/dev/null ||
fatal "no subuid range for $(id -un); add one to /etc/subuid and /etc/subgid"
# Ubuntu 24.04 and later refuse unprivileged user namespaces unless the binary
# that asks has a profile. Without this rootlesskit dies on its own re-exec:
# "fork/exec /proc/self/exe: operation not permitted".
# The profile names the binary by path, so a bundle installed somewhere else
# needs its own. Test for a profile covering this path, not for a file: the
# wrong test passes on a host that has one for a different prefix, and
# rootlesskit then fails at its own re-exec with permission denied.
RK_PROFILE="/etc/apparmor.d/$(printf '%s' "${ROOTLESSKIT#/}" | tr / .)"
if [ "$(sysctl -n kernel.apparmor_restrict_unprivileged_userns 2>/dev/null || echo 0)" = "1" ] &&
! grep -rqsF "$ROOTLESSKIT" /etc/apparmor.d/ 2>/dev/null; then
say "apparmor" "allowing user namespaces for $ROOTLESSKIT (needs sudo)"
sudo tee "$RK_PROFILE" >/dev/null <<PROF
abi <abi/4.0>,
include <tunables/global>
$ROOTLESSKIT flags=(unconfined) {
userns,
include if exists <local/brig-rootlesskit>
}
PROF
sudo apparmor_parser -r "$RK_PROFILE"
fi
# The VMM runs as this user inside that namespace, where supplementary groups
# are gone -- so membership of the kvm group never reaches cloud-hypervisor and
# KVM_CREATE_VM returns EPERM. Grant the user directly. The udev rule is what
# makes it stick: a bare setfacl is wiped by the next event on the device.
# /dev/vhost-vsock is the same story one layer along: qemu takes vsock from the
# kernel device, where cloud-hypervisor implements it in userspace, so which
# devices a run needs depends on the monitor urunc picks. /dev/vhost-net is not
# in this list: a guest boots and reaches the network without it, and it has no
# udev database entry here, so a rule for it would never fire anyway.
#
# Two preconditions decide whether that rule can do anything, and neither says
# so when it is unmet. udev runs RUN+= without a shell and discards the result,
# so on a host without setfacl the rule is written, reloaded, triggered, and
# grants nothing. And a device whose module is not loaded has no udev database
# entry, so there is nothing for a rule to match or a trigger to reach -- a
# /dev node left behind by an unloaded module looks present and is unreachable.
# Both end as KVM_CREATE_VM returning EPERM on the first sandbox, three layers
# from the cause.
if [ "$OPEN_DEVICES" != true ]; then
if ! command -v setfacl >/dev/null 2>&1 || ! command -v getfacl >/dev/null 2>&1; then
fatal "setfacl is missing (apt install acl); the device grant is made with it"
fi
fi
# A kernel without the module has nothing to load, and asking sudo to try is a
# password prompt for nothing. install.sh makes the same test before it
# downloads anything.
for m in kvm vhost_vsock; do
[ -d "/sys/module/$m" ] && continue
if command -v modinfo >/dev/null 2>&1 && ! modinfo "$m" >/dev/null 2>&1; then
continue
fi
sudo modprobe "$m" >/dev/null 2>&1 || true
done
# Loading it now only covers this boot. Nothing asks for vhost_vsock again on
# the next one: the module has no hardware to autoload from, and nothing can
# open /dev/vhost-vsock to trigger it because the node only exists once the
# module does. So the host is told to keep loading it. kvm is not in here --
# that one autoloads from the CPU, which is why /dev/kvm is present on a host
# that never asked for it. Only what this kernel really has is written: an
# entry for an absent module is a warning on every boot. Any file that already
# lists the module will do, so a re-run, or a host an admin prepared with a
# file of their own, needs no sudo here.
MODULES_FILE=/etc/modules-load.d/brig.conf
MODULES_WANT="# brig: the vsock device the monitor gives the guest. Written by
# brig-rootless-setup.sh; the matching udev rule grants access to it.
vhost_vsock"
if [ -d /sys/module/vhost_vsock ] &&
! grep -qsx 'vhost_vsock' /etc/modules-load.d/*.conf /etc/modules; then
say "modules" "asking this host to load vhost_vsock at boot (needs sudo)"
printf '%s\n' "$MODULES_WANT" | sudo tee "$MODULES_FILE" >/dev/null
fi
# Each user's grant is a file of its own, so a second user's setup adds a grant
# and leaves the first one in place. Older setups all wrote
# 99-brig-kvm.rules, and each run replaced the user named there. A host set up
# that way keeps that file: it still grants the user it names, and nothing here
# writes or removes it.
#
# --open-devices is a mode for the whole host, so it has one file of its own.
# Its name sorts after every 99-brig-kvm*.rules, so its MODE="0666" wins over
# their MODE="0660" while it exists. The per-user rules name the mode because
# udev does not undo a mode an earlier rule set: without it, a host whose open
# rule was removed stayed at 0666.
USER_RULE="/etc/udev/rules.d/99-brig-kvm-$(id -un).rules"
OPEN_RULE=/etc/udev/rules.d/99-brig-open-devices.rules
if [ "$OPEN_DEVICES" = true ]; then
RULE_FILE="$OPEN_RULE"
RULE_WANT=$(cat <<RULE
# brig: --open-devices. The monitor runs as the container image's user, and an
# image whose user is not root maps into a subuid that no per-user grant can
# name. Mode 0666 lets those images run. It is wider than the per-user ACL,
# which is why it is opt-in. Remove this file to go back to the per-user
# grants in 99-brig-kvm-<user>.rules.
KERNEL=="kvm", SUBSYSTEM=="misc", MODE="0666"
KERNEL=="vhost-vsock", SUBSYSTEM=="misc", MODE="0666"
RULE
)
else
RULE_FILE="$USER_RULE"
RULE_WANT=$(cat <<RULE
# brig: a rootless brig runs the VMM as the invoking user, inside a user
# namespace that drops supplementary groups, so the kvm group never reaches
# the monitor. Grant the user directly -- narrower than mode 0666, and
# reapplied on every event for these devices, which a one-shot setfacl is not.
# An image whose user is not root needs --open-devices instead.
KERNEL=="kvm", SUBSYSTEM=="misc", MODE="0660", RUN+="/usr/bin/setfacl -m u:$(id -un):rw /dev/kvm"
KERNEL=="vhost-vsock", SUBSYSTEM=="misc", MODE="0660", RUN+="/usr/bin/setfacl -m u:$(id -un):rw /dev/vhost-vsock"
RULE
)
fi
# What the devices allow decides whether anything is written. The file names
# do not: an admin may have granted this user in a file of their own, and a
# user without sudo can then finish the setup.
HAVE_DEV=""
NEED_RULE=false
for d in kvm vhost-vsock; do
[ -e "/dev/$d" ] || continue
HAVE_DEV="$HAVE_DEV $d"
device_granted "$d" || NEED_RULE=true
done
# A device set to 0666 by hand goes back at the next event on it, so
# --open-devices wants its rule in place as well.
if [ "$OPEN_DEVICES" = true ] && [ ! -f "$OPEN_RULE" ]; then
NEED_RULE=true
fi
if [ "$NEED_RULE" = true ]; then
if [ "$OPEN_DEVICES" = true ]; then
say "devices" "opening$HAVE_DEV to every user of this host (needs sudo)"
else
say "devices" "granting $(id -un) access to$HAVE_DEV (needs sudo)"
fi
printf '%s\n' "$RULE_WANT" | sudo tee "$RULE_FILE" >/dev/null
sudo udevadm control --reload-rules
for d in $HAVE_DEV; do sudo udevadm trigger --subsystem-match=misc --sysname-match="$d"; done
sudo udevadm settle --timeout=10 >/dev/null 2>&1 || true
# Read the result back rather than trusting the trigger: udev reports
# nothing to whoever asked, so a rule that reached nothing is otherwise
# discovered by a sandbox failing to boot.
for d in $HAVE_DEV; do
device_granted "$d" && continue
fatal "the rule did not reach /dev/$d.
'udevadm info /dev/$d' says whether udev knows the device at all: a node left
behind by an unloaded module has no entry there, and no rule can match it."
done
fi
mkdir -p "$CONF/cni/net.d" "$DATA/nerdctl" "$(dirname "$UNIT")"
# CRI and NRI serve kubelets, and NRI cannot bind its socket under /var/run as
# a user. brig drives containerd directly, so neither is wanted.
cat > "$CONF/containerd.toml" <<CFG
version = 3
disabled_plugins = ['io.containerd.cri.v1.runtime', 'io.containerd.cri.v1.images', 'io.containerd.grpc.v1.cri', 'io.containerd.nri.v1.nri']
[grpc]
address = '/run/containerd/containerd.sock'
[debug]
level = 'info'
CFG
# The bundle's nerdctl.toml with every root-owned path moved under $HOME. No
# address: brig-env.sh sets it, and it resolves inside the namespace.
cat > "$CONF/nerdctl.toml" <<CFG
debug = false
namespace = "$NAMESPACE"
snapshotter = "$SNAPSHOTTER"
cni_path = "$CNI_DIR"
cni_netconfpath = "$CONF/cni/net.d"
data_root = "$DATA/nerdctl"
cgroup_manager = "systemd"
hosts_dir = ["$ETC_DIR/certs.d"]
CFG
cp "$ETC_DIR"/cni/net.d/* "$CONF/cni/net.d/" 2>/dev/null || true
# containerd-rootless.sh execs the first containerd on PATH, so the bundle goes
# in front: daemon and shims then come from one build.
cat > "$UNIT" <<CFG
[Unit]
Description=brig containerd (rootless, %u)
Documentation=https://github.com/brig-sh/brig
[Service]
Type=notify
# rootlesskit forks before containerd starts, so readiness arrives from a pid
# systemd did not spawn. Without this the unit times out on a daemon that is
# already serving.
NotifyAccess=all
Delegate=yes
KillMode=mixed
Restart=always
RestartSec=5
TimeoutStartSec=60
Environment=PATH=$PREFIX/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# urunc looks for /etc/urunc/config.toml, and writing that symlink needs root.
# The shim inherits this from containerd, so the bundle's own config is found
# without touching /etc.
Environment=URUNC_CONFIG_FILE=$ETC_DIR/urunc.toml
ExecStart=$ROOTLESS_SH --config $CONF/containerd.toml
LimitNOFILE=infinity
LimitNPROC=infinity
TasksMax=infinity
[Install]
WantedBy=default.target
CFG
# Until this existed, sudo was the only way to run brig here, and every run
# wrote root-owned state into the invoking user's home. Any one of these stops
# an unprivileged brig dead, and brig doctor reports none of them.
for d in "$HOME/brig" "$HOME/.brig" "$HOME/.sigstore" "$CONF"; do
[ -e "$d" ] || continue
owned_by_me "$d" && continue
say "reclaim" "$d belongs to root from an earlier sudo run; taking it back"
sudo chown -R "$(id -u):$(id -g)" "$d"
done
# Without lingering the daemon dies with the last login shell of this user,
# taking every running sandbox with it.
loginctl enable-linger "$(id -un)" >/dev/null 2>&1 || true
systemctl --user daemon-reload
systemctl --user reset-failed brig-containerd.service 2>/dev/null || true
systemctl --user enable brig-containerd.service
# restart, not "enable --now": a daemon already running keeps its old PATH and
# environment, so a re-run after the bundle moved would leave containerd
# resolving shims and monitors out of the previous prefix.
systemctl --user restart brig-containerd.service
i=0
while [ "$i" -lt 15 ]; do
[ -S "$XDG_RUNTIME_DIR/containerd-rootless/api.sock" ] && break
i=$((i + 1)); sleep 1
done
say "state" "$(systemctl --user is-active brig-containerd.service)"
say "config" "$CONF"
say "data" "$DATA/nerdctl"
printf '\nNow run brig without sudo: brig doctor && brig run ubuntu ~/some/project\n'
SETUPBODY
chmod 0755 "$STAGE/bin/brig-rootless-setup.sh"
}
write_uninstaller() {
cat > "$STAGE/bin/brig-uninstall.sh" <<'UNEOF'
#!/bin/sh
#
# Remove a standalone brig install. Part of the brig bundle.
#
# Reads .install-stamp and removes exactly what that install created. A pool it
# did not create, a symlink that no longer points into this tree, a group it did
# not add, and anything belonging to the host are left alone.
#
# brig-uninstall.sh [--keep-data] [--yes]
set -eu
SELF_DIR="$(cd "$(dirname "$0")" && pwd)"
PREFIX_GUESS="$(dirname "$SELF_DIR")"
STAMP="$PREFIX_GUESS/.install-stamp"
info() { echo "[brig-uninstall] $*" >&2; }
warn() { echo "[brig-uninstall] WARNING: $*" >&2; }
fatal() { echo "[brig-uninstall] ERROR: $*" >&2; exit 1; }
KEEP_DATA=false
ASSUME_YES=false
for arg in "$@"; do
case "$arg" in
--keep-data) KEEP_DATA=true ;;
--yes|-y) ASSUME_YES=true ;;
--help|-h) sed -n '2,12p' "$0"; exit 0 ;;
*) fatal "unknown argument '$arg'" ;;
esac
done
[ -f "$STAMP" ] || fatal "no install stamp at $STAMP, refusing to remove anything"
# shellcheck disable=SC1090
. "$STAMP"
# A user install owns nothing outside $HOME, so it comes down without root.
if [ "${INSTALL_MODE:-system}" = "user" ]; then
[ "$(id -u)" -ne 0 ] \
|| fatal "this install belongs to a user; run the uninstaller as that user"
else
[ "$(id -u)" -eq 0 ] || fatal "run this as root"
fi
[ "$PREFIX" = "$PREFIX_GUESS" ] || fatal "stamp says $PREFIX but this script lives under $PREFIX_GUESS"
if [ "$ASSUME_YES" != "true" ]; then
echo "About to remove the brig install at $PREFIX (state in $DATA_DIR)." >&2
[ "$KEEP_DATA" = "true" ] && echo "State under $DATA_DIR will be kept." >&2
printf 'Continue? [y/N] ' >&2
read -r answer </dev/tty || answer=n
case "$answer" in y|Y|yes|YES) ;; *) info "aborted"; exit 0 ;; esac
fi
if [ "${SYSTEMD_INSTALLED:-false}" = "true" ] && command -v systemctl >/dev/null 2>&1; then
for unit in "$SERVICE_NAME" "${POOL_SERVICE_NAME:-}"; do
[ -n "$unit" ] || continue
systemctl stop "$unit.service" >/dev/null 2>&1 || true
systemctl disable "$unit.service" >/dev/null 2>&1 || true
done
systemctl daemon-reload || true
systemctl reset-failed >/dev/null 2>&1 || true
info "stopped and disabled the units"
fi
if [ "${INSTALL_MODE:-system}" = "user" ] && command -v systemctl >/dev/null 2>&1; then
systemctl --user stop brig-containerd.service >/dev/null 2>&1 || true
systemctl --user disable brig-containerd.service >/dev/null 2>&1 || true
rm -f "${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/brig-containerd.service"
systemctl --user daemon-reload >/dev/null 2>&1 || true
info "stopped and disabled the rootless containerd unit"
fi
pkill -f "containerd --config $PREFIX/etc/containerd.toml" 2>/dev/null || true
pkill -f "containerd-shim-urunc-v2.*$PREFIX" 2>/dev/null || true
sleep 1
if [ -r /proc/self/mounts ]; then
awk -v a="$PREFIX" -v b="$DATA_DIR" -v c="$RUN_DIR" \
'$2 ~ "^"a || $2 ~ "^"b || $2 ~ "^"c {print $2}' /proc/self/mounts \
| sort -r | while read -r mp; do
umount "$mp" 2>/dev/null || umount -l "$mp" 2>/dev/null || true
done
fi
if [ "${POOL_CREATED:-false}" = "true" ] && [ -n "${POOL_NAME:-}" ]; then
if dmsetup info "$POOL_NAME" >/dev/null 2>&1; then
held="$(dmsetup status "$POOL_NAME" 2>/dev/null | awk '{print $7}')"
if [ -n "$held" ] && [ "$held" != "-" ]; then
info "releasing the held metadata snapshot on $POOL_NAME"
dmsetup message "$POOL_NAME" 0 release_metadata_snap 2>/dev/null || true
fi
dmsetup ls 2>/dev/null | awk -v p="$POOL_NAME" '$1 ~ "^"p"-snap" {print $1}' \
| while read -r thin; do
dmsetup remove "$thin" 2>/dev/null || dmsetup remove -f "$thin" 2>/dev/null || true
done
info "removing device-mapper pool $POOL_NAME"
dmsetup remove "$POOL_NAME" 2>/dev/null \
|| dmsetup remove -f "$POOL_NAME" 2>/dev/null \
|| warn "could not remove the pool $POOL_NAME, it may still be in use"
fi
elif [ -n "${POOL_NAME:-}" ]; then
info "pool $POOL_NAME was not created by this install, leaving it alone"
fi
for dev in "${POOL_LOOP_DATA:-}" "${POOL_LOOP_META:-}"; do
[ -n "$dev" ] || continue
[ -b "$dev" ] || continue
back="$(losetup -l -n -O BACK-FILE "$dev" 2>/dev/null | tr -d ' ')"
case "$back" in
"$DATA_DIR"/*) info "detaching $dev"; losetup -d "$dev" 2>/dev/null || true ;;
*) warn "$dev no longer backs a file under $DATA_DIR, leaving it attached" ;;
esac
done
if [ -n "${ETC_SYMLINK:-}" ]; then
if [ -L "$ETC_SYMLINK" ] && [ "$(readlink "$ETC_SYMLINK")" = "$PREFIX/etc/urunc.toml" ]; then
rm -f "$ETC_SYMLINK"
info "removed $ETC_SYMLINK"
if [ -n "${ETC_SYMLINK_BACKUP:-}" ] && [ -e "$ETC_SYMLINK_BACKUP" ]; then
mv "$ETC_SYMLINK_BACKUP" "$ETC_SYMLINK"
info "restored the previous $ETC_SYMLINK"
fi
else
warn "$ETC_SYMLINK no longer points into $PREFIX, leaving it alone"
fi
rmdir /etc/urunc 2>/dev/null || true
fi
for l in "${BRIG_LAUNCHER:-}" "${BRIGD_LAUNCHER:-}"; do
[ -n "$l" ] || continue
if [ -f "$l" ] && grep -q "$PREFIX/bin" "$l" 2>/dev/null; then
rm -f "$l"
info "removed $l"
fi
done
# drop_bridge_rules <bridge>: delete the rules naming <bridge> with -i or -o
# from the CNI-* chains (filter and nat, IPv4 and IPv6). The CNI firewall
# plugin adds two such rules per bridge and never removes them. The chains are
# shared with other CNI networks, so they stay, and so do other bridges' rules.
# Each rule goes to iptables-restore as iptables-save printed it, so the shell
# never parses its quoted comment. A failed delete only warns, with iptables'
# own reason. The xtables lock is waited on for 5 seconds at most, so a lock
# another process holds cannot hang the uninstall.
drop_bridge_rules() {
for ipt in iptables ip6tables; do
command -v "$ipt-save" >/dev/null 2>&1 || continue
command -v "$ipt-restore" >/dev/null 2>&1 || continue
for table in filter nat; do
"$ipt-save" -t "$table" 2>/dev/null | awk -v br="$1" '
$1 == "-A" && $2 ~ /^CNI-/ {
for (i = 3; i < NF; i++)
if (($i == "-i" || $i == "-o") && $(i + 1) == br) { print; next }
}' | while IFS= read -r rule; do
if err="$(printf '*%s\n-D %s\nCOMMIT\n' "$table" "${rule#-A }" \
| "$ipt-restore" -w 5 --noflush 2>&1)"; then
info "removed $ipt -t $table ${rule#-A }"
else
warn "could not remove $ipt -t $table ${rule#-A }: $err"
fi
done
done
done
}
if [ "${INSTALL_MODE:-system}" != "user" ] && [ -n "${BRIDGE_NAME:-}" ]; then
if ip link show "$BRIDGE_NAME" >/dev/null 2>&1; then
ip link set "$BRIDGE_NAME" down 2>/dev/null || true
ip link delete "$BRIDGE_NAME" 2>/dev/null || true
info "removed the CNI bridge $BRIDGE_NAME"
fi
# The rules outlive the bridge, so they go whether or not it is still up.
drop_bridge_rules "$BRIDGE_NAME"
fi
if [ "${GROUP_CREATED:-false}" = "true" ] && [ -n "${BRIG_GROUP:-}" ]; then
if command -v groupdel >/dev/null 2>&1; then
if groupdel "$BRIG_GROUP" 2>/dev/null; then
info "removed the $BRIG_GROUP group"
else
warn "could not remove the $BRIG_GROUP group (a user may still list it)"
fi
fi
fi
rm -rf "${PREFIX:?}"
rm -rf "${RUN_DIR:?}"
info "removed $PREFIX and $RUN_DIR"
if [ "$KEEP_DATA" = "true" ]; then
info "kept state under $DATA_DIR"
else
rm -rf "${DATA_DIR:?}"
info "removed $DATA_DIR"
fi
# brig-rootless-setup.sh writes the per-user config and containerd's data root
# outside the bundle, so a user install is only fully gone once those are too.
if [ "${INSTALL_MODE:-system}" = "user" ] && [ "$KEEP_DATA" != "true" ]; then
rm -rf "${XDG_CONFIG_HOME:-$HOME/.config}/brig"
rm -rf "$(dirname "${PREFIX:?}")/nerdctl"
info "removed the per-user brig config and containerd data root"
fi
# data/ and agent/ are siblings under one root; drop it once both are gone. This
# is a no-op while --keep-data preserved agent/, or if anything else lives there.
rmdir "$(dirname "${PREFIX:?}")" 2>/dev/null || true
info "done"
UNEOF
chmod 0755 "$STAGE/bin/brig-uninstall.sh"
}
fetch() {
curl -sfL --retry 3 --retry-delay 2 -o "$2" "$1" || fatal "failed to download $1"
}
# Verify against an upstream sums file when the project publishes one. A
# component without published checksums is not fatal; the whole tarball is
# checksummed and signed at release time either way.
verify() {
file="$1"; sums_url="$2"; name="$3"
actual="$(sha256sum "$file" | awk '{print $1}')"
[ -n "$sums_url" ] || { info " $name: no upstream checksums, recorded $actual"; return 0; }
if curl -sfL --retry 2 -o "$TMP_DIR/sums.txt" "$sums_url" 2>/dev/null; then
expected="$(awk -v n="$name" '$2 == n || $2 == "*"n {print $1; exit}' "$TMP_DIR/sums.txt")"
[ -n "$expected" ] || expected="$(awk 'NF==1 {print $1; exit}' "$TMP_DIR/sums.txt")"
if [ -n "$expected" ] && [ "$expected" != "$actual" ]; then
fatal "checksum mismatch for $name: expected $expected, got $actual"
fi
info " $name: verified"
else
info " $name: no upstream checksums, recorded $actual"
fi
}
# checkout_source <repo> <branch> <ref> <dir>: put github.com/<repo> into <dir>
# at exactly the commit <ref>, or at the tip of <branch> when <ref> is empty,
# and print the commit it checked out. The commit is fetched by its SHA: a
# --depth 1 --branch clone stops containing a pinned commit as soon as the
# branch moves past it. GitHub serves any reachable commit by SHA, a pull
# request's head included.
checkout_source() {
cs_repo="$1"; cs_branch="$2"; cs_ref="$3"; cs_dir="$4"
# stdout carries the commit and nothing else.
git init -q "$cs_dir" >&2
git -C "$cs_dir" remote add origin "https://github.com/$cs_repo" >&2
if [ -n "$cs_ref" ]; then
git -C "$cs_dir" fetch -q --depth 1 origin "$cs_ref" >&2 \
|| fatal "could not fetch $cs_repo@$cs_ref"
else
git -C "$cs_dir" fetch -q --depth 1 origin "refs/heads/$cs_branch" >&2 \
|| fatal "could not fetch the $cs_branch branch of $cs_repo"
fi
git -C "$cs_dir" -c advice.detachedHead=false checkout -q FETCH_HEAD >&2
cs_head="$(git -C "$cs_dir" rev-parse HEAD)"
if [ -n "$cs_ref" ] && [ "$cs_head" != "$cs_ref" ]; then
fatal "$cs_repo: checked out $cs_head, not the pinned $cs_ref"
fi
echo "$cs_head"
}
# pinned_note <name> <repo> <branch> <ref> <commit>: log what was checked out,
# and warn when it was a branch tip.
pinned_note() {
if [ -n "$4" ]; then
info "$1: checked out $2@$5 (pinned${3:+; from branch $3})"
else
info "WARNING: $1: built $2@$5, the tip of $3 at build time. It is not pinned,"
info "WARNING: and a rebuild of this bundle version may carry different code."
fi
}
info "building $NAME (variant $VARIANT)"