summaryrefslogtreecommitdiff
path: root/check-kernel.in
blob: 7a6500c459cfa56fb5b4ce57896c87fc75ee41ab (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
#!/bin/sh

# check-kernel #VERSION#

verwendung() {
  >&2 echo 'check-kernel checks if the installed kernel is currently running'
  >&2 echo ''
  >&2 echo 'Usage: check-kernel [OPTIONS]'
  >&2 echo '  -r,--reboot     reboot system if installed kernel is not yet running'
  >&2 echo \
'#HELPTEXT#       #'
  exit 1
}

eval set -- "$(
  getopt -o r \
    --long reboot \
    --long help \
    --long version \
    -n "$(basename "$0")" -- "$@" \
    || echo verwendung
)"

reboot=false
while true; do
  case "$1" in
    -r|--reboot)
      reboot=true
    ;;
    --help)
      verwendung 0
      ;;
    --version)
      echo '#VERSION#'
      exit 0
      ;;
    --)
      shift
      break
    ;;
    *)
      >&2 echo "FEHLER: Verstehe Option \"$1\" doch nicht! Ich beende."
      verwendung
    ;;
  esac
  shift
done

if [ -f '#ETCDIR#/check-kernel.conf' ]; then
  if ! . '#ETCDIR#/check-kernel.conf'; then
    >&2 printf 'Sourcing "#ETCDIR#/check-kernel.conf" failed.\n'
    exit 1
  fi
fi

unset installed
if which pacman >/dev/null 2>&1; then
  # arch linux
  running=$(
    uname -r | \
      sed '
        s|-ARCH$||
        s|\(\.0\)\?-arch|.|
      '
  )
  installed=$(
    {
      pacman -Q linux 2>/dev/null || \
      pacman -Q | \
        grep '^linux-' | \
        sed '
          s/-headers / /
        ' | \
        uniq -d
    } | \
      cut -d' ' -f2 | \
      sed 's/\.arch/./' | \
      sort -V | \
      tail -n1
  )
elif which apt >/dev/null 2>&1; then
  # debian
  running=$(
    uname -r
  )
  installed=$(
    dpkg-query -Wf'${Package} ${Status}\n' 'linux-image-*' | \
      sed -n '
        s|^linux-image-||
        T
        s/ install ok installed$//
        T
        p
      ' | \
      grep -x '\([^-]\+-\)\{2\}[^-]\+' | \
      sort -V | \
      tail -n1
  )
else
  running=$(
    uname -r
  )
  installed=$(
    ls /boot | \
      sed -n '
        s/^vmlinuz-\([0-9.]\+\)$/\1/
        T
        p
      ' | \
      sort -V | \
      tail -n1
  )
fi
installed=$(
  printf '%s\n' "${installed}" | \
    sed '
      s/\(\.0\)\+$//
    '
)
running=$(
  printf '%s\n' "${running}" | \
    sed '
      s/\(\.0\)\+$//
    '
)

if [ -z "${installed}" ] || \
  [ "$(echo "${installed}" | wc -l)" -ne 1 ]; then
  >&2 printf 'Cannot determin installed kernel.\n'
  exit 2
fi

if [ "${running}" = "${installed}" ]; then
  if ! ${reboot}; then
    >&2 printf 'The installed kernel (%s) is currently running.\n' \
      "${installed}"
  fi
  exit 0
else
  >&2 printf 'The installed (%s) and running kernel (%s) differ.\n' \
    "${installed}" \
    "${running}"
  if ${reboot}; then
    if ! reboot_check_hook; then
      >&2 printf 'reboot_check_hook() failed.\n'
      exit 1
    fi
    >&2 printf 'Press enter to reboot ...'
    read s
    if [ -z "${s}" ]; then
      PATH=$PATH:/usr/bin:/usr/sbin:/bin:/sbin reboot
    fi
  else
    exit 1
  fi
fi