编译兼容内核模块的版本

前篇 已经成功编译出了适合 avd 使用的内核,然而同时需要编译兼容的内核模块,过于复杂。经过一番探索,发现只要找到正确的源码,还是能编译出与发布的 avd 镜像兼容的内核。

认识版本号

还是回到 /proc/version 上,内核的版本号提供了很多信息,比如 -g 后跟着的是一个 12 位 16 进制数,表示编译的内核的源码树的提交 hash ,此外还有编译器版本。

1
Linux version 6.1.23-android14-4-00257-g7e35917775b8-ab9964412 (build-user@build-host) (Android (9796371, based on r487747) clang version 17.0.0 (https://android.googlesource.com/toolchain/llvm-project d9f89f4d16663d5012e5c09495f3b30ece3d2362), LLD 17.0.0) #1 SMP PREEMPT Mon Apr 17 20:50:58 UTC 2023

然而只有这些信息并不足以找到所有的代码,因为 repo clone 下来的实际上是多个仓库,而提交 hash 只是 common 仓库下的。

注意到 -ab 后面跟着一串数字,实际上对应了 Android CI 的 id 。

1
2
3
4
5
6
7
8
# common/scripts/setlocalversion

# finally, add the abXXX number if BUILD_NUMBER is set
if test -n "${BUILD_NUMBER}"; then
res="$res-ab${BUILD_NUMBER}"
fi

echo "$res"

可以看到 Android CI 中有分支 aosp_kernel-common-android14-6.1

包含了数个内核 CI 构建,其中 kernel_virt_x86_64 才是我们需要的内核。随便点开一个 artifcat ,url 形如:

1
https://ci.android.com/builds/submitted/11381674/kernel_virt_x86_64/latest

把 11381674 换成我们的内核的 build number 9964412 ,即可找到这一次构建。

https://ci.android.com/builds/submitted/9964412/kernel_virt_x86_64/latest

下载 bzImage ,发现和 avd 的 kernel-ranchu 还是有一定出入,不过是可以启动的。因此,我们只要根据 CI 中输出的信息,找到构建时各个仓库的提交即可。

CI BuildInfo

https://ci.android.com/builds/submitted/9964412/kernel_virt_x86_64/latest/view/BUILD_INFO

(文末附有 json 原文)

parsed_manifest 提供了构建时的 manifest 对应的所有仓库的 revision ,比如 common ,可以看到提交 hash 和我们的内核是一致的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"linkFiles": [
{
"dest": ".source_date_epoch_dir",
"src": "."
}
],
"name": "kernel/common",
"path": "common",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "7e35917775b8b3e3346a87f294e334e258bf15e6"
},

name 是远程仓库的 path ,path 是本地的 path 。由于不知道如何用 repo 处理这些,因此我们手动 cd 到 path 对应的目录,检出对应的 revision 。

1
2
3
4
cd $PATH
git fetch aosp $REVISION
git branch my $REVISION
git checkout my

编译 virtual device kernel

build info 还告诉了我们如何构建这种 kernel_virt :

1
tools/bazel run --verbose_failures --jobs=%cpu% --make_jobs=%cpu% --config=android_ci --profile=%dist_dir%/logs/command.profile.json //common-modules/virtual-device:virtual_device_x86_64_dist -- --dist_dir=%dist_dir% --flat

我们应该运行 //common-modules/virtual-device:virtual_device_x86_64_dist

实际上文档也有提到,不过我以为只是构建模块。

1
tools/bazel run --config=fast --config=stamp --lto=none //common-modules/virtual-device:virtual_device_x86_64_dist -- --dist_dir=virt

如此一来构建的内核就和已有的模块兼容了,我们也可以顺利地将 KernelSU 集成进去。

附录

json
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
{
"best_target_priority": 1,
"bid": "9964412",
"branch": "aosp_kernel-common-android14-6.1",
"branch_priority": 1,
"build_configs": {},
"build_dependencies": [],
"build_type": "submitted",
"dependency_targets": [],
"device_dir": "/buildbot/src/android/common-android14-6.1",
"docker_image": null,
"enable_docker": true,
"git-pull": null,
"git-server": "https://android.googlesource.com",
"hostname": "abfarm-2004-0434",
"inc-build": false,
"java-version": null,
"last_logfile": null,
"out_dir": "/buildbot/src/android/common-android14-6.1/out",
"parsed_manifest": {
"parsedDefault": {
"remote": "aosp",
"revision": "master",
"syncJ": "4"
},
"projects": [
{
"linkFiles": [
{
"dest": "tools/bazel",
"src": "kleaf/bazel.sh"
},
{
"dest": "WORKSPACE",
"src": "kleaf/bazel.WORKSPACE"
},
{
"dest": "build/build.sh",
"src": "build.sh"
},
{
"dest": "build/build_abi.sh",
"src": "build_abi.sh"
},
{
"dest": "build/build_test.sh",
"src": "build_test.sh"
},
{
"dest": "build/build_utils.sh",
"src": "build_utils.sh"
},
{
"dest": "build/config.sh",
"src": "config.sh"
},
{
"dest": "build/envsetup.sh",
"src": "envsetup.sh"
},
{
"dest": "build/_setup_env.sh",
"src": "_setup_env.sh"
},
{
"dest": "build/multi-switcher.sh",
"src": "multi-switcher.sh"
},
{
"dest": "build/abi",
"src": "abi"
},
{
"dest": "build/static_analysis",
"src": "static_analysis"
}
],
"name": "kernel/build",
"path": "build/kernel",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "b0377a072bb3f78cdacfd6d809914a9d1b0c0148"
},
{
"linkFiles": [
{
"dest": ".source_date_epoch_dir",
"src": "."
}
],
"name": "kernel/common",
"path": "common",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "7e35917775b8b3e3346a87f294e334e258bf15e6"
},
{
"name": "kernel/tests",
"path": "kernel/tests",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "c90a1c1b226b975cc31e709fa96fc1c6ecdbe272"
},
{
"name": "kernel/configs",
"path": "kernel/configs",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "52a7267d6a9f9efabf3cb43839bb5e7f7ff05be3"
},
{
"name": "kernel/common-modules/virtual-device",
"path": "common-modules/virtual-device",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "0d03de3246301028775f05ea388c2c444344a268"
},
{
"cloneDepth": "1",
"name": "platform/prebuilts/clang/host/linux-x86",
"path": "prebuilts/clang/host/linux-x86",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "4f7e5adc160ab726ac5bafb260de98e612904c50"
},
{
"cloneDepth": "1",
"name": "platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8",
"path": "prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "f7b0d5b0ee369864d5ac3e96ae24ec9e2b6a52da"
},
{
"cloneDepth": "1",
"name": "platform/prebuilts/build-tools",
"path": "prebuilts/build-tools",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "dc92e06585a7647bf739a2309a721b82fcfa01d4"
},
{
"cloneDepth": "1",
"name": "platform/prebuilts/clang-tools",
"path": "prebuilts/clang-tools",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "5611871963f54c688d3ac49e527aecdef21e8567"
},
{
"cloneDepth": "1",
"name": "kernel/prebuilts/build-tools",
"path": "prebuilts/kernel-build-tools",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "2597cb1b5525e419b7fa806373be673054a68d29"
},
{
"name": "platform/system/tools/mkbootimg",
"path": "tools/mkbootimg",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "2680066d0844544b3e78d6022cd21321d31837c3"
},
{
"cloneDepth": "1",
"name": "platform/prebuilts/bazel/linux-x86_64",
"path": "prebuilts/bazel/linux-x86_64",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "4fdb9395071ff22118311d434d697c2b6fd887b4"
},
{
"cloneDepth": "1",
"name": "platform/prebuilts/jdk/jdk11",
"path": "prebuilts/jdk/jdk11",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "491e6aa056676f29c4541f71bd738e4e876e4ba2"
},
{
"cloneDepth": "1",
"name": "toolchain/prebuilts/ndk/r23",
"path": "prebuilts/ndk-r23",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "19ac7e4eded12adb99d4f613490dde6dd0e72664"
},
{
"name": "platform/external/bazel-skylib",
"path": "external/bazel-skylib",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "f998e5dc13c03f0eae9e373263d3afff0932c738"
},
{
"name": "platform/build/bazel_common_rules",
"path": "build/bazel_common_rules",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "707b2c5fe3d0d7d934a93e00a8a4062e83557831"
},
{
"name": "platform/external/stardoc",
"path": "external/stardoc",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "e83f522ee95419e55d2c5654aa6e0143beeef595"
},
{
"name": "platform/external/python/absl-py",
"path": "external/python/absl-py",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "393d0b1e3f0fea3e95944a2fd3282cc9f76d4f14"
},
{
"name": "kernel/manifest",
"path": "kernel/manifest",
"remote": {
"fetch": "https://android.googlesource.com/"
},
"revision": "0c7921f01bec06d4957c8b36bb0efa84727c8fd6"
}
],
"remotes": [
{
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
}
],
"superproject": {
"name": "kernel/superproject",
"remote": {
"fetch": "https://android.googlesource.com/",
"name": "aosp",
"review": "https://android.googlesource.com/"
},
"revision": "cbe96ab89d71a8b85db7c868325242c36346893f"
}
},
"platform": "linux",
"presubmit_incremental_build": false,
"proof_build": false,
"repo-dict": {
"kernel/build": "b0377a072bb3f78cdacfd6d809914a9d1b0c0148",
"kernel/common": "7e35917775b8b3e3346a87f294e334e258bf15e6",
"kernel/common-modules/virtual-device": "0d03de3246301028775f05ea388c2c444344a268",
"kernel/configs": "52a7267d6a9f9efabf3cb43839bb5e7f7ff05be3",
"kernel/manifest": "0c7921f01bec06d4957c8b36bb0efa84727c8fd6",
"kernel/prebuilts/build-tools": "2597cb1b5525e419b7fa806373be673054a68d29",
"kernel/tests": "c90a1c1b226b975cc31e709fa96fc1c6ecdbe272",
"platform/build/bazel_common_rules": "707b2c5fe3d0d7d934a93e00a8a4062e83557831",
"platform/external/bazel-skylib": "f998e5dc13c03f0eae9e373263d3afff0932c738",
"platform/external/python/absl-py": "393d0b1e3f0fea3e95944a2fd3282cc9f76d4f14",
"platform/external/stardoc": "e83f522ee95419e55d2c5654aa6e0143beeef595",
"platform/prebuilts/bazel/linux-x86_64": "4fdb9395071ff22118311d434d697c2b6fd887b4",
"platform/prebuilts/build-tools": "dc92e06585a7647bf739a2309a721b82fcfa01d4",
"platform/prebuilts/clang-tools": "5611871963f54c688d3ac49e527aecdef21e8567",
"platform/prebuilts/clang/host/linux-x86": "4f7e5adc160ab726ac5bafb260de98e612904c50",
"platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.17-4.8": "f7b0d5b0ee369864d5ac3e96ae24ec9e2b6a52da",
"platform/prebuilts/jdk/jdk11": "491e6aa056676f29c4541f71bd738e4e876e4ba2",
"platform/system/tools/mkbootimg": "2680066d0844544b3e78d6022cd21321d31837c3",
"toolchain/prebuilts/ndk/r23": "19ac7e4eded12adb99d4f613490dde6dd0e72664"
},
"repo-init-branch": "common-android14-6.1",
"repo-manifest": "kernel/manifest",
"repo_manifest_file": "default.xml",
"reset_image_build": false,
"rollout": [],
"src_ctrl": "repo",
"sync_finish_time": 1681844162.2685213,
"sync_start_time": 1681844077.0726051,
"sync_succeed": 1,
"target": {
"apiary_target": "kernel_virt_x86_64",
"dir_list": [
"hci_vhci.ko",
"modules.builtin",
"usbip-core.ko",
"btusb.ko",
"gki-info.txt",
"manifest_9964412.xml",
"virt_wifi.ko",
"mt76x2u.ko",
"mac80211_hwsim.ko",
"virtual_device_x86_64_modules",
"repo.prop",
"mt76x0-common.ko",
"mt76-usb.ko",
"virtio_pci.ko",
"virtio_balloon.ko",
"vmlinux.symvers",
"mt76x2-common.ko",
"gs_usb.ko",
"mt76x0u.ko",
"mt76.ko",
"vhci-hcd.ko",
"system_dlkm.modules.load",
"virtio_pci_modern_dev.ko",
"goldfish_pipe.ko",
"vkms.ko",
"virtio_snd.ko",
"btintel.ko",
"kernel_x86_64_modules",
"modules.builtin.modinfo",
"system_dlkm_staging_archive.tar.gz",
"virtio_net.ko",
"virtio_input.ko",
"vmlinux",
"system_heap.ko",
"boot.img",
"vmw_vsock_virtio_transport.ko",
"goldfish_sync.ko",
"System.map",
"kernel-uapi-headers.tar.gz",
"virtio-rng.ko",
"mt76x02-usb.ko",
"kernel-headers.tar.gz",
"test_meminit.ko",
"modules.load",
"test_mappings.zip",
"pulse8-cec.ko",
"goldfish_address_space.ko",
"multiple.intoto.jsonl",
"initramfs.img",
"rtc-test.ko",
"dummy-cpufreq.ko",
"virtio_pmem.ko",
"virtio_pci_legacy_dev.ko",
"virtio_console.ko",
"btrtl.ko",
"nd_virtio.ko",
"applied.prop",
"boot-img.tar.gz",
"goldfish_battery.ko",
"virtio_blk.ko",
"virtio-gpu.ko",
"dummy_hcd.ko",
"system_dlkm.modules.blocklist",
"net_failover.ko",
"virtio_dma_buf.ko",
"mt76x02-lib.ko",
"system_dlkm.img",
"bzImage",
"failover.ko",
"logs/git.log",
"logs/execute_build_result.textproto",
"logs/git_thread.log",
"logs/command.profile.json",
"logs/build_tee_error.log",
"logs/buildbot_trace.trace",
"logs/execute_build_config.textproto",
"logs/git_metrics.textproto",
"logs/SUCCEEDED",
"logs/STARTED",
"logs/build.log",
"BUILD_INFO"
],
"dist-dir": "/buildbot/dist_dirs/aosp_kernel-common-android14-6.1-linux-kernel_virt_x86_64/9964412",
"name": "kernel_virt_x86_64",
"rules": [
[
"tools/bazel run --verbose_failures --jobs=80 --make_jobs=80 --config=android_ci --profile=/buildbot/dist_dirs/aosp_kernel-common-android14-6.1-linux-kernel_virt_x86_64/9964412/logs/command.profile.json //common-modules/virtual-device:virtual_device_x86_64_dist -- --dist_dir=/buildbot/dist_dirs/aosp_kernel-common-android14-6.1-linux-kernel_virt_x86_64/9964412 --flat",
"build.log",
true,
false
]
],
"storage_path": "/bigstore/android-build/builds/aosp_kernel-common-android14-6.1-linux-kernel_virt_x86_64/9964412/c6318ad0db1b918a3a7cb10a3a2ef07e68f5615af82c2179b252a7440889de29",
"target_finish_time": 1681844566.1232665,
"target_start_time": 1681844163.1656117,
"target_status": 1
},
"trident_usage": {
"external_disk_no_space": false,
"trident_used": false
},
"use_goma": false,
"worknode": {
"containerId": "L95800000959995395",
"creationTimeMillis": "1681839400348",
"currentAttempt": {
"attemptId": "IsVTpUkjTSk0VVc+HIsF7Q==",
"progressMessages": [
{
"displayMessage": "Build 9964412 for node L95800000959995395:N80600001359138780 has been inserted",
"messageString": "Build 9964412 for node L95800000959995395:N80600001359138780 has been inserted",
"timeMillis": "1681839407679"
}
],
"startTimeMillis": "1681839402883"
},
"heartbeatTimeMillis": "15724800000",
"id": "L95800000959995395:N80600001359138780",
"inputEdges": [
{
"neighborId": "L95800000959995395:N85500001359138757"
}
],
"isFinal": false,
"lastUpdatedMillis": "1681839407737",
"nodeClass": "postsubmit",
"retryStatus": {
"maximumRetries": 4,
"retryCount": 0
},
"revision": "P9C4ksNppTyoTHpyqwANsQ==",
"status": "scheduled",
"workExecutorType": "submittedBuild",
"workParameters": {
"submittedBuild": {
"branch": "aosp_kernel-common-android14-6.1",
"branchConfig": {
"sloTier": "bestEffort"
},
"buildId": "9964412",
"gerritPollerTimestamp": "1681839297836",
"manuallyTriggered": false,
"syncTimeoutSecond": 2700,
"target": {
"buildCommands": [
"tools/bazel run --verbose_failures --jobs=%cpu% --make_jobs=%cpu% --config=android_ci --profile=%dist_dir%/logs/command.profile.json //common-modules/virtual-device:virtual_device_x86_64_dist -- --dist_dir=%dist_dir% --flat"
],
"buildPlatform": "linux",
"disabled": false,
"incrementalBuild": false,
"launchcontrolName": "kernel_virt_x86_64",
"name": "kernel_virt_x86_64",
"platformVersion": "docker",
"priority": "high",
"product": "kernel_virt_x86_64",
"target": "kernel_virt_x86_64"
}
}
},
"workerId": "buildassembler_buildnotifier_workers"
}
}