summaryrefslogtreecommitdiff
path: root/libre/dkms-libre/dkms.systemd
blob: 03e6bfe97bab1b9b9aefffb3406c29d9d3499500 (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
#!/bin/bash

. /etc/default/dkms

# list dkms modules for the current running kernel
list_dkms_modules() {
  shopt -s nullglob
  declare -a DKMS_MOULES
  # dkms status needs uname -r to list correctly intalled version for current kernel
  local modules_path=($(dkms status -k "$(uname -r)"|sed -rn 's#(.*), (.*), (.*), (.*): installed#\1/\2/\3/\4#p'))
  for p in "${modules_path[@]}"; do
    for m in /var/lib/dkms/"$p"/module/*.ko{,gz}; do
      m=${m##*/}
      m=${m%.gz}
      m=${m%.ko}
      DKMS_MODULES+=("$m")
    done
  done
}

# load installed kernel modules for the current kernel version
load_dkms_modules() {
  local ret=0
  list_dkms_modules
  for m in "${DKMS_MODULES[@]}"; do
    modprobe "$m"
    ret+=$?
  done
  return $ret
}

# unload installed kernel modules for the current kernel version
unload_dkms_modules() {
  list_dkms_modules
  # ask for removal
  for m in "${DKMS_MODULES[@]}"; do
    modprobe --remove --quiet "$m"
  done
  # check modules are unloaded
  # sometimes modprobe -r fail but modules are removed
  for m in "${DKMS_MODULES[@]}"; do
    [[ -e /sys/modules/$m ]] && return 1
  done
  return 0
}

case "$1" in
  start)
    dkms autoinstall -k "$(uname -r)"
    [[ $LOAD =~ [Yy][Ee][Ss] ]] && load_dkms_modules
  ;;
  stop)
    [[ $UNLOAD =~ [Yy][Ee][Ss] ]] && unload_dkms_modules
  ;;
  *)
    echo "usage: $0 {start|stop}" >&2
  ;;
esac

:
# vim:set ts=2 sw=2 ft=sh et: