KERNELWATCH_ROOT prefixes every path agent/install.sh and server/install.sh read or write and skips their root check. Unset (as in production) every path is unchanged; the only edits are quoting and mkdir -p for system directories that already exist. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
149 lines
5.4 KiB
Bash
Executable File
149 lines
5.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Install or upgrade kernelwatch-agent on a Debian 12/13 or Ubuntu 22.04/24.04 host.
|
|
#
|
|
# sudo ./install.sh --server https://kernelwatch.example.internal [--ca ca.pem]
|
|
# [--with-needrestart] [--with-apt-update | --without-apt-update]
|
|
# [--report-hostname NAME | --default-report-hostname]
|
|
#
|
|
# --with-needrestart installs needrestart (Ubuntu has it already) so the
|
|
# dashboard can show services needing a restart, and sets it to only list
|
|
# services after apt upgrades instead of asking interactively.
|
|
#
|
|
# --with-apt-update refreshes apt's package lists (apt-get update) before each
|
|
# hourly report, so new kernel and security updates show up within the hour.
|
|
# --without-apt-update turns that off again. Without either, it stays as it was.
|
|
#
|
|
# --report-hostname makes the agent report NAME as its hostname instead of its
|
|
# own, for a host registered under a different name (the dashboard notes a
|
|
# mismatch). --default-report-hostname goes back to the machine's own. Without
|
|
# either, it stays as it was.
|
|
#
|
|
# The host token (from `kernelwatch add-host <name>` on the server) is read
|
|
# from --token-file, the KERNELWATCH_TOKEN environment variable, or a prompt,
|
|
# so it never lands in shell history. Re-running without --server or a token
|
|
# keeps the existing config and token (that's an upgrade).
|
|
set -euo pipefail
|
|
|
|
here=$(cd "$(dirname "$0")" && pwd)
|
|
# Prefix for every path the installer writes or reads. Only the test suite sets
|
|
# it; it also skips the root check, since the tests can't be root.
|
|
root=${KERNELWATCH_ROOT:-}
|
|
etc=$root/etc/kernelwatch
|
|
server=
|
|
ca=
|
|
token_file=
|
|
with_needrestart=0
|
|
apt_update=
|
|
report_hostname=keep
|
|
|
|
while (($#)); do
|
|
case $1 in
|
|
--server) server=${2:?--server needs a URL}; shift 2 ;;
|
|
--ca) ca=${2:?--ca needs a file}; shift 2 ;;
|
|
--token-file) token_file=${2:?--token-file needs a file}; shift 2 ;;
|
|
--with-needrestart) with_needrestart=1; shift ;;
|
|
--with-apt-update) apt_update=on; shift ;;
|
|
--without-apt-update) apt_update=off; shift ;;
|
|
--report-hostname)
|
|
report_hostname=${2:?--report-hostname needs a name}; shift 2
|
|
[[ $report_hostname =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$ ]] ||
|
|
{ echo "invalid --report-hostname '$report_hostname'" >&2; exit 2; } ;;
|
|
--default-report-hostname) report_hostname=off; shift ;;
|
|
-h | --help) sed -n '2,25s/^# \{0,1\}//p' "$0"; exit 0 ;;
|
|
*) echo "unknown argument: $1" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
[[ -n $root || $(id -u) == 0 ]] || { echo "run as root" >&2; exit 1; }
|
|
|
|
# shellcheck source=/dev/null
|
|
. "$root/etc/os-release"
|
|
case "$ID:$VERSION_ID" in
|
|
debian:12 | debian:13 | ubuntu:22.04 | ubuntu:24.04 | ubuntu:26.04) ;;
|
|
*) echo "warning: untested OS $PRETTY_NAME; continuing" >&2 ;;
|
|
esac
|
|
|
|
if ! command -v curl >/dev/null; then
|
|
apt-get install -y --no-install-recommends curl ca-certificates
|
|
fi
|
|
if ((with_needrestart)); then
|
|
if [[ ! -x $root/usr/sbin/needrestart ]]; then
|
|
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends needrestart
|
|
fi
|
|
install -d -m 0755 "$root/etc/needrestart/conf.d"
|
|
cat >"$root/etc/needrestart/conf.d/kernelwatch.conf" <<'NRCONF'
|
|
# Installed by kernelwatch: after apt upgrades, list services that need a
|
|
# restart instead of asking. Delete this file to restore the default.
|
|
$nrconf{restart} = 'l';
|
|
NRCONF
|
|
fi
|
|
|
|
mkdir -p "$root/usr/local/sbin" "$root/etc/systemd/system"
|
|
install -d -m 0755 "$etc"
|
|
install -m 0755 "$here/kernelwatch-agent" "$root/usr/local/sbin/kernelwatch-agent"
|
|
install -m 0755 "$here/kernelwatch-livepatch" "$root/usr/local/sbin/kernelwatch-livepatch"
|
|
install -m 0644 "$here/kernelwatch-agent.service" "$here/kernelwatch-agent.timer" \
|
|
"$here/kernelwatch-needrestart.service" "$here/kernelwatch-livepatch.service" \
|
|
"$here/kernelwatch-apt-update.service" "$root/etc/systemd/system/"
|
|
|
|
case $apt_update in
|
|
on) touch "$etc/apt-update" ;;
|
|
off) rm -f "$etc/apt-update" ;;
|
|
esac
|
|
|
|
if [[ -n $ca ]]; then
|
|
install -m 0644 "$ca" "$etc/ca.pem"
|
|
fi
|
|
|
|
# Writing the config below replaces it, so remember a REPORT_HOSTNAME to keep.
|
|
prev_report_hostname=$(sed -n 's/^REPORT_HOSTNAME=//p' "$etc/agent.conf" 2>/dev/null | tail -n1 || true)
|
|
|
|
if [[ -n $server ]]; then
|
|
{
|
|
echo "SERVER_URL=$server"
|
|
echo "TOKEN_FILE=/etc/kernelwatch/agent.token"
|
|
[[ -e $etc/ca.pem ]] && echo "CA_FILE=/etc/kernelwatch/ca.pem"
|
|
} >"$etc/agent.conf"
|
|
chmod 0644 "$etc/agent.conf"
|
|
elif [[ ! -e $etc/agent.conf ]]; then
|
|
echo "no /etc/kernelwatch/agent.conf yet; pass --server URL" >&2
|
|
exit 1
|
|
fi
|
|
|
|
case $report_hostname in
|
|
keep) report_hostname=$prev_report_hostname ;;
|
|
off) report_hostname= ;;
|
|
esac
|
|
sed -i '/^REPORT_HOSTNAME=/d' "$etc/agent.conf"
|
|
[[ -z $report_hostname ]] || echo "REPORT_HOSTNAME=$report_hostname" >>"$etc/agent.conf"
|
|
chmod 0644 "$etc/agent.conf"
|
|
|
|
token=
|
|
if [[ -n $token_file ]]; then
|
|
token=$(tr -d '[:space:]' <"$token_file")
|
|
[[ -n $token ]] || { echo "empty token in $token_file" >&2; exit 1; }
|
|
elif [[ -n ${KERNELWATCH_TOKEN:-} ]]; then
|
|
token=$KERNELWATCH_TOKEN
|
|
elif [[ ! -s $etc/agent.token ]]; then
|
|
read -r -s -p "Host token: " token
|
|
echo
|
|
[[ -n $token ]] || { echo "empty token" >&2; exit 1; }
|
|
fi
|
|
if [[ -n $token ]]; then
|
|
(umask 077 && printf '%s\n' "$token" >"$etc/agent.token")
|
|
fi
|
|
chown root:root "$etc/agent.token"
|
|
chmod 0600 "$etc/agent.token"
|
|
|
|
systemctl daemon-reload
|
|
systemctl enable --now kernelwatch-agent.timer
|
|
|
|
echo "Sending first report..."
|
|
if systemctl start kernelwatch-agent.service; then
|
|
echo "OK. Check the dashboard, or: journalctl -u kernelwatch-agent"
|
|
else
|
|
echo "First report failed:" >&2
|
|
journalctl -u kernelwatch-agent -n 20 --no-pager >&2
|
|
exit 1
|
|
fi
|