-
Notifications
You must be signed in to change notification settings - Fork 0
/
.lib.sh
139 lines (139 loc) · 3.15 KB
/
.lib.sh
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
#!/bin/bash
HOST_KERNEL_PAT='host/([^/]+)/kernel/([^/]+)'
list_kernel_installed() {
(
set -e -u
vmlinuz_pat='/boot/vmlinuz-([0-9]+).([0-9]+).([0-9]+)(.+)-g(.*)'
for vmlinuz in /boot/vmlinuz-*
do
[[ $vmlinuz =~ $vmlinuz_pat ]] || continue
version="${BASH_REMATCH[1]}"
patchlevel="${BASH_REMATCH[2]}"
sublevel="${BASH_REMATCH[3]}"
localversion="${BASH_REMATCH[4]}"
commit="${BASH_REMATCH[5]}"
kernelrelease="${version}.${patchlevel}.${sublevel}${localversion}-g${commit}"
[[ -f "/boot/config-${kernelrelease}" ]] || continue
[[ -f "/boot/initrd.img-${kernelrelease}" ]] || continue
[[ -f "/boot/System.map-${kernelrelease}" ]] || continue
[[ -f "/boot/vmlinuz-${kernelrelease}" ]] || continue
[[ -d "/lib/modules/${kernelrelease}" ]] || continue
echo "${kernelrelease}"
done
return 0
)
}
host_kernel_localversion() {
(
set -e -u
: ${HOST_KERNEL:=$1}
if ! [[ ${HOST_KERNEL} =~ ${HOST_KERNEL_PAT} ]]
then
return 1
fi
host_kernel_localversion="${BASH_REMATCH[2]}"
echo "${host_kernel_localversion}"
return 0
)
}
host_kernel_config() {
(
set -e -u
host_kernel="${HOST_KERNEL:=$1}"
if ! [[ $host_kernel =~ ${HOST_KERNEL_PAT} ]]
then
return 1
fi
host_kernel_config="${HOST_KERNEL}/config"
test -f "${host_kernel_config}"
echo "${host_kernel_config}"
return 0
)
}
host_kernel_src() {
(
set -e -u
: ${HOST_KERNEL:=$1}
if ! [[ ${HOST_KERNEL} =~ ${HOST_KERNEL_PAT} ]]
then
return 1
fi
host_kernel_src="${HOST_KERNEL}/src"
if ! grep -q "${host_kernel_src}" .gitmodules
then
return 1
fi
echo "${host_kernel_src}"
return 0
)
}
host_kernel_commit() {
(
set -e -u
: ${HOST_KERNEL:=$1}
if ! [[ ${HOST_KERNEL} =~ ${HOST_KERNEL_PAT} ]]
then
return 1
fi
host_kernel_src=$(host_kernel_src)
git submodule status "${host_kernel_src}" | awk '{print $1}'
return 0
)
}
host_kernel() {
(
set -e -u
: ${HOST_KERNEL:=$1}
if ! test -d "${HOST_KERNEL}"
then
return 1
fi
if ! [[ ${HOST_KERNEL} =~ ${HOST_KERNEL_PAT} ]]
then
return 1
fi
host="${BASH_REMATCH[1]}"
if ! [ "${HOSTNAME}" = "${host}" ]
then
return 1
fi
echo "${HOST_KERNEL}"
return 0
)
}
__host_kernel_installed() {
# List kernel installed that maches $1
(
set -e -u
: ${HOST_KERNEL:=$1}
host_kernel_localversion=$(host_kernel_localversion "${HOST_KERNEL}")
host_kernel_commit=$(host_kernel_commit "${HOST_KERNEL}")
kernelrelease_pat="([0-9]+).([0-9]+).([0-9]+)-(.+)-g(.*)"
for kernelrelease in $(list_kernel_installed)
do
[[ ${kernelrelease} =~ ${kernelrelease_pat} ]]
version="${BASH_REMATCH[1]}"
patchlevel="${BASH_REMATCH[2]}"
sublevel="${BASH_REMATCH[3]}"
localversion="${BASH_REMATCH[4]}"
commit="${BASH_REMATCH[5]}"
host_kernel_commit_pat="${commit}.*"
[[ ${localversion} = ${host_kernel_localversion} ]] || continue
[[ ${host_kernel_commit} =~ ${host_kernel_commit_pat} ]] || continue
echo "${kernelrelease}"
done
return 0
)
}
host_kernel_installed() {
(
set -e -u
: ${HOST_KERNEL:=$1}
if __host_kernel_installed | wc -l | xargs test 1 -ne
then
return 1
fi
__host_kernel_installed
return 0
)
}