Permalink
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (88 sloc) 3.3 KB
#!/bin/sh
set -e
systemctl_stop() {
unit="$1"
for i in $(seq 10); do
if ! systemctl is-active -q "$unit"; then
echo "$unit is stopped."
break
fi
echo "Stoping $unit [attempt $i]"
systemctl stop -q "$unit" || true
sleep .1
done
}
if [ "$1" = "purge" ]; then
# snap.mount.service is a trusty thing
systemctl_stop snap.mount.service
units=$(systemctl list-unit-files --full | grep '^snap[-.]' | cut -f1 -d ' ' | grep -vF snap.mount.service || true)
mounts=$(echo "$units" | grep '^snap[-.].*\.mount$' || true)
services=$(echo "$units" | grep '^snap[-.].*\.service$' || true)
for unit in $services $mounts; do
# ensure its really a snap mount unit or systemd unit
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 "Stopping $unit"
systemctl_stop "$unit"
# if it is a mount unit, we can find the snap name in the mount
# unit (we just ignore unit files)
snap=$(grep 'Where=/snap/' "/etc/systemd/system/$unit"|cut -f3 -d/)
rev=$(grep 'Where=/snap/' "/etc/systemd/system/$unit"|cut -f4 -d/)
if [ -n "$snap" ]; then
echo "Removing snap $snap"
# aliases
if [ -d /snap/bin ]; then
find /snap/bin -maxdepth 1 -lname "$snap" -delete
find /snap/bin -maxdepth 1 -lname "$snap.*" -delete
fi
# generated binaries
rm -f "/snap/bin/$snap"
rm -f "/snap/bin/$snap".*
# snap mount dir
# we pass -d (clean up loopback devices) for trusty compatibility
umount -d -l "/snap/$snap/$rev" 2> /dev/null || true
rm -rf "/snap/$snap/$rev"
rm -f "/snap/$snap/current"
# snap data dir
rm -rf "/var/snap/$snap/$rev"
rm -rf "/var/snap/$snap/common"
rm -f "/var/snap/$snap/current"
# opportunistic remove (may fail if there are still revisions left
for d in "/snap/$snap" "/var/snap/$snap"; do
if [ -d "$d" ]; then
rmdir --ignore-fail-on-non-empty "$d" || true
fi
done
fi
echo "Removing $unit"
rm -f "/etc/systemd/system/$unit"
rm -f "/etc/systemd/system/multi-user.target.wants/$unit"
done
# generated readme files
rm -f "/snap/README"
echo "Final directory cleanup"
for d in "/snap/bin" "/snap" "/var/snap"; do
if [ -d "$d" ]; then
rmdir --ignore-fail-on-non-empty $d
fi
done
echo "Discarding preserved snap namespaces"
# opportunistic as those might not be actually mounted
for mnt in /run/snapd/ns/*.mnt; do
umount -l "$mnt" || true
rm -f "$mnt"
done
for fstab in /run/snapd/ns/*.fstab; do
rm -f "$fstab"
done
umount -l /run/snapd/ns/ || true
echo "Removing extra snap-confine apparmor rules"
rm -f /etc/apparmor.d/snap.core.*.usr.lib.snapd.snap-confine
echo "Removing snapd cache"
rm -f /var/cache/snapd/*
echo "Removing snapd state"
rm -rf /var/lib/snapd
fi
#DEBHELPER#