Skip to content

Instantly share code, notes, and snippets.

@lateautumn233
Last active April 12, 2024 13:15
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lateautumn233/939be0528a2cc34af66864bead58e68a to your computer and use it in GitHub Desktop.
Save lateautumn233/939be0528a2cc34af66864bead58e68a to your computer and use it in GitHub Desktop.
在安卓上运行Lxc容器

Lxc on Android 📱

1. 编译内核

1.1. 调整内核配置

使用此仓库快捷添加配置

git fetch https://github.com/lateautumn233/android_kernel_docker main
git merge -s ours --no-commit --allow-unrelated-histories --squash FETCH_HEAD
git read-tree --prefix=docker -u FETCH_HEAD
echo "source \"docker/Kconfig\"" >> arch/arm64/Kconfig
git commit -m -a "Imported docker/ from https://github.com/lateautumn233/android_kernel_docker"

然后再自行编译 🥵🥵🥵

1.2. 内核补丁

--- orig/net/netfilter/xt_qtaguid.c     2020-05-12 12:13:14.000000000 +0300
+++ my/net/netfilter/xt_qtaguid.c       2019-09-15 23:56:45.000000000 +0300
@@ -737,7 +737,7 @@
{
        struct proc_iface_stat_fmt_info *p = m->private;
        struct iface_stat *iface_entry;
-       struct rtnl_link_stats64 dev_stats, *stats;
+       struct rtnl_link_stats64 *stats;
        struct rtnl_link_stats64 no_dev_stats = {0};  
@@ -745,13 +745,8 @@
        current->pid, current->tgid, from_kuid(&init_user_ns, current_fsuid()));
        iface_entry = list_entry(v, struct iface_stat, list);
+       stats = &no_dev_stats; 
-       if (iface_entry->active) {
-               stats = dev_get_stats(iface_entry->net_dev,
-                                     &dev_stats);
-       } else {
-               stats = &no_dev_stats;
-       }
        /*
         * If the meaning of the data changes, then update the fmtX
         * string.

具体编译内核 以下省略n字

2. 使用termux安装lxc

pkg update && pkg install root-repo && apt install lxc tsu

2.1. 配置并启动lxc容器

首先需要挂载cgroup

tsu
mount -t tmpfs -o mode=755 tmpfs /sys/fs/cgroup
mkdir -p /sys/fs/cgroup/devices
mount -t cgroup -o devices cgroup /sys/fs/cgroup/devices

lxc-setup-cgroups #可能不需要

创建容器

# 创建之前修改网络配置
请参考下面2.1配置网络

lxc-create -t download -n my-container -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images --no-validate

# 依次填入发行版 版本号 架构
# 然后启动
lxc-start -n my-container -d -F

# 很快啊就报错了
#Failed to mount cgroup at /sys/fs/cgroup/systemd: Operation not permitted
#[!!!!!!] Failed to mount API filesystems.
#Exiting PID 1...

# 解决方法1
echo "lxc.init.cmd = /sbin/init systemd.unified_cgroup_hierarchy=0" >> $PREFIX/share/lxc/config/common.conf.d/systemd.conf
# 解决方法2
mkdir -p /sys/fs/cgroup/systemd && mount -t cgroup cgroup -o none,name=systemd /sys/fs/cgroup/systemd

调整lxc容器密码

chroot path /bin/su -
lxc-attach -n my-container passwd

2.1. 配置网络

配置网络目前测试通过的有2种

2.1.1 使用host模式

在创建容器之前进行以下调整

sed -i 's/lxc\.net\.0\.type = empty/lxc.net.0.type = none/g' $PREFIX/etc/lxc/default.conf

如果你想host下运行docker接下来还要做以下调整 在lxc容器内输入 !!!

getway=$(ip route get 8.8.8.8 | awk '{ for(i=1; i<=NF; i++) { if($i == "via") { print $(i+1); break; } } }')

sudo ip rule add pref 1 from all lookup main
sudo ip rule add pref 2 from all lookup default
sudo ip route add default via $getway dev wlan0
sudo ip rule add from all lookup main pref 30000
update-alternatives --set iptables /usr/sbin/iptables-legacy
iptables -t filter -F

#如果可以启动了就不需要再使用下面命令
iptables -t filter -X

hos模式一般可以自动解析dns当然也不排除意外情况 如果无法解析DNS

echo 8.8.8.8 > /etc/resolv.conf
systemctl stop systemd-resolved
systemctl disable systemd-resolved
# systemctl enable systemd-networkd

2.1.1 使用veth模式

以下第一次需要配置

apt install dnsmasq wget -y
#修改默认lxc配置
echo -e "lxc.net.0.type = veth \
\nlxc.net.0.link = lxcbr0 \
\nlxc.net.0.flags = up \
\nlxc.net.0.hwaddr = 00:16:3e:xx:xx:xx" \
> $PREFIX/etc/lxc/default.conf
# 修改lxc-net
wget https://qiuqiu233.top/d/linux-deploy/lxc/lxc-net -O $PREFIX/libexec/lxc/lxc-net && chmod a+x $PREFIX/libexec/lxc/lxc-net

每次重启设备后想要使用veth请

$PREFIX/libexec/lxc/lxc-net start

dns无法解析请参考上面的host的修改dns

喵喵喵喵喵

参考文章:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment