From 7a0dc6bf9f2fd384dc42bd3c026526afe07dac11 Mon Sep 17 00:00:00 2001 From: bluesky Date: Wed, 24 Jul 2024 20:22:58 +0800 Subject: [PATCH] feat: use mmdebstrap to build rootfs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 使用 mmdebstrap 制作根文件系统,定制化更小。 * 添加多架构支持。 * 修改为 nile 版本 Log: new rootfs --- .github/workflows/build-rootfs.yml | 66 ++++++++++++++++++++++++++++++ README.md | 10 +---- build.sh | 45 +++++++++++++++++--- config/apt/sources.list | 5 +++ config/hooks.chroot/second-stage | 26 ++++++++++++ config/packages.list/packages.list | 20 +++++++++ openkylin-anything.list | 1 - sources.list | 3 -- version.txt | 1 + yangtze.multistrap | 18 -------- 10 files changed, 159 insertions(+), 36 deletions(-) create mode 100644 .github/workflows/build-rootfs.yml mode change 100755 => 100644 build.sh create mode 100644 config/apt/sources.list create mode 100755 config/hooks.chroot/second-stage create mode 100644 config/packages.list/packages.list delete mode 100644 openkylin-anything.list delete mode 100644 sources.list create mode 100644 version.txt delete mode 100644 yangtze.multistrap diff --git a/.github/workflows/build-rootfs.yml b/.github/workflows/build-rootfs.yml new file mode 100644 index 0000000..160d905 --- /dev/null +++ b/.github/workflows/build-rootfs.yml @@ -0,0 +1,66 @@ +name: build rootfs + +on: + push: + branches: [ 'master' ] + paths: + - 'build.sh' + - 'config/**' + - 'version.txt' + pull_request: + branches: [ 'master' ] + paths: + - 'build.sh' + - 'config/**' + - 'version.txt' + workflow_dispatch: + +jobs: + build: + name: Build tar file + runs-on: ubuntu-latest + env: + GITHUB_TOKEN: ${{ secrets.TOKEN }} + + steps: + - name: Maximize build space + uses: easimon/maximize-build-space@master + with: + # 这个值是保留给系统的空间大小,之前设置太小,总会在安装依赖或者编译过程中报设备空间不足的错误而导致编译失败 + root-reserve-mb: 4096 + swap-size-mb: 512 + remove-dotnet: 'true' + remove-android: 'true' + remove-codeql: 'true' + + - name: Checkout repository + uses: actions/checkout@v4 + + - name: update source and add binfmts # 更新源到 noble + run: | + sudo sed -i 's/jammy/noble/g' /etc/apt/sources.list + sudo apt update -y && sudo apt install mmdebstrap qemu-user-static usrmerge qemu-user systemd + sudo apt-mark hold usrmerge + sudo cp -v /usr/lib/arch-test/riscv64 /usr/lib/arch-test/loong64 + sudo sed -i "/riscv64 => 'riscv64',/a\ loong64 => 'loongarch64'," /usr/bin/mmdebstrap + sudo systemctl restart systemd-binfmt + + - name: Run build.sh + run: | + sudo bash build.sh + + - name: Read version + id: read_version + run: echo "VERSION=$(cat version.txt)" >> $GITHUB_ENV + + - name: Upload file to release + uses: "marvinpinto/action-automatic-releases@latest" + with: + repo_token: "${{ env.GITHUB_TOKEN }}" + automatic_release_tag: "v${{ env.VERSION }}" + prerelease: true + title: "v${{ env.VERSION }}" + files: | + rootfs/openkylin-rootfs-amd64.tar.gz + rootfs/openkylin-rootfs-arm64.tar.gz + rootfs/openkylin-rootfs-riscv64.tar.gz \ No newline at end of file diff --git a/README.md b/README.md index 9aff549..88de242 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,11 @@ # 介绍 -使用 Multistrap 工具制作根文件系统,Multistrap是一个工具,可以用来构建一个完整的、可启动的、根文件系统。这个根文件系统可以被 Docker 和 WSL 使用。 - +使用 mmdebstrap 工具制作根文件系统,mmdebstrap 是一个工具,允许你选择性地包含或排除某些软件包,这样可以创建更小的、定制化的根文件系统。这个根文件系统可以被 Docker 和 WSL 使用。 # 使用 -yangtze.multistrap 是配置文件。 - ```bash bash build.sh -sudo tar -cf yangtze-rootfs.tar -C /yangtze-rootfs . ``` -# 声明 - -参考 [deepin-docker](https://github.com/BLumia/deepin-docker) 制作 +支持多加个架构 amd64 arm64 riscv64 架构。 diff --git a/build.sh b/build.sh old mode 100755 new mode 100644 index e2e697d..a8569e1 --- a/build.sh +++ b/build.sh @@ -1,14 +1,47 @@ #!/bin/bash -set -e +set -e -u -apt update +sudo apt update +# 不进行交互安装 export DEBIAN_FRONTEND=noninteractive +ROOTFS=`mktemp -d` +dist_version="nile" +dist_name="openkylin" +readarray -t REPOS < ./config/apt/sources.list +PACKAGES=`cat ./config/packages.list/packages.list | grep -v "^-" | xargs | sed -e 's/ /,/g'` +OUT_DIR=$(dirname "$(realpath "$0")")/rootfs -apt install multistrap -y +mkdir -p $OUT_DIR +sudo apt install -y curl git mmdebstrap qemu-user-static usrmerge usr-is-merged binfmt-support systemd-container +# 开启异架构支持 +sudo systemctl start systemd-binfmt -multistrap -f yangtze.multistrap +# 安装软件包签名公钥 +curl http://archive.build.openkylin.top/openkylin/pool/main/o/openkylin-keyring/openkylin-keyring_2022.05.12-ok1_all.deb --output openkylin-keyring.deb +sudo apt install ./openkylin-keyring.deb && rm ./openkylin-keyring.deb -cp sources.list /yangtze-rootfs/etc/apt/sources.list -cp openkylin-anything.list /yangtze-rootfs/etc/apt/sources.list.d/openkylin-anything.list +curl http://archive.build.openkylin.top/openkylin/pool/main/o/openkylin-archive-anything/openkylin-archive-anything_2023.02.06-ok4_all.deb --output openkylin-archive-anything.deb +sudo apt install ./openkylin-archive-anything.deb && rm ./openkylin-archive-anything.deb + +for arch in amd64 arm64 riscv64; do + sudo mmdebstrap \ + --hook-dir=/usr/share/mmdebstrap/hooks/merged-usr \ + --include=$PACKAGES \ + --components="main,cross,pty" \ + --variant=minbase \ + --architectures=${arch} \ + --customize=./config/hooks.chroot/second-stage \ + $dist_version \ + $ROOTFS \ + "${REPOS[@]}" + + # 生成压缩包 + pushd $OUT_DIR + rm -rf $dist_name-rootfs-$arch.tar.gz + sudo tar -zcf $dist_name-rootfs-$arch.tar.gz -C $ROOTFS . + # 删除临时文件夹 + sudo rm -rf $ROOTFS + popd +done diff --git a/config/apt/sources.list b/config/apt/sources.list new file mode 100644 index 0000000..1dea0a0 --- /dev/null +++ b/config/apt/sources.list @@ -0,0 +1,5 @@ +deb http://archive.build.openkylin.top/openkylin nile main cross pty +deb http://archive.build.openkylin.top/openkylin nile-security main cross pty +deb http://archive.build.openkylin.top/openkylin nile-updates main cross pty +deb http://archive.build.openkylin.top/openkylin nile-proposed main cross pty +deb http://ppa.build.openkylin.top/kylinsoft/anything/openkylin/ yangtze main \ No newline at end of file diff --git a/config/hooks.chroot/second-stage b/config/hooks.chroot/second-stage new file mode 100755 index 0000000..e7ffc89 --- /dev/null +++ b/config/hooks.chroot/second-stage @@ -0,0 +1,26 @@ +#!/bin/sh + +rootdir="$1" + +# 执行命令的辅助函数 +run_in_chroot() { + systemd-nspawn -D "$rootdir" bash -c "$@" +} + +# 设置语言 +run_in_chroot " +sed -i -E 's/#[[:space:]]?(en_US.UTF-8[[:space:]]+UTF-8)/\1/g' /etc/locale.gen +sed -i -E 's/#[[:space:]]?(zh_CN.UTF-8[[:space:]]+UTF-8)/\1/g' /etc/locale.gen + +locale-gen +DEBIAN_FRONTEND=noninteractive dpkg-reconfigure locales +" + +# 微软提供的 wsl 启动器会调用adduser,需要将 USERS_GID 和 USERS_GROUP 注释。 +run_in_chroot "sed -i -e 's/USERS_GID=100/#USERS_GID=100/' -e 's/USERS_GROUP=users/#USERS_GROUP=users/' /etc/adduser.conf" + +# 清理缓存 +run_in_chroot " +apt clean +rm -rf /var/cache/apt/archives/* +" \ No newline at end of file diff --git a/config/packages.list/packages.list b/config/packages.list/packages.list new file mode 100644 index 0000000..9a5a47b --- /dev/null +++ b/config/packages.list/packages.list @@ -0,0 +1,20 @@ +ca-certificates +locales +sudo +apt +adduser +polkitd +systemd +dbus-daemon +parted +curl +vim +init +iputils-ping +net-tools +pciutils +lshw +iproute2 +iptables +locales +openkylin-archive-anything \ No newline at end of file diff --git a/openkylin-anything.list b/openkylin-anything.list deleted file mode 100644 index af5eb65..0000000 --- a/openkylin-anything.list +++ /dev/null @@ -1 +0,0 @@ -deb http://ppa.build.openkylin.top/kylinsoft/anything/openkylin/ yangtze main \ No newline at end of file diff --git a/sources.list b/sources.list deleted file mode 100644 index a330974..0000000 --- a/sources.list +++ /dev/null @@ -1,3 +0,0 @@ -deb http://archive.build.openkylin.top/openkylin/ yangtze main cross pty -deb http://archive.build.openkylin.top/openkylin/ yangtze-security main cross pty -deb http://archive.build.openkylin.top/openkylin/ yangtze-updates main cross pty \ No newline at end of file diff --git a/version.txt b/version.txt new file mode 100644 index 0000000..1cc5f65 --- /dev/null +++ b/version.txt @@ -0,0 +1 @@ +1.1.0 \ No newline at end of file diff --git a/yangtze.multistrap b/yangtze.multistrap deleted file mode 100644 index 091b37b..0000000 --- a/yangtze.multistrap +++ /dev/null @@ -1,18 +0,0 @@ -[General] - -arch=amd64 -directory=/yangtze-rootfs -cleanup=true -noauth=false -unpack=true -explicitsuite=false -multiarch= -aptsources=Ubuntu -bootstrap=yangtze - -[yangtze] - -packages=apt ca-certificates locales sudo systemd openkylin-archive-anything neofetch htop vim git fakeroot build-essential libncurses-dev xz-utils libssl-dev bc flex libelf-dev bison -source=http://archive.build.openkylin.top/openkylin/ -keyring=openkylin-keyring -suite=yangtze