Skip to content

Commit d2d4428

Browse files
config/init: add magisk module support
Current module is very simple. It only implements the mounting of tmpfs, the initialization of cgroup, the startup of lxc-net, and the self startup of lxc-autostart.😥 Signed-off-by: DreamConnected <1487442471@qq.com>
1 parent 53f2fc4 commit d2d4428

8 files changed

Lines changed: 233 additions & 1 deletion

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
3+
export LD_LIBRARY_PATH=/system/lib64:/system/lib:@LIBDIR@:/data/sysroot/lib64:/data/sysroot/lib
4+
5+
# lxc-autostart
6+
mkdir -p @LOCALSTATEDIR@/lock
7+
@LIBEXECDIR@/lxc/lxc-containers start
8+
9+
while true do
10+
@LXCHOOKDIR@/android-network
11+
sleep 5
12+
done

config/init/module/customize.sh.in

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/sh
2+
3+
ui_print "- [I] Checking env..."
4+
5+
if [ -n $KSU ]; then
6+
ui_print "- [D] Su manager is KernelSU."
7+
fi
8+
9+
if [ $ARCH = "arm64" ]; then
10+
continue
11+
else
12+
abort "- [E] Unsupported arch: $ARCH."
13+
fi
14+
15+
if [ $API -ge 30 ]; then
16+
continue
17+
else
18+
ui_print "- [W] Func memfd_create() required API 30(R)."
19+
fi
20+
21+
ui_print "- [D] Fetching download link..."
22+
23+
DOWNLOAD_URLS=$(curl -s https://api.github.com/repos/Container-On-Android/lxc/releases/latest | grep browser_download_url | cut -d'"' -f4 | grep -E 'gz$')
24+
25+
URL_LXC=$(echo "$DOWNLOAD_URLS" | sed -n '1p')
26+
URL_SYSROOT=$(echo "$DOWNLOAD_URLS" | sed -n '2p')
27+
28+
if [ -z $URL_LXC ] && [ -z $URL_SHARE ]; then
29+
abort "- [E] Error: URL_LXC: $URL_LXC URL_SHARE: $URL_SHARE."
30+
else
31+
ui_print "- [D] Fetch ok..."
32+
fi
33+
34+
ui_print "- [I] Downloading lxc..."
35+
36+
wget -q $URL_LXC -O /data/local/tmp/lxc.tar.gz
37+
38+
if [ $? -eq 0 ]; then
39+
ui_print "- [I] Download lxc ok..."
40+
else
41+
rm /data/local/tmp/lxc.tar.gz
42+
abort "- [E] Failed to download lxc. ($URL_LXC)"
43+
fi
44+
45+
ui_print "- [I] Downloading lxc-sysroot..."
46+
47+
wget -q $URL_SYSROOT -O /data/local/tmp/lxc-sysroot.tar.gz
48+
49+
if [ $? -eq 0 ]; then
50+
ui_print "- [D] Download lxc-sysroot ok..."
51+
else
52+
rm /data/local/tmp/lxc-sysroot.tar.gz
53+
abort "- [E] Failed to download lxc-sysroot. ($URL_SYSROOT)"
54+
fi
55+
56+
ui_print "- [I] Installing lxc..."
57+
mkdir -p @PREFIXDIR@
58+
mkdir -p /data/sysroot
59+
tar zxf /data/local/tmp/lxc.tar.gz -C @PREFIXDIR@
60+
tar zxf /data/local/tmp/lxc-sysroot.tar.gz -C /data/sysroot
61+
ui_print "- [I] OK!"
62+
63+
ui_print "- [I] Clean!"
64+
rm /data/local/tmp/lxc.tar.gz
65+
rm /data/local/tmp/lxc-sysroot.tar.gz

config/init/module/meson.build

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-License-Identifier: MIT
2+
3+
if 'module' in init_script
4+
configured_files = []
5+
6+
foreach module_files: [
7+
'boot-completed.sh.in',
8+
'customize.sh.in',
9+
'module.prop.in',
10+
'service.sh.in',
11+
'uninstall.sh.in',
12+
]
13+
configured_file = configure_file(
14+
input: module_files,
15+
output: '@BASENAME@',
16+
configuration: conf,
17+
)
18+
configured_files += configured_file
19+
endforeach
20+
21+
configure_file(
22+
command: [zip, '-rj', '@OUTPUT@', '@INPUT@'],
23+
input: configured_files,
24+
output: 'lxc-module.zip',
25+
install: true,
26+
install_dir: project_source_root)
27+
28+
endif

config/init/module/module.prop.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id=lxc
2+
name=LXC-Daemon
3+
version=1.0.0
4+
versionCode=10001
5+
author=@Container-On-Android
6+
description=description

config/init/module/service.sh.in

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/sh
2+
3+
MODDIR=${0%/*}
4+
PKG_TERMUX=/data/data/com.termux
5+
6+
NET_STATE=""
7+
NET_FUNC=""
8+
GPU_STATE=""
9+
AUD_STATE=""
10+
DAEMON_STATE=""
11+
12+
LXC_DIR="@PREFIXDIR@"
13+
LXC_TMPDIR=$LXC_DIR/tmp
14+
LXC_NET=$LXC_DIR/libexec/lxc/lxc-net
15+
LXC_NET_BUILTIN=$LXC_DIR/libexec/lxc/lxc-net-builtin
16+
LXC_DNSMASQ=$LXC_DIR/bin/dnsmasq
17+
18+
NET_FUNC=""
19+
GPU_FUNC=""
20+
GPU_FUNC_DIR=/storage/emulated/0/lxc
21+
22+
# Init desc
23+
sed -i "s|description=.*|description={}|g" $MODDIR/module.prop
24+
25+
# Setup lxc-tmp
26+
if ! mountpoint -q $LXC_TMPDIR; then
27+
mkdir -p $LXC_TMPDIR
28+
mount -t tmpfs tmpfs $LXC_TMPDIR
29+
else
30+
continue
31+
fi
32+
33+
# Setup Cgroup
34+
if mountpoint -q /sys/fs/cgroup; then
35+
if mountpoint -q /sys/fs/cgroup/unified > /dev/null 2>&1; then
36+
umount /sys/fs/cgroup/unified
37+
fi
38+
umount /sys/fs/cgroup
39+
mount -t tmpfs tmpfs /sys/fs/cgroup
40+
mkdir -p /sys/fs/cgroup/unified
41+
mount -t cgroup2 cgroup2 -o rw,nosuid,nodev,noexec,relatime /sys/fs/cgroup/unified
42+
else
43+
mount -t tmpfs tmpfs /sys/fs/cgroup
44+
mkdir -p /sys/fs/cgroup/unified
45+
mount -t cgroup2 cgroup2 -o rw,nosuid,nodev,noexec,relatime /sys/fs/cgroup/unified
46+
fi
47+
48+
for subsys in cpu cpuacct memory freezer devices blkio pids cpuset systemd; do
49+
if ! mountpoint -q /sys/fs/cgroup/$subsys > /dev/null 2>&1; then
50+
mkdir -p /sys/fs/cgroup/$subsys
51+
52+
if [ $subsys == "systemd" ]; then
53+
mount -t cgroup -o none,name=$subsys cgroup /sys/fs/cgroup/$subsys
54+
else
55+
mount -t cgroup -o rw,nosuid,nodev,noexec,relatime,$subsys cgroup /sys/fs/cgroup/$subsys
56+
fi
57+
58+
if [ $? -eq 0 ]; then
59+
echo "$subsys ok"
60+
else
61+
echo "$subsys failed"
62+
fi
63+
else
64+
echo "$subsys mounted"
65+
fi
66+
done
67+
68+
# Start lxc-net
69+
if [ -e $LXC_DNSMASQ ]; then
70+
$LXC_NET start
71+
NET_FUNC="full"
72+
else
73+
$LXC_NET_BUILTIN start
74+
NET_FUNC="IPv6 disabled, Hotspot conflict."
75+
fi
76+
77+
if [ -e $LXC_TMPDIR/lxc/network_up ]; then
78+
NET_STATE="lxc-net ✅ ($NET_FUNC)"
79+
else
80+
NET_STATE="lxc-net ❎"
81+
fi
82+
83+
# Start lxc-gpu
84+
if [ -e $GPU_FUNC_DIR/gpu ]; then
85+
GPU_FUNC=$(cat $GPU_FUNC_DIR/gpu)
86+
if [ "$GPU_FUNC" = "termux virgl" ]; then
87+
GPU_STATE="lxc-gpu ✅ ($GPU_FUNC)"
88+
cat > $GPU_FUNC_DIR/gpu.conf <<EOF
89+
# This file is generated by LXC-daemon, to use this
90+
# config, you can add "lxc.include = $GPU_FUNC_DIR/gpu.conf"
91+
# in $LXC_DIR/lib/lxc/<name>/config.
92+
# need to run "systemctl mask tmp.mount" in container.
93+
lxc.mount.entry = $PKG_TERMUX/files/usr/tmp tmp tmpfs bind,rw 0 0
94+
EOF
95+
else
96+
GPU_STATE="lxc-gpu ✅ (Unsupported $GPU_FUNC)"
97+
fi
98+
else
99+
GPU_STATE="lxc-gpu ❎ (not loaded)"
100+
fi
101+
102+
# Start lxc-audio
103+
AUD_STATE="lxc-audio ❎"
104+
105+
DAEMON_STATE="$NET_STATE, $GPU_STATE, $AUD_STATE"
106+
107+
sed -i "s|description={}|description=$DAEMON_STATE|g" $MODDIR/module.prop

config/init/module/uninstall.sh.in

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
export LD_LIBRARY_PATH=/system/lib64:/system/lib:@LIBDIR@:/data/sysroot/lib64:/data/sysroot/lib
4+
5+
for container in $(lxc-ls); do
6+
echo "stopping: $container"
7+
lxc-stop -n "$container" 2>/dev/null || lxc-stop -n "$container" -k
8+
done
9+
10+
rm -rf @PREFIXDIR@

meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ srcconf.set_quoted('RUNTIME_PATH', runtimepath)
110110
srcconf.set_quoted('SBINDIR', sbindir)
111111

112112
conf = configuration_data()
113+
conf.set('PREFIXDIR', prefixdir)
114+
conf.set('LIBDIR', libdir)
113115
conf.set('BINDIR', bindir)
114116
conf.set('LIBEXECDIR', libexecdir)
115117
conf.set('LOCALSTATEDIR', localstatedir)
@@ -327,6 +329,7 @@ endif
327329
sh = find_program('sh')
328330
date = find_program('date')
329331
git = find_program('git', required: false)
332+
zip = find_program('zip', required: false)
330333
time_epoch = run_command(sh, '-c', 'echo "$SOURCE_DATE_EPOCH"', check: true).stdout().strip()
331334
if time_epoch == '' and git.found() and run_command('test', '-e', '.git', check: false).returncode() == 0
332335
# If we're in a git repository, use the creation time of the latest git tag.
@@ -938,6 +941,7 @@ if want_install_init
938941
subdir('config/init/systemd')
939942
subdir('config/init/sysvinit')
940943
subdir('config/init/monitd')
944+
subdir('config/init/module')
941945
subdir('config/sysconfig')
942946
endif
943947
if want_selinux

meson_options.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ option('examples', type: 'boolean', value: true,
1515

1616
# was --init-script in autotools
1717
option('init-script', type: 'array',
18-
choices: ['systemd', 'sysvinit', 'monitd'], value: ['monitd'],
18+
choices: ['systemd', 'sysvinit', 'monitd', 'module'], value: ['module'],
1919
description: 'init script')
2020

2121
# was --systemd-unidir in autotools

0 commit comments

Comments
 (0)