一,宿主側修改
qemu增加以下啟動參數
-chardev socket,id=charserial0,path=./kernel-console,server=on,wait=off,logfile=./serial.log
-serial chardev:charserial0
二,虛機側修改
2.1 需增加以下內核啟動參數
cat /proc/cmdline
... console=ttyS0,115200n8
2.2 Android系統側修改
userdebug默認已打開console終端功能,如果是user版本需要手動打開。
init.rc
$ cat init.rc
...
service console /system/bin/sh
class core
console
disabled
user shell
group shell log readproc
seclabel u:r:shell:s0
setenv HOSTNAME console
on init && property:ro.debuggable=1
start console
三,host側通過虛擬串口連接虛機
3.1 通過socat工具連接虛擬串口
socat安裝
$ sudo apt install socat
socat連接,可以正常通過終端交互。
$ sudo socat unix-connect:./kernel-console stdio
console:/ #
console:/ # dmesg
[ 0.000000] Linux version 5.15.111 #2 SMP PREEMPT Wed Nov 29 19:56:34 CST 2023
[ 0.000000] Command line: BOOT_IMAGE=/android-2023-11-29/kernel stack_depot_disable=on cgroup_disable=pressure root=/dev/ram0 console=ttyS0,115200n8 console=ttyS0,115200n8 quiet
[ 0.000000] KERNEL supported cpus:
[ 0.000000] Intel GenuineIntel
[ 0.000000] AMD AuthenticAMD
[ 0.000000] Hygon HygonGenuine
[ 0.000000] Centaur CentaurHauls
[ 0.000000] zhaoxin Shanghai
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
[ 0.000000] x86/fpu: Supporting XSAVE feature 0x200: 'Protection Keys User registers'
[ 0.000000] x86/fpu: xstate_offset[2]: 576, xstate_sizes[2]: 256
[ 0.000000] x86/fpu: xstate_offset[9]: 832, xstate_sizes[9]: 8
[ 0.000000] x86/fpu: Enabled xstate features 0x207, context size is 840 bytes, using 'compacted' format.
[ 0.000000] signal: max sigframe size: 3376
[ 0.000000] BIOS-provided physical RAM map:
[ 0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009ffff] usable
[ 0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000007fffff] usable
[ 0.000000] BIOS-e820: [mem 0x0000000000800000-0x0000000000807fff] ACPI NVS
[ 0.000000] BIOS-e820: [mem 0x0000000000808000-0x000000000080afff] usable
3.2 同時將串口log保存到本地,可以通過命令查看。
$ tail -f serial.log