2025/6/19

Cuttlefish Android Emulator

ref: 原來在 aosp 中的 emulator (goldfish) 在run aosp android14 的時候, qemu x86 kernel 一邊沒 lz4,一邊 ramfs 好像不一樣。
然後 aosp source project site 說明 竟然都寫 cuttlefish 了。

cuttlefish 已經是獨立的 project,clone 下來後,build 成 deb,需要 libtinfo5,所以ubuntu24.04 用 libtinfo6 會 build fail。
只好download libtinfo5 的 deb 下來 install,才能 build 成功。
tools/buildutils/build_packages.sh

build 完會有這麼多 deb..
  • cuttlefish-orchestration_1.13.0_amd64.deb : 在 google 的 雲端server 用的(?)
  • cuttlefish-base_1.13.0_amd64.deb : 這個就是 emulator 的 deb
  • cuttlefish-integration_1.13.0_amd64.deb : 跟google 雲端上的 emulator溝通用的一些tool
  • base/ cuttlefish-common-build-deps_1.13.0_amd64.deb :
  • cuttlefish-user_1.13.0_amd64.deb : web server 版的 emulator,會run 成一個 web server,用 browser 來操作
  • /frontend/cuttlefish-frontend-build-deps_1.13.0_all.deb :
  • cuttlefish-common_1.13.0_amd64.deb : [DEPRECATED]
所以把 base 和 user 安裝(用 apt),之後,會產生 幾個 group,要把自己加進去:
sudo usermod -aG kvm,cvdnetwork,render $USER
然後因為會產生 /dev/ 下的node,所以要 reboot.

準備 system image,從 aosp build, target 是 aosp_cf_x86_64_phone-trunk-userdebug
build 完aosp source code,就會有 launch_avd 這個 command.
follow google 說明直接 launch 的話,會有 Error:
launch_avd
....
ailed to connect:No such device
[2025-06-19T10:09:00.523729147+00:00 ERROR crosvm] exiting with error 1: the architecture failed to build the vm

Caused by:
    failed to create a PCI root hub: failed to create proxy device: Failed to configure tube: failed to receive packet: Connection reset by peer (os error 104)
Detected unexpected exit of monitored subprocess /home/charles-chang/aosp/out/host/linux-x86/bin/process_restarter
Subprocess /home/charles-chang/aosp/out/host/linux-x86/bin/process_restarter (16314) has exited with exit code 1
Failed to connect:No such device
Client closed the connection
Client closed the connection
Client closed the connection
[2025-06-19T10:09:01.549915546+00:00 ERROR crosvm] exiting with error 1: the architecture failed to build the vm

Caused by:
    failed to create a PCI root hub: failed to create proxy device: Failed to configure tube: failed to receive packet: Connection reset by peer (os error 104)
....
不知道是什麼原因,但是加上 gpumode 之後就可以開起來..
launch_cvd --gpu_mode=gfxstream
...
VIRTUAL_DEVICE_BOOT_STARTED
VIRTUAL_DEVICE_NETWORK_MOBILE_CONNECTED
VIRTUAL_DEVICE_BOOT_COMPLETED
Virtual device booted successfully
依照 google cuttlefish 的說明,這樣啟動的 emulator 是沒有螢幕的,所以用 adb 可以連線。
開啟 webserver 的話,加上 --daemon
launch_cvd --gpu_mode=gfxstream --daemon
...
Virtual device booted successfully
VIRTUAL_DEVICE_BOOT_COMPLETED
這樣會跑在background,用 browser 開啟 http://localhost:8443 就會看到 cvd-1
按下 "connect" 就會出現android 手機畫面..
google 建議用 webrtc,效果會更好。
launch_cvd --gpu_mode=gfxstream -start_webrtc --daemon
停止要用 cvd reset 或 stop_cvd

cuttlefish 使用 virtual network,所以沒版法直接 listen public IP。
如果其他機器要連線,要用 ssh-tunnel:
  • 在 remote mynb 啟動 cuttlefish -start_webrtc
  • 在 local pc 設定 ssl tunnel : ssh -L 50000:localhost:8443 myname@mynb
  • local pc 的 browser 開啟 localhost:50000


cuttlefish 啟動過就會在 $HOME 之下create 一個 cuttlefish 目錄,其中
  • assembly : 設定檔
  • environment : 會有folder env-1,裡面是上次run 的環境變數
  • instance : 會有 folder cvd-1 (emulator id),上次的instance 紀錄
然後在 $HOME 下,會有cuttlefish_XXX link 到 cuttlefish 下的folder/file。
所以一旦emulator 出問題,關掉下次再開也是出問題,用 cvd reset 也無效,就要手動刪掉那些 cvd-1 目錄。

所以在 ~/cuttlefish/instance/cvd-1/ 裡:
  • kernel.log : 就是 kernel message
  • console : 就是 emulator 的 console,會是 link 到 /dev/pty/X,可以用 screen 來開



emulator 是否run 的起來,竟然和host和 android image 有關,使用 Adnroid-14 AOSP build 出來的 aosp_cf image,在有些pc 上可以 run,有些不行,有些要加 --gpu_mode,有些不能加。
而且 run 起來後,app 啟動計次顯示就會發生問題,系統反應也變慢。
但是使用 -b android-latest-release 的image則幾乎所有機器都沒問題,而且只需要加 --start_webrtc 就可以,不用管 --gpu_mode,cuttlefish 會自己偵測。

沒有留言:

張貼留言