Debian Codegraph @

scripts/sbin/install-kernel-for-client

raw symbols SCIP
1 #!/bin/bash
2 # Author: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw>
3 # License: GPL
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #
19
20 # Load DRBL setting and functions
21 # Setting
22 # Source function library.
23 [ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions
24
25 # Source DRBL setting
26 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
27 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
28
29 #
30 USAGE() {
31 echo "Usage: $0 [OPTION]"
32 echo "OPTION"
33 language_help_prompt_by_idx_no
34 echo "-k, --client-kernel-pkg KERNEL_PKG: specify the KERNEL_PKG (rpm or deb) which you want DRBL client to use."
35 echo "-s, --client-kernel-ver-from-server KERNEL_VER: use the kernel version KERNEL_VER from server, i.e. from /boot/vmlinuz-KERNEL_VER and /usr/lib/modules/KERNEL_VER..."
36 echo "-c, --just-cp-files: just install files for client, do not create kernel tag for client and do not run some script. Usually this option is useed to install linux-restricted-modules in ubuntu."
37 echo "-v, --verbose: prints verbose information"
38 }
39
40 # default settings
41 just_cp_files="no"
42 depmod_stderr="/dev/null"
43
44 # Parse command-line options
45 while [ $# -gt 0 ]; do
46 case "$1" in
47 -l|--language)
48 shift
49 if [ -z "$(echo $1 |grep ^-.)" ]; then
50 # skip the -xx option, in case
51 specified_lang="$1"
52 shift
53 fi
54 ;;
55 -k|--client-kernel-pkg)
56 shift
57 if [ -z "$(echo $1 |grep ^-.)" ]; then
58 # skip the -xx option, in case
59 client_kernel_pkg="$1"
60 shift
61 fi
62 ;;
63 -s|--client-kernel-ver-from-server)
64 shift
65 if [ -z "$(echo $1 |grep ^-.)" ]; then
66 # skip the -xx option, in case
67 client_kernel_ver_from_server="$1"
68 shift
69 fi
70 ;;
71 -c|--just-cp-files)
72 shift; just_cp_files="yes"
73 ;;
74 -v|--verbose)
75 verbose="on"
76 shift;;
77 -*) echo "${0}: ${1}: invalid option" >&2
78 USAGE >& 2
79 exit 2 ;;
80 *) break ;;
81 esac
82 done
83
84 #
85 check_if_root
86
87 #
88 ask_and_load_lang_set $specified_lang
89
90 #
91 if [ "$verbose" = "on" ]; then
92 # The default output for depmod is surpressed, now turn it on
93 depmod_stderr="/dev/stderr"
94 fi
95
96 # parse the input kernel package
97 if [ -n "$client_kernel_pkg" ]; then
98 if [ -n "$(file -Ls $client_kernel_pkg | grep -i RPM)" ]; then
99 mode="rpm"
100 elif [ -n "$(file -Ls $client_kernel_pkg | grep -i "Debian binary package")" ]; then
101 mode="deb"
102 fi
103 elif [ -n "$client_kernel_ver_from_server" -a \
104 -f /boot/vmlinuz-$client_kernel_ver_from_server -a \
105 -d /usr/lib/modules/$client_kernel_ver_from_server ]; then
106 mode="local"
107 client_kernel_ver="$client_kernel_ver_from_server"
108 else
109 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
110 echo "You must provide the kernel package (rpm or deb) or the kernel in the server"
111 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
112 USAGE
113 exit 1
114 fi
115 case "$mode" in
116 rpm)
117 client_kernel_ver="$(rpm -qpl $client_kernel_pkg | grep -E "^/boot/vmlinuz-" | sed -e "s|/boot/vmlinuz-||g")"
118 if [ -z "$client_kernel_ver" ]; then
119 # For OpenSuSE 11.1, kernel-default or kernel-pae, there is no vmlinuz-* file, try another method, i.e. use the path:
120 # /usr/lib/modules/2.6.27.7-9-default/...
121 client_kernel_ver="$(rpm -qpl $client_kernel_pkg | grep -E "/usr/lib/modules/[[:print:]]+" | awk -F" " '{print $NF}' | awk -F"/" '/\/usr\/lib\/modules/ {print $4}' | sort | uniq)"
122 fi
123 # we might need /tftpboot/node_root/var/lock/rpm/ for locking in some dist (like CentOS 4.4).
124 mkdir -p $drbl_common_root/var/lock/rpm/
125 echo "Installing $client_kernel_pkg for clients... "
126 echo "$msg_install_kernel_might_take_several_minutes "
127 # We do not care about those files put into $drbl_common_root/usr/
128 rpm -ivh --root $drbl_common_root/ --force --nodeps --noscripts $client_kernel_pkg
129 rc=$?
130 echo "$msg_done!"
131 ;;
132 deb)
133 client_kernel_ver="$(dpkg --contents $client_kernel_pkg | awk -F" " '{print $NF}' | grep -E "./boot/vmlinuz-" | sed -e "s|\./boot/vmlinuz-||g")"
134 if [ -z "$client_kernel_ver" ]; then
135 # For deb linux-modules-*, there is no vmlinuz-* file, try another method, i.e. from dpkg-deb -f:
136 client_kernel_ver="$(dpkg-deb -f $client_kernel_pkg Package | sed 's/^linux-modules-//')"
137 fi
138 echo "Installing $client_kernel_pkg for clients... "
139 echo "$msg_install_kernel_might_take_several_minutes "
140
141 # Check if "^./lib/modules/" exists. If so, it's old format (Debian <= 12)
142 cvrt_flag="no"
143 if [ -n "$(dpkg -c $client_kernel_pkg | awk '{print $NF}' | grep -Ew "^\.\/lib\/modules\/")" ]; then
144 cvrt_flag="yes"
145 fi
146
147 dpkg-deb --extract $client_kernel_pkg $drbl_common_root
148 rc=$?
149 # 2025/Sep/21
150 # "dpkg-deb --extract" command will overwrite the linked file $drbl_common_root/lib file even when it already exists.
151 # For Debian <= 12, linux-image will have files under /lib/modules/, e.g.:
152 # linux-image-6.1.0-39-amd64:
153 # /lib/modules/6.1.0-39-amd64/kernel/arch/x86/crypto/aegis128-aesni.ko
154 # /lib/modules/6.1.0-39-amd64/kernel/arch/x86/crypto/aesni-intel.ko
155 # /lib/modules/6.1.0-39-amd64/kernel/arch/x86/crypto/blowfish-x86_64.ko
156 # While for newer Debian/Ubuntu, they are like:
157 # linux-image-6.12.38+deb13-amd64:
158 # /usr/lib/modules/6.12.38+deb13-amd64/kernel/arch/x86/crypto/aegis128-aesni.ko.xz
159 # /usr/lib/modules/6.12.38+deb13-amd64/kernel/arch/x86/crypto/aesni-intel.ko.xz
160 # /usr/lib/modules/6.12.38+deb13-amd64/kernel/arch/x86/crypto/blowfish-x86_64.ko.xz
161 # Hence
162 # If $drbl_common_root/lib is a dir, not linked file, and if dir $drbl_common_root/lib/modules/ exists
163 # linux-image is still in the old path /lib/modules/*
164 # We move $drbl_common_root/lib/moduels to drbl_common_root/usr/lib/modules/, remove linked file $drbl_common_root/lib
165 # and create a link file $drbl_common_root/lib to drbl_common_root/usr/lib
166 if [ "$cvrt_flag" = "yes" ]; then
167 if [ -d "$drbl_common_root/lib" ]; then
168 if [ -d "$drbl_common_root/lib/modules" ]; then
169 echo -n "Converting the old path of kernel modules $drbl_common_root/lib to $drbl_common_root/usr/lib "
170 mkdir -p $drbl_common_root/usr/lib/modules/ # in case modules dir does not exist.
171 rsync -a $drbl_common_root/lib/modules/* $drbl_common_root/usr/lib/modules/
172 fi
173 rm -rf $drbl_common_root/lib/*
174 rmdir $drbl_common_root/lib/
175 (cd $drbl_common_root; ln -fs usr/lib lib)
176 fi
177 fi
178 echo "$msg_done!"
179 ;;
180 "local")
181 echo "Installing kernel $client_kernel_ver for clients... "
182 echo -n "$msg_install_kernel_might_take_several_minutes"
183 echo -n "."
184 mkdir -p $drbl_common_root/boot/ $drbl_common_root/usr/lib/modules/
185 echo -n "."
186 rsync -a /boot/*$client_kernel_ver $drbl_common_root/boot/
187 echo -n "."
188 rsync -a /usr/lib/modules/$client_kernel_ver $drbl_common_root/usr/lib/modules/
189 echo "$msg_done!"
190 rc=0
191 ;;
192 esac
193
194 if [ "$rc" -eq 1 ]; then
195 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
196 echo "Failed to install the kernel $client_kernel_pkg!"
197 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
198 exit 1
199 fi
200
201 if [ ! "$just_cp_files" = "yes" ]; then
202 # For FC 22 or later, rpm -ivh doesn't put /boot/System.map-$client_kernel_ver with --noscripts
203 # Therefore we copy vmlinuz, System.map-$client_kernel_ver, config-$client_kernel_ver
204 # This is only required for Fedora rpm. Ubuntu's linux-modules deb contains System.map-$client_kernel_ver
205 if [ ! -e "$drbl_common_root/boot/System.map-$client_kernel_ver" -a \
206 -n "$(ls $client_kernel_pkg | grep -iE "^.*.rpm")" ]; then
207 KRC=0
208 echo "Preparing client's /boot/System.map-$client_kernel_ver..."
209 _ksystem_map_realpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grep -E 'System.map' | grep -vE '^/boot/')"
210 _ksystem_map_bootpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grep -E '^/boot/System.map' )"
211 if [ -e "$drbl_common_root/$_ksystem_map_realpath" \
212 -a "$_ksystem_map_bootpath" = "/boot/System.map-$client_kernel_ver" ]
213 then
214 cp -a $drbl_common_root/$_ksystem_map_realpath $drbl_common_root/$_ksystem_map_bootpath
215 else
216 KRC=1
217 fi
218
219 echo "Preparing client's /boot/vmlinuz-$client_kernel_ver..."
220 _kvmlinuz_realpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grep -E 'vmlinuz$' | grep -vE '^/boot/')"
221 _kvmlinuz_bootpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grep -E '^/boot/vmlinuz' )"
222 if [ -e "$drbl_common_root/$_kvmlinuz_realpath" \
223 -a "$_kvmlinuz_bootpath" = "/boot/vmlinuz-$client_kernel_ver" ]
224 then
225 cp -a $drbl_common_root/$_kvmlinuz_realpath $drbl_common_root/$_kvmlinuz_bootpath
226 else
227 KRC=1
228 fi
229
230 echo "Preparing client's /boot/config-$client_kernel_ver ..."
231 _kconfig_realpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grep -E 'config$' | grep -vE '^/boot/')"
232 _kconfig_bootpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grep -E '^/boot/config' )"
233 if [ -e "$drbl_common_root/$_kconfig_realpath" \
234 -a "$_kconfig_bootpath" = "/boot/config-$client_kernel_ver" ]
235 then
236 cp -a $drbl_common_root/$_kconfig_realpath $drbl_common_root/$_kconfig_bootpath
237 else
238 KRC=1
239 fi
240
241 # Output error messages about failing to prepare kernel for clients.
242 if [ "$KRC" -ne 0 ]; then
243 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
244 echo "Failed to prepare Linux kernel for DRBL clients in RedHat-like environment."
245 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
246 echo "$msg_program_stop!"
247 exit 1
248 fi
249 fi
250 # Need to create the files
251 # $pxecfg_pd/kernel_version_in_initrd.txt
252 # $pxecfg_pd/client_kernel_arch.txt
253 # Only when we can find something like /tftpboot/node_root/boot/System.map-2.6.27.39-0.2-default we can find KARCH.
254 if [ -e "$drbl_common_root/boot/System.map-$client_kernel_ver" ]; then
255 # make a tag in the system for client's kernel arch.
256 KARCH="$(LC_ALL=C drbl-check-kernel-cpu-arch --drbl-client $client_kernel_ver)"
257 if [ -z "$KARCH" ]; then
258 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
259 echo "Client CPU arch NOT found! The $pxecfg_pd/client_kernel_arch.txt will be empty!"
260 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
261 echo -n "$msg_press_enter_to_continue"
262 read
263 fi
264 mkdir -p $pxecfg_pd/
265 echo "$KARCH" > $pxecfg_pd/client_kernel_arch.txt
266 fi
267
268 # UGLY... find a better to strip the tail for MDK kernel
269 # for MDK kernel-2.6.8.1.24mdk-1-1mdk, however the kernel tag is 2.6.8.1-24mdk
270 # we need to strip the -1-1mdk
271 client_kernel_ver="$(echo $client_kernel_ver | sed -e "s/-1-1mdk//g")"
272 fi
273
274 # Create modules.dep...
275 # we still neeed to build $drbl_common_root/boot/System.map-$client_kernel_ver for client if kernel is installed from package
276 if [ "$mode" != "local" ]; then
277 if [ -e "$drbl_common_root/boot/System.map-$client_kernel_ver" ]; then
278 echo -n "Generating modules.dep and map files for clients... "
279 depmod -ae -b $drbl_common_root/ -F $drbl_common_root/boot/System.map-$client_kernel_ver $client_kernel_ver 2> $depmod_stderr
280 echo "done!"
281 else
282 echo "$drbl_common_root/boot/System.map-$client_kernel_ver NOT found! Assume it's in another kernel package."
283 fi
284 fi
285
286 # we stop here for kernel extra/module package
287 [ "$just_cp_files" = "yes" ] && exit 0
288
289 # Firmwares. For Ubuntu, the firmwares come with kernel package, however, it's not true for other distributions. We can only try to use that from server
290 if [ -d "/usr/lib/firmware" ]; then
291 echo "Preparing the kernel firmware for clients..."
292 rsync -a /usr/lib/firmware $drbl_common_root/usr/lib/
293 fi
294
295 exit 0