Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1156 lines (1048 loc) · 48 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1156 lines (1048 loc) · 48 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
#
# brig installer for Linux.
#
# Fetches one release tarball -- built and published by THIS repository, which
# bundles everything: brig itself (from brig-sh/brig's release), the urunc runtime,
# the monitors, the boot assets, and every config file, systemd unit and wrapper,
# laid out as a /var/lib/brig/data tree. install.sh unpacks it and wires it into the host:
# a private containerd, a device-mapper pool, a unix group for the socket, and
# (optionally) a systemd service. It builds nothing and fetches nothing else.
#
# curl -fsSL https://raw.githubusercontent.com/NOFireAI/brig-standalone-linux/main/install.sh | sh -
#
# Two output modes, both with a progress bar:
# quiet (default) one progress bar and the final summary
# verbose (INSTALL_BRIG_VERBOSE=true) a line per stage as well
#
# Run install.sh --help for the environment variables it honors.
set -eu
# ---------------------------------------------------------------- pins
# The one artifact this installer fetches: a per-arch tarball released by this
# repository, which runs release.yml and bundles the whole brig runtime.
# "latest" resolves to its newest release.
DEFAULT_RELEASE_REPO="NOFireAI/brig-standalone-linux"
DEFAULT_RELEASE_VERSION="latest"
GITHUB="https://github.com"
# The layout the tarball is built with, and the one a root install uses.
# k3s-style: one root (/var/lib/brig) with two sibling trees -- the immutable
# bundle under data/ and mutable state under agent/ -- the socket in /run, and
# the launchers on the host PATH.
BUILD_ROOT_DIR="/var/lib/brig"
BUILD_PREFIX="$BUILD_ROOT_DIR/data"
BUILD_DATA_DIR="$BUILD_ROOT_DIR/agent"
BUILD_RUN_DIR="/run/brig"
# Root installs into the host layout, a normal user into their home. --system
# and --user force it, for a root user who wants a home install or the reverse.
if [ "$(id -u)" -eq 0 ]; then
INSTALL_MODE="${INSTALL_BRIG_MODE:-system}"
else
INSTALL_MODE="${INSTALL_BRIG_MODE:-user}"
fi
# The tarball's configs carry the build layout as literal paths, so a user
# install rewrites them once the tree is down: see retarget_tree.
set_layout() {
if [ "$INSTALL_MODE" = "user" ]; then
ROOT_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/brig"
RUN_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/brig"
LAUNCH_DIR="$HOME/.local/bin"
else
ROOT_DIR="$BUILD_ROOT_DIR"
RUN_DIR="$BUILD_RUN_DIR"
LAUNCH_DIR="/usr/local/bin"
fi
PREFIX="$ROOT_DIR/data" # immutable binaries, config, boot assets
DATA_DIR="$ROOT_DIR/agent" # mutable state: containerd store, snapshots, pool
BIN_DIR="$PREFIX/bin"
ETC_DIR="$PREFIX/etc"
CONTAINERD_SOCK="$RUN_DIR/containerd.sock"
STAMP="$PREFIX/.install-stamp"
PINS="$PREFIX/pins.env"
}
SERVICE_NAME="brig-containerd"
POOL_SERVICE_NAME="brig-devpool"
POOL_NAME="brig-devpool"
NAMESPACE="brig"
# Keyless cosign records the workflow that built an artifact. checksums.txt is
# signed by this repository's release workflow, so verification is bound to it.
SIG_IDENTITY_REGEXP="${INSTALL_BRIG_SIG_IDENTITY:-^https://github.com/NOFireAI/brig-standalone-linux/.github/workflows/release.yml@refs/tags/}"
SIG_OIDC_ISSUER="${INSTALL_BRIG_SIG_ISSUER:-https://token.actions.githubusercontent.com}"
# ---------------------------------------------------------------- logging
# say() prints a stage detail, only in verbose mode. warn()/fatal() always print,
# on a fresh line so they never land on top of the progress bar.
VERBOSE=false
BAR_ACTIVE=false
_break_bar() { if [ "$BAR_ACTIVE" = "true" ]; then printf '\n' >&2; BAR_ACTIVE=false; fi; }
say() { if [ "$VERBOSE" = "true" ]; then _break_bar; echo " $*" >&2; fi; }
warn() { _break_bar; echo "[brig-install] WARNING: $*" >&2; }
fatal() { _break_bar; echo "[brig-install] ERROR: $*" >&2; exit 1; }
STEP=0
TOTAL=9
BAR_WIDTH=28
# progress <label>: advance the bar one stage. In quiet mode it redraws a single
# in-place bar; in verbose mode it prints a numbered stage line instead.
progress() {
STEP=$((STEP + 1))
if [ "$VERBOSE" = "true" ]; then
_break_bar
echo "[brig-install] ($STEP/$TOTAL) $1" >&2
return 0
fi
pct=$(( STEP * 100 / TOTAL ))
filled=$(( STEP * BAR_WIDTH / TOTAL ))
bar=""; i=0
while [ "$i" -lt "$BAR_WIDTH" ]; do
if [ "$i" -lt "$filled" ]; then bar="$bar#"; else bar="$bar."; fi
i=$((i + 1))
done
printf '\r[brig-install] [%s] %3d%% %-22.22s' "$bar" "$pct" "$1" >&2
BAR_ACTIVE=true
if [ "$STEP" -ge "$TOTAL" ]; then _break_bar; fi
# Never let a false test above become progress's return status: under `set -e`
# a non-zero return from this standalone call would abort the install.
return 0
}
usage() {
cat >&2 <<'USAGE'
Usage: install.sh [--help] [--verbose] [--quiet] [--system|--user]
Fetches the brig release tarball and installs it. As root the tree goes to
/var/lib/brig/data with state in /var/lib/brig and the socket in /run/brig. As a
normal user it goes to ~/.local/share/brig instead and nothing outside $HOME is
touched, which needs the host prepared once: see docs/rootless.md. It builds
nothing.
INSTALL_BRIG_RELEASE_REPO repo that publishes the tarball
(default: NOFireAI/brig-standalone-linux)
INSTALL_BRIG_RELEASE_VERSION release tag, or "latest" (default: latest)
INSTALL_BRIG_BUNDLE a local tarball (or URL) to install instead of
fetching the release
INSTALL_BRIG_MODE system | user (default: user unless root)
INSTALL_BRIG_ROOTLESS true to install the rootless bundle, which lets
any user of a system install run brig-ctl
rootless. Implied by a user install
INSTALL_BRIG_GROUP unix group granted the socket (default: brig)
INSTALL_BRIG_USER user added to that group (default: $SUDO_USER)
INSTALL_BRIG_SYSTEMD ask | yes | no. Install the systemd service?
(default: ask; non-interactive falls back to yes)
INSTALL_BRIG_VERBOSE true for a line per stage (default: false, quiet)
INSTALL_BRIG_SKIP_START true to lay the tree down without starting it
INSTALL_BRIG_SKIP_SIGCHECK true to install a remote tarball unverified. It
keeps no signed record of the kernel and initrd,
so brig warns before every run
INSTALL_BRIG_REQUIRE_SIGCHECK true to refuse unless the cosign signature
over checksums.txt verifies
INSTALL_BRIG_FORCE true to take over a /var/lib/brig/data we did not create
INSTALL_BRIG_SNAPSHOTTER set to overlayfs to skip devmapper pool setup
(brig defaults to overlayfs; export
CONTAINERD_SNAPSHOTTER=devmapper to switch at runtime)
INSTALL_BRIG_POOL_SIZE thin pool data size (default: 100G, sparse)
INSTALL_BRIG_POOL_PREALLOC true to fallocate the pool backing files
INSTALL_BRIG_DEBUG true for set -x
USAGE
}
# ---------------------------------------------------------------- environment
setup_env() {
set_layout
RELEASE_REPO="${INSTALL_BRIG_RELEASE_REPO:-$DEFAULT_RELEASE_REPO}"
RELEASE_VERSION="${INSTALL_BRIG_RELEASE_VERSION:-$DEFAULT_RELEASE_VERSION}"
RELEASE_RESOLVED=""
BUNDLE="${INSTALL_BRIG_BUNDLE:-}"
BRIG_GROUP="${INSTALL_BRIG_GROUP:-brig}"
BRIG_USER="${INSTALL_BRIG_USER:-${SUDO_USER:-}}"
SYSTEMD_CHOICE="${INSTALL_BRIG_SYSTEMD:-ask}"
case "${INSTALL_BRIG_VERBOSE:-false}" in true|1) VERBOSE=true ;; esac
# A user install is the rootless path, so it implies the rootless bundle.
WANT_ROOTLESS="${INSTALL_BRIG_ROOTLESS:-false}"
# Where sudo can ask for a password. A test points it at a file.
TTY="${INSTALL_BRIG_TTY:-/dev/tty}"
[ "$INSTALL_MODE" = "user" ] && WANT_ROOTLESS=true
SKIP_START="${INSTALL_BRIG_SKIP_START:-false}"
SKIP_SIGCHECK="${INSTALL_BRIG_SKIP_SIGCHECK:-false}"
# For a caller that installs cosign first and wants the signature to be
# load-bearing rather than best-effort.
REQUIRE_SIGCHECK="${INSTALL_BRIG_REQUIRE_SIGCHECK:-false}"
FORCE="${INSTALL_BRIG_FORCE:-false}"
# brig uses overlayfs by default; a user switches at runtime by exporting
# CONTAINERD_SNAPSHOTTER=devmapper. devmapper is still configured in
# containerd, and its thin pool is provisioned when the kernel supports it
# (unless the user opts out with INSTALL_BRIG_SNAPSHOTTER=overlayfs).
SNAPSHOTTER=overlayfs
WANT_DEVMAPPER=true
[ "${INSTALL_BRIG_SNAPSHOTTER:-}" = "overlayfs" ] && WANT_DEVMAPPER=false
# A thin pool needs losetup and dmsetup, which a user namespace does not
# get. A user install is overlayfs only.
[ "$INSTALL_MODE" = "user" ] && WANT_DEVMAPPER=false
POOL_SIZE="${INSTALL_BRIG_POOL_SIZE:-100G}"
POOL_META_SIZE="${INSTALL_BRIG_POOL_META_SIZE:-10G}"
POOL_PREALLOC="${INSTALL_BRIG_POOL_PREALLOC:-false}"
case "${INSTALL_BRIG_DEBUG:-false}" in true|1) set -x ;; esac
TMP_DIR="$(mktemp -d -t brig-install.XXXXXX)"
trap cleanup_tmp EXIT INT TERM
}
cleanup_tmp() {
[ -n "${TMP_DIR:-}" ] && [ -d "$TMP_DIR" ] && rm -rf "$TMP_DIR"
return 0
}
# ---------------------------------------------------------------- preflight
verify_system() {
if [ "$INSTALL_MODE" = "system" ]; then
[ "$(id -u)" -eq 0 ] \
|| fatal "a system install must run as root. Run with --user to install into \$HOME"
elif [ "$(id -u)" -eq 0 ]; then
fatal "a user install must not run as root: the tree would land root-owned in a home"
fi
case "$(uname -s)" in
Linux) ;;
*) fatal "brig's runtime installs on Linux only, this is $(uname -s)" ;;
esac
case "$(uname -m)" in
x86_64|amd64) ARCH=amd64 ;;
aarch64|arm64) ARCH=arm64 ;;
*) fatal "unsupported architecture $(uname -m)" ;;
esac
for tool in curl tar sha256sum sed awk truncate; do
command -v "$tool" >/dev/null 2>&1 || fatal "'$tool' is required and was not found"
done
# A user install makes it with the other packages: see verify_rootless_prereqs.
if [ "$INSTALL_MODE" != "user" ]; then
have_iptables || warn "iptables not found, so a sandbox network cannot start: the CNI firewall plugin runs it"
fi
# Probe for devmapper thin-pool support so the pool can be provisioned (and
# devmapper made switchable at runtime). Missing tools or a kernel without
# CONFIG_DM_THIN_PROVISIONING is not an error: devmapper is simply skipped
# and brig runs on overlayfs.
DM_AVAILABLE=false
if [ "$WANT_DEVMAPPER" = "true" ] \
&& command -v dmsetup >/dev/null 2>&1 \
&& command -v losetup >/dev/null 2>&1 \
&& command -v blockdev >/dev/null 2>&1; then
modprobe dm_thin_pool 2>/dev/null || true
dmsetup targets 2>/dev/null | grep -q '^thin-pool' && DM_AVAILABLE=true
fi
HAVE_SYSTEMD=true
command -v systemctl >/dev/null 2>&1 || HAVE_SYSTEMD=false
}
# What a user install cannot do for itself, in three classes. Each is a
# confusing failure somewhere else if it goes unchecked: rootlesskit dies on its
# own re-exec without the AppArmor profile, KVM_CREATE_VM returns EPERM without
# access to the device, and newuidmap is setuid-root.
#
# - No root can fix a missing login session or systemd, so those stop the
# install.
# - Packages and a subuid range brig-rootless-setup.sh does not make. With a
# sudo that runs without a password, the installer makes them here, before
# the download. A fresh Ubuntu cloud image lacks uidmap and acl, and
# stopping on them stopped a user who had that sudo.
# - The rest brig-rootless-setup.sh makes with sudo at the end of the
# install.
#
# Without a sudo that runs without a password, any of these would stop at a
# prompt after the whole bundle is downloaded and unpacked. So the install
# stops here instead.
#
# Whatever needs root is printed as one block of commands for this user and
# this prefix. An admin runs it once, and the next install needs no sudo. The
# checks and the commands mirror brig-rootless-setup.sh; keep them in step.
# Debian keeps it in /usr/sbin, which a user's PATH does not have, and the CNI
# plugins find it there all the same.
have_iptables() {
command -v iptables >/dev/null 2>&1 || [ -x /usr/sbin/iptables ] || [ -x /sbin/iptables ]
}
# install_tools_cmd <tool>...: the root command that installs the tools, with the
# package manager this host has and the names it gives them. newuidmap is in
# uidmap on Debian and Ubuntu and in shadow elsewhere; setfacl is in acl
# everywhere; iptables is iptables-nft on the RHEL family and iptables elsewhere. A host with none of these gets a comment, which installs nothing,
# so the check that follows still stops on the missing tool.
install_tools_cmd() {
names=""
for tool in "$@"; do
case "$tool" in
newuidmap) if command -v apt-get >/dev/null 2>&1; then names="$names uidmap"
elif command -v dnf >/dev/null 2>&1 || command -v yum >/dev/null 2>&1; then names="$names shadow-utils"
else names="$names shadow"; fi ;;
setfacl) names="$names acl" ;;
iptables) if command -v dnf >/dev/null 2>&1 || command -v yum >/dev/null 2>&1; then names="$names iptables-nft"
else names="$names iptables"; fi ;;
esac
done
if command -v apt-get >/dev/null 2>&1; then
echo "DEBIAN_FRONTEND=noninteractive apt-get install -y$names </dev/null"
elif command -v dnf >/dev/null 2>&1; then
echo "dnf install -y$names"
elif command -v yum >/dev/null 2>&1; then
echo "yum install -y$names"
elif command -v zypper >/dev/null 2>&1; then
echo "zypper --non-interactive install$names"
elif command -v pacman >/dev/null 2>&1; then
echo "pacman -S --noconfirm --needed$names"
else
echo "# install$names with this host's package manager: none that this installer knows is here"
fi
}
# Whether sudo runs without a password now. Under `curl ... | sh`, stdin is the
# script, so a user whose sudo needs a password is asked for it here, once, on
# the terminal: a user in a sudo group, and only when there is a terminal.
sudo_ready() {
sudo -n true 2>/dev/null && return 0
[ -z "${SUDO_ASKED:-}" ] || return 1
SUDO_ASKED=true
case " $(id -nG 2>/dev/null) " in
*" sudo "* | *" admin "* | *" wheel "*) ;;
*) return 1 ;;
esac
( : < "$TTY" ) 2>/dev/null || return 1
warn "this host needs root's setup, so sudo asks for your password once"
# The terminal is where the password comes from, not a file sudo reads.
# shellcheck disable=SC2024
sudo -v < "$TTY" || return 1
sudo -n true 2>/dev/null
}
verify_rootless_prereqs() {
me="$(id -un)"
miss=""
need=""
setup_can=true
fix="$TMP_DIR/root-commands.sh"
# The part of $fix that brig-rootless-setup.sh does not make.
own="$TMP_DIR/root-own.sh"
: > "$fix"
: > "$own"
[ -d "${XDG_RUNTIME_DIR:-/run/user/$(id -u)}" ] \
|| miss="$miss\n no ${XDG_RUNTIME_DIR:-/run/user/$(id -u)}: log in as this user first"
[ "$HAVE_SYSTEMD" = "true" ] \
|| miss="$miss\n no systemctl: a user install needs a systemd user session"
pkgs=""
command -v newuidmap >/dev/null 2>&1 \
|| { need="$need\n newuidmap is missing"; pkgs="$pkgs newuidmap"; }
command -v setfacl >/dev/null 2>&1 \
|| { need="$need\n setfacl is missing"; pkgs="$pkgs setfacl"; }
# The CNI firewall plugin of every sandbox network runs it. openSUSE Leap 16
# ships nftables alone, and there every run failed in the network setup.
have_iptables \
|| { need="$need\n iptables is missing"; pkgs="$pkgs iptables"; }
if [ -n "$pkgs" ]; then
setup_can=false
# shellcheck disable=SC2086
install_tools_cmd $pkgs | tee -a "$own" >> "$fix"
fi
# A range that overlaps another user's maps both users' containers onto the
# same host uids, so the range offered starts past every one handed out.
if ! grep -q "^$me:" /etc/subuid 2>/dev/null || ! grep -q "^$me:" /etc/subgid 2>/dev/null; then
setup_can=false
need="$need\n no subuid range for $me"
first=$(cat /etc/subuid /etc/subgid 2>/dev/null \
| awk -F: '$2 + $3 > m {m = $2 + $3} END {print (m > 100000 ? m : 100000)}')
last=$((first + 65535))
echo "usermod --add-subuids $first-$last --add-subgids $first-$last $me" | tee -a "$own" >> "$fix"
fi
# The profile names the binary by path, so the one a /var/lib install left
# behind does not cover a tree in $HOME.
if [ "$(sysctl -n kernel.apparmor_restrict_unprivileged_userns 2>/dev/null || echo 0)" = "1" ] \
&& ! grep -rqsF "$BIN_DIR/rootlesskit" /etc/apparmor.d/ 2>/dev/null; then
need="$need\n no AppArmor profile for $BIN_DIR/rootlesskit"
profile="/etc/apparmor.d/$(printf '%s' "${BIN_DIR#/}/rootlesskit" | tr / .)"
cat >> "$fix" <<FIX
cat > '$profile' <<'PROF'
abi <abi/4.0>,
include <tunables/global>
$BIN_DIR/rootlesskit flags=(unconfined) {
userns,
include if exists <local/brig-rootlesskit>
}
PROF
apparmor_parser -r '$profile'
FIX
fi
# Readable here is the wrong question. The monitor runs inside a user
# namespace where this user's supplementary groups are gone, so the kvm
# group that makes /dev/kvm openable at this prompt reaches nothing in
# there -- testing r/w passes on exactly the hosts that then fail to boot a
# sandbox. Look for what the setup grants: this user by name, or a mode
# that covers everyone. A module that is not loaded has no device yet, and
# its device needs the grant once it has one.
grant=false
for d in kvm vhost-vsock; do
[ -e "/dev/$d" ] || continue
getfacl -p "/dev/$d" 2>/dev/null | grep -q "^user:$me:rw" && continue
[ "$(stat -c %a "/dev/$d" 2>/dev/null || echo 0)" = 666 ] && continue
need="$need\n no access to /dev/$d from inside the user namespace"
grant=true
done
load=""
for m in kvm vhost_vsock; do
[ -d "/sys/module/$m" ] && continue
module_exists "$m" || continue
need="$need\n the $m module is not loaded"
load="$load $m"
grant=true
done
if [ "$grant" = true ]; then
cat >> "$fix" <<FIX
cat > /etc/udev/rules.d/99-brig-kvm-$me.rules <<'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:$me:rw /dev/kvm"
KERNEL=="vhost-vsock", SUBSYSTEM=="misc", MODE="0660", RUN+="/usr/bin/setfacl -m u:$me:rw /dev/vhost-vsock"
RULE
udevadm control --reload-rules
FIX
fi
for m in $load; do
echo "modprobe $m" >> "$fix"
done
# vhost_vsock does not autoload, so the host is told to load it at boot.
case " $load " in *" vhost_vsock "*) vsock=true ;; *) vsock=false ;; esac
[ -d /sys/module/vhost_vsock ] && vsock=true
if [ "$vsock" = true ] && ! grep -qsx 'vhost_vsock' /etc/modules-load.d/*.conf /etc/modules; then
need="$need\n vhost_vsock is not loaded at boot"
cat >> "$fix" <<'FIX'
cat > /etc/modules-load.d/brig.conf <<'CONF'
# 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
CONF
FIX
fi
if [ "$grant" = true ]; then
cat >> "$fix" <<'FIX'
udevadm trigger --subsystem-match=misc --sysname-match=kvm
udevadm trigger --subsystem-match=misc --sysname-match=vhost-vsock
udevadm settle --timeout=10 || true
FIX
fi
# Anything an earlier sudo run left root-owned in this home stops an
# unprivileged brig, and the setup takes it back with sudo.
for d in "$HOME/brig" "$HOME/.brig" "$HOME/.sigstore" "${XDG_CONFIG_HOME:-$HOME/.config}/brig"; do
[ -e "$d" ] || continue
[ "$(stat -c %u "$d" 2>/dev/null || echo -1)" = "$(id -u)" ] && continue
need="$need\n $d is not owned by $me"
echo "chown -R $(id -u):$(id -g) '$d'" >> "$fix"
done
if [ -z "$miss" ] && [ -z "$need" ]; then
return 0
fi
if [ -z "$miss" ] && [ "$setup_can" = false ] && [ -z "${OWN_MADE:-}" ] \
&& sudo_ready; then
OWN_MADE=true
warn "making what brig-rootless-setup.sh does not, with sudo:"
sed 's/^/ /' "$own" >&2
# A distribution without apt-get fails here, and the host is then one the
# commands below are for. Otherwise the check runs again on the new host.
if sudo -n sh -eu "$own" >&2; then
verify_rootless_prereqs
return 0
fi
fi
if [ -z "$miss" ] && [ "$setup_can" = true ] && sudo_ready; then
# Counted, not assumed: this said "two" whatever it had found, which
# reads as a second thing the reader has missed.
n=$(printf '%b' "$need" | grep -c '^ ')
if [ "$n" = 1 ]; then what="one host setting is"; else what="$n host settings are"; fi
# shellcheck disable=SC2059
printf "[brig-install] WARNING: $what still missing:$need\n" >&2
warn "brig-rootless-setup.sh will make them with sudo"
return 0
fi
_break_bar
# shellcheck disable=SC2059
printf "[brig-install] ERROR: this host is not set up for a rootless brig:$miss$need\n\n" >&2
if [ -n "$need" ]; then
if [ -n "${OWN_MADE:-}" ]; then
why="making them with sudo failed, above"
else
why="sudo cannot run here without a password"
fi
cat >&2 <<MSG
Nothing was downloaded: $why.
Run the commands below as root, or ask an admin to, and then run this
installer again. It needs no sudo after that.
sudo sh -eu <<'BRIG_ROOT'
$(cat "$fix")
BRIG_ROOT
MSG
if [ -z "${OWN_MADE:-}" ]; then
echo " With sudo of your own, run 'sudo -v' and then this installer again: it" >&2
echo " makes these itself." >&2
fi
fi
echo " See docs/rootless.md." >&2
exit 1
}
# Whether the running kernel has the module, loaded or not. Without modinfo
# there is no telling, and the answer that leads to a working host is yes.
module_exists() {
command -v modinfo >/dev/null 2>&1 || return 0
modinfo "$1" >/dev/null 2>&1
}
# An existing prefix is only ours if we stamped it.
verify_root_dir() {
PRIOR_POOL_CREATED=""
PRIOR_ETC_CREATED=""
PRIOR_ETC_BACKUP=""
PRIOR_GROUP_CREATED=""
[ -e "$PREFIX" ] || return 0
if [ -f "$STAMP" ]; then
PRIOR_POOL_CREATED="$(awk -F= '$1 == "POOL_CREATED" {print $2}' "$STAMP")"
PRIOR_ETC_CREATED="$(awk -F= '$1 == "ETC_SYMLINK_CREATED" {print $2}' "$STAMP")"
PRIOR_ETC_BACKUP="$(awk -F= '$1 == "ETC_SYMLINK_BACKUP" {print $2}' "$STAMP")"
PRIOR_GROUP_CREATED="$(awk -F= '$1 == "GROUP_CREATED" {print $2}' "$STAMP")"
say "found an existing brig install at $PREFIX, upgrading in place"
return 0
fi
if [ "$FORCE" = "true" ]; then
warn "$PREFIX exists and was not created by this installer, taking it over"
return 0
fi
fatal "$PREFIX exists and has no install stamp.
Set INSTALL_BRIG_FORCE=true to take it over."
}
# Ask whether to install the systemd service, before the progress bar starts so
# the prompt is not drawn over. In a non-interactive run (no controlling tty)
# there is no one to ask, so default to yes; INSTALL_BRIG_SYSTEMD forces it.
ask_systemd() {
INSTALL_SYSTEMD=false
# The per-user daemon is a systemd --user unit, written by
# brig-rootless-setup.sh, so there is nothing to ask about.
[ "$INSTALL_MODE" = "user" ] && return 0
if [ "$HAVE_SYSTEMD" != "true" ]; then
[ "$SYSTEMD_CHOICE" = "yes" ] && warn "systemd not found, cannot install the service"
return 0
fi
case "$SYSTEMD_CHOICE" in
yes) INSTALL_SYSTEMD=true; return 0 ;;
no) INSTALL_SYSTEMD=false; return 0 ;;
esac
# ask. Opening it is the test, because /dev/tty passes -r with no
# controlling terminal and then fails to open, which printed the prompt and
# an error before defaulting anyway. The open goes in a subshell: dash
# treats a redirection failure on a compound command as fatal, even as an
# if condition, and takes the whole script with it.
if (: </dev/tty) 2>/dev/null; then
printf '[brig-install] Install and enable the %s systemd service now? [Y/n] ' "$SERVICE_NAME" >&2
read -r ans </dev/tty || ans=y
case "$ans" in n|N|no|NO) INSTALL_SYSTEMD=false ;; *) INSTALL_SYSTEMD=true ;; esac
else
INSTALL_SYSTEMD=true
say "no tty to prompt on, installing the systemd service (set INSTALL_BRIG_SYSTEMD=no to skip)"
fi
}
# ---------------------------------------------------------------- fetch
# curl with a real download progress bar in both modes: it is the one long step.
fetch_bar() {
curl -fL --retry 3 --retry-delay 2 --progress-bar -o "$2" "$1" \
|| fatal "download failed: $1"
}
fetch_quiet() {
curl -sfL --retry 2 -o "$2" "$1"
}
resolve_release_version() {
[ -n "$RELEASE_RESOLVED" ] && return 0
if [ "$RELEASE_VERSION" = "latest" ]; then
RELEASE_RESOLVED="$(curl -sfL "https://api.github.com/repos/$RELEASE_REPO/releases" \
| sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | head -1)"
[ -n "$RELEASE_RESOLVED" ] \
|| fatal "could not find a release in $RELEASE_REPO. Set INSTALL_BRIG_RELEASE_VERSION."
else
RELEASE_RESOLVED="$RELEASE_VERSION"
fi
}
# Put the release tarball in $TMP_DIR/bundle.tar.gz, from a local path, a URL, or
# the release. A remote tarball is verified against the release's cosign-signed
# checksums.txt; a local path is taken as given (the airgapped case).
fetch_tarball() {
TARBALL="$TMP_DIR/bundle.tar.gz"
case "$BUNDLE" in
"")
resolve_release_version
# Two artifacts per release. A user install needs the rootless
# one, and a root install takes it only when asked, so a node
# keeps the smaller bundle by default.
if [ "$WANT_ROOTLESS" = "true" ]; then
ARCHIVE="brig-standalone-$RELEASE_RESOLVED-rootless-linux-$ARCH.tar.gz"
else
ARCHIVE="brig-standalone-$RELEASE_RESOLVED-linux-$ARCH.tar.gz"
fi
base="$GITHUB/$RELEASE_REPO/releases/download/$RELEASE_RESOLVED"
say "fetching $ARCHIVE from $RELEASE_REPO ($RELEASE_RESOLVED)"
_break_bar
fetch_bar "$base/$ARCHIVE" "$TARBALL"
verify_tarball "$base" "$ARCHIVE" "$TARBALL"
;;
http://*|https://*)
ARCHIVE="${BUNDLE##*/}"
say "fetching $BUNDLE"
_break_bar
fetch_bar "$BUNDLE" "$TARBALL"
verify_tarball "${BUNDLE%/*}" "$ARCHIVE" "$TARBALL"
;;
*)
[ -f "$BUNDLE" ] || fatal "tarball '$BUNDLE' not found"
say "installing from the local tarball $BUNDLE"
cp "$BUNDLE" "$TARBALL"
# The release's checksums.txt, signature and certificate, when
# they were copied across beside the tarball, are the record
# keep_release_record keeps. The tarball is still taken as given.
for f in checksums.txt checksums.txt.sig checksums.txt.pem; do
if [ -f "$(dirname "$BUNDLE")/$f" ]; then
cp "$(dirname "$BUNDLE")/$f" "$TMP_DIR/$f"
fi
done
;;
esac
}
# checksums.txt covers every asset and carries the one signature.
verify_tarball() {
base="$1"; name="$2"; file="$3"
if [ "$SKIP_SIGCHECK" = "true" ]; then
warn "INSTALL_BRIG_SKIP_SIGCHECK is set, not verifying the tarball"
return 0
fi
# Installing an unlisted tarball is the one outcome this function exists to
# prevent, so a missing checksums.txt stops the install. Setting
# INSTALL_BRIG_SKIP_SIGCHECK above is the way to say that is wanted.
fetch_quiet "$base/checksums.txt" "$TMP_DIR/checksums.txt" \
|| fatal "no checksums.txt next to the tarball, cannot verify it.
Set INSTALL_BRIG_SKIP_SIGCHECK=true to install it unverified anyway."
# The signature is fetched whether or not cosign is here to check it:
# keep_release_record puts it beside the boot assets, where brig checks it
# with the bundle's own cosign before a boot.
sig_fetched=false
if fetch_quiet "$base/checksums.txt.pem" "$TMP_DIR/checksums.txt.pem" \
&& fetch_quiet "$base/checksums.txt.sig" "$TMP_DIR/checksums.txt.sig"; then
sig_fetched=true
fi
if command -v cosign >/dev/null 2>&1 && [ "$sig_fetched" = "true" ]; then
# A signature that fails is not a signature that is absent. The first
# says the bytes or the identity are wrong and is fatal; only the second
# degrades to the hash.
cosign verify-blob \
--certificate "$TMP_DIR/checksums.txt.pem" \
--signature "$TMP_DIR/checksums.txt.sig" \
--certificate-identity-regexp "$SIG_IDENTITY_REGEXP" \
--certificate-oidc-issuer "$SIG_OIDC_ISSUER" \
"$TMP_DIR/checksums.txt" >/dev/null 2>&1 \
|| fatal "the checksums.txt signature did not verify against
$SIG_IDENTITY_REGEXP
This is what a tampered or mis-signed release looks like. Refusing."
say "checksums.txt signature verified"
elif [ "$REQUIRE_SIGCHECK" = "true" ]; then
fatal "INSTALL_BRIG_REQUIRE_SIGCHECK is set and the signature could not be
checked: cosign is missing, or the release carries no .sig and .pem."
else
say "cosign not present, checking the hash only"
fi
expected="$(awk -v n="$name" '$2 == n || $2 == "*"n {print $1; exit}' "$TMP_DIR/checksums.txt")"
[ -n "$expected" ] || fatal "$name is not listed in checksums.txt"
actual="$(sha256sum "$file" | awk '{print $1}')"
[ "$expected" = "$actual" ] || fatal "checksum mismatch for $name"
say "verified $name"
}
# ---------------------------------------------------------------- unpack
# Unpack the tarball and move its tree into $PREFIX atomically: stage into
# $PREFIX/<part>.new and rename, so a failed run leaves the old install intact.
# State dirs and the socket dir are created here too.
unpack_tarball() {
mkdir -p "$TMP_DIR/x"
tar -xzf "$TARBALL" -C "$TMP_DIR/x" || fatal "could not unpack the tarball"
root="$(find "$TMP_DIR/x" -mindepth 1 -maxdepth 1 -type d | head -1)"
{ [ -n "$root" ] && [ -d "$root/bin" ]; } || fatal "the tarball does not look like a brig tree"
[ -f "$root/pins.env" ] || fatal "the tarball has no pins.env"
# The rootless path lives in the bundle, not in this script, so a user
# install on a bundle built without it would lay down a tree with no
# brig-rootless-setup.sh and no rootlesskit and fail at the last stage.
# Read it from the staged tree, before anything is moved into place: a
# refusal here leaves an existing install untouched.
BUNDLE_ROOTLESS="$(awk -F= '$1 == "ROOTLESS" {print $2}' "$root/pins.env")"
if [ "$INSTALL_MODE" = "user" ] && [ "$BUNDLE_ROOTLESS" != "true" ]; then
fatal "this bundle carries no rootless path, so it cannot serve a user install.
Install a brig-standalone-*-rootless-* bundle, or run as root for a system install."
fi
a_arch="$(awk -F= '$1 == "ARCH" {print $2}' "$root/pins.env")"
[ -z "$a_arch" ] || [ "$a_arch" = "$ARCH" ] \
|| fatal "the tarball is for linux/$a_arch, this host is linux/$ARCH"
mkdir -p "$PREFIX"
for d in bin libexec etc share; do
[ -d "$root/$d" ] || continue
rm -rf "$PREFIX/$d.new"
mv "$root/$d" "$PREFIX/$d.new"
[ -d "$PREFIX/$d" ] && rm -rf "$PREFIX/$d.old" && mv "$PREFIX/$d" "$PREFIX/$d.old"
mv "$PREFIX/$d.new" "$PREFIX/$d"
rm -rf "$PREFIX/$d.old"
done
[ -f "$root/pins.env" ] && cp "$root/pins.env" "$PREFIX/pins.env"
chmod 0755 "$PREFIX"
keep_release_record
mkdir -p "$DATA_DIR/containerd" "$DATA_DIR/nerdctl" "$DATA_DIR/log" "$RUN_DIR"
retarget_tree
# No snapshotter retargeting: the tarball's configs default brig to overlayfs
# and keep devmapper configured in containerd, so CONTAINERD_SNAPSHOTTER=devmapper
# switches at runtime without rewriting anything.
}
# Keep the release's checksums.txt, and its signature and certificate, beside
# the boot assets. checksums.txt lists <bundle>.boot-assets.sha256, which is
# share/guest/SHA256SUMS, so brig can check the kernel and initrd against a
# record the release signed before every boot.
#
# Without the three files brig cannot check that record: it warns before every
# run, and refuses under BRIG_VERIFY=require. A local tarball with nothing
# beside it, a remote one under INSTALL_BRIG_SKIP_SIGCHECK, and a bundle built
# before the record (no SHA256SUMS) keep none, as asked. A release that gave
# checksums.txt and not its signature, or whose checksums.txt does not list
# this SHA256SUMS, keeps none and says so: brig would refuse that record before
# every boot.
keep_release_record() {
guest="$PREFIX/share/guest"
[ -f "$guest/SHA256SUMS" ] || return 0
if [ ! -s "$TMP_DIR/checksums.txt" ]; then
say "no checksums.txt from a release, so brig cannot check the boot assets' record"
return 0
fi
for f in checksums.txt.sig checksums.txt.pem; do
if [ ! -s "$TMP_DIR/$f" ]; then
warn "the release gave no $f, so no record of the kernel and initrd is kept.
brig warns before every run that it cannot check them, and refuses under BRIG_VERIFY=require."
return 0
fi
done
sums="$(sha256sum "$guest/SHA256SUMS" | awk '{print $1}')"
if ! awk -v h="$sums" '$1 == h && $2 ~ /[.]boot-assets[.]sha256$/ { found = 1 } END { exit !found }' \
"$TMP_DIR/checksums.txt"; then
warn "the release's checksums.txt does not list this bundle's SHA256SUMS, so no record of
the kernel and initrd is kept. brig warns before every run that it cannot check them."
return 0
fi
for f in checksums.txt checksums.txt.sig checksums.txt.pem; do
cp "$TMP_DIR/$f" "$guest/$f" || fatal "could not keep $f in $guest"
chmod 0644 "$guest/$f"
done
say "kept the release's signed checksums.txt in $guest"
}
# Move the tree's idea of where it lives. The generated wrappers, configs and
# units carry the build layout as literal paths; no binary does. The grep at the
# end is what keeps that true: a tenth file added upstream fails the install
# here instead of half-working on the host.
retarget_tree() {
b_prefix="$(awk -F= '$1 == "PREFIX" {print $2}' "$PINS" 2>/dev/null)"
b_data="$(awk -F= '$1 == "DATA_DIR" {print $2}' "$PINS" 2>/dev/null)"
b_run="$(awk -F= '$1 == "RUN_DIR" {print $2}' "$PINS" 2>/dev/null)"
# Bundles built before pins.env recorded the layout used the default one.
[ -n "$b_prefix" ] || b_prefix="$BUILD_PREFIX"
[ -n "$b_data" ] || b_data="$BUILD_DATA_DIR"
[ -n "$b_run" ] || b_run="$BUILD_RUN_DIR"
if [ "$b_prefix" = "$PREFIX" ] && [ "$b_data" = "$DATA_DIR" ] && [ "$b_run" = "$RUN_DIR" ]; then
return 0
fi
grep -rIl -e "$b_prefix" -e "$b_data" -e "$b_run" "$BIN_DIR" "$ETC_DIR" 2>/dev/null \
| while read -r f; do
sed -i "s|$b_data|$DATA_DIR|g; s|$b_prefix|$PREFIX|g; s|$b_run|$RUN_DIR|g" "$f" \
|| fatal "could not retarget $f"
say "retargeted ${f#"$PREFIX/"}"
done
left="$(grep -rIl -e "$b_prefix" -e "$b_data" -e "$b_run" "$BIN_DIR" "$ETC_DIR" 2>/dev/null || true)"
[ -z "$left" ] || fatal "retargeting left the build layout behind in:
$left"
say "retargeted the tree from $b_prefix to $PREFIX"
}
# ---------------------------------------------------------------- group
# The brig group owns the private containerd socket, so a non-root user can drive
# the stack. The systemd unit chgrps the socket to this group by name.
setup_group() {
# Nothing owns the socket but this user: it lives in their runtime dir.
if [ "$INSTALL_MODE" = "user" ]; then
say "user install, not creating a group"
return 0
fi
GROUP_CREATED=false
command -v getent >/dev/null 2>&1 || { warn "getent not found, cannot manage the $BRIG_GROUP group"; return 0; }
if getent group "$BRIG_GROUP" >/dev/null 2>&1; then
GROUP_CREATED="${PRIOR_GROUP_CREATED:-false}"
elif command -v groupadd >/dev/null 2>&1; then
if groupadd --system "$BRIG_GROUP" 2>/dev/null || groupadd "$BRIG_GROUP" 2>/dev/null; then
GROUP_CREATED=true
say "created the $BRIG_GROUP group"
else
warn "could not create the $BRIG_GROUP group"
return 0
fi
else
warn "groupadd not found, skipping the $BRIG_GROUP group"
return 0
fi
if [ -n "$BRIG_USER" ] && [ "$BRIG_USER" != "root" ] \
&& id "$BRIG_USER" >/dev/null 2>&1 && command -v usermod >/dev/null 2>&1; then
if usermod -aG "$BRIG_GROUP" "$BRIG_USER" 2>/dev/null; then
say "added $BRIG_USER to the $BRIG_GROUP group (re-login to pick it up)"
else
warn "could not add $BRIG_USER to $BRIG_GROUP"
fi
fi
}
# ---------------------------------------------------------------- storage
pool_exists() { dmsetup info "$1" >/dev/null 2>&1; }
check_held_metadata_snap() {
held="$(dmsetup status "$1" 2>/dev/null | awk '{print $7}')"
if [ -n "$held" ] && [ "$held" != "-" ]; then
warn "pool $1 is holding a metadata snapshot ($held)."
warn "Release it with: dmsetup message $1 0 release_metadata_snap"
fi
}
setup_storage() {
POOL_CREATED=false
POOL_LOOP_DATA=""
POOL_LOOP_META=""
if [ "$DM_AVAILABLE" != "true" ]; then
say "devmapper thin-pool unavailable, skipping pool setup (brig uses overlayfs)"
return 0
fi
dm_dir="$DATA_DIR/devmapper"
mkdir -p "$dm_dir" "$DATA_DIR/devmapper-snap"
data="$dm_dir/data"; meta="$dm_dir/meta"
if pool_exists "$POOL_NAME"; then
say "device-mapper pool $POOL_NAME already exists, reusing it"
POOL_CREATED="${PRIOR_POOL_CREATED:-false}"
POOL_LOOP_DATA="$(losetup -j "$data" 2>/dev/null | cut -d: -f1)"
POOL_LOOP_META="$(losetup -j "$meta" 2>/dev/null | cut -d: -f1)"
check_held_metadata_snap "$POOL_NAME"
return 0
fi
for f in "$data:$POOL_SIZE" "$meta:$POOL_META_SIZE"; do
path="${f%%:*}"; size="${f##*:}"
[ -f "$path" ] && continue
if [ "$POOL_PREALLOC" = "true" ]; then
fallocate -l "$size" "$path" || fatal "fallocate failed for $path"
else
truncate -s "$size" "$path" || fatal "truncate failed for $path"
fi
done
POOL_LOOP_DATA="$(losetup -j "$data" | cut -d: -f1)"
[ -n "$POOL_LOOP_DATA" ] || POOL_LOOP_DATA="$(losetup --find --show "$data")"
POOL_LOOP_META="$(losetup -j "$meta" | cut -d: -f1)"
[ -n "$POOL_LOOP_META" ] || POOL_LOOP_META="$(losetup --find --show "$meta")"
sectors=$(( $(blockdev --getsize64 "$POOL_LOOP_DATA") / 512 ))
dmsetup create "$POOL_NAME" \
--table "0 $sectors thin-pool $POOL_LOOP_META $POOL_LOOP_DATA 128 32768" \
|| fatal "dmsetup create failed for $POOL_NAME"
POOL_CREATED=true
say "created device-mapper pool $POOL_NAME"
check_held_metadata_snap "$POOL_NAME"
}
# ---------------------------------------------------------------- host wiring
# The three host-global things the tarball cannot carry: the urunc config symlink
# at a hardcoded path, the brig launchers on PATH, and (below) the systemd units.
host_integration() {
# /etc/urunc/config.toml is a hardcoded constant in urunc.
ETC_SYMLINK_CREATED=false
ETC_SYMLINK_BACKUP=""
# A user install cannot write /etc, and does not need to: the user unit
# passes URUNC_CONFIG_FILE, which the shim inherits from containerd.
if [ "$INSTALL_MODE" = "user" ]; then
write_launchers
return 0
fi
target="/etc/urunc/config.toml"
mkdir -p /etc/urunc
if [ -L "$target" ] && [ "$(readlink "$target")" = "$ETC_DIR/urunc.toml" ]; then
ETC_SYMLINK_CREATED="${PRIOR_ETC_CREATED:-false}"
ETC_SYMLINK_BACKUP="${PRIOR_ETC_BACKUP:-}"
elif [ -e "$target" ] || [ -L "$target" ]; then
ETC_SYMLINK_BACKUP="$target.pre-brig-install"
mv "$target" "$ETC_SYMLINK_BACKUP"
ln -s "$ETC_DIR/urunc.toml" "$target"
ETC_SYMLINK_CREATED=true
warn "a file was already at $target, saved as $ETC_SYMLINK_BACKUP"
else
ln -s "$ETC_DIR/urunc.toml" "$target"
ETC_SYMLINK_CREATED=true
fi
write_launchers
}
# brig and brigd launchers: source the env, then exec the real binary. Kept off
# the tree so `brig` is on a normal PATH while the tree stays relocatable.
write_launchers() {
mkdir -p "$LAUNCH_DIR"
for prog in brig brigd; do
[ -x "$BIN_DIR/$prog" ] || continue
cat > "$LAUNCH_DIR/$prog" <<LAUNCH
#!/bin/sh
# brig launcher, generated by install.sh.
[ -f "$ETC_DIR/brig-env.sh" ] && . "$ETC_DIR/brig-env.sh"
exec "$BIN_DIR/$prog" "\$@"
LAUNCH
chmod 0755 "$LAUNCH_DIR/$prog"
done
}
# ---------------------------------------------------------------- stamp
write_stamp() {
cat > "$STAMP" <<STAMPEOF
# What this install created. The uninstaller removes exactly this. Generated.
INSTALL_MODE=$INSTALL_MODE
PREFIX=$PREFIX
DATA_DIR=$DATA_DIR
RUN_DIR=$RUN_DIR
INSTALL_DATE=$(date -u +%Y-%m-%dT%H:%M:%SZ)
NAMESPACE=$NAMESPACE
SNAPSHOTTER=$SNAPSHOTTER
BRIDGE_NAME=brig0
SERVICE_NAME=$SERVICE_NAME
POOL_SERVICE_NAME=$POOL_SERVICE_NAME
SYSTEMD_INSTALLED=$INSTALL_SYSTEMD
POOL_NAME=$([ "$DM_AVAILABLE" = "true" ] && echo "$POOL_NAME" || echo "")
POOL_CREATED=$POOL_CREATED
POOL_LOOP_DATA=$POOL_LOOP_DATA
POOL_LOOP_META=$POOL_LOOP_META
BRIG_GROUP=$BRIG_GROUP
GROUP_CREATED=${GROUP_CREATED:-false}
BRIG_LAUNCHER=$LAUNCH_DIR/brig
BRIGD_LAUNCHER=$LAUNCH_DIR/brigd
ETC_SYMLINK=$([ "$INSTALL_MODE" = "user" ] && echo "" || echo "/etc/urunc/config.toml")
ETC_SYMLINK_CREATED=$ETC_SYMLINK_CREATED
ETC_SYMLINK_BACKUP=$ETC_SYMLINK_BACKUP
STAMPEOF
chmod 0600 "$STAMP"
}
# ---------------------------------------------------------------- systemd