summarylogtreecommitdiffstats
path: root/alpm-hook
blob: b7bb7851062618b24285fa32858ce1086724f6e5 (plain)
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
#!/bin/bash

#
# Copyright © 2017 Sébastien Luttringer
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

# display what to run and run it quietly
run() {
	echo "==> $*"
	"$@" > /dev/null
}

# check kernel is valid for action
# it means kernel and its headers are installed
# $1: kernel version
check_kernel() {
	local kver="$1"; shift
	if [[ ! -d "$install_tree/$kver/kernel" ]]; then
		echo "==> No kernel $kver modules. You must install them to use DKMS!"
		return 1
	elif [[ ! -e "$install_tree/$kver/build/Makefile" ]]; then
		echo "==> No kernel $kver headers. You must install them to use DKMS!"
		return 1
	fi
	return 0
}

# handle actions on module addition/upgrade/removal
# $1: module name
# $2: module version
# $3: dkms action
parse_module() {
	pushd "$install_tree" >/dev/null
	local path
	for path in */build/; do
		local kver="${path%%/*}"
		dkms_register "$1" "$2" "$kver" "$3"
	done
	popd >/dev/null
}

# handle actions on kernel addition/upgrade/removal
# $1: kernel version
# $2: dkms action
parse_kernel() {
	local path
	for path in "$source_tree"/*-*/dkms.conf; do
		if [[ -f "$path" && "$path" =~ ^$source_tree/([^/]+)-([^/]+)/dkms\.conf$ ]]; then
			dkms_register "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "$1" "$2"
		fi
	done
}

# register a dkms call
# this function suppress echo call for a module
# $1: module name, $2: module version, $3: kernel version, $4: action
dkms_register() {
	DKMS_ACTION["$1/$2/$3"]="$4"
}

# check whether the dependencies of a module are installed
# $1: module name/module version
# $2: kernel version
check_dependency() {
	local BUILD_DEPENDS
	mod_name=${1%/*}
	mod_ver=${1#*/}
	readarray -t BUILD_DEPENDS <<<$(source "$source_tree/$mod_name-$mod_ver/dkms.conf"; printf '%s\n' "${BUILD_DEPENDS[@]}")
	[ "$BUILD_DEPENDS" = "" ] && unset BUILD_DEPENDS
	for dep in "${BUILD_DEPENDS[@]}"; do
		if [ ! "$(dkms status -m "$dep" -k "$2" | grep -o installed)" = "installed" ]; then
			return 1
		fi
	done
	return 0
}

# run registered dkms commands
dkms_run() {
	local nvk mod kver cont
	cont=y
	while [ "$cont" = "y" ]; do
		cont=n
		for nvk in "${!DKMS_ACTION[@]}"; do
			mod=${nvk%/*}
			kver=${nvk##*/}
			check_kernel "$kver" || continue
			if [ "${DKMS_ACTION[$nvk]}" = "install" ]; then
				check_dependency "$mod" "$kver" || continue
			fi
			run dkms "${DKMS_ACTION[$nvk]}" "$mod" -k "$kver"
			cont=y
			unset DKMS_ACTION[$nvk]
		done
	done
	# show warning for modules not installed
	for nvk in "${!DKMS_ACTION[@]}"; do
		[ "${DKMS_ACTION[$nvk]}" = "install" ] || continue
		mod=${nvk%/*}
		kver=${nvk##*/}
		check_kernel "$kver" || continue
		echo "==> WARNING: Cannot resolve dependencies for module $mod, kernel version $kver"
	done
}

# emulated program entry point
main() {
	[[ -n "$DKMS_ALPM_HOOK_DEBUG" ]] && set -x

	# prevent to have all each dkms call to fail
	if (( EUID )); then
		echo 'You must be root to use this hook' >&2
		exit 1
	fi

	# check args count
	if (( $# < 1 )); then
		echo "usage: ${0##*/} dkms-arguments" >&2
		exit 1
	fi

	# dkms path from framework config
	# note: the alpm hooks which trigger this script use static path
	source_tree='/usr/src'
	install_tree='/usr/lib/modules'
	source /etc/dkms/framework.conf

	# check source_tree and install_tree exists
	local path
	for path in "$source_tree" "$install_tree"; do
		if [[ ! -d "$path" ]]; then
			echo "==> Missing mandatory directory: $path. Exiting!"
			return 1
		fi
	done

	# Storage for DKMS action to run
	declare -A DKMS_ACTION

	# parse stdin paths to guess what do do
	while read -r path; do
		if [[ "/$path" =~ ^$source_tree/([^/]+)-([^/]+)/dkms\.conf$ ]]; then
			parse_module "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "$@"
		elif [[ "/$path" =~ ^$install_tree/([^/]+)/ ]]; then
			parse_kernel "${BASH_REMATCH[1]}" "$@"
		fi
	done

	dkms_run

	return 0
}

main "$@"