systemctl daemon-reload

for unit in snapd.apparmor.service snapd.socket; do
    systemctl try-restart $unit
done

systemctl preset snapd.apparmor.service
systemctl preset snapd.failure.service
systemctl preset snapd.socket

units=$(systemctl list-unit-files --full | grep '^snap[-.]' | cut -f1 -d ' ' | grep -vF snap.mount.service || true)
mounts=$(echo "$units" | grep '^snap[-.].*\.mount$' || true)

for unit in $mounts; do
	if ! grep -q 'What=/var/lib/snapd/snaps/' "/etc/systemd/system/$unit" && ! grep -q 'X-Snappy=yes' "/etc/systemd/system/$unit"; then
		echo "Skipping non-snapd systemd unit $unit"
		continue
	fi

	echo "Starting $unit"
	systemctl start "$unit" || true
done

setcap -q - /usr/lib/snapd/snap-confine < /usr/lib/snapd/snap-confine.caps
