File: filesystems

package info (click to toggle)
hibernate 2.0%2B15%2Bg88d54a8-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 740 kB
  • ctags: 114
  • sloc: sh: 1,223; makefile: 17
file content (123 lines) | stat: -rw-r--r-- 3,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler FilesystemsOptions
AddConfigHelp "Unmount <mount point or device> [...]" "If you have network shares or external devices that should be unmounted before suspending, list them here."
AddConfigHelp "Mount <mount point or device> [...]" "If you have network shares or external devices that should be mounted after resuming, list them here."
AddConfigHelp "UnmountFSTypes <filesystem type> [...]" "Unmounts any filesystems of the given types. This is most useful for network filesystems such as smbfs and nfs."
AddConfigHelp "UnmountGraceTime <seconds>" "Time between sending SIGTERM to processes and SIGKILL to allow them to cleanup gracefully.. The default is 1 second."

FS_UNMOUNT_GRACE=1

FilesystemsUnmount() {
    local mnt
    local unmounts

    [ -z "$FS_UNMOUNTS" ] && return 0
    for mnt in $FS_UNMOUNTS ; do
	unmounts="$unmounts"`awk '($1 == "'$mnt'" || $2 == "'$mnt'" ){print " " $2}' /proc/mounts`
    done
    [ -z "$unmounts" ] && return 0
    FilesystemsUnmountReal $unmounts
    # exit status preserved.
}

FilesystemsUnmountReal() {
    local mnt
    local ret
    local needsleep

    needsleep=0

    # Send TERM to processes first
    if [ x"$KILL_PROGRAMS" = "x1" ] ; then
	for mnt in $* ; do
	    if ! awk '($2 == "'$mnt'"){exit 1}' /proc/mounts ; then
		vecho 1 "Sending SIGTERM to processes using $mnt..."
		fuser -s -k -15 $mnt 2>/dev/null
		fuser -s $mnt && needsleep=1
	    fi
	done
    fi

    [ "$needsleep" -eq 1 ] && sleep $FS_UNMOUNT_GRACE

    # Send KILL to processes and unmount
    ret=0
    for mnt in $* ; do
	if ! awk '($2 == "'$mnt'"){exit 1}' /proc/mounts ; then
	    if [ x"$KILL_PROGRAMS" = "x1" ] ; then
		vecho 1 "Sending SIGKILL to processes using $mnt..."
		fuser -s -k -9 $mnt 2>/dev/null
	    fi
	    vecho 1 "Unmounting $mnt ..."
	    umount $mnt || ret=1
	fi
    done
    return $ret
}

FSTypesUnmount() {
    local fstype
    local ret

    [ -z "$FS_TYPES" ] && return 0
    ret=0

    for fstype in $FS_TYPES ; do
	local unmounts
	unmounts=`awk '($3 == "'$fstype'") {print $2}' /proc/mounts | sort -r`
	[ -z "$unmounts" ] && continue
	vecho 2 "Filesystems of type \"$fstype\" to unmount: $unmounts"
	FilesystemsUnmountReal $unmounts || ret=$?
    done
    return $ret
}

FilesystemsMount() {
    local mnt
    local ret
    [ -z "$FS_MOUNTS" ] && return 0

    ret=0
    for mnt in $FS_MOUNTS ; do
	vecho 1 "Mounting $mnt ..."
	mount $mnt || ret=1
    done
    return $ret
}

FilesystemsOptions() {
    case $1 in
	unmount)
	    [ -z "$FS_UNMOUNTS" ] && AddSuspendHook 50 FilesystemsUnmount
	    shift
	    FS_UNMOUNTS="$FS_UNMOUNTS $@"
	    ;;
	unmountfstypes)
	    [ -z "$FS_TYPES" ] && AddSuspendHook 45 FSTypesUnmount
	    shift
	    FS_TYPES="$FS_TYPES $@"
	    ;;
	unmountgracetime)
	    case $2 in
		*[!0-9.]*)
		    vecho 0 "UnmountGraceTime not numeric (expecting number of seconds)"
		    ;;
		*)
		    FS_UNMOUNT_GRACE=$2
		    ;;
	    esac
	;;
	mount)
	    [ -z "$FS_MOUNTS" ] && AddResumeHook 50 FilesystemsMount
	    shift
	    FS_MOUNTS="$FS_MOUNTS $@"
	    ;;
	*)
	    return 1
    esac
    return 0
}

# $Id$