| 1 | #!/bin/bashscip-shell filesystem . . `/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 | [scip-shell system . . `[`. -f /etc/rc.d/init.d/functionsscip-shell filesystem . . `/etc/rc.d/init.d/functions`. ] && . /etc/rc.d/init.d/functionsscip-shell source . . `/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 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 31 | echoscip-shell system . . echo. "Usage: $0 [OPTION]" |
| 32 | echoscip-shell system . . echo. "OPTION" |
| 33 | language_help_prompt_by_idx_no |
| 34 | echoscip-shell system . . echo. "-k, --client-kernel-pkg KERNEL_PKG: specify the KERNEL_PKG (rpm or deb) which you want DRBL client to use." |
| 35 | echoscip-shell system . . 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 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "-v, --verbose: prints verbose information" |
| 38 | } |
| 39 | |
| 40 | # default settings |
| 41 | just_cp_files="no" |
| 42 | depmod_stderr="/dev/nullscip-shell filesystem . . `/dev/null`." |
| 43 | |
| 44 | # Parse command-line options |
| 45 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 46 | case "$1" in |
| 47 | -l|--language) |
| 48 | shift |
| 49 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 78 | USAGEscip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. "$verbose" = "on" ]; then |
| 92 | # The default output for depmod is surpressed, now turn it on |
| 93 | depmod_stderr="/dev/stderrscip-shell filesystem . . `/dev/stderr`." |
| 94 | fi |
| 95 | |
| 96 | # parse the input kernel package |
| 97 | if [scip-shell system . . `[`. -n "$client_kernel_pkg" ]; then |
| 98 | if [scip-shell system . . `[`. -n "$(filescip-shell system . . file. -Ls $client_kernel_pkg | grepscip-shell system . . grep. -i RPM)" ]; then |
| 99 | mode="rpm" |
| 100 | elif [scip-shell system . . `[`. -n "$(filescip-shell system . . file. -Ls $client_kernel_pkg | grepscip-shell system . . grep. -i "Debian binary package")" ]; then |
| 101 | mode="deb" |
| 102 | fi |
| 103 | elif [scip-shell system . . `[`. -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 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 110 | echoscip-shell system . . echo. "You must provide the kernel package (rpm or deb) or the kernel in the server" |
| 111 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 112 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). |
| 113 | exit 1 |
| 114 | fi |
| 115 | case "$mode" in |
| 116 | rpm) |
| 117 | client_kernel_ver="$(rpm -qpl $client_kernel_pkg | grepscip-shell system . . grep. -E "^/boot/vmlinuz-" | sedscip-shell system . . sed. -e "s|/boot/vmlinuz-||g")" |
| 118 | if [scip-shell system . . `[`. -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 | grepscip-shell system . . grep. -E "/usr/lib/modules/[[:print:]]+" | awkscip-shell system . . awk. -F" " '{print $NF}' | awkscip-shell system . . awk. -F"/" '/\/usr\/lib\/modules/ {print $4}scip-shell filesystem . . `/\/usr\/lib\/modules/ {print $4}`.' | sortscip-shell system . . sort. | uniqscip-shell system . . uniq.)" |
| 122 | fi |
| 123 | # we might need /tftpboot/node_root/var/lock/rpm/ for locking in some dist (like CentOS 4.4). |
| 124 | mkdirscip-shell system . . mkdir. -p $drbl_common_root/var/lock/rpm/ |
| 125 | echoscip-shell system . . echo. "Installing $client_kernel_pkg for clients... " |
| 126 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "$msg_done!" |
| 131 | ;; |
| 132 | deb) |
| 133 | client_kernel_ver="$(dpkgscip-shell system . . dpkg. --contents $client_kernel_pkg | awkscip-shell system . . awk. -F" " '{print $NF}' | grepscip-shell system . . grep. -E "./boot/vmlinuz-" | sedscip-shell system . . sed. -e "s|\./boot/vmlinuz-||g")" |
| 134 | if [scip-shell system . . `[`. -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-debscip-shell system . . dpkg-deb. -f $client_kernel_pkg Package | sedscip-shell system . . sed. 's/^linux-modules-//')" |
| 137 | fi |
| 138 | echoscip-shell system . . echo. "Installing $client_kernel_pkg for clients... " |
| 139 | echoscip-shell system . . 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 [scip-shell system . . `[`. -n "$(dpkgscip-shell system . . dpkg. -c $client_kernel_pkg | awkscip-shell system . . awk. '{print $NF}' | grepscip-shell system . . grep. -Ew "^\.\/lib\/modules\/")" ]; then |
| 144 | cvrt_flag="yes" |
| 145 | fi |
| 146 | |
| 147 | dpkg-debscip-shell system . . 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 [scip-shell system . . `[`. "$cvrt_flag" = "yes" ]; then |
| 167 | if [scip-shell system . . `[`. -d "$drbl_common_root/lib" ]; then |
| 168 | if [scip-shell system . . `[`. -d "$drbl_common_root/lib/modules" ]; then |
| 169 | echoscip-shell system . . echo. -n "Converting the old path of kernel modules $drbl_common_root/lib to $drbl_common_root/usr/lib " |
| 170 | mkdirscip-shell system . . mkdir. -p $drbl_common_root/usr/lib/modules/ # in case modules dir does not exist. |
| 171 | rsyncscip-shell system . . rsync. -a $drbl_common_root/lib/modules/* $drbl_common_root/usr/lib/modules/ |
| 172 | fi |
| 173 | rmscip-shell system . . rm. -rf $drbl_common_root/lib/* |
| 174 | rmdirscip-shell system . . rmdir. $drbl_common_root/lib/ |
| 175 | (cd $drbl_common_root; lnscip-shell system . . ln. -fs usr/lib lib) |
| 176 | fi |
| 177 | fi |
| 178 | echoscip-shell system . . echo. "$msg_done!" |
| 179 | ;; |
| 180 | "local") |
| 181 | echoscip-shell system . . echo. "Installing kernel $client_kernel_ver for clients... " |
| 182 | echoscip-shell system . . echo. -n "$msg_install_kernel_might_take_several_minutes" |
| 183 | echoscip-shell system . . echo. -n "." |
| 184 | mkdirscip-shell system . . mkdir. -p $drbl_common_root/boot/ $drbl_common_root/usr/lib/modules/ |
| 185 | echoscip-shell system . . echo. -n "." |
| 186 | rsyncscip-shell system . . rsync. -a /boot/*$client_kernel_ver $drbl_common_root/boot/ |
| 187 | echoscip-shell system . . echo. -n "." |
| 188 | rsyncscip-shell system . . rsync. -a /usr/lib/modules/$client_kernel_ver $drbl_common_root/usr/lib/modules/ |
| 189 | echoscip-shell system . . echo. "$msg_done!" |
| 190 | rc=0 |
| 191 | ;; |
| 192 | esac |
| 193 | |
| 194 | if [scip-shell system . . `[`. "$rc" -eq 1 ]; then |
| 195 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 196 | echoscip-shell system . . echo. "Failed to install the kernel $client_kernel_pkg!" |
| 197 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 198 | exit 1 |
| 199 | fi |
| 200 | |
| 201 | if [scip-shell system . . `[`. ! "$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 [scip-shell system . . `[`. ! -e "$drbl_common_root/boot/System.map-$client_kernel_ver" -a \ |
| 206 | -n "$(lsscip-shell system . . ls. $client_kernel_pkg | grepscip-shell system . . grep. -iE "^.*.rpm")" ]; then |
| 207 | KRC=0 |
| 208 | echoscip-shell system . . echo. "Preparing client's /boot/System.map-$client_kernel_ver..." |
| 209 | _ksystem_map_realpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grepscip-shell system . . grep. -E 'System.map' | grepscip-shell system . . grep. -vE '^/boot/')" |
| 210 | _ksystem_map_bootpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grepscip-shell system . . grep. -E '^/boot/System.map' )" |
| 211 | if [scip-shell system . . `[`. -e "$drbl_common_root/$_ksystem_map_realpath" \ |
| 212 | -a "$_ksystem_map_bootpath" = "/boot/System.map-$client_kernel_ver" ] |
| 213 | then |
| 214 | cpscip-shell system . . cp. -a $drbl_common_root/$_ksystem_map_realpath $drbl_common_root/$_ksystem_map_bootpath |
| 215 | else |
| 216 | KRC=1 |
| 217 | fi |
| 218 | |
| 219 | echoscip-shell system . . echo. "Preparing client's /boot/vmlinuz-$client_kernel_ver..." |
| 220 | _kvmlinuz_realpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grepscip-shell system . . grep. -E 'vmlinuz$' | grepscip-shell system . . grep. -vE '^/boot/')" |
| 221 | _kvmlinuz_bootpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grepscip-shell system . . grep. -E '^/boot/vmlinuz' )" |
| 222 | if [scip-shell system . . `[`. -e "$drbl_common_root/$_kvmlinuz_realpath" \ |
| 223 | -a "$_kvmlinuz_bootpath" = "/boot/vmlinuz-$client_kernel_ver" ] |
| 224 | then |
| 225 | cpscip-shell system . . cp. -a $drbl_common_root/$_kvmlinuz_realpath $drbl_common_root/$_kvmlinuz_bootpath |
| 226 | else |
| 227 | KRC=1 |
| 228 | fi |
| 229 | |
| 230 | echoscip-shell system . . echo. "Preparing client's /boot/config-$client_kernel_ver ..." |
| 231 | _kconfig_realpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grepscip-shell system . . grep. -E 'config$' | grepscip-shell system . . grep. -vE '^/boot/')" |
| 232 | _kconfig_bootpath="$(LC_ALL=C rpm -qpl $client_kernel_pkg | grepscip-shell system . . grep. -E '^/boot/config' )" |
| 233 | if [scip-shell system . . `[`. -e "$drbl_common_root/$_kconfig_realpath" \ |
| 234 | -a "$_kconfig_bootpath" = "/boot/config-$client_kernel_ver" ] |
| 235 | then |
| 236 | cpscip-shell system . . 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 [scip-shell system . . `[`. "$KRC" -ne 0 ]; then |
| 243 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 244 | echoscip-shell system . . echo. "Failed to prepare Linux kernel for DRBL clients in RedHat-like environment." |
| 245 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 246 | echoscip-shell system . . 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 [scip-shell system . . `[`. -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 [scip-shell system . . `[`. -z "$KARCH" ]; then |
| 258 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 259 | echoscip-shell system . . echo. "Client CPU arch NOT found! The $pxecfg_pd/client_kernel_arch.txt will be empty!" |
| 260 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 261 | echoscip-shell system . . echo. -n "$msg_press_enter_to_continue" |
| 262 | read |
| 263 | fi |
| 264 | mkdirscip-shell system . . mkdir. -p $pxecfg_pd/ |
| 265 | echoscip-shell system . . 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="$(echoscip-shell system . . echo. $client_kernel_ver | sedscip-shell system . . 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 [scip-shell system . . `[`. "$mode" != "local" ]; then |
| 277 | if [scip-shell system . . `[`. -e "$drbl_common_root/boot/System.map-$client_kernel_ver" ]; then |
| 278 | echoscip-shell system . . 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 | echoscip-shell system . . echo. "done!" |
| 281 | else |
| 282 | echoscip-shell system . . 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 | [scip-shell system . . `[`. "$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 [scip-shell system . . `[`. -d "/usr/lib/firmwarescip-shell filesystem . . `/usr/lib/firmware`." ]; then |
| 291 | echoscip-shell system . . echo. "Preparing the kernel firmware for clients..." |
| 292 | rsyncscip-shell system . . rsync. -a /usr/lib/firmwarescip-shell filesystem . . `/usr/lib/firmware`. $drbl_common_root/usr/lib/ |
| 293 | fi |
| 294 | |
| 295 | exit 0 |