红米6pro-sakura编译、打包并运行Docker、kernelSU 的自定义Android内核

💡 本次环境为debian

寻找内核树

前往 XDA 或者 LineageOS 寻找适合自己设备的 kernel

这里以 sakura 为例

拉取内核

 git clone 内核树

准备环境

cd ./android_kernel_xiaomi_msm8998
apt install sudo -y
sudo apt install -y build-essential openssl pkg-config libssl-dev libncurses5-dev pkg-config minizip libelf-dev flex bison  libc6-dev libidn11-dev rsync bc liblz4-tool libncurses-dev
sudo apt install -y gcc-aarch64-linux-gnu dpkg-dev dpkg git ccache
sudo apt install -y git libssl-dev gcc-arm-linux-gnueabi clang build-essential libncurses5-dev bzip2 make python-is-python3 gcc g++ grep bc curl bison flex openssl lzop ccache unzip libssl-dev zlib1g-dev ninja-build texinfo file ca-certificates ccache wget cmake texinfo ca-certificates zlib1g-dev xz-utils libelf-dev gcc-arm-linux-gnueabihf

首次编译

首次不更改任何参数直接编译一次,是否正常。能正常编译再修改内核参数,否则重新寻找或网上解决

export ARCH=arm64
export SUBARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

make clean && make mrproper && rm -rf out/*
# make O=out xiaomi/xiaomi.config
# make O=out sakura_defconfig
make O=out msm8953-perf_defconfig # msm8953-perf_defconfig 为配置名
make KCFLAGS=-w O=out -j$(nproc) 2>&1 | grep -E "Image|error|错误|WARNING|警告|ERROR|错误|FAIL|失败|ERRORS|错误|ERROR:|错误:|ERROR: |错误: |ERROR:|错误:|ERROR:|错误:" #KCFLAGS=-w  通过环境变量传递参数允许警告而不中断编译

如果编译成功,最后会显示 Image 字样

  GZIP    arch/arm64/boot/**Image**.gz
  CAT     arch/arm64/boot/**Image**.gz-dtb
make[1]: Leaving directory '/workspace/android_kernel_xiaomi_msm8953-20241010-lineage-21/out'

资源在 ./out/arch/arm64/boot

(base) root@VM-8-31-ubuntu:/workspace/android_kernel_xiaomi_msm8953-20241010-lineage-21# ll ./out/arch/arm64/boot
total 75200
drwxr-xr-x  3 root root     4096 Dec  2 15:52 ./
drwxr-xr-x  9 root root     4096 Dec  2 15:47 ../
-rw-r--r--  1 root root      142 Dec  2 15:52 .Image.cmd
-rw-r--r--  1 root root     3271 Dec  2 15:52 .Image.gz-dtb.cmd
-rw-r--r--  1 root root      147 Dec  2 15:52 .Image.gz.cmd
-rw-r--r--  1 root root 38092808 Dec  2 15:52 Image
-rw-r--r--  1 root root 13118343 Dec  2 15:52 Image.gz
-rw-r--r--  1 root root 29652891 Dec  2 15:52 Image.gz-dtb
drwxr-xr-x 24 root root     4096 Dec  2 15:47 dts/
(base) root@VM-8-31-ubuntu:/workspace/android_kernel_xiaomi_msm8953-20241010-lineage-21# 

修改配置

修改机型配置,增加 docker 需要环境,如我的为 sakura_defconfig ,直接在文件末尾添加下面的懒人配置:

这里讲下正常获取配置过程

### 手机下载脚本文件检测


通过 Termux 执行以下命令


```shell
pkg install tsu
pkg install wget
wget https://raw.githubusercontent.com/moby/moby/master/contrib/check-config.sh
chmod +x check-config.sh
sed -i '1s_.*_#!/data/data/com.termux/files/usr/bin/bash_' check-config.sh
sudo ./check-config.sh
```


或通过adb执行


```shell
wget https://raw.githubusercontent.com/moby/moby/master/contrib/check-config.sh
adb shell sh check-config.sh
```


会显示如下结果


```shell
info: reading kernel config from /proc/config.gz ...

Generally Necessary:
- cgroup hierarchy: cgroupv2
  Controllers:
  - cpu: missing
  - cpuset: missing
  - io: missing
  - memory: missing
  - pids: missing
- CONFIG_NAMESPACES: enabled
- CONFIG_NET_NS: enabled
- CONFIG_PID_NS: missing
- CONFIG_IPC_NS: missing
- CONFIG_UTS_NS: enabled
- CONFIG_CGROUPS: enabled
- CONFIG_CGROUP_CPUACCT: enabled
- CONFIG_CGROUP_DEVICE: missing
- CONFIG_CGROUP_FREEZER: enabled
- CONFIG_CGROUP_SCHED: enabled
- CONFIG_CPUSETS: enabled
- CONFIG_MEMCG: enabled
- CONFIG_KEYS: enabled
- CONFIG_VETH: missing
- CONFIG_BRIDGE: enabled
- CONFIG_BRIDGE_NETFILTER: missing
- CONFIG_IP_NF_FILTER: enabled
- CONFIG_IP_NF_MANGLE: enabled
- CONFIG_IP_NF_TARGET_MASQUERADE: enabled
- CONFIG_NETFILTER_XT_MATCH_ADDRTYPE: missing
- CONFIG_NETFILTER_XT_MATCH_CONNTRACK: enabled
- CONFIG_NETFILTER_XT_MATCH_IPVS: missing
- CONFIG_NETFILTER_XT_MARK: enabled
- CONFIG_IP_NF_NAT: enabled
- CONFIG_NF_NAT: enabled
- CONFIG_POSIX_MQUEUE: missing
- CONFIG_NF_NAT_IPV4: enabled
- CONFIG_NF_NAT_NEEDED: enabled

Optional Features:
- CONFIG_USER_NS: missing
- CONFIG_SECCOMP: enabled
- CONFIG_SECCOMP_FILTER: enabled
- CONFIG_CGROUP_PIDS: missing
- CONFIG_MEMCG_SWAP: enabled
- CONFIG_MEMCG_SWAP_ENABLED: enabled
- CONFIG_IOSCHED_CFQ: enabled
- CONFIG_CFQ_GROUP_IOSCHED: enabled
- CONFIG_BLK_CGROUP: enabled
- CONFIG_BLK_DEV_THROTTLING: missing
- CONFIG_CGROUP_PERF: missing
- CONFIG_CGROUP_HUGETLB: missing
- CONFIG_NET_CLS_CGROUP: missing
- CONFIG_CGROUP_NET_PRIO: missing
- CONFIG_CFS_BANDWIDTH: missing
- CONFIG_FAIR_GROUP_SCHED: enabled
- CONFIG_IP_NF_TARGET_REDIRECT: enabled
- CONFIG_IP_VS: missing
- CONFIG_IP_VS_NFCT: missing
- CONFIG_IP_VS_PROTO_TCP: missing
- CONFIG_IP_VS_PROTO_UDP: missing
- CONFIG_IP_VS_RR: missing
- CONFIG_SECURITY_SELINUX: enabled
- CONFIG_SECURITY_APPARMOR: missing
- CONFIG_EXT4_FS: enabled
- CONFIG_EXT4_FS_POSIX_ACL: missing
- CONFIG_EXT4_FS_SECURITY: enabled
    enable these ext4 configs if you are using ext3 or ext4 as backing filesystem
- Network Drivers:
  - "overlay":
    - CONFIG_VXLAN: missing
    - CONFIG_BRIDGE_VLAN_FILTERING: missing
      Optional (for encrypted networks):
      - CONFIG_CRYPTO: enabled
      - CONFIG_CRYPTO_AEAD: enabled
      - CONFIG_CRYPTO_GCM: enabled
      - CONFIG_CRYPTO_SEQIV: enabled
      - CONFIG_CRYPTO_GHASH: enabled
      - CONFIG_XFRM: enabled
      - CONFIG_XFRM_USER: enabled
      - CONFIG_XFRM_ALGO: enabled
      - CONFIG_INET_ESP: enabled
      - CONFIG_NETFILTER_XT_MATCH_BPF: enabled
      - CONFIG_INET_XFRM_MODE_TRANSPORT: enabled
  - "ipvlan":
    - CONFIG_IPVLAN: missing
  - "macvlan":
    - CONFIG_MACVLAN: missing
    - CONFIG_DUMMY: enabled
  - "ftp,tftp client in container":
    - CONFIG_NF_NAT_FTP: enabled
    - CONFIG_NF_CONNTRACK_FTP: enabled
    - CONFIG_NF_NAT_TFTP: enabled
    - CONFIG_NF_CONNTRACK_TFTP: enabled
- Storage Drivers:
  - "btrfs":
    - CONFIG_BTRFS_FS: missing
    - CONFIG_BTRFS_FS_POSIX_ACL: missing
  - "overlay":
    - CONFIG_OVERLAY_FS: enabled
  - "zfs":
    - /dev/zfs: missing
    - zfs command: missing
    - zpool command: missing

Limits:
- /proc/sys/kernel/keys/root_maxkeys: 
    This should be set to at least 10000, for example set: sysctl -w kernel/keys/root_maxkeys=1000000
```


对照上面的结果,将 missing 项一个个手动开启
# docker懒人配置
CONFIG_NAMESPACES=y
CONFIG_NET_NS=y
CONFIG_PID_NS=y
CONFIG_IPC_NS=y
CONFIG_UTS_NS=y
CONFIG_CGROUPS=y
CONFIG_CGROUP_CPUACCT=y
CONFIG_CGROUP_DEVICE=y
CONFIG_CGROUP_FREEZER=y
CONFIG_CGROUP_SCHED=y
CONFIG_CPUSETS=y
CONFIG_MEMCG=y
CONFIG_KEYS=y
CONFIG_VETH=y
CONFIG_BRIDGE=y
CONFIG_BRIDGE_NETFILTER=y
CONFIG_IP_NF_FILTER=y
CONFIG_IP_NF_TARGET_MASQUERADE=y
CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=y
CONFIG_NETFILTER_XT_MATCH_CONNTRACK=y
CONFIG_NETFILTER_XT_MATCH_IPVS=y
CONFIG_NETFILTER_XT_MARK=y
CONFIG_IP_NF_NAT=y
CONFIG_NF_NAT=y
CONFIG_POSIX_MQUEUE=y
CONFIG_NF_NAT_IPV4=y
CONFIG_NF_NAT_NEEDED=y
CONFIG_CGROUP_BPF=y
CONFIG_USER_NS=y
CONFIG_SECCOMP=y
CONFIG_SECCOMP_FILTER=y
CONFIG_CGROUP_PIDS=y
CONFIG_MEMCG_SWAP=y
CONFIG_MEMCG_SWAP_ENABLED=y
CONFIG_IOSCHED_CFQ=y
CONFIG_CFQ_GROUP_IOSCHED=y
CONFIG_BLK_CGROUP=y
CONFIG_BLK_DEV_THROTTLING=y
CONFIG_CGROUP_PERF=y
CONFIG_CGROUP_HUGETLB=y
CONFIG_NET_CLS_CGROUP=y
CONFIG_CGROUP_NET_PRIO=y
CONFIG_CFS_BANDWIDTH=y
CONFIG_FAIR_GROUP_SCHED=y
CONFIG_RT_GROUP_SCHED=y
CONFIG_IP_NF_TARGET_REDIRECT=y
CONFIG_IP_VS=y
CONFIG_IP_VS_NFCT=y
CONFIG_IP_VS_PROTO_TCP=y
CONFIG_IP_VS_PROTO_UDP=y
CONFIG_IP_VS_RR=y
CONFIG_SECURITY_SELINUX=y
CONFIG_SECURITY_APPARMOR=y
CONFIG_EXT4_FS=y
CONFIG_EXT4_FS_POSIX_ACL=y
CONFIG_EXT4_FS_SECURITY=y
CONFIG_VXLAN=y
CONFIG_BRIDGE_VLAN_FILTERING=y
CONFIG_CRYPTO=y
CONFIG_CRYPTO_AEAD=y
CONFIG_CRYPTO_GCM=y
CONFIG_CRYPTO_SEQIV=y
CONFIG_CRYPTO_GHASH=y
CONFIG_XFRM=y
CONFIG_XFRM_USER=y
CONFIG_XFRM_ALGO=y
CONFIG_INET_ESP=y
CONFIG_INET_XFRM_MODE_TRANSPORT=y
CONFIG_IPVLAN=y
CONFIG_MACVLAN=y
CONFIG_DUMMY=y
CONFIG_NF_NAT_FTP=y
CONFIG_NF_CONNTRACK_FTP=y
CONFIG_NF_NAT_TFTP=y
CONFIG_NF_CONNTRACK_TFTP=y
CONFIG_AUFS_FS=y
CONFIG_BTRFS_FS=y
CONFIG_BTRFS_FS_POSIX_ACL=y
CONFIG_BLK_DEV_DM=y
CONFIG_DM_THIN_PROVISIONING=y
CONFIG_OVERLAY_FS=y
# 补充1
CONFIG_VLAN_8021Q=y
CONFIG_BRIDGE_VLAN_FILTERING=y
CONFIG_NET_L3_MASTER_DEV=y
CONFIG_IPVLAN=y
# kernelSU
CONFIG_KPROBES=y
CONFIG_HAVE_KPROBES=y
CONFIG_KPROBE_EVENTS=y
# 补充2
# HUGETLB_PAGE=y
CONFIG_HUGETLB_PAGE=y

二次编译

按照首次编译的流程,再编译一次,看是否有错误。

这里通过懒人配置之后,还是会有一个参数 CONFIG_CGROUP_HUGETLB不会开启,这里先暂时不管,等开启 kernelSU 之后再手工开

image.png

开启kernelSU 支持

按照官方文档

💡 INFO
KernelSU 1.0 及更高版本已经不再支持非 GKI 内核,最后的支持版本为 v0.9.5,请注意使用正确的版本。

[**KernelSU 1.0 及更高版本已经不再支持非 GKI 内核**](https://github.com/tiann/KernelSU/issues/1705),最后的支持版本为 `v0.9.5`,请注意使用正确的版本。

而我的设备属于非GKI所以只能选择 V0.9.5,kernelSU 开启比较简单,官方提供了一键脚本

cd 内核树根目录
curl -LSs "https://cfproxy.000000888.xyz/my/https://raw.githubusercontent.com/tiann/KernelSU/main/kernel/setup.sh?cdn=pass" | bash -s v0.9.5
[+] Setting up KernelSU...
Cloning into 'KernelSU'...
remote: Enumerating objects: 17956, done.
remote: Counting objects: 100% (59/59), done.
remote: Compressing objects: 100% (45/45), done.
remote: Total 17956 (delta 27), reused 29 (delta 8), pack-reused 17897 (from 1)
Receiving objects: 100% (17956/17956), 15.50 MiB | 14.20 MiB/s, done.
Resolving deltas: 100% (11056/11056), done.
[+] Repository cloned.
No local changes to save
[-] Stashed current changes.
Already up to date.
[+] Repository updated.
Note: checking out 'v0.9.5'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at b766b985 website: fixed small typo (#1780)
[-] Checked out v0.9.5.
[+] Symlink created.
[+] Modified Makefile.
[+] Modified Kconfig.
[+] Done.

我这里遇到了几次找不到依赖头文件的问题:

  CC      lib/fdt_strerror.o
../drivers/kernelsu/allowlist.c:10:10: fatal error: linux/compiler_types.h: No such file or directory

GPT说是脚本执行的过程中有问题,最后多执行了N次才算成功,所以最好这里按照 首次编译 流程重新编译一次,确保没有错误

GPT回答

```shell
从日志来看,问题出在你运行了 kernelSU_setup.sh 脚本之后,内核代码发生了更改。脚本显然尝试为你的内核添加 KernelSU 模块,但在执行过程中出现了一些问题。这导致了以下情况:

未知的 git checkout 错误
日志中显示了一个错误:

error: unknown switch `s'
这表明 kernelSU_setup.sh 脚本尝试调用 git checkout,但传入了无效参数 -s。

编译中断错误 (fatal error: linux/compiler_types.h: No such file or directory)
脚本可能修改了你的内核源码,或者添加了新文件(如 drivers/kernelsu/allowlist.c),但这些文件没有正确地设置依赖路径。

解决方法
1. 检查 KernelSU 的代码集成情况
脚本主要会对以下文件或路径做修改:

drivers/kernelsu/(新增的模块代码路径)
Makefile(可能修改了编译规则)
Kconfig(可能修改了模块配置)
执行以下命令检查最近的文件更改:

git status
git diff
如果 drivers/kernelsu/ 的内容不完整(如缺少依赖文件),可能需要重新初始化 KernelSU 的代码:

rm -rf drivers/kernelsu
bash kernelSU_setup.sh
```

手工开启MISSING

CONFIG_CGROUP_HUGETLB依赖于CONFIG_HUGETLB_PAGE,但是将这个配置直接加到.config不生效,界面上也不显示具体的开启路径

image.png

image.png

export ARCH=arm64
export SUBARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

make clean && make mrproper && rm -rf out/*
# make O=out xiaomi/xiaomi.config
# make O=out sakura_defconfig
make O=out msm8953-perf_defconfig # msm8953-perf_defconfig 为配置名
# make O=out msm8953_defconfig
make O=out menuconfig #进入图形化配置界面,**先找到CONFIG_HUGETLB_PAGE开启并保存配置.config,再开启CONFIG_CGROUP_HUGETLB保存配置**

💡 ### CONFIG_HUGETLB_PAGE路径位置:

问了下 gpt 说是在File systems -> Pseudo filesystems -> HugeTLB file system support,在界面上启用,再在配置文件里加上CONFIG_CGROUP_HUGETLB,再打开界面看到就是启用状态

### `CONFIG_HUGETLB_PAGE`路径位置:


问了下 gpt 说是在`File systems` -> `Pseudo filesystems` -> `HugeTLB file system support`,在界面上启用,再在配置文件里加上`CONFIG_CGROUP_HUGETLB`,再打开界面看到就是启用状态

💡 ### CONFIG_CGROUP_HUGETLB路径位置:

General setup -> Control Group support (CGROUPS [=y]) -> HugeTLB controller  
### `CONFIG_CGROUP_HUGETLB`路径位置:


```mathematica
General setup -> Control Group support (CGROUPS [=y]) -> HugeTLB controller
```

💡 ### CONFIG_CFS_BANDWIDTH开启条件:

image.png

路径

General setup --> Control Group support --> CPU controller  

必须开启

GROUPS [=y] && CGROUP_SCHED [=y] && FAIR_GROUP_SCHED [=y]   

不能开启

SCHED_WALT [=n]  
# 直接在defconfig中修改  
# CONFIG_SCHED_WALT=y  
# CONFIG_SCHED_WALT is not set  
### CONFIG_CFS_BANDWIDTH开启条件:


![image.png](https://prod-files-secure.s3.us-west-2.amazonaws.com/e0ef7df6-04ca-4d74-a8b1-a36a3e913ed3/b00b0b46-6a38-4b79-9089-e8d1b09d132d/image.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45FSPPWI6X%2F20241203%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20241203T231742Z&X-Amz-Expires=3600&X-Amz-Signature=4012540316feb524d3ccc6e5dbdd200f5a0fb9c9b7ae438ad84482ab470fbe87&X-Amz-SignedHeaders=host&x-id=GetObject)


**路径**


```lua
General setup --> Control Group support --> CPU controller
```


必须开启


```shell
GROUPS [=y] && CGROUP_SCHED [=y] && FAIR_GROUP_SCHED [=y] 
```


不能开启


```shell
SCHED_WALT [=n]
# 直接在defconfig中修改
# CONFIG_SCHED_WALT=y
# CONFIG_SCHED_WALT is not set
```

保存之后最好确认下

grep -E "CONFIG_CGROUP_HUGETLB|CONFIG_HUGETLB_PAGE" out/.config

返回结果是 Y 则正确

CONFIG_CGROUP_HUGETLB=y
CONFIG_HUGETLB_PAGE=y

再次编译

export ARCH=arm64
export SUBARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-

# make O=out oldconfig
# make O=out olddefconfig
# make O=out -j$(nproc) V=1 2>&1 | tee build.log
make KCFLAGS=-w O=out -j$(nproc) 2>&1 | grep -E "Image|error|错误|WARNING|警告|ERROR|错误|FAIL|失败|ERRORS|错误|ERROR:|错误:|ERROR: |错误: |ERROR:|错误:|ERROR:|错误:" #KCFLAGS=-w  通过环境变量传递参数允许警告而不中断编译

打包

[基础教程 2] 如何使用 AnyKernel 3 打包内核 (1)

**如何使用 AnyKernel 3 打包内核**


正如标题,这依然是基础教程。经验丰富朋友依然可以无视。


---


1.AnyKernel 3 是什么?


[AnyKernel3](https://github.com/osm0sis/AnyKernel3) 是由 XDA 论坛的开发人员 [osm0sis](https://github.com/osm0sis) 开发的刷写安卓设备内核的刷机模板。事实上,它的功能十分强大。能兼容许多自定义操作。


例如:

1. 只修改内核部分而不干扰其它(比如可以保留 Magisk)
2. 自定义内核安装(例如让用户选择安装时的操作)
3. 自动修补 vbmeta(取决于你是否开启此功能)
等
而这篇教程只聚焦于基础,不涉及更复杂的内容。所以,放心,没有什么高难度内容。
使用 AnyKernel 3 的前提是您的设备具有自定义 RECOVERY(例如:TWRP,OrangeFox,或者支持 adb sideload)。如果不具备此条件,您可以尝试解包 / 打包 boot.img 来进行内核替换。如果您拥有 SU 权限,则可以使用内核刷写 APP 然后使用 AnyKernel 3 刷机。(例如:Kernel Flasher,Franco Kernel Manager,EX Kernel Manager。)

---


**2. 使用 AnyKernel 3**


在电脑使用如下命令 clone 此项目:


```text
git clone https://github.com/osm0sis/AnyKernel3.git
```


然后进入 AnyKernel 3 的根目录:


接下来我们来了解 AnyKernel 3 的基本结构目录:


```shell
META-INF
modules
patch
ramdisk
tools
LICENSE
README.md
anykernel.sh
```


作为初学者,我们先不去关心其他的文件,只用关心 **anykernel.sh** 和 **LICENSE**。


先用你喜欢的文本编辑器打开 **anykernel.sh**,可以看到很多内容,别急我先把它精简,


精简后的内容如下:


```shell
### AnyKernel3 Ramdisk Mod Script
## osm0sis @ xda-developers

### AnyKernel setup
# global properties
properties() { '
kernel.string=ExampleKernel by osm0sis @ xda-developers
do.devicecheck=1
do.modules=0
do.systemless=1
do.cleanup=1
do.cleanuponabort=0
device.name1=
device.name2=
device.name3=
device.name4=
device.name5=
supported.versions=
supported.patchlevels=
supported.vendorpatchlevels=
'; } # end properties

### AnyKernel install
## boot files attributes
boot_attributes() {
set_perm_recursive 0 0 755 644 $ramdisk/*;
set_perm_recursive 0 0 750 750 $ramdisk/init* $ramdisk/sbin;
} # end attributes

# boot shell variables
block= XXXX ;
is_slot_device=0;
ramdisk_compression=auto;
patch_vbmeta_flag=auto;

# import functions/variables and setup patching - see for reference (DO NOT REMOVE)
. tools/ak3-core.sh;

# boot install
dump_boot; # use split_boot to skip ramdisk unpack, e.g. for devices with init_boot ramdisk

write_boot; # use flash_boot to skip ramdisk repack, e.g. for devices with init_boot ramdisk
## end boot install
```


那么这里介绍你需要了解的部分:

1. `kernel.string=` : 这里需要你填写内核的名称,你可以随意写。
2. `do.devicecheck=` : 填写 1 开启,0 是关闭。这里是用来检测设备名称的,和下面 device.name1~5 形成对应。如果设备名称不对应,就无法刷入进设备。
3. device.name1 ~5 : 这里可以填写你的设备名称。注意,有些手机是开发代号,有的是设备名称具体依靠你自行判断。使用这些参数需要先开启 do.devicecheck。
4. block= XXXX : 需要刷写内核的分区,一般是 boot。
5. is_slot_device= : 填写 1 开启,0 是关闭,auto 是自动判断。设备使用是使用 AB 插槽。一般是 VAB 分区或者 AB 分区的则需要开启。
6. patch_vbmeta_flag= : 填写 1 开启,0 是关闭,auto 是自动判断。是否修补 vbmeta 来关闭 AVB 验证。这项需要看设备,所以具体依靠你自行判断。
知道了这些,你就可以用下面的通用脚本来自行修改属于你的内核刷机包了。
通用脚本:

```shell
### AnyKernel3 Ramdisk Mod Script
## osm0sis @ xda-developers

### AnyKernel setup
# global properties
properties() { '
kernel.string=ExampleKernel by osm0sis @ xda-developers # 请自行填写内核名字
do.devicecheck=0 # 关闭了设备检测
do.modules=0
do.systemless=1
do.cleanup=1
do.cleanuponabort=0
supported.versions=
supported.patchlevels=
supported.vendorpatchlevels=
'; } # end properties

### AnyKernel install
## boot files attributes
boot_attributes() {
set_perm_recursive 0 0 755 644 $ramdisk/*;
set_perm_recursive 0 0 750 750 $ramdisk/init* $ramdisk/sbin;
} # end attributes

# boot shell variables
block=boot;  # 刷写分区是boot
is_slot_device=auto; # 自动检测是否AB插槽
ramdisk_compression=auto;
patch_vbmeta_flag=auto;

# import functions/variables and setup patching - see for reference (DO NOT REMOVE)
. tools/ak3-core.sh;

# boot install
dump_boot; # use split_boot to skip ramdisk unpack, e.g. for devices with init_boot ramdisk

write_boot; # use flash_boot to skip ramdisk repack, e.g. for devices with init_boot ramdisk
## end boot install
```


**注意这个脚本仅仅是一个可用的模板,其中内容仍然需要你按照上述内容进行修改并检查!**


### 完整代码


    ```shell
    ### AnyKernel3 Ramdisk Mod Script
    ## osm0sis @ xda-developers
    
    ### AnyKernel setup
    # global properties
    properties() { '
    kernel.string=ExampleKernel by osm0sis @ 3
    do.devicecheck=0
    do.modules=0
    do.systemless=1
    do.cleanup=1
    do.cleanuponabort=0
    device.name1=maguro
    device.name2=toro
    device.name3=toroplus
    device.name4=tuna
    device.name5=
    supported.versions=
    supported.patchlevels=
    supported.vendorpatchlevels=
    '; } # end properties
    
    
    ### AnyKernel install
    ## boot files attributes
    boot_attributes() {
    set_perm_recursive 0 0 755 644 $RAMDISK/*;
    set_perm_recursive 0 0 750 750 $RAMDISK/init* $RAMDISK/sbin;
    } # end attributes
    
    # boot shell variables
    # BLOCK=/dev/block/platform/omap/omap_hsmmc.0/by-name/boot;
    block=boot;  # 刷写分区是boot
    # IS_SLOT_DEVICE=0;
    is_slot_device=auto; # 自动检测是否AB插槽
    RAMDISK_COMPRESSION=auto;
    PATCH_VBMETA_FLAG=auto;
    
    # import functions/variables and setup patching - see for reference (DO NOT REMOVE)
    . tools/ak3-core.sh;
    
    # boot install
    dump_boot; # use split_boot to skip ramdisk unpack, e.g. for devices with init_boot ramdisk
    
    # init.rc
    backup_file init.rc;
    replace_string init.rc "cpuctl cpu,timer_slack" "mount cgroup none /dev/cpuctl cpu" "mount cgroup none /dev/cpuctl cpu,timer_slack";
    
    # init.tuna.rc
    backup_file init.tuna.rc;
    insert_line init.tuna.rc "nodiratime barrier=0" after "mount_all /fstab.tuna" "\tmount ext4 /dev/block/platform/omap/omap_hsmmc.0/by-name/userdata /data remount nosuid nodev noatime nodiratime barrier=0";
    append_file init.tuna.rc "bootscript" init.tuna;
    
    # fstab.tuna
    backup_file fstab.tuna;
    patch_fstab fstab.tuna /system ext4 options "noatime,barrier=1" "noatime,nodiratime,barrier=0";
    patch_fstab fstab.tuna /cache ext4 options "barrier=1" "barrier=0,nomblk_io_submit";
    patch_fstab fstab.tuna /data ext4 options "data=ordered" "nomblk_io_submit,data=writeback";
    append_file fstab.tuna "usbdisk" fstab;
    
    write_boot; # use flash_boot to skip ramdisk repack, e.g. for devices with init_boot ramdisk
    ## end boot install
    
    
    ## init_boot files attributes
    #init_boot_attributes() {
    #set_perm_recursive 0 0 755 644 $RAMDISK/*;
    #set_perm_recursive 0 0 750 750 $RAMDISK/init* $RAMDISK/sbin;
    #} # end attributes
    
    # init_boot shell variables
    #BLOCK=init_boot;
    #IS_SLOT_DEVICE=1;
    #RAMDISK_COMPRESSION=auto;
    #PATCH_VBMETA_FLAG=auto;
    
    # reset for init_boot patching
    #reset_ak;
    
    # init_boot install
    #dump_boot; # unpack ramdisk since it is the new first stage init ramdisk where overlay.d must go
    
    #write_boot;
    ## end init_boot install
    
    
    ## vendor_kernel_boot shell variables
    #BLOCK=vendor_kernel_boot;
    #IS_SLOT_DEVICE=1;
    #RAMDISK_COMPRESSION=auto;
    #PATCH_VBMETA_FLAG=auto;
    
    # reset for vendor_kernel_boot patching
    #reset_ak;
    
    # vendor_kernel_boot install
    #split_boot; # skip unpack/repack ramdisk, e.g. for dtb on devices with hdr v4 and vendor_kernel_boot
    
    #flash_boot;
    ## end vendor_kernel_boot install
    
    
    ## vendor_boot files attributes
    #vendor_boot_attributes() {
    #set_perm_recursive 0 0 755 644 $RAMDISK/*;
    #set_perm_recursive 0 0 750 750 $RAMDISK/init* $RAMDISK/sbin;
    #} # end attributes
    
    # vendor_boot shell variables
    #BLOCK=vendor_boot;
    #IS_SLOT_DEVICE=1;
    #RAMDISK_COMPRESSION=auto;
    #PATCH_VBMETA_FLAG=auto;
    
    # reset for vendor_boot patching
    #reset_ak;
    
    # vendor_boot install
    #dump_boot; # use split_boot to skip ramdisk unpack, e.g. for dtb on devices with hdr v4 but no vendor_kernel_boot
    
    #write_boot; # use flash_boot to skip ramdisk repack, e.g. for dtb on devices with hdr v4 but no vendor_kernel_boot
    ## end vendor_boot install
    
    
    ```


---


**3. 打包你的内核**


复制你的内核进入 AnyKernel 3 根目录,假设你的内核名是 **Image.gz**。


那么此时你的 AnyKernel 3 的根目录应该看上是这样的:


```shell
META-INF
modules
patch
ramdisk
tools
LICENSE
README.md
anykernel.sh
Image.gz
```


之后用 zip 工具压缩根目录下所有内容。请记住,**是 zip**,不是 rar,7z,tar 等!!!


然后你就可以用 TWRP 刷入并测试内核了。


关于 **LICENSE**:


**LICENSE** 是许可证,这意味着 AnyKernel 3 是受到法律保护的,你需要按照法律许可内行动。请严格遵守此 **LICENSE** 的限制。


---


**不同设备要的内核不一样。有的设备是 Image.gz,有的是 Image.gz-dtb。更详细的内容阅读官方文档:**[**https://source.android.com/docs/core/architecture/bootloader/dtb-images?hl=zh-cn**](https://source.android.com/docs/core/architecture/bootloader/dtb-images?hl=zh-cn)


![image](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAOVBMVEX///+AgICfn5+Hh4f39/fPz8+vr6+QkJDg4ODv7+/n5+fAwMCoqKiYmJjHx8fX19e4uLjQ0NDIyMhhw/XSAAACBUlEQVRYw6VX2ZKEIAw0ATnk8Pj/j90q3V13SSOxpl+mHEknaUKCUw9xTZnpBLu02ukV4lGoAR9Rbb4Xgii7ztxTF35/ay4p1kdzm2mI/KDn4kkB0w3iICVmbO9IDae31zMkoo8YZkJgl5IrGh1WpHaN34VdDUks//YfrNjsn/cO8NtnAedhivl+G6D9kCH8Buhl/JptLv0dLMIcuqnTBY8TGCdhelvohfGjI3mEEybYZKGdZTKokcfNipCXpg4IysgoMAwDl5KegFGwy2cEYZpfEBQkAjpnehFpQ2HRCwKeDKHdhQiQgGBcEE5NgEWwhgAm/eSo2BchmAXMPUMwBSaEYlE/0RPIGWxLb8BkwiiLam6n/kgzx+3+IOoSBGSbr5+0nN43cz0SnE9Wpr9O0/qz2psf0jCtTrqSR8zFK2lu1D5FjZtpnLX1wfGu/Pxn3e8QillUXGxa3A07J8fimjqb9tixrD8JXJJ8UQp7NcN6n7LrScvQTLAqxtEItTm0/r5wKOH/j9AgutAIc5MzE4VXBLa56UUjOsAwB2+bhuteprCIf9wr+yp7tl5H7C0TzW/tZQwaJQ+i2mX2w92M/BBpMEMhkiGzPtOXhyBCGaYZPFHBH6l2z90cRcfh3QJr8lWjsq1nn2Xe0hHCEvaUmL/btRaxyo/vFKdXiCFlPmk8u9rvdl/WWxJO0oeE7gAAAABJRU5ErkJggg==)


xuxin3101


这个教程最好别看, 不看你可能会用, 看了就用不来了


![image](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAOVBMVEX///+AgICfn5+Hh4f39/fPz8+vr6+QkJDg4ODv7+/n5+fAwMCoqKiYmJjHx8fX19e4uLjQ0NDIyMhhw/XSAAACBUlEQVRYw6VX2ZKEIAw0ATnk8Pj/j90q3V13SSOxpl+mHEknaUKCUw9xTZnpBLu02ukV4lGoAR9Rbb4Xgii7ztxTF35/ay4p1kdzm2mI/KDn4kkB0w3iICVmbO9IDae31zMkoo8YZkJgl5IrGh1WpHaN34VdDUks//YfrNjsn/cO8NtnAedhivl+G6D9kCH8Buhl/JptLv0dLMIcuqnTBY8TGCdhelvohfGjI3mEEybYZKGdZTKokcfNipCXpg4IysgoMAwDl5KegFGwy2cEYZpfEBQkAjpnehFpQ2HRCwKeDKHdhQiQgGBcEE5NgEWwhgAm/eSo2BchmAXMPUMwBSaEYlE/0RPIGWxLb8BkwiiLam6n/kgzx+3+IOoSBGSbr5+0nN43cz0SnE9Wpr9O0/qz2psf0jCtTrqSR8zFK2lu1D5FjZtpnLX1wfGu/Pxn3e8QillUXGxa3A07J8fimjqb9tixrD8JXJJ8UQp7NcN6n7LrScvQTLAqxtEItTm0/r5wKOH/j9AgutAIc5MzE4VXBLa56UUjOsAwB2+bhuteprCIf9wr+yp7tl5H7C0TzW/tZQwaJQ+i2mX2w92M/BBpMEMhkiGzPtOXhyBCGaYZPFHBH6l2z90cRcfh3QJr8lWjsq1nn2Xe0hHCEvaUmL/btRaxyo/vFKdXiCFlPmk8u9rvdl/WWxJO0oeE7gAAAABJRU5ErkJggg==)


a289672082


Nothing to preview

修改完配置文件后,把上一步 arch/arm64/boot/Image.gz-dtb 放到 Anykernel3 的根目录下,然后在根目录中执行 zip -r <压缩包名. zip> * 即可

最后通过 twrpadb sideload xxx.zip 刷入内核。

检测结果

编译完的内核还有三个地方是红色 (missing)

image.png

第一处Controllers经测试不是内核参数的问题,需要运行一次这行命令:

sudo mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup && sudo mount -t tmpfs -o mode=755 tmpfs /sys/fs/cgroup && sudo mkdir -p /sys/fs/cgroup/devices && sudo mount -t cgroup -o devices cgroup /sys/fs/cgroup/devices && sudo mkdir -p /sys/fs/cgroup/systemd && sudo mount -t cgroup cgroup -o none,name=systemd /sys/fs/cgroup/systemd

下面两个无法解决,因为是非必须项,先不管它了。

补丁

有的提到还需要对net/netfilter/xt_qtaguid.c做补丁,但我这个内核代码里没有这个文件就跳过了。

补充

为了避免内核参数有遗漏,我们把https://github.com/lateautumn233/android_kernel_docker里的提到的内核参数也加到.config里,再重新编译一次。

运行docker

💡 这里采用 termux 的方式运行docker
termux中,根目录的环境变量为 $PREFIX

termux中,根目录的环境变量为 `$PREFIX`

换源

termux-change-repo
# 选择第二个按空格回车,选择一个国内源,比如阿里云

image.png

或者

sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.aliyun.com/termux/termux-packages-24 stable main@' $PREFIX/etc/apt/sources.list

更新list

apt update
pkg update

安装ssh

apt install openssh -y
whoami #查看并记住用户名
passwd #更改密码
sshd #启动sshd,默认端口为8022

挂载cgroup

pkg install tsu # 安装sudo,这里注意kernelsu授权超级用户
sudo mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup #这个命令每次手机重启都要运行一次

安装、运行docker&docker-compose

docker

安装

pkg install root-repo && pkg install docker

运行

sudo dockerd --iptables=false &
sudo docker ps #没有报错则运行成功

docker运行别人遇到的错误

`sudo docker run hello-world`


这时候启动 docker 容器会报错,


```text
docker: Error response from daemon: failed to create task for container: failed to start shim: start failed: io.containerd.runc.v2: create new shim socket: listen unix /data/data/com.termux/files/usr/var/run/containerd/s/fca432b16f1e32bdfce67923b7e94f3ab7f741783e5032a938bd6869d8b6d3af: bind: invalid argument: exit status 1: unknown.
```


![1914604-20240718103130502-1354906515.png](https://img2024.cnblogs.com/other/1914604/202407/1914604-20240718103130502-1354906515.png)


这里并不清楚什么原因,但偶然记得之前在酷安上看到一个东西,过去[翻了翻](https://www.coolapk.com/feed/51581431?shareKey=MmRlNTgxOTVmNjliNjY5M2QwMGU~&shareUid=4285440&shareFrom=com.coolapk.market_14.2.3) [2],说是要降级`containerd`包的版本。先查看下`containerd`包的版本:


![1914604-20240718103130715-1986297534.png](https://img2024.cnblogs.com/other/1914604/202407/1914604-20240718103130715-1986297534.png)


然后下载帖子里这个包,用 adb 传到手机上 (xftp 也可以),然后使用`dpkg -i containerd_1.6.21-1_aarch64.deb`安装


![1914604-20240718103130942-1236550263.png](https://img2024.cnblogs.com/other/1914604/202407/1914604-20240718103130942-1236550263.png)


接着查看版本`apt show containerd -a`就看到已经安装上了


然后重新启动`sudo dockerd --iptables=false`,在跑`hello-world`容器就正常了


![1914604-20240718103131189-1415257182.png](https://img2024.cnblogs.com/other/1914604/202407/1914604-20240718103131189-1415257182.png)


试了下跑 python 也可以,那说明可以在手机跑爬虫了:


![1914604-20240718103131493-1609208315.png](https://img2024.cnblogs.com/other/1914604/202407/1914604-20240718103131493-1609208315.png)


Ubuntu 镜像也是可以的:


![1914604-20240718103131704-1846008507.png](https://img2024.cnblogs.com/other/1914604/202407/1914604-20240718103131704-1846008507.png)

docker-compose

安装

wget https://github.com/docker/compose/releases/download/v2.31.0/docker-compose-linux-aarch64 #版本号结合实际情况来
mv docker-compose-linux-aarch64 /data/data/com.termux/files/usr/bin/docker-compose
chmod +x /data/data/com.termux/files/usr/bin/docker-compose
docker-compose --version

termux中daemon.json路径

mkdir -p /data/data/com.termux/files/usr/etc/docker
touch /data/data/com.termux/files/usr/etc/docker/daemon.json
cat $PREFIX/etc/docker/daemon.json

创建lxc容器

💡 同样需要挂载cgroup

配置网络

sed -i 's/lxc\.net\.0\.type = empty/lxc.net.0.type = none/g'  /data/data/com.termux/files/usr/etc/lxc/default.conf

安装ubuntu容器

前期准备

# 对应错误信息No such file or directory - Failed to open file "/data/data/com.termux/files/home/.config/lxc/default.conf"
mkdir -p /data/data/com.termux/files/home/.config/lxc
touch /data/data/com.termux/files/home/.config/lxc/default.conf

# No uid mapping for container root
nano /data/data/com.termux/files/home/.config/lxc/default.conf
    lxc.idmap = u 0 100000 65536
    lxc.idmap = g 0 100000 65536
    
echo "lxc.idmap = u 0 100000 65536" >> /data/data/com.termux/files/home/.config/lxc/default.conf
echo "lxc.idmap = g 0 100000 65536" >> /data/data/com.termux/files/home/.config/lxc/default.conf

# Failed to chown "/data/data/com.termux/files/home/.local/share/lxc/ubuntu/rootfs" to container root
sudo chown -R 100000:100000 /data/data/com.termux/files/home/.local/share/lxc/ubuntu/rootfs

# 临时禁用 SELinux 或 AppArmor
sudo setenforce 0  # 临时禁用 SELinux
sudo aa-disable /usr/bin/lxc-usernsexec  # 临时禁用 AppArmor

下载安装

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

# 然后选择“ubuntu”, “jammy”,“arm64” ## !!注意不要选成amd64了
# 或者直接在参数中指定
sudo lxc-create -t download -n ubuntu -- --dist ubuntu --release jammy --arch arm64 --server mirrors.tuna.tsinghua.edu.cn/lxc-images --no-validate

# debian11 模板
sudo  lxc-create -n debian11 -t download -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images --dist debian --release bullseye --arch arm64 --no-validate

修改密码

# vim开启鼠标的话,可输入“:set mouse=n”关闭
# 使用vi设置root密码:
vi  /data/data/com.termux/files/usr/var/lib/lxc/ubuntu/rootfs/etc/shadow
--- a/shadow    2023-10-31 08:57:36.605348208 +0800
+++ b/shadow    2023-10-31 08:58:10.808684949 +0800
@@ -1,4 +1,4 @@
-root:*:19598:0:99999:7:::
+root:paa5KD6arxLr2:19598:0:99999:7:::
 daemon:*:19598:0:99999:7:::
 bin:*:19598:0:99999:7:::
# 修改密码,就是修改用户名后 2 个:之间的内容,星号改为:paa5KD6arxLr2,这个密码的明文是123456

启动容器

sudo lxc-start -n ubuntu -d -F
# -d 或 --daemon :以守护进程(后台)模式启动容器。容器将在后台运行,不会占用当前终端。
# -F 或 --foreground :在前台模式下启动容器。容器将在当前终端中运行,并占用终端。如果终端关闭,容器也会停止。

# 检查容器状态
sudo lxc-info -n ubuntu
# 检查容器日志
sudo lxc-console -n ubuntu

# **以下是一些常见的 lxc-start 命令示例:**

#在前台启动容器:
sudo lxc-start -n ubuntu -F
#在后台启动容器:
sudo lxc-start -n ubuntu -d
#指定 LXC 路径并启动容器:
sudo lxc-start -n ubuntu -P /path/to/lxc -d
#定义内存限制并启动容器:
sudo lxc-start -n ubuntu -d -s lxc.cgroup.memory.limit_in_bytes=512M

删除现有容器

sudo lxc-destroy -n 容器名

欢迎指出任何有错误或不够清晰的表达,可以在下面评论区评论。

×

喜欢就点赞,疼爱就打赏

//