Repository navigation
Expand file tree
/
Copy pathlinux-gpu-stack-install.sh
More file actions
executable file
·439 lines (372 loc) · 12 KB
/
Copy pathlinux-gpu-stack-install.sh
File metadata and controls
executable file
·439 lines (372 loc) · 12 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
#!/bin/bash
#
# Navop Linux GPU and desktop dependency stack installer.
#
# Navop links against a small set of system libraries and reaches EGL through
# dlopen. On a host that ships neither a Mesa user space nor a DRI driver the
# window cannot be created and Navop reports "Failed to create surface". This
# archive carries the missing pieces: the Mesa software renderer (llvmpipe)
# plus the X11 / Wayland / input-method client libraries the binary needs.
#
# Everything is copied onto the paths the dynamic loader already searches, so
# every Navop package form (release tarball, .deb, .rpm, AppImage) picks the
# stack up with no extra configuration and no environment variables.
#
# The installer is additive. A library is only copied when the host does not
# already resolve that SONAME, and the whole Mesa stack is skipped when the
# host already exposes a usable EGL plus a DRI driver. Use --force to
# overwrite, --dry-run to preview, --uninstall to remove.
#
# Usage:
# sudo ./install.sh install everything the host is missing
# ./install.sh --dry-run print the plan without touching the system
# sudo ./install.sh --force overwrite libraries that already exist
# sudo ./install.sh --prefix DIR stage under DIR instead of / (testing)
# sudo ./install.sh --uninstall remove the files this installer wrote
# ./install.sh --help
set -euo pipefail
PACKAGE_NAME="navop-gpu-stack"
DRY_RUN=0
FORCE=0
UNINSTALL=0
PREFIX=""
# Mesa loads a DRI driver by file name from its compile-time search directory.
# Any driver found in these locations means the host already has a working Mesa
# and must be left alone.
HOST_DRI_DRIVER_DIRECTORIES=(
"/usr/lib/x86_64-linux-gnu/dri"
"/usr/lib/aarch64-linux-gnu/dri"
"/usr/lib64/dri"
"/usr/lib/dri"
)
# Records the loader configuration we may add, so --uninstall can take it back.
LD_CONF_FILENAME="navop-gpu-stack.conf"
LD_CONF_PATH="/etc/ld.so.conf.d/${LD_CONF_FILENAME}"
ARCH=""
TARGET=""
LIBDIR=""
DRI_DIR=""
EGL_VENDOR_DIR=""
GLIBC_BASELINE=""
SOURCE_DISTRIBUTION=""
INSTALLED_FILES=()
SKIPPED_FILES=()
REMOVED_FILES=()
KEPT_FILES=()
# Uninstall must not touch anything the archive merely could have installed.
# The manifest lists every candidate, including the entries that were skipped
# because the host already resolved them, so the set that was actually written
# is recorded here and --uninstall replays only that.
RECORD_PATH=""
INSTALL_RECORDS=()
info() { printf '%s\n' "$*"; }
warn() { printf '%s\n' "$*" >&2; }
fail() {
printf 'Error: %s\n' "$*" >&2
exit 1
}
usage() {
sed -n '3,26p' "$0" | sed 's/^# \{0,1\}//'
}
parse_arguments() {
while [ "$#" -gt 0 ]; do
case "$1" in
--dry-run) DRY_RUN=1 ;;
--force) FORCE=1 ;;
--uninstall) UNINSTALL=1 ;;
--prefix)
shift
[ "$#" -gt 0 ] || fail "--prefix requires a directory"
PREFIX="$1"
;;
--prefix=*) PREFIX="${1#--prefix=}" ;;
-h|--help)
usage
exit 0
;;
*) fail "unknown argument: $1 (try --help)" ;;
esac
shift
done
if [ -n "$PREFIX" ]; then
case "$PREFIX" in
*/) PREFIX="${PREFIX%/}" ;;
esac
[ "$PREFIX" != "/" ] || PREFIX=""
fi
}
require_command() {
command -v "$1" >/dev/null 2>&1 || fail "required command is not available: $1"
}
locate_ldconfig() {
if command -v ldconfig >/dev/null 2>&1; then
command -v ldconfig
return 0
fi
local candidate
for candidate in /sbin/ldconfig /usr/sbin/ldconfig /usr/bin/ldconfig; do
if [ -x "$candidate" ]; then
printf '%s\n' "$candidate"
return 0
fi
done
fail "ldconfig was not found; this host has no glibc dynamic loader"
}
resolve_package_root() {
local source="${BASH_SOURCE[0]}"
while [ -L "$source" ]; do
local directory
directory="$(cd -P -- "$(dirname -- "$source")" && pwd)"
source="$(readlink -- "$source")"
case "$source" in
/*) ;;
*) source="$directory/$source" ;;
esac
done
cd -P -- "$(dirname -- "$source")" && pwd
}
load_metadata() {
local root="$1"
local environment_file="$root/install.env"
local manifest_file="$root/manifest.tsv"
[ -f "$environment_file" ] || fail "missing ${environment_file}"
[ -f "$manifest_file" ] || fail "missing ${manifest_file}"
# shellcheck disable=SC1090
. "$environment_file"
ARCH="${NAVOP_GPU_STACK_ARCH:-}"
TARGET="${NAVOP_GPU_STACK_TARGET:-}"
LIBDIR="${NAVOP_GPU_STACK_LIBDIR:-}"
DRI_DIR="${NAVOP_GPU_STACK_DRI_DIR:-}"
EGL_VENDOR_DIR="${NAVOP_GPU_STACK_EGL_VENDOR_DIR:-}"
GLIBC_BASELINE="${NAVOP_GPU_STACK_GLIBC_BASELINE:-}"
SOURCE_DISTRIBUTION="${NAVOP_GPU_STACK_SOURCE_DISTRIBUTION:-}"
[ -n "$ARCH" ] || fail "install.env does not declare NAVOP_GPU_STACK_ARCH"
[ -n "$LIBDIR" ] || fail "install.env does not declare NAVOP_GPU_STACK_LIBDIR"
}
host_architecture() {
case "$(uname -m)" in
x86_64|amd64) printf 'x86_64\n' ;;
aarch64|arm64) printf 'aarch64\n' ;;
*) uname -m ;;
esac
}
verify_host_architecture() {
local host
host="$(host_architecture)"
if [ "$host" != "$ARCH" ]; then
fail "this archive targets ${ARCH} but the host reports ${host}; download the matching ${PACKAGE_NAME} archive"
fi
}
soname_is_resolved() {
local soname="$1"
"$LD_CONFIG" -p 2>/dev/null |
awk -v soname="$soname" '$1 == soname { found = 1 } END { exit found ? 0 : 1 }'
}
host_has_dri_driver() {
local directory entry
for directory in "${HOST_DRI_DRIVER_DIRECTORIES[@]}"; do
[ -d "$directory" ] || continue
for entry in "$directory"/*_dri.so; do
[ -e "$entry" ] && return 0
done
done
return 1
}
host_has_usable_gl() {
# Both halves are required: glvnd needs libEGL.so.1 to dispatch, and Mesa
# needs a DRI driver behind it. Either one alone leaves Navop unable to open
# a window, which is exactly the state this archive exists to repair.
soname_is_resolved "libEGL.so.1" || return 1
host_has_dri_driver
}
warn_about_glibc() {
[ -n "$GLIBC_BASELINE" ] || return 0
local host_version
host_version="$("$LD_CONFIG" --version 2>/dev/null | head -n 1 | awk '{print $NF}')"
[ -n "$host_version" ] || return 0
if [ "$(printf '%s\n%s\n' "$GLIBC_BASELINE" "$host_version" | sort -V | head -n 1)" != "$GLIBC_BASELINE" ]; then
warn "warning: this host ships glibc ${host_version}, older than the ${GLIBC_BASELINE} baseline the bundled stack was built against; the libraries will be installed but may refuse to load."
fi
}
sha256_of() {
sha256sum -- "$1" | awk '{print $1}'
}
install_file() {
local source="$1"
local destination="$2"
local expected_digest="$3"
if [ -e "$destination" ] && [ "$FORCE" -eq 0 ]; then
if [ "$(sha256_of "$destination")" = "$expected_digest" ]; then
SKIPPED_FILES+=("$destination (already installed)")
else
SKIPPED_FILES+=("$destination (host copy kept)")
fi
return 0
fi
if [ "$DRY_RUN" -eq 1 ]; then
INSTALLED_FILES+=("$destination (dry run)")
return 0
fi
mkdir -p -- "$(dirname -- "$destination")"
cp -- "$source" "$destination"
chmod 0644 -- "$destination"
INSTALLED_FILES+=("$destination")
local record
printf -v record '%s\t%s' "$expected_digest" "$destination"
INSTALL_RECORDS+=("$record")
}
write_install_record() {
if [ "$DRY_RUN" -eq 1 ] || [ "$#" -eq 0 ]; then
return 0
fi
mkdir -p -- "$(dirname -- "$RECORD_PATH")"
printf '%s\n' "$@" > "$RECORD_PATH"
}
remove_file() {
local destination="$1"
local expected_digest="$2"
[ -e "$destination" ] || return 0
if [ "$(sha256_of "$destination")" != "$expected_digest" ]; then
KEPT_FILES+=("$destination (modified since installation)")
return 0
fi
if [ "$DRY_RUN" -eq 0 ]; then
rm -f -- "$destination"
fi
REMOVED_FILES+=("$destination")
}
ensure_loader_configuration() {
local directory="$PREFIX$LIBDIR"
if [ "$DRY_RUN" -eq 0 ]; then
mkdir -p -- "$directory"
fi
[ -z "$PREFIX" ] || return 0
# RHEL-style hosts already list /usr/lib64 in ld.so.conf; Debian-style hosts
# do not, so the directory has to be registered explicitly.
local existing
for existing in /etc/ld.so.conf /etc/ld.so.conf.d/*.conf; do
[ -f "$existing" ] || continue
if grep -qE "^[[:space:]]*${directory}/?[[:space:]]*$" "$existing" 2>/dev/null; then
return 0
fi
done
info "Registering ${directory} in the dynamic loader configuration (${LD_CONF_PATH})"
if [ "$DRY_RUN" -eq 0 ]; then
mkdir -p -- /etc/ld.so.conf.d
printf '%s\n' "$directory" > "$LD_CONF_PATH"
INSTALLED_FILES+=("$LD_CONF_PATH")
fi
}
drop_loader_configuration() {
[ -f "$LD_CONF_PATH" ] || return 0
if [ "$DRY_RUN" -eq 0 ]; then
rm -f -- "$LD_CONF_PATH"
fi
REMOVED_FILES+=("$LD_CONF_PATH")
}
refresh_loader_cache() {
[ -z "$PREFIX" ] || return 0
[ "$DRY_RUN" -eq 0 ] || return 0
"$LD_CONFIG" >/dev/null 2>&1 ||
warn "warning: ldconfig failed; the loader cache may be stale until the next boot."
}
install_stack() {
local root="$1"
local kind relative group digest destination source
local install_gl=1
if [ "$FORCE" -eq 0 ] && host_has_usable_gl; then
install_gl=0
info "Host already provides a usable EGL and DRI driver; leaving the existing Mesa stack alone."
fi
while IFS=$'\t' read -r kind relative group digest; do
[ -n "$kind" ] || continue
[ -n "$relative" ] || fail "manifest.tsv contains an entry without a path"
if [ "$group" = "gl" ] && [ "$install_gl" -eq 0 ]; then
continue
fi
source="$root/payload/$relative"
destination="$PREFIX/$relative"
[ -f "$source" ] || fail "archive payload is incomplete: ${source} is missing"
if [ "$kind" = "library" ] && [ "$FORCE" -eq 0 ] &&
soname_is_resolved "$(basename -- "$relative")"; then
SKIPPED_FILES+=("$(basename -- "$relative") (already provided by the host)")
continue
fi
install_file "$source" "$destination" "$digest"
done < "$root/manifest.tsv"
if [ "$install_gl" -eq 1 ]; then
ensure_loader_configuration
fi
if [ "$DRY_RUN" -eq 0 ] && [ "${#INSTALL_RECORDS[@]}" -gt 0 ]; then
write_install_record "${INSTALL_RECORDS[@]}"
INSTALLED_FILES+=("$RECORD_PATH")
fi
}
uninstall_stack() {
local digest destination
[ -f "$RECORD_PATH" ] || fail \
"no installation record at ${RECORD_PATH}; nothing to replay. Remove the stack by hand if it was installed by an older version."
while IFS=$'\t' read -r digest destination; do
[ -n "$destination" ] || continue
remove_file "$destination" "$digest"
done < "$RECORD_PATH"
if [ "$DRY_RUN" -eq 0 ]; then
rm -f -- "$RECORD_PATH"
rmdir -- "$(dirname -- "$RECORD_PATH")" 2>/dev/null || true
fi
drop_loader_configuration
refresh_loader_cache
}
report() {
local title="$1"
shift
[ "$#" -gt 0 ] || return 0
info ""
info "$title"
local entry
for entry in "$@"; do
info " - $entry"
done
}
main() {
parse_arguments "$@"
local root
root="$(resolve_package_root)"
load_metadata "$root"
RECORD_PATH="$PREFIX/usr/lib/${PACKAGE_NAME}/installed.tsv"
require_command uname
require_command awk
require_command sha256sum
LD_CONFIG="$(locate_ldconfig)"
if [ -z "$PREFIX" ] && [ "$(id -u)" -ne 0 ]; then
fail "installing into /usr requires root; re-run with sudo, or use --prefix for a staging directory"
fi
verify_host_architecture
if [ "$DRY_RUN" -eq 1 ]; then
info "Dry run: no files will be written."
fi
info "${PACKAGE_NAME} for ${TARGET} (built on ${SOURCE_DISTRIBUTION})"
info "Loader directory: $PREFIX$LIBDIR"
info "DRI driver directory: $PREFIX$DRI_DIR"
if [ "$UNINSTALL" -eq 1 ]; then
uninstall_stack
report "Removed:" "${REMOVED_FILES[@]}"
report "Kept (modified since installation):" "${KEPT_FILES[@]}"
info ""
info "Uninstall complete."
return 0
fi
warn_about_glibc
install_stack "$root"
refresh_loader_cache
report "Installed:" "${INSTALLED_FILES[@]}"
report "Skipped:" "${SKIPPED_FILES[@]}"
info ""
if [ "$DRY_RUN" -eq 1 ]; then
info "Dry run complete. Re-run without --dry-run to apply."
else
info "Done. Start Navop again; no environment variables are required."
info "Remove this stack later with: $0 --uninstall"
fi
}
main "$@"