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