summarylogtreecommitdiffstats
path: root/hook.sh
blob: 6c685ee35dbd8861d7d4daf92d41c0a789bd3835 (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
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
#!/bin/bash

#
# Copyright © 2018-2021, 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
  local ret=$?
  (( $ret )) && echo "==> WARNING: \`$*' exited $ret"
  return $ret
}

# check whether the dependencies of a module are installed
# $1: module name
# $2: module version
# $3: kernel version
check_dependency() { (
  source "$source_tree/$1-$2/dkms.conf"
  local mod lines line
  for mod in "${BUILD_DEPENDS[@]}"; do
    mapfile lines < <(dkms status -m "$mod" -k "$3")
    for line in "${lines[@]}"; do
      [[ "$line" =~ "$mod/"[^,]+", $3, "[^:]+': installed' ]] && break 2
    done
    exit 1
  done
  exit 0
) }

# check whether the modules should be built with this kernel version
# $1: module name
# $2: module version
# $3: kernel version
check_buildexclusive() {
  local BUILD_EXCLUSIVE_KERNEL=$(source "$source_tree/$1-$2/dkms.conf"; printf '%s\n' "$BUILD_EXCLUSIVE_KERNEL")
  [[ "$3" =~ $BUILD_EXCLUSIVE_KERNEL ]]
}

# list all kernel versions
all_kver() {
  pushd "$install_tree" >/dev/null
  local path
  for path in */build/; do
    echo "${path%%/*}"
  done
  popd >/dev/null
}

# list all module name/version for a specific kernel version
# $1: kernel version
all_nv_from_kver() {
  local path
  for path in "$source_tree"/*-*/dkms.conf; do
    if [[ -f "$path" && "$path" =~ ^$source_tree/([^/]+)-([^/]+)/dkms\.conf$ ]]; then
      echo "${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
    fi
  done
}

# list of modules/version for installed/built kernel version
# $1: kernel version
built_nv_from_kver() {
  local line
  dkms status -k "$1" | while read -r line; do
    if [[ "$line" =~ ^([^/]+/[^,]+)", $1, "[^:]+": "(built|installed) ]]; then
      echo "${BASH_REMATCH[1]}"
    fi
  done
}

# list installed or built kernel version for a specific module version
# $1 : module name/module version
built_kver_from_nv() {
  local line
  dkms status "$1" | while read -r line; do
    if [[ "$line" =~ ^"$1, "([^,]+)", "[^:]+": "(built|installed) ]]; then
      echo "${BASH_REMATCH[1]}"
    fi
  done
}

# install registered modules
dkms_install() {
  # list of modules to build for a specific kernel
  local -A tobuild=()

  # add new/updated modules for all kernels to the build list
  local nv kver
  for nv in "${!DKMS_MODULES[@]}"; do
    for kver in $(all_kver); do
      tobuild["$nv/$kver"]=''
    done
  done
  # add modules for new/updated kernels to the build list
  for kver in "${!KERNEL_VERSIONS[@]}"; do
    for nv in $(all_nv_from_kver "$kver"); do
      tobuild["$nv/$kver"]=''
    done
  done

  # list of kver which requires depmod refresh
  local -A depmods=()

  # let's build and install
  local nvk mod mver
  local -i retry=1
  while (( $retry > 0 )); do
    retry=0
    for nvk in "${!tobuild[@]}"; do
      [[ "$nvk" =~ ([^/]+)/([^/]+)/(.+) ]] || continue
      mod="${BASH_REMATCH[1]}"
      mver="${BASH_REMATCH[2]}"
      kver="${BASH_REMATCH[3]}"
      # do not build excluded modules
      if ! check_buildexclusive "$mod" "$mver" "$kver"; then
        unset tobuild[$nvk]
        continue
      # skip modules with missing kernel headers
      elif [[ ! -d "$install_tree/$kver/build/include" ]]; then
        ERROR_MESSAGES+=("Missing $kver kernel headers for module $mod/$mver.")
        unset tobuild[$nvk]
        continue
      # skip modules with missing kernel package
      elif [[ ! -d "$install_tree/$kver/kernel" ]]; then
        ERROR_MESSAGES+=("Missing $kver kernel modules tree for module $mod/$mver.")
        unset tobuild[$nvk]
        continue
      # postpone modules with missing dependencies
      elif ! check_dependency "$mod" "$mver" "$kver"; then
        continue
      fi
      # give it a try dkms
      run dkms install --no-depmod "$mod/$mver" -k "$kver"
      if (( $? == 0 )); then
        # register kernel version for later depmod
        depmods[$kver]=''
      fi
      unset tobuild[$nvk]
      # maybe this module was a dep of another, so we retry
      retry=1
    done
  done
  # run depmod later for performance improvments
  if (( $DKMS_DEPMOD )); then
    for kver in "${!depmods[@]}"; do
      run depmod "$kver"
    done
  fi
  # add errors messages for missing dependencies modules
  for nvk in "${!tobuild[@]}"; do
    [[ "$nvk" =~ ([^/]+/[^/]+)/(.+) ]] || continue
    nv="${BASH_REMATCH[1]}"
    kver="${BASH_REMATCH[2]}"
    ERROR_MESSAGES+=("Missing dependencies to install module $nv for kernel $kver.")
  done
}

# remove registered modules
# run depmod once per kernel for performance improvments
dkms_remove() {
  local nv kver
  local -A depmods=()
  # remove full modules first
  for nv in "${!DKMS_MODULES[@]}"; do
    # try to remove modules one by one to keep the depmod optimization
    for kver in $(built_kver_from_nv "$nv"); do
      run dkms remove --no-depmod "$nv" -k "$kver"
      if (( $? == 0 )); then
        # register kernel version for later depmod
        depmods[$kver]=''
      else
        ERROR_MESSAGES+=("Failed to remove module $nv for kernel $kver.")
      fi
    done
    # ensure module removal (even if only added)
    if [[ $(dkms status "$nv") ]]; then
      run dkms remove "$nv"
      (( $? == 0 )) || ERROR_MESSAGES+=("Failed to remove module $nv.")
    fi
  done
  # remove modules for a specific kernel version
  for kver in "${!KERNEL_VERSIONS[@]}"; do
    for nv in $(built_nv_from_kver "$kver"); do
      run dkms remove --no-depmod "$nv" -k "$kver"
      if (( $? == 0 )); then
        # register kernel version for later depmod
        depmods[$kver]=''
      else
        ERROR_MESSAGES+=("Failed to remove module $nv for kernel $kver.")
      fi
    done
  done
  # run depmod later for performance improvments
  if (( $DKMS_DEPMOD )); then
    for kver in "${!depmods[@]}"; do
      run depmod "$kver"
    done
  fi
}

# display hook usage and exit $1 (default 1)
usage() {
  cat << EOF >&2
usage: ${0##*/} <options> install|remove
options: -D  Do not run depmod
EOF
  exit ${1:-1}
}

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

  # prevent each dkms call from failing with authorization errors
  if (( EUID )); then
    echo 'You must be root to use this hook' >&2
    return 1
  fi

  # parse command line options
  declare -i DKMS_DEPMOD=1
  local opt
  while getopts 'hD' opt; do
    case $opt in
      D) DKMS_DEPMOD=0;;
      *) usage;;
    esac
  done
  shift $((OPTIND - 1))
  (( $# != 1 )) && usage

  # parse command action to early exit
  case "$1" in
    install|remove) declare -r DKMS_ACTION="$1";;
    *) usage;;
  esac

  # 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!" >&2
      return 1
    fi
  done

  # global storage for changed DKMS modules
  # we use associate arrays to prevent duplication
  # the key is <module name>/<module version>/<kernel version>
  declare -A DKMS_MODULES

  # global storage for changed linux kernels
  # we use associate arrays to prevent duplication
  declare -A KERNEL_VERSIONS

  # global storage for error messages
  declare -a ERROR_MESSAGES

  # parse stdin paths to guess what we should install/remove
  while read -r path; do
    if [[ "/$path" =~ ^$source_tree/([^/]+)-([^/]+)/dkms\.conf$ ]]; then
      # we match file updates on dkms modules sources
      DKMS_MODULES["${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"]=''
    elif [[ "/$path" =~ ^$install_tree/([^/]+)/ ]]; then
      # we match file updates on kernels install/removal
      KERNEL_VERSIONS["${BASH_REMATCH[1]}"]=''
    fi
  done

  dkms_$DKMS_ACTION

  # display errors at the end, to maximize readers
  local msg
  for msg in "${ERROR_MESSAGES[@]}"; do
    echo "==> ERROR: $msg" >&2
  done

  return 0
}

main "$@"