Introduction:
LXC is an operating system-level virtualization technology that provides a user space interface for Linux kernel container functionality. It packages the application software system into a software container, which includes the code of the application software itself, as well as the required operating system kernel and libraries.
1. Kernel Compilation#
1.1. Adjust Kernel Configuration#
Use this repository to quickly add configurations.
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"
Then compile on your own.
🥵🥵🥵
1.2. Kernel Patch#
- Fix possible panic situations
--- 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.
- Fix missing cpuset prefix in cgroup
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -3786,6 +3786,10 @@ static int cgroup_add_file(struct cgroup_subsys_state *css, struct cgroup *cgrp,
cfile->kn = kn;
spin_unlock_irq(&cgroup_file_kn_lock);
}
+ if (cft->ss && (cgrp->root->flags & CGRP_ROOT_NOPREFIX) && !(cft->flags & CFTYPE_NO_PREFIX)) {
+ snprintf(name, CGROUP_FILE_NAME_MAX, "%s.%s", cft->ss->name, cft->name);
+ kernfs_create_link(cgrp->kn, name, kn);
+ }
return 0;
}
Specifically compile the kernel
The following is omitted.
2. Install LXC using Termux#
pkg update && pkg install root-repo && apt install lxc tsu
2.1. Configure and Start LXC Containers#
First, you need to mount 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 #may not be necessary
Create a container
# Modify the network configuration before creating
Please refer to the following 2.1 network configuration
lxc-create -t download -n my-container -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images --no-validate
# Fill in the distribution version, version number, and architecture in order
# Then start
lxc-start -n my-container -d -F
# It will soon report an error
#Failed to mount cgroup at /sys/fs/cgroup/systemd: Operation not permitted
#[!!!!!!] Failed to mount API filesystems.
#Exiting PID 1...
# Solution 1
echo "lxc.init.cmd = /sbin/init systemd.unified_cgroup_hierarchy=0" >> $PREFIX/share/lxc/config/common.conf.d/systemd.conf
# Solution 2
mkdir -p /sys/fs/cgroup/systemd && mount -t cgroup cgroup -o none,name=systemd /sys/fs/cgroup/systemd
Adjust the password for the LXC container
chroot path /bin/su -
lxc-attach -n my-container passwd
Configure DNS resolution inside the container
echo -e "[Match]\nName=wlan0\n\n[Network]\nDHCP=yes\n\n[DHCP]\nRouteMetric=120" \
> /etc/systemd/network/21-wireless.network
systemctl restart systemd-networkd
2.1. Network Configuration#
There are currently two tested network configuration methods
2.1.1 Use host mode#
Make the following adjustments before creating the container
sed -i 's/lxc\.net\.0\.type = empty/lxc.net.0.type = none/g' $PREFIX/etc/lxc/default.conf
If you want to run Docker in host mode, you need to make the following adjustments next
Start the container and enter in the LXC container !!!
update-alternatives --set iptables /usr/sbin/iptables-legacy
iptables -t filter -F
#If you can start, you don't need to use the following command
iptables -t filter -X
echo 8.8.8.8 > /etc/resolv.conf
systemctl stop systemd-resolved
systemctl disable systemd-resolved
# systemctl enable systemd-networkd
2.1.1 Use veth mode#
The following configuration is required for the first time
apt install dnsmasq wget -y
#Modify the default LXC configuration
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
# Modify 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
If you want to use veth after restarting the device, please
$PREFIX/libexec/lxc/lxc-net start
Meow meow meow meow meow
Reference articles:#
- Docker on Android
- LXC Images Software Repository Mirror Usage Guide
- Failed to mount cgroup at /sys/fs/cgroup/systemd: Operation not permitted
- Managing Network Cards with systemd-networkd
- How to configure systemd-resolved and systemd-networkd to use local DNS server for resolving local domains and remote DNS server for remote domains?