github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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 mounts to ${SNAP_MOUNT_DIR} or /var/snap done by parallel
    23      # installs or LP:#1668659
    24      for mp in /snap /var/snap; do
    25          if grep -q " $mp $mp" /proc/self/mountinfo; then
    26              umount -l "$mp" || true
    27          fi
    28      done
    29  
    30      units=$(systemctl list-unit-files --full | grep '^snap[-.]' | cut -f1 -d ' ' | grep -vF snap.mount.service || true)
    31      mounts=$(echo "$units" | grep '^snap[-.].*\.mount$' || true)
    32      services=$(echo "$units" | grep '^snap[-.].*\.service$' || true)
    33  
    34      for unit in $services $mounts; do
    35          # ensure its really a snap mount unit or systemd unit
    36          if ! grep -q 'What=/var/lib/snapd/snaps/' "/etc/systemd/system/$unit" && ! grep -q 'X-Snappy=yes' "/etc/systemd/system/$unit"; then
    37              echo "Skipping non-snapd systemd unit $unit"
    38              continue
    39          fi
    40  
    41          echo "Stopping $unit"
    42          systemctl_stop "$unit"
    43  
    44          # if it is a mount unit, we can find the snap name in the mount
    45          # unit (we just ignore unit files)
    46          snap=$(grep 'Where=/snap/' "/etc/systemd/system/$unit"|cut -f3 -d/)
    47          rev=$(grep 'Where=/snap/' "/etc/systemd/system/$unit"|cut -f4 -d/)
    48          if [ -n "$snap" ]; then
    49              echo "Removing snap $snap and revision $rev"
    50              # aliases
    51              if [ -d /snap/bin ]; then
    52                  find /snap/bin -maxdepth 1 -lname "$snap" -delete
    53                  find /snap/bin -maxdepth 1 -lname "$snap.*" -delete
    54              fi
    55              # generated binaries
    56              rm -f "/snap/bin/$snap"
    57              rm -f "/snap/bin/$snap".*
    58              # snap mount dir
    59              # we pass -d (clean up loopback devices) for trusty compatibility
    60              umount -d -l "/snap/$snap/$rev" 2> /dev/null || true
    61              rm -rf "/snap/$snap/$rev"
    62              rm -f "/snap/$snap/current"
    63              # snap data dir
    64              rm -rf "/var/snap/$snap/$rev"
    65              rm -rf "/var/snap/$snap/common"
    66              rm -f "/var/snap/$snap/current"
    67              # opportunistic remove (may fail if there are still revisions left
    68              for d in "/snap/$snap" "/var/snap/$snap"; do
    69                  if [ -d "$d" ]; then
    70                      rmdir --ignore-fail-on-non-empty "$d" || true
    71                  fi
    72              done
    73              # udev rules
    74              find /etc/udev/rules.d -name "*-snap.${snap}.rules" -execdir rm -f "{}" \;
    75              # dbus policy files
    76              if [ -d /etc/dbus-1/system.d ]; then
    77                  find /etc/dbus-1/system.d -name "snap.${snap}.*.conf" -execdir rm -f "{}" \;
    78              fi
    79              # modules
    80              rm -f "/etc/modules-load.d/snap.${snap}.conf"
    81              # timer and socket units
    82              find /etc/systemd/system -name "snap.${snap}.*.timer" -o -name "snap.${snap}.*.socket" | while read -r f; do
    83                  systemctl_stop "$(basename "$f")"
    84                  rm -f "$f"
    85              done
    86              # user services, sockets, and timers - we make no attempt to stop any of them.
    87              # TODO: ask snapd to ask each snapd.session-agent.service to stop snaps
    88              # user-session services and stop itself.
    89              find /etc/systemd/user -name "snap.${snap}.*.timer" -o -name "snap.${snap}.*.socket" -o -name "snap.${snap}.*.service" | while read -r f; do
    90                  rm -f "$f"
    91              done
    92          fi
    93  
    94          echo "Removing $unit"
    95          rm -f "/etc/systemd/system/$unit"
    96          rm -f "/etc/systemd/system/multi-user.target.wants/$unit"
    97      done
    98  
    99      # snapd session-agent
   100      rm -f /etc/systemd/user/snapd.session-agent.socket
   101      rm -f /etc/systemd/user/snapd.session-agent.service
   102      rm -f /etc/systemd/user/sockets.target.wants/snapd.session-agent.socket
   103  
   104      # generated readme files
   105      rm -f "/snap/README"
   106  
   107      echo "Discarding preserved snap namespaces"
   108      # opportunistic as those might not be actually mounted
   109      if [ -d /run/snapd/ns ]; then
   110          if [ "$(find /run/snapd/ns/ -name "*.mnt" | wc -l)" -gt 0 ]; then
   111              for mnt in /run/snapd/ns/*.mnt; do
   112                  umount -l "$mnt" || true
   113                  rm -f "$mnt"
   114              done
   115          fi
   116          find /run/snapd/ns/ \( -name '*.fstab' -o -name '*.user-fstab' -o -name '*.info' \) -delete
   117          umount -l /run/snapd/ns/ || true
   118      fi
   119  
   120      # inside containers we have a generator that creates a bind mount to /snap
   121      if [ -e /run/systemd/container ]; then
   122          echo "Unmount /snap inside a container"
   123          umount /snap || true
   124      fi
   125  
   126      echo "Final directory cleanup"
   127      for d in "/snap/bin" "/snap" "/var/snap"; do
   128          # Force remove due to directories for old revisions could still exist
   129          rm -rf "$d"
   130          if [ -d "$d" ]; then
   131              echo "Cannot remove directory $d"
   132          fi
   133      done
   134  
   135      echo "Removing extra snap-confine apparmor rules"
   136      rm -f /etc/apparmor.d/snap.core.*.usr.lib.snapd.snap-confine
   137  
   138      echo "Removing snapd cache"
   139      rm -rf /var/cache/snapd/*
   140  
   141      echo "Removing snapd state"
   142      rm -rf /var/lib/snapd
   143  fi
   144  
   145  #DEBHELPER#