github.com/rigado/snapd@v2.42.5-go-mod+incompatible/packaging/debian-sid/snapd.postrm (about) 1 #!/bin/sh 2 3 set -e 4 5 systemctl_stop() { 6 unit="$1" 7 8 echo "Stopping unit $unit" 9 systemctl stop -q "$unit" || true 10 11 for i in $(seq 20); do 12 echo "Waiting until unit $unit is stopped [attempt $i]" 13 if ! systemctl is-active -q "$unit"; then 14 echo "$unit is stopped." 15 return 16 fi 17 sleep .1 18 done 19 } 20 21 if [ "$1" = "purge" ]; then 22 # undo any bind mount to /snap that resulted from LP:#1668659 23 # (that bug can't happen in trusty -- and doing this would mess up snap.mount.service there) 24 if grep -q "/snap /snap" /proc/self/mountinfo; then 25 umount -l /snap || true 26 fi 27 28 units=$(systemctl list-unit-files --full | grep '^snap[-.]' | cut -f1 -d ' ' | grep -vF snap.mount.service || true) 29 mounts=$(echo "$units" | grep '^snap[-.].*\.mount$' || true) 30 services=$(echo "$units" | grep '^snap[-.].*\.service$' || true) 31 32 for unit in $services $mounts; do 33 # ensure its really a snap mount unit or systemd unit 34 if ! grep -q 'What=/var/lib/snapd/snaps/' "/etc/systemd/system/$unit" && ! grep -q 'X-Snappy=yes' "/etc/systemd/system/$unit"; then 35 echo "Skipping non-snapd systemd unit $unit" 36 continue 37 fi 38 39 echo "Stopping $unit" 40 systemctl_stop "$unit" 41 42 # if it is a mount unit, we can find the snap name in the mount 43 # unit (we just ignore unit files) 44 snap=$(grep 'Where=/snap/' "/etc/systemd/system/$unit"|cut -f3 -d/) 45 rev=$(grep 'Where=/snap/' "/etc/systemd/system/$unit"|cut -f4 -d/) 46 if [ -n "$snap" ]; then 47 echo "Removing snap $snap and revision $rev" 48 # aliases 49 if [ -d /snap/bin ]; then 50 find /snap/bin -maxdepth 1 -lname "$snap" -delete 51 find /snap/bin -maxdepth 1 -lname "$snap.*" -delete 52 fi 53 # generated binaries 54 rm -f "/snap/bin/$snap" 55 rm -f "/snap/bin/$snap".* 56 # snap mount dir 57 # we pass -d (clean up loopback devices) for trusty compatibility 58 umount -d -l "/snap/$snap/$rev" 2> /dev/null || true 59 rm -rf "/snap/$snap/$rev" 60 rm -f "/snap/$snap/current" 61 # snap data dir 62 rm -rf "/var/snap/$snap/$rev" 63 rm -rf "/var/snap/$snap/common" 64 rm -f "/var/snap/$snap/current" 65 # opportunistic remove (may fail if there are still revisions left 66 for d in "/snap/$snap" "/var/snap/$snap"; do 67 if [ -d "$d" ]; then 68 rmdir --ignore-fail-on-non-empty "$d" || true 69 fi 70 done 71 # udev rules 72 find /etc/udev/rules.d -name "*-snap.${snap}.rules" -execdir rm -f "{}" \; 73 # dbus policy files 74 if [ -d /etc/dbus-1/system.d ]; then 75 find /etc/dbus-1/system.d -name "snap.${snap}.*.conf" -execdir rm -f "{}" \; 76 fi 77 # timer files 78 find /etc/systemd/system -name "snap.${snap}.*.timer" | while read -r f; do 79 systemctl_stop "$(basename "$f")" 80 rm -f "$f" 81 done 82 fi 83 84 echo "Removing $unit" 85 rm -f "/etc/systemd/system/$unit" 86 rm -f "/etc/systemd/system/multi-user.target.wants/$unit" 87 done 88 89 # generated readme files 90 rm -f "/snap/README" 91 92 echo "Final directory cleanup" 93 for d in "/snap/bin" "/snap" "/var/snap"; do 94 # Force remove due to directories for old revisions could still exist 95 rm -rf "$d" 96 if [ -d "$d" ]; then 97 echo "Cannot remove directory $d" 98 fi 99 done 100 101 echo "Discarding preserved snap namespaces" 102 # opportunistic as those might not be actually mounted 103 if [ -d /run/snapd/ns ]; then 104 if [ "$(find /run/snapd/ns/ -name "*.mnt" | wc -l)" -gt 0 ]; then 105 for mnt in /run/snapd/ns/*.mnt; do 106 umount -l "$mnt" || true 107 rm -f "$mnt" 108 done 109 fi 110 if [ "$(find /run/snapd/ns/ -name "*.fstab" | wc -l)" -gt 0 ]; then 111 for fstab in /run/snapd/ns/*.fstab; do 112 rm -f "$fstab" 113 done 114 fi 115 umount -l /run/snapd/ns/ || true 116 fi 117 118 echo "Removing extra snap-confine apparmor rules" 119 rm -f /etc/apparmor.d/snap.core.*.usr.lib.snapd.snap-confine 120 121 echo "Removing snapd cache" 122 rm -rf /var/cache/snapd/* 123 124 echo "Removing snapd state" 125 rm -rf /var/lib/snapd 126 fi 127 128 #DEBHELPER#