-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplatform.func
More file actions
127 lines (112 loc) · 3.71 KB
/
Copy pathplatform.func
File metadata and controls
127 lines (112 loc) · 3.71 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
#!/usr/bin/env bash
# Copyright (c) 2021-2026 community-scripts ORG
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
# ==============================================================================
# LXC-PLATFORM.FUNC - PROXMOX VE / INCUS PLATFORM DETECTION
# ==============================================================================
#
# Detects whether scripts run on a Proxmox VE host, an Incus host, or inside
# a container (update mode). Used by build.func to load the correct backend.
#
# Override: export LXC_PLATFORM=pve|incus|incus-container|container
#
# ==============================================================================
[[ -n "${_LXC_PLATFORM_FUNC_LOADED:-}" ]] && return
_LXC_PLATFORM_FUNC_LOADED=1
# The client is not always on PATH: a non-login shell misses /snap/bin, and
# sudo's secure_path can drop it too. `command -v incus` then failed, detection
# answered "container", and the ct script updated the host instead of building.
_lxc_incus_bin() {
if command -v incus &>/dev/null; then
command -v incus
return 0
fi
local p
for p in /usr/bin/incus /usr/local/bin/incus /snap/bin/incus /opt/incus/bin/incus; do
[[ -x "$p" ]] && {
printf '%s\n' "$p"
return 0
}
done
return 1
}
# Detection finding the client is not enough - every later `incus` call in the
# engine has to resolve too. Idempotent, and a no-op when it is already there.
ensure_incus_on_path() {
command -v incus &>/dev/null && return 0
local bin dir
bin="$(_lxc_incus_bin)" || return 1
dir="$(dirname "$bin")"
case ":${PATH}:" in
*":${dir}:"*) ;;
*)
PATH="${dir}:${PATH}"
export PATH
;;
esac
}
detect_lxc_platform() {
if [[ -n "${LXC_PLATFORM:-}" ]]; then
echo "$LXC_PLATFORM"
return
fi
# Container markers come first now: the host test below no longer needs a
# reachable daemon, so a container carrying the client could match it.
if [[ -S /dev/incus/sock ]] || [[ -f /etc/profile.d/incus-motd.sh ]]; then
echo "incus-container"
return
fi
if command -v pveversion &>/dev/null; then
echo "pve"
return
fi
# `incus info` needs a running daemon AND group membership, neither of which
# a non-root operator is guaranteed. On-disk state settles it either way.
local bin
if bin="$(_lxc_incus_bin)"; then
if "$bin" info &>/dev/null 2>&1 || [[ -d /var/lib/incus ]] || [[ -S /var/run/incus/unix.socket ]]; then
echo "incus"
return
fi
fi
echo "container"
}
is_incus_lxc_backend() {
local platform
platform="$(detect_lxc_platform)"
[[ "$platform" == "incus" || "$platform" == "incus-container" ]]
}
is_incus_host() {
[[ "$(detect_lxc_platform)" == "incus" ]]
}
is_incus_container() {
[[ "$(detect_lxc_platform)" == "incus-container" ]]
}
# Writable state directory for defaults/diagnostics.
# Root (or writable /usr/local) → /usr/local/community-scripts
# Non-root Incus users → ~/.config/community-scripts
community_scripts_dir() {
if [[ -n "${COMMUNITY_SCRIPTS_STATE_DIR:-}" ]]; then
mkdir -p "$COMMUNITY_SCRIPTS_STATE_DIR" 2>/dev/null || true
echo "$COMMUNITY_SCRIPTS_STATE_DIR"
return 0
fi
local system_dir="/usr/local/community-scripts"
if [[ "$(id -u)" -eq 0 ]]; then
mkdir -p "$system_dir" 2>/dev/null || true
echo "$system_dir"
return 0
fi
if [[ -d "$system_dir" && -w "$system_dir" ]]; then
echo "$system_dir"
return 0
fi
if mkdir -p "$system_dir" 2>/dev/null && [[ -w "$system_dir" ]]; then
echo "$system_dir"
return 0
fi
local user_dir="${XDG_CONFIG_HOME:-${HOME:-/tmp}/.config}/community-scripts"
mkdir -p "$user_dir" 2>/dev/null || user_dir="/tmp/community-scripts-$$"
mkdir -p "$user_dir" 2>/dev/null || true
echo "$user_dir"
}