unmount network/encrypted storgage when suspending

Some file systems hangs on resume from suspend/hibernate, especially network file systems and FUSE-filesystems(encfs by example). I’ve created a script to unmount file systems when suspending/hibernating:

sudo touch /etc/pm/sleep.d/20_unmount_storage
sudo chmod +x /etc/pm/sleep.d/20_unmount_storage
sudo nano /etc/pm/sleep.d/20_unmount_storage

/etc/pm/sleep.d/20_unmount_storage:

#!/bin/bash
#
# Script which unmounts network storage before suspend and hibernate.
#
# Arve Seljebu 7. May 2011

PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Which mountpoints we want to umount
MOUNTPOINT[0]=/media/storage/encfs
MOUNTPOINT[1]=/media/storage
#MOUNTPOINT[2]=/media/storage2
# User to run from (user who mounted them)
USER=username

if [ ! -x /bin/mountpoint ]; then
  echo "Did not find executable /bin/mounpoint"
  exit 0
fi

case "${1}" in
  hibernate|suspend)
    for i in "${MOUNTPOINT[@]}"
    do
    # check if the mountpoint is mounted
    echo "Checking if mountpoint $i is mounted..."
    sudo -u $USER mountpoint $i > /dev/null
    if [ "$?" == "0" ]; then
      # try to umount with fusermount, if not succsessful, try with umount
      echo "Mountpoint $i mounted, umounting..."
      fusermount -u $i
      if [ "$?" != "0" ];
      then umount $i
      fi
    fi
  done
  echo "Done..."
  exit 0
  ;;
  resume|thaw)
  # nothing
  ;;
esac

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>