Debian Codegraph @

scripts/bin/drbl-check-kernel-cpu-arch

raw symbols SCIP
1 #!/bin/bash
2 # Author: Steven Shiau <steven _at_ clonezilla org>
3 # License: GPL
4 # Description: If this program is able to find the arch, it will print. Otherwise, nothing will be shown.
5
6 Usage() {
7 echo "Usage:"
8 echo "$(basename $0) [-c|--drbl-client] KERNEL_VER"
9 echo "-c|--drbl-client is to check DRBL client's kernel arch."
10 echo "If not specified, it will check DRBL server's kernel arch."
11 echo "Ex: $0 2.4.20-31.9smp"
12 }
13
14 # Load DRBL setting and functions
15 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
16
17 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
18
19 # Parse command-line options
20 while [ $# -gt 0 ]; do
21 case "$1" in
22 -c|--drbl-client)
23 shift; check_drbl_client_kernel="yes"
24 ;;
25 -*) echo "${0}: ${1}: invalid option" >&2
26 Usage >& 2
27 exit 2 ;;
28 *) break ;;
29 esac
30 done
31 KERNEL=$1
32
33 [ -z "$KERNEL" ] && exit 1
34
35 if [ "$check_drbl_client_kernel" = "yes" ]; then
36 CONFIG_FILE=$drbl_common_root/boot/config-$KERNEL
37 else
38 CONFIG_FILE=/boot/config-$KERNEL
39 fi
40
41 if [ ! -f "$CONFIG_FILE" ]; then
42 # Determine alternative paths for fallbacks
43 if [ "$check_drbl_client_kernel" = "yes" ]; then
44 KERNEL_FILE=$drbl_common_root/boot/vmlinuz-$KERNEL
45 MODULES_DIR=$drbl_common_root/lib/modules/$KERNEL
46 [ ! -d "$MODULES_DIR" ] && MODULES_DIR=$drbl_common_root/usr/lib/modules/$KERNEL
47 else
48 KERNEL_FILE=/boot/vmlinuz-$KERNEL
49 MODULES_DIR=/lib/modules/$KERNEL
50 [ ! -d "$MODULES_DIR" ] && MODULES_DIR=/usr/lib/modules/$KERNEL
51 fi
52
53 # Fallback 1: If requested kernel is the running kernel, use uname -m
54 if [ "$KERNEL" = "$(uname -r)" ]; then
55 case "$(uname -m)" in
56 x86_64)
57 echo "x86_64"
58 exit 0
59 ;;
60 aarch64|arm64)
61 echo "arm64"
62 exit 0
63 ;;
64 esac
65 fi
66
67 # Fallback 2: Try to find file or directory to query package manager
68 target=""
69 if [ -f "$KERNEL_FILE" ]; then
70 target="$KERNEL_FILE"
71 elif [ -d "$MODULES_DIR" ]; then
72 target="$MODULES_DIR"
73 fi
74
75 if [ -n "$target" ]; then
76 if type dpkg &>/dev/null && type dpkg-query &>/dev/null; then
77 pkg_name="$(dpkg -S "$target" 2>/dev/null | cut -d: -f1)"
78 if [ -n "$pkg_name" ]; then
79 pkg_arch="$(dpkg-query -W -f='${Architecture}\n' "$pkg_name" 2>/dev/null)"
80 case "$pkg_arch" in
81 amd64)
82 echo "x86_64"
83 exit 0
84 ;;
85 arm64)
86 echo "arm64"
87 exit 0
88 ;;
89 esac
90 fi
91 elif type rpm &>/dev/null; then
92 pkg_arch="$(rpm -q --qf '%{ARCH}\n' -f "$target" 2>/dev/null)"
93 case "$pkg_arch" in
94 x86_64)
95 echo "x86_64"
96 exit 0
97 ;;
98 aarch64)
99 echo "arm64"
100 exit 0
101 ;;
102 esac
103 fi
104 fi
105
106 # Fallback 3: Use file utility on the kernel file if available
107 if [ -f "$KERNEL_FILE" ] && type file &>/dev/null; then
108 file_info="$(file -b "$KERNEL_FILE" 2>/dev/null)"
109 if echo "$file_info" | grep -qi -E "x86_64|x86-64|boot executable bzImage|Intel 80386"; then
110 echo "x86_64"
111 exit 0
112 elif echo "$file_info" | grep -qi -E "arm64|aarch64"; then
113 echo "arm64"
114 exit 0
115 fi
116 fi
117
118 # Fallback 4: Try checking the module file formats in the modules directory
119 if [ -d "$MODULES_DIR" ] && type file &>/dev/null; then
120 # find a kernel module .ko and check its file type
121 ko_file="$(find "$MODULES_DIR" -name "*.ko*" -type f -print -quit 2>/dev/null)"
122 if [ -n "$ko_file" ]; then
123 file_info="$(file -b "$ko_file" 2>/dev/null)"
124 if echo "$file_info" | grep -qi -E "x86-64|x86_64"; then
125 echo "x86_64"
126 exit 0
127 elif echo "$file_info" | grep -qi -E "aarch64|arm64"; then
128 echo "arm64"
129 exit 0
130 fi
131 fi
132 fi
133
134 # If all fallbacks failed, exit with failure
135 exit 1
136 fi
137
138 CPULIST="CONFIG_X86_64 CONFIG_ARM64"
139 for icpu in $CPULIST; do
140 if grep -q "$icpu=y" $CONFIG_FILE; then
141 arch="$(echo $icpu | sed -e "s/CONFIG_M//g" -e "s/CONFIG_//g" -e "s/TSC$//g" -e "s/MMX$//g" | tr "[A-Z]" "[a-z]")"
142 case "$arch" in
143 [3456]86) arch=i${arch} ;;
144 *) arch=${arch} ;;
145 esac
146 echo "$arch"
147 fi
148 done