File: modules

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 (372 lines) | stat: -rw-r--r-- 9,903 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler ModulesOptions
AddConfigHelp "UnloadModules <module name> [...]" "Names of modules to unload prior to suspending."
AddConfigHelp "LoadModules auto|<module name> [...]" "Names of modules to load after resuming. If auto is specified, the modules that were unloaded previously are loaded."
AddConfigHelp "UnloadBlacklistedModules <boolean>" "Try to remove any modules that are known to be incompatible with hibernation prior to suspending."
AddConfigHelp "UnloadAllModules <boolean>" "Try to remove all modules loaded prior to suspending."
AddConfigHelp "LoadModulesFromFile <filename>" "Load default modules after resuming from a given filename. Each module name must appear on its own line, and lines starting with # are ignored. (eg, Debian's /etc/modules, Gentoo's /etc/modules.autoload)"

MODULES_BLACKLIST_FILE="/etc/hibernate/blacklisted-modules"

NON_RECURSIVE_MODPROBE=

# xrmmod calls an appropriate program to unload modules. We want to use
# modprobe -r, if modprobe supports the -N option also (non-recursive).
# Otherwise, we fallback to rmmod.
xrmmod() {
    if [ -n "$NON_RECURSIVE_MODPROBE" ] ; then
	$NON_RECURSIVE_MODPROBE "$@" > /dev/null 2>&1
	return $?
    fi

    if modprobe -r -N > /dev/null 2>&1 ; then
	NON_RECURSIVE_MODPROBE="modprobe -r -N"
    else
	NON_RECURSIVE_MODPROBE="rmmod"
    fi
    vecho 3 "Removing modules with $NON_RECURSIVE_MODPROBE."
    $NON_RECURSIVE_MODPROBE "$@" > /dev/null 2>&1
}

CheckModuleSupport() {
    [ -f "/proc/modules" ] && return 0
    vecho 1 "Kernel does not have module support compiled in! Module functions disabled."
    return 1
}

# SaveKernelModprobe disables module loading while suspending, by setting
# the kernel's modprobe to /doesnt/exist, and saving the old value.
SaveKernelModprobe() {
    [ -n "$KERNEL_MODPROBE" ] && return 0
    if [ -r "/proc/sys/kernel/modprobe" ] ; then
	KERNEL_MODPROBE=`cat /proc/sys/kernel/modprobe`
	if [ -n "$KERNEL_MODPROBE" ] ; then
	    vecho 3 "Saved /proc/sys/kernel/modprobe is $KERNEL_MODPROBE"
	    echo "/doesnt/exist" > /proc/sys/kernel/modprobe
	fi
    fi
    if [ -r "/proc/sys/kernel/hotplug" ] ; then
	KERNEL_HOTPLUG=`cat /proc/sys/kernel/hotplug`
	if [ -n "$KERNEL_HOTPLUG" ] ; then
	    vecho 3 "Saved /proc/sys/kernel/hotplug is $KERNEL_HOTPLUG"
	    echo "/doesnt/exist" > /proc/sys/kernel/hotplug
	fi
    fi

    return 0
}

RestoreKernelModprobe() {
    if [ -n "$KERNEL_MODPROBE" ] ; then
	echo $KERNEL_MODPROBE > /proc/sys/kernel/modprobe
	unset KERNEL_MODPROBE
    fi
    if [ -n "$KERNEL_HOTPLUG" ] ; then
	echo $KERNEL_HOTPLUG > /proc/sys/kernel/hotplug
	unset KERNEL_HOTPLUG
    fi
    return 0
}

ModulesUnload() {
    [ -z "$MOD_UNLOAD" ] && return 0
    CheckModuleSupport || return 0
    local mod
    local ret
    ret=0
    for mod in $MOD_UNLOAD ; do
	vecho 2 -n "Unloading module $mod... "

	if ! grep -q "^$mod " /proc/modules ; then
	    vecho 2 "not loaded."
	    continue
	fi

	if xrmmod `FindModuleDeps $mod` ; then
	    vecho 2
	else
	    ret=1
	    vecho 2 "failed!"
	    [ "$VERBOSITY" -eq 1 ] && vecho 1 "Unloading module $mod failed!"
	fi
	# It's possible that the module was unloaded anyway (eg, if it Ctrl+C'd)
	MOD_UNLOADED="$MOD_UNLOADED $mod"
    done
    return $ret
}

ModulesLoad() {
    [ -z "$MOD_LOAD" ] && return 0
    CheckModuleSupport || return 0
    RestoreKernelModprobe

    local mod
    for mod in $MOD_LOAD ; do
	if [ "$mod" = "auto" ] ; then
	    for mod in $MOD_UNLOADED ; do
		vecho 2 "Loading module $mod (from auto)..."
		modprobe $mod
	    done
	    continue
	fi
	vecho 2 "Loading module $mod..."
	modprobe $mod
    done
    return 0
}

ModulesUnloadAllOnce() {
    local module
    for module in `awk '($3==0){print $1}' /proc/modules` ;do
	case $module in
	    suspend_*)
		vecho 2 "Skipping suspend module $module."
		;;
	    *)
		vecho 2 "Unloading module $module..."
		xrmmod $module && MOD_UNLOADED="$MOD_UNLOADED $module"
		;;
	esac
    done
}

Get26ModulesFile() {
    if grep -q '^2\.4\.' /proc/sys/kernel/osrelease ; then
	sed -n -e '/\[\(.*\)\]/ { s/^\([^ ]\+\).*\[\(.*\)\].*$/\1:0:0:\2 /g; y/ :/, /; p; d }; { s/^\([^ ]\+\).*$/\1 0 0 -/g; p }' < /proc/modules
    else
	cat /proc/modules
    fi
}

FindModuleDeps() {
    Get26ModulesFile | awk -v "module=$1" '
function find_related_modules(mod, i, a) {
    modules[mod] = deps[mod]
    split(deps[mod], a, ",")
    for (i in a) {
        if (a[i] == "") continue
        if (!modules[a[i]]) {
            modules[a[i]] = deps[a[i]]
            find_related_modules(a[i])
        }
    }
}

function top_sort(modules, i, j) {
    changed = 1
    while (changed) {
        changed = 0
        for(i in modules) {
            if (modules[i] == "") {
                print i
                delete modules[i]
                for (j in modules) {
                    sub("^"i",", "", modules[j])
                    sub(","i",", ",", modules[j])
                }
                changed = 1
            }
        }
    }
}

BEGIN {
    have_module = 0
}

{
    if ($4 != "-") deps[$1] = $4
    if ($1 == module) have_module = 1
}

END {
    if (have_module == 1) {
        find_related_modules(module) # puts results into "modules" array
        top_sort(modules)
    }
}
    '
}

IsInVersionRange() {
    local kver
    kver=$1
    shift

    local min_ver
    local max_ver
    while [ -n "$*" ] ; do
	min_ver=`echo $1 | awk 'BEGIN{FS="[^0-9]"}{print($1*0x10000)+($2*0x100)+$3}'`
	max_ver=`echo $2 | awk 'BEGIN{FS="[^0-9]"}{print($1*0x10000)+($2*0x100)+$3}'`
	shift 2
	[ "$kver" -ge "$min_ver" ] && [ "$kver" -le "$max_ver" ] && return 0
    done

    return 1
}

ModulesUnloadBlacklist() {
    [ x"$MODULES_UNLOAD_BLACKLIST" = "x1" ] || return 0
    CheckModuleSupport || return 0

    if [ ! -r "$MODULES_BLACKLIST_FILE" ] ; then
	vecho 1 "Blacklisted modules file doesn't exist or isn't readable."
	return 1
    fi


    local kver=`awk 'BEGIN{FS="[^0-9]"}{print($1*0x10000)+($2*0x100)+$3}' /proc/sys/kernel/osrelease`

    # Loop over every line in given file.
    vecho 2 "Unloading blacklisted modules listed $MODULES_BLACKLIST_FILE"
    local failedmods=""
    while true ; do
	local mod vers use_mod_ver
	use_mod_ver=
	read mod vers
	[ $? -ne 0 ] && [ -z "$mod" ] && break
	case $mod in
	    \#*|"") continue ;;
	    @*)
		mod=${mod#@}
		use_mod_ver=1
		;;
	esac
	if [ -n "$vers" ] ; then
	    if [ -z "$use_mod_ver" ] ; then
		IsInVersionRange $kver $vers || continue
	    else
		local modver
		modver=`modinfo $mod 2>/dev/null | sed -e 's/^version:[ \t]\+[^0-9]*\(.*\)$/\1/;t;d' | awk 'BEGIN{FS="[^0-9]"}{print($1*0x10000)+($2*0x100)+$3}'`
		vecho 3 "Module version for $mod is $modver"
		if [ -n "$modver" ] ; then
		    IsInVersionRange $modver $vers || continue
		fi
	    fi
	fi
	local deps
	deps=`FindModuleDeps $mod`
	[ -z "$deps" ] && continue # not loaded
	vecho 2 "Unloading blacklisted module $mod (and dependencies)"
	local i
	for i in $deps ; do
	    vecho 3 "Unloading $i ..."
	    if xrmmod $i ; then
		MOD_UNLOADED="$MOD_UNLOADED $i"
	    else
		failedmods="$failedmods $i"
	    fi
	done
    done < $MODULES_BLACKLIST_FILE

    [ -z "$failedmods" ] && return 0
    vecho 0 "Some modules failed to unload: $failedmods"
    # Blacklisted modules failing to suspend is noteworthy, and should fail.
    return 1
}

ModulesUnloadAll() {
    [ x"$MODULES_UNLOADALL" = "x1" ] || return 0
    CheckModuleSupport || return 0

    local modbefore
    local modafter
    local mod_retry_count

    # read from /proc/modules which modules are loaded
    modbefore="`awk '($3==0){print $1}' /proc/modules`"
    ModulesUnloadAllOnce
    modafter="`awk '($3==0){print $1}' /proc/modules`"

    # check which are still loaded and retry until nothing changes
    mod_retry_count=0
    while [ "$modafter" != "$modbefore" ] ; do
	modbefore="$modafter"

	# Some sleep implementations might not support decimal sleep values:
	sleep 0.2 > /dev/null 2>&1 ; [ $? -ne 0 ] && [ $? -ne 130 ] && sleep 1

	ModulesUnloadAllOnce
	modafter="`awk '($3==0){print $1}' /proc/modules`"
	[ "$mod_retry_count" -gt 15 ] && break
	[ -z "$modafter" ] && return 0
	mod_retry_count=$(($mod_retry_count+1))
    done

    vecho 0 "Some modules failed to unload: "`awk '{print $1}' /proc/modules`
    # If they're trying to unload everything, some things are bound to fail.
    # So don't complain about it.
    return 0
}

ModulesLoadFromFile() {
    [ -n "$MODULES_FROMFILE" ] || return 0
    [ -r "$MODULES_FROMFILE" ] || return 0
    CheckModuleSupport || return 0
    RestoreKernelModprobe

    local MOD
    local args
    # Loop over every line in given file.
    vecho 2 "Loading modules listed $MODULES_FROMFILE"
    while true ; do
	read MOD args
	[ $? -ne 0 ] && [ -z "$MOD" ] && break
	case "${MOD}" in
	    \#*|"") continue ;;
	esac
	vecho 2 "Loading $MOD"
	modprobe ${MOD} ${args}
    done < $MODULES_FROMFILE

    return 0
}


ModulesOptions() {
    case $1 in
	unloadmodules)
	    [ -z "$MOD_UNLOAD" ] && AddSuspendHook 90 ModulesUnload
	    shift
	    MOD_UNLOAD="$MOD_UNLOAD $@"
	    ;;
	loadmodules)
	    [ -z "$MOD_LOAD" ] && AddResumeHook 90 ModulesLoad
	    shift
	    MOD_LOAD="$MOD_LOAD $@"
	    ;;
	unloadblacklistedmodules)
	    if BoolIsOn "$1" "$2" ; then
		MODULES_UNLOAD_BLACKLIST=1
		AddSuspendHook 91 ModulesUnloadBlacklist
	    fi
	    ;;
	unloadallmodules)
	    if BoolIsOn "$1" "$2" ; then
		MODULES_UNLOADALL=1
		AddSuspendHook 91 ModulesUnloadAll
	    fi
	    ;;
	loadmodulesfromfile)
	    if [ -z "$MODULES_FROMFILE" ] ; then
		MODULES_FROMFILE="$2"
		AddResumeHook 91 ModulesLoadFromFile
	    fi
	    ;;
	*)
	    return 1
    esac
    return 0
}

# Do this globally to ensure it is always done.
if [ -z "$MODPROBE_HOOKED" ] ; then
    AddSuspendHook 89 SaveKernelModprobe
    AddResumeHook 89 RestoreKernelModprobe
    MODPROBE_HOOKED=1
    MOD_UNLOADED="" # Clear this early on
fi

# $Id$