前言
业务需要大量模拟器场景进行测试,单纯的在windows开模拟器不现实,本想着拉取AOSP源码进行编译然后制作镜像,后来发现开源redroid有现成镜像,于是拿来直接使用并在debian11下成功实现多开
系统版本:debian11
内核:5.10.0-26-amd64
docker 24.0.7
注:旧贴麻烦大大帮忙删除下,帖子挪动到安卓这,看到的人会更多,希望对大家有帮助
旧贴:https://www.52pojie.cn/thread-1882484-1-1.html
1.方案选择
1.1.方案一k8s集群部署(开源镜像为基础)
测试结论:需要在宿主机上配置binder或BinderFS即可。注意直接用k8s可以直接测通。如果你有自己的构建机以及统一维护的发布系统可以使用这套。(测试通过)
1.2.方案二物理机docker部署发布
测试结论:新物理机 配置docker、binder或BinderFS就可使用。轻量级适合独立维护。(测试通过)
2.环境配置(物理机上执行)
2.1开启binder(只能开启一个模拟器)
sudo modprobe binder_linux devices=binder1,binder2,binder3,binder4,binder5,binder6
chmod 666 /dev/binder*
2.2开启BinderFS(可以开启多个模拟器)
# 1.安装必要的工具
sudo apt-get update
sudo apt-get install build-essential libncurses5-dev flex bison libssl-dev libelf-dev bc
# 2.下载当前Debian内核版本的源代码
sudo apt-get source linux-source
# 3.进入内核源码目录
cd linux-*
# 4.配置内核,运行配置工具以启用BinderFS支持。使用 make menuconfig 或 make nconfig:
make menuconfig
# 出现选项就按这个流程走 General setup -> Pseudo filesystems -> Binder IPC and BinderFS file system 启用这些选项,然后保存并退出
# 也可以检查下源码目录下.config文件,并设置一下内容
CONFIG_ANDROID_BINDER_IPC=y
CONFIG_ANDROID_BINDERFS=y
CONFIG_ANDROID_BINDER_DEVICES="binder,hwbinder,vndbinder"
CONFIG_ANDROID_BINDER_IPC_SELFTEST=y
# 5.编译内核
make -j$(nproc)
# 6.安装新内核
sudo make modules_install
sudo make install
# 7.重新启动
sudo reboot
2.3配置Magisk(android root,如有需要)
#下载magisk 并拆解出libmagisk64.so与libbusybox.so
mkdir ~/MagiskOnRedroid
cd ~/MagiskOnRedroid
find -maxdepth 1 -iname "magisk*" -not -name "*.apk" -exec rm -r {} \;
magisk_file="app-debug.apk"
# download Magisk 9b61bdfc(25201) debug as you did i guess
# 下载magisk apk
if [ ! -f $magisk_file ]; then
wget "https://cdn.jsdelivr.net/gh/topjohnwu/magisk-files@1cea72840fbf690f9a95512d03721f6a710fe02e/app-debug.apk"
fi
# because my machine x64 i will choose x86_64
# 解压并提取两个so,根据系统镜像位数来提取
unzip -j $magisk_file "lib/x86_64/libmagisk64.so" -d magisk
unzip -j $magisk_file "lib/x86_64/libbusybox.so" -d magisk
# 重命名
mv -v magisk/libmagisk64.so magisk/magisk
mv -v magisk/libbusybox.so magisk/busybo
# 压缩,将magisk app 和依赖so都放到一起,供后续脚本使用
tar --transform 's/.*\///g' -cf ~/magisk.tar --absolute-names $( find ~/MagiskOnRedroid | grep -E "magisk/|app-debug.apk$" )
2.3.1.配置magisk所需脚本-remove.rc
cat <<\EOF > ~/remove.rc
on early-init
export PATH /sbin:/product/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin:/system_ext/bin:/system/bin:/system/xbin:/odm/bin:/vendor/bin:/vendor/xbin
chmod 0700 /magisk.tar
chown root root /magisk.tar
chmod 0700 /setup.sh
chown root root /setup.sh
exec root root -- /setup.sh
service magisk-d /sbin/magisk --daemon
user root
oneshot
on boot
start magisk-d
on post-fs-data
start logd
rm /dev/.magisk-unblock
start s1
wait /dev/.magisk-unblock 5
rm /dev/.magisk-unblock
service s1 /sbin/magisk --post-fs-data
user root
oneshot
service s2 /sbin/magisk --service
class late_start
user root
oneshot
on property:sys.boot_completed=1
exec /sbin/magisk --boot-complete
on property:init.svc.zygote=restarting
exec /sbin/magisk --zygote-restart
on property:init.svc.zygote=stopped
exec /sbin/magisk --zygote-restart
EOF
sudo chmod 644 ~/remove.rc
sudo chown root:root ~/remove.rc
2.3.2.配置magisk所需脚本-setup.sh
cat <<\EOF > ~/setup.sh
#!/system/bin/sh
# rm /system/fonts/NotoColorEmoji.ttf
tmpPushed=/magisk
rm -rf $tmpPushed
mkdir $tmpPushed
tar -xvf /magisk.tar --no-same-owner -C $tmpPushed
umount /magisk.tar ; rm -v /magisk.tar
mkdir /sbin
chown root:root /sbin
# chmod 0700 /sbin
chmod 0751 /sbin
cp $tmpPushed/magisk /sbin/
cp $tmpPushed/app-debug.apk /sbin/stub.apk
find /sbin -type f -exec chmod 0755 {} \;
find /sbin -type f -exec chown root:root {} \;
# add /sbin
# /sbin/
# ├── magisk
# └── stub.apk
ln -f -s /sbin/magisk /system/xbin/su
mkdir /product/bin
chmod 751 /product/bin
ln -f -s /sbin/magisk /product/bin/su
# add su (override `/system/xbin/su`)
# /product/bin/
# └── su -> /sbin/magisk
mkdir -p /data/adb/magisk
chmod 700 /data/adb
mv $tmpPushed/busybox /data/adb/magisk/
chmod -R 755 /data/adb/magisk
chmod -R root:root /data/adb/magisk
# /data/adb/
# ├── magisk
# │ └── busybox
# rm -rf $tmpPushed
EOF
sudo chmod 700 ~/setup.sh
sudo chown root:root ~/setup.sh
3.配置文件
3.1.k8s sts.yml
# /dev/binder 如果对内核进行编译ll饿是不需要添加这个
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: redroid11
spec:
replicas: 1
selector:
matchLabels:
app: redroid11
serviceName: redroid
template:
metadata:
labels:
app: redroid11
spec:
terminationGracePeriodSeconds: 0
nodeName: xxxxx.xxxx.xxx.xx
containers:
- name: redroid
image: redroid/redroid:11.0.0-latest
securityContext:
privileged: true
capabilities:
add:
- SYS_ADMIN
volumeMounts:
- mountPath: /dev/binder
name: binder1
- mountPath: /dev/hwbinder
name: binder2
- mountPath: /dev/vndbinder
name: binder3
- name: data
mountPath: /data
readinessProbe:
exec:
command:
- /system/bin/sh
- -c
- test "1" = "`/system/bin/getprop sys.boot_completed`"
initialDelaySeconds: 5
stdin: true
tty: true
volumes:
- name: data
emptyDir: {}
- name: binder1
hostPath:
path: /dev/binder4
- name: binder2
hostPath:
path: /dev/binder5
- name: binder3
hostPath:
path: /dev/binder6
---
apiVersion: v1
kind: Service
metadata:
name: redroid
spec:
ports:
- port: 5555
targetPort: 5555
selector:
app: redroid11
3.2.docker启动命令
# 如果开启BinderFS就不需要挂载/dev/binder了
# 开源镜像启动命令
docker run -itd --rm --privileged \
-v /dev/binder1:/dev/binder \
-v /dev/binder2:/dev/hwbinder \
-v /dev/binder3:/dev/vndbinder \
# 磁盘挂载,映射安卓存储的路径
-v ~/data11:/data \
-p 5555:5555 \
<镜像名称>
# 多启动调整端口即可
docker run -itd --rm --privileged \
# 磁盘挂载,映射安卓存储的路径
-v ~/data11:/data \
-p 5556:5555 \
<镜像名称>
# 需要使用magisk就挂载进去,.rc文件会在系统初始化的时候就加载,并启动magisk服务。
-v ~/remove.rc:/vendor/etc/init/remove.rc \
-v ~/setup.sh:/setup.sh \
-v ~/magisk.tar:/magisk.tar \
4.检测
# 本机测试安装
apt install adb
# 查看设备,端口为启动的端口
adb devices或adb connect localhost:5555
#安装scrcpy 可视化查看
apt install scrcpy
scrcpy -s [设备号||localhost:5555]
看到以下界面即为成功
20240301更新
宿主机如果没有开启网桥,会造成设备无法链接问题。
#1.先把网桥开起来
modprobe bridge
apt-get install bridge-utils
#2.调整docker里默认网桥信息
*/docker/daemon.json
"bridge": "",
"bip": "xxx.xxx.xxx.xxx/xx",
#3.把docker热重启关闭
"live-restore": false
#4.重启docker
#5.确认ok后恢复热重启配置
live-restore": true