File: //snap/docker/current/meta/hooks/configure
#!/bin/bash
set -eux
HOOK_LOG="${SNAP_COMMON}/hooks/${SNAP_REVISION}/$(basename "$0").log" && mkdir -p "${HOOK_LOG%/*}"
exec &> >(tee -a "${HOOK_LOG}")
# shellcheck source=nvidia/lib
. "${SNAP}/usr/share/nvidia-container-toolkit/lib"
# shellcheck source=lib/daemon-config
. "${SNAP}/lib/daemon-config"
# If the user unset the data-root, set it to the default
data_root="$(snapctl get data-root)"
if [ -z "$data_root" ] ; then
snapctl set data-root="${DEFAULT_DATA_ROOT}"
fi
# Flag to trigger service restart if any condition requires it #
SVC_RESTART=false
# install and post-refresh seed daemon.json; do the same if it has gone missing since
[ -f "${DAEMON_CONFIG_FILE}" ] || cp "${DAEMON_CONFIG_BASE}" "${DAEMON_CONFIG_FILE}"
# snapd rolls back the snap config transaction when this hook fails, but not file
# writes; restore the pre-hook daemon.json and managed-key list so a failure cannot
# leave the file claiming a value that snap get no longer shows.
snapshot_daemon_config
on_exit() {
rc=$?
[ "$rc" -eq 0 ] || restore_daemon_config || true
rm -rf "$DAEMON_CONFIG_ROLLBACK"
exit "$rc"
}
trap on_exit EXIT
# Apply user-provided daemon options #
reject_unsupported_daemon_config
apply_user_daemon_config
if nvidia_support_disabled; then
# Remove nvidia runtime config, if it exists #
jq -r 'del(.runtimes.nvidia)' "${SNAP_DATA}/config/daemon.json" > "${SNAP_DATA}/config/daemon.json.new"
# If it was removed [ there was a change ], copy in the new config, remove CDI config, and set service restart flag #
if ! cmp "${SNAP_DATA}/config/daemon.json"{,.new} >/dev/null ; then
mv "${SNAP_DATA}/config/daemon.json"{.new,}
rm -f "${SNAP_DATA}/etc/cdi/nvidia.yaml"
rm -f "${SNAP_DATA}/etc/nvidia-container-runtime/config.toml"
SVC_RESTART=true
else
rm -f "${SNAP_DATA}/config/daemon.json.new"
fi
fi
# Restart services if required #
# If oneshot services are inactive they don't respond to restart, so stop/start #
if $SVC_RESTART ; then
snapctl stop "${SNAP_NAME}"
snapctl start "${SNAP_NAME}"
# Otherwise, just restart the nvidia-container-toolkit #
else
snapctl stop "${SNAP_NAME}.nvidia-container-toolkit"
snapctl start "${SNAP_NAME}.nvidia-container-toolkit"
fi