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