# android-chroot **Repository Path**: jinchengsong/android-chroot ## Basic Information - **Project Name**: android-chroot - **Description**: 在android上使用chroot来使用linux的发行版,如ubuntu - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-08-28 - **Last Updated**: 2024-08-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 在android中使用chroot安装linux的发行版(ubuntu) ## 摘要 chroot是在Unix和类Unix系统(如Linux)中的操作,用于改变进程的根目录,实现在一个已有的linux操作系统中,启动另一个linnux操作系统的方式(会缺失部分功能)。 通过在android系统中实现chroot,启动其他基于linux的操作系统,扩展android系统的功能,赋予旧手机新的生命力。 ## 思路 1、在基于x86的ubuntu虚拟机中使用qemu模拟arm 2、在模拟的arm环境中进行chroot安装脚本的开发与调试 3、对android手机进行root 4、通过adb的root权限,执行调试好的安装脚本,将ubuntu系统安装到android中 5、通过magisk实现开机启动ubuntu,并能通过ssh登录到这个ubuntu中 ## 过程 1. **安装qemu支持** ```sh # 在x86的ubuntu虚拟机中执行 sudo apt install qemu-user-static -y ``` 2. **chroot安装脚本开发与调试** git地址:[android-chroot: 在android上使用chroot来使用linux的发行版,如ubuntu (gitee.com)](https://gitee.com/jinchengsong/android-chroot) ```sh # 仅列出关键点 #下载busybox,扩展android shell的功能 wget -O busybox https://www.busybox.net/downloads/binaries/1.31.0-defconfig-multiarch-musl/busybox-armv8l #下载最新arm版本的Ubuntu 22.04 base档案系统 wget https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cdimage/ubuntu-base/releases/22.04.4/release/ubuntu-base-22.04-base-arm64.tar.gz tar xpvf ubuntu-base-22.04-base-arm64.tar.gz --numeric-owner #挂载目录 busybox mount --bind /dev $UBUNTUPATH/dev busybox mount --bind /sys $UBUNTUPATH/sys busybox mount --bind /proc $UBUNTUPATH/proc busybox mount -t devpts devpts $UBUNTUPATH/dev/pts busybox chroot ubuntu ``` 在脚本开发过程中,主要问题如下: - ubuntu arm版本的国内镜像问题:使用清华的Ubuntu Ports 软件仓库解决 - chroot下的systemd问题:使用开源的`servicectl`解决 - ssh无法登录问题:新增普通用户解决,root用户登录需要开启指定配置(麻烦) 3. **手机root** ```sh # 下载Magisk_patcher:https://github.com/affggh/Magisk_patcher/releases/ # 下载magisk:https://github.com/topjohnwu/Magisk/releases # 从刷机包中提交boot.img ``` 1、将下载得到magisk安装包,放入Magisk_patcher的prebuilt目录中 2、使用Magisk_patcher对提取的boot.img进行修补,然后通过fastboot刷入修补后的new-boot.img ```sh fastboot flash boot new-boot.img ``` 4. **执行安装脚本** ```sh # 开启root adb root #执行安装 adb push install_chroot_ubuntu22.sh /data adb shell sh chmod +x /data/install_chroot_ubuntu22.sh adb shell sh /data/install_chroot_ubuntu22.sh #执行启动 sh /data/chrootubuntu/startubuntu22.sh & ``` 在windows上测试ssh登录 ```sh ssh jincs@x.x.x.x ``` 5. **配置开机启动** ```sh # 创建一个startubuntu22.sh的软连接,magisk会在开机时进行扫描并启动它 ln -s /data/chrootubuntu/startubuntu22.sh /data/adb/service.d/startubuntu22.sh ``` 6. ***优化ubuntu的性能** ```sh #在adb中,使用stop命令,关闭android系统的进程,重新启动,使用start命令 stop ``` ## 参考 - [解决chroot运行的系统无法使用systemd服务 - 兮陌 (simaek.com)](https://www.simaek.com/archives/274/) - [安卓手机在Termux基于chroot运行Ubuntu的rootfs|冰白寒祭的博客 (bingbaihanji.com)](https://www.bingbaihanji.com/archives/AndroidRunsLinuxRootfs) - [在 ubuntu@x86 用 chroot 到 ARM 平台的 rootfs / VMWare/Linux/Ubuntu/Fedora/CentOS/U-BOOT / WhyCan Forum(哇酷开发者社区)](https://whycan.com/t_3251.html) - [ubuntu-ports | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror](https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu-ports/) - [affggh/Magisk_patcher: Patch boot image with magisk on windows/linux/macos (github.com)](https://github.com/affggh/Magisk_patcher) - [topjohnwu/Magisk: The Magic Mask for Android (github.com)](https://github.com/topjohnwu/Magisk) - [ChrootOnAndroid - Debian Wiki](https://wiki.debian.org/ChrootOnAndroid)