Skip to content

Commit

Permalink
Optimize the replace kernel logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ophub committed Sep 29, 2021
1 parent 7d2645b commit 6d726ac
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-armbian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
sudo chmod +x make
sudo ./make s922x_s905x3_s905d
sudo ./make s905x_s905w 5.4.149
cd ./build/output/images && sudo gzip *.img
cd ./build/output/images && sudo gzip *.img && sync
echo "FILEPATH=$PWD" >> $GITHUB_ENV
echo "FILE_DATE=$(date +"%Y.%m.%d.%H%M")" >> $GITHUB_ENV
echo "::set-output name=status::success"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/use-releases-file-to-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
sudo chmod +x make
sudo ./make s922x_s905x3_s905d
sudo ./make s905x_s905w 5.4.149
cd ./build/output/images && sudo gzip *.img
cd ./build/output/images && sudo gzip *.img && sync
echo "FILEPATH=$PWD" >> $GITHUB_ENV
echo "FILE_DATE=$(date +"%Y.%m.%d.%H%M")" >> $GITHUB_ENV
echo "::set-output name=status::success"
Expand Down
81 changes: 81 additions & 0 deletions README.cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 可以安装在 Amlogic S9xxx 系列盒子中使用的 Armbian 系统

查看英文说明 | [View English description](README.md)

为 Amlogic S9xxx 系列盒子编译 Armbian 系统。支持写入 EMMC 中使用,支持更新内核等功能。支持的 Amlogic S9xxx 系列型号有 ***`s922x, s905x3, s905x2, s912, s905d, s905x, s905w`*** 等,例如 ***`Belink GT-King, Belink GT-King Pro, UGOOS AM6 Plus, X96-Max+, HK1-Box, H96-Max-X3, Phicomm-N1, Octopus-Planet, Fiberhome HG680P, ZTE B860H`*** 等盒子。

最新的 Armbian 固件可以在 [Releases](https://github.com/ophub/amlogic-s9xxx-armbian/releases) 中下载。欢迎 `Fork` 并进行个性化定制。如果对你有用,可以点仓库右上角的 `Star` 表示支持。

## Armbian 固件说明

| 型号 | 盒子 | [可选内核](https://github.com/ophub/flippy-kernel/tree/main/library) | Armbian 固件 |
| ---- | ---- | ---- | ---- |
| s922x | [Belink](https://tokopedia.link/RAgZmOM41db), [Belink-Pro](https://tokopedia.link/sfTHlfS41db), [Ugoos-AM6-Plus](https://tokopedia.link/pHGKXuV41db) | 全部 | armbian_aml_s922x_buster_*.img |
| s905x3 | [X96-Max+](https://tokopedia.link/uMaH09s41db), [HK1-Box](https://tokopedia.link/xhWeQgTuwfb), [H96-Max-X3](https://tokopedia.link/KuWvwoYuwfb), [Ugoos-X3](https://tokopedia.link/duoIXZpdGgb), [X96-Air](https://tokopedia.link/5WHiETbdGgb), [A95XF3-Air](https://tokopedia.link/ByBL45jdGgb) | 全部 | armbian_aml_s905x3_buster_*.img |
| s905x2 | [X96Max-4G](https://tokopedia.link/HcfLaRzjqeb), [X96Max-2G](https://tokopedia.link/HcfLaRzjqeb) | 全部 | armbian_aml_s905x2_buster_*.img |
| s912 | [H96-Pro-Plus](https://tokopedia.link/jb42fsBdGgb), Octopus-Planet | 全部 | armbian_aml_s912_buster_*.img |
| s905d | Phicomm-N1 | 全部 | armbian_aml_s905d_buster_*.img |
| s905x | [HG680P](https://tokopedia.link/HbrIbqQcGgb), [B860H](https://tokopedia.link/LC4DiTXtEib) | 5.4.* | armbian_aml_s905x_buster_*.img |
| s905w | [X96-Mini](https://tokopedia.link/ro207Hsjqeb), [TX3-Mini](https://www.tokopedia.com/beststereo/tanix-tx3-mini-2gb-16gb-android-7-1-kodi-17-3-amlogic-s905w-4k-tv-box) | 5.4.* | armbian_aml_s905w_buster_*.img |

## 安装及升级 Armbian 的相关说明

选择和你的盒子型号对应的 Armbian 固件,使用 [Rufus](https://rufus.ie/) 或者 [balenaEtcher](https://www.balena.io/etcher/) 等工具将固件写入 USB 里,然后把写好固件的 USB 插入盒子。

- ### 安装 Armbian

登录 Armbian 系统 (默认用户: root, 默认密码: 1234) → 输入命令:

```yaml
armbian-install
```

- ### 更新 Armbian

查询 [可选内核](https://github.com/ophub/flippy-kernel/tree/main/library) 版本,登录 Armbian 系统 → 输入命令:

```yaml
# 使用 root 用户运行 (sudo -i), 输入命令: armbian-update <内核版本>
armbian-update 5.10.66
```

## Armbian 固件制作方法

- 不同的 Amlogic armbian 固件,使用对应的 soc 代码生成。 请根据你的盒子的 soc 型号进行选择。支持 `s922x``s905x3``s905x2``s912``s905d``s905x``s905w`

- 编译单个盒子的 Armbian 时,输入对应的 soc 型号,如 `sudo ./make s905x3`。 如果同时编译多个不同盒子时,将对应的 soc 型号使用 `_` 连接, 如 `sudo ./make s922x_s905x3`

- 可选项: 替换内核。如 `sudo ./make s905x 5.4.149`。当一次编译多个内核版本时,内核版本号使用 `_` 连接。如 `sudo ./make s922x_s905x3 5.4.149_5.14.6`。编译程序会自动到 [内核库](https://github.com/ophub/flippy-kernel/tree/main/library) 中查找你指定的内核系列是否有更新的版本,如果有则直接使用最新版。如果你想使用指定的固定版本,可以在版本号后使用 false 参数固定版本,如 `sudo ./make s905x 5.4.149 false`

💡提示:当前 ***`s905x`******`s905w`*** 系列的盒子只支持使用 `5.4.*` 内核,不能使用 5.10.* 或更高版本,请在编译时指定替换内核。其他型号的盒子可任选内核版本使用。

- ### 使用 GitHub Action 进行编译

1. 关于 Workflows 文件的配置在 [.yml](.github/workflows) 文件里。可以设置需要编译的盒子的 `soc` 等参数,具体详见 `Make Armbian for amlogic s9xxx` 节点。

2. 全新编译:在 [Action](https://github.com/ophub/amlogic-s9xxx-armbian/actions) 页面里选择 ***`Build Armbian For Amlogic`*** ,点击 ***`Run workflow`*** 按钮即可编译。

3. 再次编译:如果 [Releases](https://github.com/ophub/amlogic-s9xxx-armbian/releases) 中有已经编译好的 `Armbian_.*_Lepotato_.*.img.gz` 文件,你只是想再次制作其他不同 soc 的盒子,可以跳过 Armbian 源文件的编译,直接进行二次制作。在 [Action](https://github.com/ophub/amlogic-s9xxx-armbian/actions) 页面中选择 ***`Use Releases file to build`*** ,点击 ***`Run workflow`*** 按钮即可二次编译。

- ### 本地化打包

1. 安装必要的软件包(如 Ubuntu 20.04 LTS 用户)

```yaml
sudo apt-get update -y
sudo apt-get full-upgrade -y
sudo apt-get install -y $(curl -fsSL git.io/ubuntu-2004-openwrt)
```

2. 克隆仓库到本地 `git clone --depth 1 https://github.com/ophub/amlogic-s9xxx-armbian.git`
3. 在根目录下创建文件夹 `build/output/images` ,并上传采用 `lepotato` 分支生成的 Armbian 镜像文件 ( 如:`Armbian_21.11.0-trunk_Lepotato_buster_current_5.10.67.img` ) 到 `~/amlogic-s9xxx-armbian/build/output/images` 目录里。
4. 进入 `~/amlogic-s9xxx-armbian` 根目录,然后运行 `sudo ./make s905x3` 命令即可生成指定 soc 的 Armbian 镜像文件。生成的文件保存在 `build/output/images` 目录里。

## 鸣谢

编译的 [armbian](https://github.com/armbian/build) 系统直接使用了官方当前的最新源码进行实时编译。为不同的盒子制作专用的 Armbian 系统时采用了 [flippy](https://github.com/unifreq/openwrt_packit)`amlogic s9xxx openwrt` 制作的内核及 `u-boot` 等资源。程序的开发思路来自 [ebkso](https://www.kflyo.com/howto-compile-armbian-for-n1-box) 等作者的教程。感谢各位的奉献和分享,让我们可以在 Amlogic s9xxx 盒子里使用 Armbian 系统。

## License

[LICENSE](https://github.com/ophub/amlogic-s9xxx-armbian/blob/main/LICENSE) © OPHUB

11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Armbian for Amlogic S9xxx STB

View Chinese description | [查看中文说明](README.cn.md)

Compile the Armbian for Amlogic S9xxx STB. including install to EMMC and update related functions. Support Amlogic S9xxx STB are ***`s922x, s905x3, s905x2, s912, s905d, s905x, s905w`***, etc. such as ***`Belink GT-King, Belink GT-King Pro, UGOOS AM6 Plus, X96-Max+, HK1-Box, H96-Max-X3, Phicomm-N1, Octopus-Planet, Fiberhome HG680P, ZTE B860H`***, etc.

The latest version of the Armbian firmware can be downloaded in [Releases](https://github.com/ophub/amlogic-s9xxx-armbian/releases).
The latest version of the Armbian firmware can be downloaded in [Releases](https://github.com/ophub/amlogic-s9xxx-armbian/releases). Welcome to `Fork` and personalize it. If it is useful to you, you can click on the `Star` in the upper right corner of the warehouse to show your support.

## Armbian Firmware instructions

Expand Down Expand Up @@ -36,13 +38,16 @@ Query the available [kernel_version](https://github.com/ophub/flippy-kernel/tree
# Run as root user (sudo -i), input command: armbian-update <kernel_version>
armbian-update 5.10.66
```

## Armbian firmware make method

- Different amlogic armbian firmware, use the corresponding soc code to generate. Please choose according to your box soc model. Supported soc are `s922x`, `s905x3`, `s905x2`, `s912`, `s905d`, `s905x`, `s905w`.

- compile `a single soc` can be directly input `sudo ./make s905x3`. When `multiple soc` is compiled at the same time, please use `_` to connect multiple soc, such as `sudo ./make s922x_s905x3`

- Optionality: Replace the kernel. Run Eg: `sudo ./make s905x 5.4.149`. ***`s905x`*** and ***`s905w`*** currently only support `5.4.*` kernels, and do not support 5.10 and other kernels. Please add kernel substitution variables when compiling these two models of devices. Other devices can be freely selected from the [Optional kernel](https://github.com/ophub/flippy-kernel/tree/main/library) library.
- Optionality: Replace the kernel. Run Eg: `sudo ./make s905x 5.4.149`. When multiple kernel versions are generated at one time, the kernel version number is connected with `_` . Run Eg: `sudo ./make s922x_s905x3 5.4.149_5.14.6`. When there is an latest version of the same series of the specified kernel version, the `latest version` will be download from [kernel library](https://github.com/ophub/flippy-kernel/tree/main/library) and used automatically. When you want to compile a `fixed kernel`, Run `sudo ./make s905x 5.4.149 false`.

💡Tips: The ***`s905x`*** and ***`s905w`*** boxs currently only support `5.4.*` kernels, Cannot use kernel version 5.10 and above. Please add kernel substitution variables when compiling these two models of devices. Other devices can be freely selected.

- ### Use GitHub Action to make instructions

Expand Down Expand Up @@ -70,5 +75,5 @@ The [armbian](https://github.com/armbian/build) directly calls the official sour

## License

[LICENSE](https://github.com/ophub/build-armbian/blob/main/LICENSE) © OPHUB
[LICENSE](https://github.com/ophub/amlogic-s9xxx-armbian/blob/main/LICENSE) © OPHUB

174 changes: 116 additions & 58 deletions make
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ tmp_aml_image=${make_path}/tmp_aml_image

kernel_library="https://github.com/ophub/flippy-kernel/tree/main/library"
#kernel_library="https://github.com/ophub/flippy-kernel/trunk/library"

build_armbian=("s922x" "s905x3" "s905x2" "s912" "s905d" "s905x" "s905w")
build_kernel=("default")
change_kernel="no"
#===== Do not modify the following parameter settings, End =======

# Specify Amlogic soc
build_armbian=("s922x" "s905x3" "s905x2" "s912" "s905d" "s905x" "s905w")
if [ -n "${1}" ]; then
unset build_armbian
oldIFS=$IFS
Expand All @@ -32,13 +35,73 @@ if [ -n "${1}" ]; then
IFS=$oldIFS
fi

# Set whether to change the kernel version
# Set whether to replace the kernel
if [ -n "${2}" ]; then
auto_kernel="${3}"
change_kernel="yes"
new_kernel="${2}"
else
change_kernel="no"
new_kernel=""
unset build_kernel
oldIFS=$IFS
IFS=_
build_kernel=(${2})
IFS=$oldIFS

# Convert kernel library address to svn format
if [[ ${kernel_library} == http* && $(echo ${kernel_library} | grep "tree/main") != "" ]]; then
kernel_library=${kernel_library//tree\/main/trunk}
fi

# Check the new version on the kernel library
if [[ -z "${auto_kernel}" || "${auto_kernel}" != "false" ]]; then

# Set empty array
TMP_ARR_KERNELS=()

# Convert kernel library address to API format
SERVER_KERNEL_URL=${kernel_library#*com\/}
SERVER_KERNEL_URL=${SERVER_KERNEL_URL//trunk/contents}
SERVER_KERNEL_URL="https://api.github.com/repos/${SERVER_KERNEL_URL}"

# Query the latest kernel in a loop
i=1
for KERNEL_VAR in ${build_kernel[*]}; do
echo -e "(${i}) Auto query the latest kernel version of the same series for [ ${KERNEL_VAR} ]"
MAIN_LINE_M=$(echo "${KERNEL_VAR}" | cut -d '.' -f1)
MAIN_LINE_V=$(echo "${KERNEL_VAR}" | cut -d '.' -f2)
MAIN_LINE_S=$(echo "${KERNEL_VAR}" | cut -d '.' -f3)
MAIN_LINE="${MAIN_LINE_M}.${MAIN_LINE_V}"
# Check the version on the server (e.g LATEST_VERSION="124")
LATEST_VERSION=$(curl -s "${SERVER_KERNEL_URL}" | grep "name" | grep -oE "${MAIN_LINE}.[0-9]+" | sed -e "s/${MAIN_LINE}.//g" | sort -n | sed -n '$p')
if [[ "$?" -eq "0" && ! -z "${LATEST_VERSION}" ]]; then
TMP_ARR_KERNELS[${i}]="${MAIN_LINE}.${LATEST_VERSION}"
else
TMP_ARR_KERNELS[${i}]="${KERNEL_VAR}"
fi
echo -e "(${i}) [ ${TMP_ARR_KERNELS[$i]} ] is latest kernel. \n"

let i++
done

# Reset the kernel array to the latest kernel version
unset build_kernel
build_kernel=${TMP_ARR_KERNELS[*]}

fi

# Synchronization related kernel
i=1
for KERNEL_VAR in ${build_kernel[*]}; do
if [ ! -d "${kernel_path}/${KERNEL_VAR}" ]; then
echo -e "(${i}) [ ${KERNEL_VAR} ] Kernel loading from [ ${kernel_library}/${KERNEL_VAR} ]"
svn checkout ${kernel_library}/${KERNEL_VAR} ${kernel_path}/${KERNEL_VAR} >/dev/null
rm -rf ${kernel_path}/${KERNEL_VAR}/.svn >/dev/null && sync
else
echo -e "(${i}) [ ${KERNEL_VAR} ] Kernel is in the local directory."
fi

let i++
done

sync
fi

die() {
Expand All @@ -47,7 +110,7 @@ die() {
}

process() {
echo -e " [\033[1;32m ${build_soc} \033[0m] ${1}"
echo -e " [\033[1;32m ${build_soc}${out_kernel} \033[0m] ${1}"
}

make_image() {
Expand Down Expand Up @@ -95,7 +158,7 @@ make_image() {
extract_armbian() {
cd ${make_path}

armbian_image_file="${tmp_aml_image}/armbian-${build_soc}.img"
armbian_image_file="${tmp_aml_image}/armbian_${build_soc}_${make_version}.img"
rm -f ${armbian_image_file} 2>/dev/null && sync
cp -f $( ls ${armbian_outputpath}/*_Lepotato_*.img 2>/dev/null | head -n 1 ) ${armbian_image_file}
sync && sleep 3
Expand All @@ -116,48 +179,30 @@ extract_armbian() {
replace_kernel() {
# Replace if specified
if [[ "${change_kernel}" == "yes" ]]; then

# 01. Check kernel files
cd ${kernel_path}

if [ "$( ls ${new_kernel}/*${new_kernel}-*.tar.gz -l 2>/dev/null | grep "^-" | wc -l )" -eq "0" ]; then
# If the kernel is not found, try to download from the server: ${kernel_library}
if [[ ${kernel_library} == http* && $(echo ${kernel_library} | grep "tree/main") != "" ]]; then
kernel_library=${kernel_library//tree\/main/trunk}
fi
svn checkout ${kernel_library}/${new_kernel} ${new_kernel} >/dev/null && rm -rf ${new_kernel}/.svn >/dev/null && sync
fi

[ -f ${new_kernel}/boot-${new_kernel}-*.tar.gz ] || die "Missing boot-${new_kernel}-*.tar.gz file!"
[ -f ${new_kernel}/dtb-amlogic-${new_kernel}-*.tar.gz ] || die "Missing dtb-amlogic-${new_kernel}-*.tar.gz file!"
[ -f ${new_kernel}/modules-${new_kernel}-*.tar.gz ] || die "Missing modules-${new_kernel}-*.tar.gz file!"

build_boot=$( ls ${new_kernel}/boot-${new_kernel}-*.tar.gz 2>/dev/null | head -n 1 )
build_dtb=$( ls ${new_kernel}/dtb-amlogic-${new_kernel}-*.tar.gz 2>/dev/null | head -n 1 )
build_modules=$( ls ${new_kernel}/modules-${new_kernel}-*.tar.gz 2>/dev/null | head -n 1 )

# 02. Rplace kernel
cd ${make_path}

# 02.01 For /boot five files
build_boot=$( ls ${kernel_path}/${new_kernel}/boot-${new_kernel}-*.tar.gz 2>/dev/null | head -n 1 )
build_dtb=$( ls ${kernel_path}/${new_kernel}/dtb-amlogic-${new_kernel}-*.tar.gz 2>/dev/null | head -n 1 )
build_modules=$( ls ${kernel_path}/${new_kernel}/modules-${new_kernel}-*.tar.gz 2>/dev/null | head -n 1 )

# 01 For /boot five files
( cd ${tmp_armbian}/boot && rm -rf *-${out_version}-* uInitrd zImage dtb* 2>/dev/null && sync )
tar -xzf ${kernel_path}/${build_boot} -C ${tmp_armbian}/boot && sync
tar -xzf ${build_boot} -C ${tmp_armbian}/boot && sync
[ "$( ls ${tmp_armbian}/boot/*-${new_kernel}-* -l 2>/dev/null | grep "^-" | wc -l )" -ge "4" ] || die "boot/ 5 files is missing."

# 02.02 For dtb files
# 02 For dtb files
mkdir -p ${tmp_armbian}/boot/dtb/amlogic && sync
tar -xzf ${kernel_path}/${build_dtb} -C ${tmp_armbian}/boot/dtb/amlogic && sync
tar -xzf ${build_dtb} -C ${tmp_armbian}/boot/dtb/amlogic && sync

# 02.03 For usr/lib/modules/*
# 03 For usr/lib/modules/*
( cd ${tmp_armbian}/usr/lib/modules && rm -rf * 2>/dev/null && sync )
tar -xzf ${kernel_path}/${build_modules} -C ${tmp_armbian}/usr/lib/modules && sync
tar -xzf ${build_modules} -C ${tmp_armbian}/usr/lib/modules && sync
( cd ${tmp_armbian}/usr/lib/modules/*/ && echo "build source" | xargs rm -f && sync )
if [ "$(ls ${tmp_armbian}/usr/lib/modules/${new_kernel}* -l 2>/dev/null | grep "^d" | wc -l)" -ne "1" ]; then
die "${tmp_armbian}/usr/lib/modules/${new_kernel}-* kernel folder is missing."
fi

sync

fi
}

Expand Down Expand Up @@ -370,32 +415,45 @@ echo -e "Ready, start build armbian... \n"
# Start loop compilation
k=1
for b in ${build_armbian[*]}; do
build_soc=${b}

echo -n "(${k}) Start build armbian [ ${b} ]. "
now_remaining_space=$(df -hT ${PWD} | grep '/dev/' | awk '{print $5}' | sed 's/.$//')
if [[ "${now_remaining_space}" -le "2" ]]; then
echo "Remaining space is less than 2G, exit this packaging. \n"
break
else
echo "Remaining space is ${now_remaining_space}G."
fi

process " (1/5) make new armbian image."
make_image
process " (2/5) extract old armbian files."
extract_armbian
process " (3/5) replace kernel for armbian."
replace_kernel
process " (4/5) copy files to new image."
copy_files
process " (5/5) clear temp files."
clean_tmp
i=1
for x in ${build_kernel[*]}; do
{
echo -n "(${k}.${i}) Start build armbian [ ${b} - ${x} ]. "
now_remaining_space=$(df -hT ${PWD} | grep '/dev/' | awk '{print $5}' | sed 's/.$//')
if [[ "${now_remaining_space}" -le "2" ]]; then
echo "Remaining space is less than 2G, exit this packaging. \n"
break
else
echo "Remaining space is ${now_remaining_space}G."
fi

build_soc="${b}"
if [ "${x}" == "default" ]; then
new_kernel=""
out_kernel=""
else
new_kernel="${x}"
out_kernel=" - ${x}"
fi

echo -e "(${k}) Build successfully. \n"
process " (1/5) make new armbian image."
make_image
process " (2/5) extract old armbian files."
extract_armbian
process " (3/5) replace kernel for armbian."
replace_kernel
process " (4/5) copy files to new image."
copy_files
process " (5/5) clear temp files."
clean_tmp

echo -e "(${k}.${i}) Build successfully. \n"
let i++
}
done

let k++

done

echo -e "Server space usage after compilation: \n$(df -hT ${PWD}) \n"
Expand Down

0 comments on commit 6d726ac

Please sign in to comment.