github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/packaging/ubuntu-16.04/snapd.postrm (about)

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