91 lines
3.3 KiB
Bash
91 lines
3.3 KiB
Bash
#
|
|
# System introspection script which defines variables for use in later dropins.
|
|
#
|
|
# Exposes system data as the SYSTEM_INFO associative array. Primarily pulls information via
|
|
# systemd-hostnamed if available; falls back to reading /etc/machine-info and /etc/hostname,
|
|
# or retrieving the system hostname with `uname`. Keys will always be the names of the D-Bus
|
|
# properties, not their corresponding fields in /etc/machine-info.
|
|
#
|
|
# Exposes OS release data as the OS_RELEASE associative array. Data is read directly from
|
|
# /etc/os-release if available, otherwise it is not provided.
|
|
#
|
|
# Exposes all /etc/machine-info fields, including those not defined by the spec, under MACHINE_INFO.
|
|
# Keys are the field names with no transforms applied.
|
|
|
|
_introspection--generate-file-parser() {
|
|
awk 'match($0, /^([A-Za-z_][A-Za-z0-9_]*)=(.*)/, ary) { print "'"$1"'[" ary[1] "]='\''" ary[2] "'\''"}'
|
|
}
|
|
|
|
_introspection--parse-file() {
|
|
local varname=$1
|
|
while read line; do
|
|
key=${${(LC)line%%=*}//_/}
|
|
val=${line#*=}
|
|
eval "${varname}[${key}]=\"${${val%\"}#\"}\""
|
|
done
|
|
}
|
|
|
|
declare -A SYSTEM_INFO
|
|
declare -A OS_RELEASE
|
|
declare -A MACHINE_INFO
|
|
declare -A INTRO_STATE
|
|
|
|
refresh-system-info() {
|
|
INTRO_STATE[SYSTEM_INFO]=none
|
|
INTRO_STATE[OS_RELEASE]=none
|
|
INTRO_STATE[MACHINE_INFO]=none
|
|
INTRO_STATE[hostname]=none
|
|
|
|
if [ -f /etc/machine-info ]; then
|
|
# Populate MACHINE_INFO array
|
|
_introspection--parse-file MACHINE_INFO </etc/machine-info
|
|
INTRO_STATE[MACHINE_INFO]=present
|
|
fi
|
|
|
|
# Parse OS release info, trying first from the new os-release file, then from the LSB standard. Variables should have more or less the same names between the two files.
|
|
if [ -f /etc/os-release ]; then
|
|
_introspection--parse-file OS_RELEASE </etc/os-release
|
|
INTRO_STATE[OS_RELEASE]=present
|
|
elif [ -f /etc/lsb_release ]; then
|
|
_introspection--parse-file OS_RELEASE </etc/lsb_release
|
|
INTRO_STATE[OS_RELEASE]=legacy_lsb_release
|
|
fi
|
|
|
|
# Parse detailed system info from hostnamectl, or synthesize what we can from /etc/machine-info and static and transient hostnames.
|
|
if type hostnamectl &>/dev/null; then
|
|
# We can get all relevant data from hostnamectl, so lets do that.
|
|
eval $(hostnamectl --json=short | jq -r 'to_entries | .[] | select((.value | type) != "array" and (.value | type) != "object" and (.value | type) != "null") | "SYSTEM_INFO[\(.key)]=\(.value | @sh)"')
|
|
INTRO_STATE[SYSTEM_INFO]=full
|
|
INTRO_STATE[hostname]=full
|
|
else
|
|
# Parse machine info if available
|
|
if [[ ${INTRO_STATE[MACHINE_INFO]} == present ]]; then
|
|
# Populate SYSTEM_INFO array as best as possible (this will be missing a _lot_ of fields that hostnamed calculates)
|
|
local shared_keys=(PrettyHostname IconName Chassis Deployment Location HardwareVendor HardwareModel HardwareVersion)
|
|
for key val in "${(@kv)MACHINE_INFO}"; do
|
|
case $key in
|
|
HardwareSku)
|
|
SYSTEM_INFO[HardwareSKU]=${MACHINE_INFO[HardwareSku]}
|
|
;;
|
|
*)
|
|
if [[ ${${key:*shared_keys}:+x} == 'x' ]]; then
|
|
SYSTEM_INFO[$key]=${MACHINE_INFO[$key]}
|
|
fi
|
|
;;
|
|
esac
|
|
done
|
|
INTRO_STATE[SYSTEM_INFO]=limited
|
|
fi
|
|
# Read current (transient) hostname
|
|
SYSTEM_INFO[Hostname]=$(uname -n)
|
|
INTRO_STATE[hostname]=transient
|
|
# Read static hostname if available
|
|
if [ -f /etc/hostname ]; then
|
|
SYSTEM_INFO[StaticHostname]=$(</etc/hostname)
|
|
INTRO_STATE[hostname]=full
|
|
fi
|
|
fi
|
|
}
|
|
|
|
refresh-system-info
|