Debian Codegraph @

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

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/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 Usagescip-shell shell shell-project 0.0.0 Usage().() {
7 echoscip-shell system . . echo. "Usage:"
8 echoscip-shell system . . echo. "$(basenamescip-shell system . . basename. $0) [-c|--drbl-client] KERNEL_VER"
9 echoscip-shell system . . echo. "-c|--drbl-client is to check DRBL client's kernel arch."
10 echoscip-shell system . . echo. "If not specified, it will check DRBL server's kernel arch."
11 echoscip-shell system . . 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 [scip-shell system . . `[`. $# -gt 0 ]; do
21 case "$1" in
22 -c|--drbl-client)
23 shift; check_drbl_client_kernel="yes"
24 ;;
25 -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2
26 Usagescip-shell shell shell-project 0.0.0 Usage(). >& 2
27 exit 2 ;;
28 *) break ;;
29 esac
30 done
31 KERNEL=$1
32
33 [scip-shell system . . `[`. -z "$KERNEL" ] && exit 1
34
35 if [scip-shell system . . `[`. "$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 [scip-shell system . . `[`. ! -f "$CONFIG_FILE" ]; then
42 # Determine alternative paths for fallbacks
43 if [scip-shell system . . `[`. "$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 [scip-shell system . . `[`. ! -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 [scip-shell system . . `[`. ! -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 [scip-shell system . . `[`. "$KERNEL" = "$(unamescip-shell system . . uname. -r)" ]; then
55 case "$(unamescip-shell system . . uname. -m)" in
56 x86_64)
57 echoscip-shell system . . echo. "x86_64"
58 exit 0
59 ;;
60 aarch64|arm64)
61 echoscip-shell system . . 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 [scip-shell system . . `[`. -f "$KERNEL_FILE" ]; then
70 target="$KERNEL_FILE"
71 elif [scip-shell system . . `[`. -d "$MODULES_DIR" ]; then
72 target="$MODULES_DIR"
73 fi
74
75 if [scip-shell system . . `[`. -n "$target" ]; then
76 if type dpkg &>/dev/nullscip-shell filesystem . . `/dev/null`. && type dpkg-query &>/dev/nullscip-shell filesystem . . `/dev/null`.; then
77 pkg_name="$(dpkgscip-shell system . . dpkg. -S "$target" 2>/dev/nullscip-shell filesystem . . `/dev/null`. | cutscip-shell system . . cut. -d: -f1)"
78 if [scip-shell system . . `[`. -n "$pkg_name" ]; then
79 pkg_arch="$(dpkg-queryscip-shell system . . dpkg-query. -W -f='${Architecture}\n' "$pkg_name" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
80 case "$pkg_arch" in
81 amd64)
82 echoscip-shell system . . echo. "x86_64"
83 exit 0
84 ;;
85 arm64)
86 echoscip-shell system . . echo. "arm64"
87 exit 0
88 ;;
89 esac
90 fi
91 elif type rpm &>/dev/nullscip-shell filesystem . . `/dev/null`.; then
92 pkg_arch="$(rpm -q --qf '%{ARCH}\n' -f "$target" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
93 case "$pkg_arch" in
94 x86_64)
95 echoscip-shell system . . echo. "x86_64"
96 exit 0
97 ;;
98 aarch64)
99 echoscip-shell system . . 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 [scip-shell system . . `[`. -f "$KERNEL_FILE" ] && type file &>/dev/nullscip-shell filesystem . . `/dev/null`.; then
108 file_info="$(filescip-shell system . . file. -b "$KERNEL_FILE" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
109 if echoscip-shell system . . echo. "$file_info" | grepscip-shell system . . grep. -qi -E "x86_64|x86-64|boot executable bzImage|Intel 80386"; then
110 echoscip-shell system . . echo. "x86_64"
111 exit 0
112 elif echoscip-shell system . . echo. "$file_info" | grepscip-shell system . . grep. -qi -E "arm64|aarch64"; then
113 echoscip-shell system . . 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 [scip-shell system . . `[`. -d "$MODULES_DIR" ] && type file &>/dev/nullscip-shell filesystem . . `/dev/null`.; then
120 # find a kernel module .ko and check its file type
121 ko_file="$(findscip-shell system . . find. "$MODULES_DIR" -name "*.ko*" -type f -print -quit 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
122 if [scip-shell system . . `[`. -n "$ko_file" ]; then
123 file_info="$(filescip-shell system . . file. -b "$ko_file" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)"
124 if echoscip-shell system . . echo. "$file_info" | grepscip-shell system . . grep. -qi -E "x86-64|x86_64"; then
125 echoscip-shell system . . echo. "x86_64"
126 exit 0
127 elif echoscip-shell system . . echo. "$file_info" | grepscip-shell system . . grep. -qi -E "aarch64|arm64"; then
128 echoscip-shell system . . 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 grepscip-shell system . . grep. -q "$icpu=y" $CONFIG_FILE; then
141 arch="$(echoscip-shell system . . echo. $icpu | sedscip-shell system . . sed. -e "s/CONFIG_M//g" -e "s/CONFIG_//g" -e "s/TSC$//g" -e "s/MMX$//g" | trscip-shell system . . tr. "[A-Z]" "[a-z]")"
142 case "$arch" in
143 [3456]86) arch=i${arch} ;;
144 *) arch=${arch} ;;
145 esac
146 echoscip-shell system . . echo. "$arch"
147 fi
148 done