Chroot ubuntu 16.04 on android
基于「Android手机中跑Ubuntu系统」。
假定成立条件:
1. ARM架构CPU
2. 拥有ROOT权限
大致流程和最终效果:需要将类似Windows下PE的小系统镜像Ubuntu core放到SD卡中,并进行挂载,然后切入到Ubuntu工作状态。一些Ubuntu core中的命令就可以跑起来了。
这里以比较热门的Ubuntu 16.04版本的Core系统 为例,官方下载ubuntu-base-16.04.3-base-armhf.tar.gz。这仅是一个压缩包,需要将其生成为「镜像」。然后进行下一步,如何生成镜像这里记录一下,但也把生成过的镜像下载地址ubuntu-base-16.04.3-base-armhf.ext2提供以便直接测试使用。
生成系统镜像
准确地讲是生成ext4根文件系统镜像,使用到工具为make_ext4fs
。
# 拷贝bin目录到system/下,为适配Android环境变量(否则会遇到问题1,2)
$ cd ubuntu-base-16.04.3-base-armhf && sudo ln -s . system
# 读取目录大小
$ sudo du -s ubuntu-base-16.04.3-base-armhf
189900 ubuntu-base-16.04.3-base-armhf
# 取一个[大于目录大小][1024倍数的]最小值 (189900/1024 + 1) * 1024 = 190464
# 生成Ext4小系统镜像
make_ext4fs -l 512M ubuntu-base-16.04.3-base-armhf.ext4 ubuntu-base-16.04.3-base-armhf
挂载小系统镜像
# 1. 导入到手机
$ adb push ubuntu-base-16.04.3-base-armhf.ext4 /sdcard/
# 2. [adb shell进入手机,获取root权限][略] 然后进行挂载
cd /sdcard/ && busybox mkdir ubuntu
busybox mount -t ext4 -o loop ubuntu-base-16.04.3-base-armhf.ext4 ubuntu
# 3. 正常情况下可以看到小系统中文件
ls ubuntu
切入Ubuntu运行环境
切入运行Ubuntu运行环境主要是使用chroot进行环境切换。
# 1. 切入Ubuntu,以下命令是以ubuntu目录为新的root目录
busybox mount --bind /dev ubuntu/dev
busybox mount --bind /proc ubuntu/proc
busybox mount --bind /sys ubuntu/sys
mkdir -p ubuntu/data/local/tmp
busybox mount --bind /data/local/tmp ubuntu/data/local/tmp
busybox chroot ubuntu
# 2. 运行su命令 注:重要,否则不能运行复杂命令
su
# 3. 运行一些命令,如下图:
查版本
ping 百度IP
ARM版本GCC
遇到的问题
问题1: apt-get cannot connect “failure resolving ‘port.ubuntu.com’”
问题2: Could not create a socket for 91.189.88.150 (f=2 t=1 p=6) - socket (13: Permission denied)
groupadd -g 3001 aid_bt
groupadd -g 3002 aid_bt_net
groupadd -g 3003 aid_inet
groupadd -g 3004 aid_net_raw
groupadd -g 3005 aid_admin
usermod -a -G aid_bt,aid_bt_net,aid_inet,aid_net_raw,aid_admin root
usermod -g aid_inet _apt
echo 'nameserver 114.114.114.114' > /etc/resolv.conf
参考:https://stackoverflow.com/a/36451445/2193455
问题3: apt下载时比较慢,使用国内软件源就会加快
将/etc/apt/sources.list
改成如下内容
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-updates main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-security main restricted universe multiverse
deb http://mirrors.ustc.edu.cn/ubuntu-ports/ xenial-backports main restricted universe multiverse