#!/bin/bash
set -euo pipefail# 檢查是否以root權限運行
if [ "$(id -u)" -ne 0 ]; then
? ? echo "錯誤:請使用root權限或sudo運行本腳本" >&2
? ? exit 1
fi# 檢測openEuler系統(兼容大小寫)
detect_distribution() {
? ? if [ -f /etc/os-release ]; then
? ? ? ? . /etc/os-release
? ? ? ? # 匹配ID為openEuler(兼容大小寫,如"openEuler"或"openeuler")
? ? ? ? if [[ "$ID" =~ ^[Oo]pen[Ee]uler$ ]]; then
? ? ? ? ? ? echo "openeuler"
? ? ? ? else
? ? ? ? ? ? echo "unsupported"
? ? ? ? fi
? ? else
? ? ? ? echo "unsupported"
? ? fi
}DISTRO=$(detect_distribution)
if [[ "$DISTRO" != "openeuler" ]]; then
? ? echo "錯誤:本腳本僅適用于openEuler系統" >&2
? ? exit 1
fi# 安裝前置依賴(openEuler 25.03使用dnf)
install_dependencies() {
? ? dnf update -y
? ? dnf install -y \
? ? ? ? curl \
? ? ? ? gnupg \
? ? ? ? ca-certificates \
? ? ? ? jq \
? ? ? ? tar \
? ? ? ? gzip \
? ? ? ? device-mapper-persistent-data \
? ? ? ? lvm2 ?# 容器存儲依賴
}# 安裝containerd(使用openEuler官方倉庫)
install_containerd() {
? ? # 安裝最新穩定版containerd
? ? dnf install -y containerd? ? # 優化containerd配置(適配openEuler 25.03的systemd和cgroupv2)
? ? sed -i '/SystemdCgroup =/c\SystemdCgroup = true' /etc/containerd/config.toml
? ? sed -i 's/cri\.containerd\.runtime\.v1\.linux/cri.containerd.untrusted-workload.v1.linux/g' /etc/containerd/config.toml ?# 啟用非信任工作負載支持? ? # 重啟并啟用服務
? ? systemctl daemon-reload
? ? systemctl restart containerd
? ? systemctl enable containerd
}# 安裝最新版nerdctl(適配openEuler架構)
install_nerdctl() {
? ? # 獲取最新穩定版(排除預發布)
? ? LATEST_NERDCTL=$(curl -s https://api.github.com/repos/containerd/nerdctl/releases/latest | jq -r '.tag_name')
? ? if [[ -z "$LATEST_NERDCTL" || "$LATEST_NERDCTL" == "null" ]]; then
? ? ? ? echo "獲取nerdctl最新版本失敗,可能是網絡問題" >&2
? ? ? ? exit 1
? ? fi? ? # 識別架構(支持x86_64/aarch64)
? ? ARCH=$(case $(uname -m) in
? ? ? ? x86_64) echo "amd64" ;;
? ? ? ? aarch64) echo "arm64" ;;
? ? ? ? *) echo "unsupported"; exit 1 ;;
? ? esac)
?? ?
?? ?echo "下載文件:https://github.com/containerd/nerdctl/releases/download/${LATEST_NERDCTL}/nerdctl-${LATEST_NERDCTL#v}-linux-${ARCH}.tar.gz"? ? # 下載并安裝
? ? curl -fsSL "https://github.com/containerd/nerdctl/releases/download/${LATEST_NERDCTL}/nerdctl-${LATEST_NERDCTL#v}-linux-${ARCH}.tar.gz" \
? ? ? ? | tar -xz -C /usr/local/bin? ? # 驗證安裝
? ? if ! command -v nerdctl &> /dev/null; then
? ? ? ? echo "nerdctl安裝失敗" >&2
? ? ? ? exit 1
? ? fi
}# 安裝buildkit并配置systemd服務(適配openEuler)
install_buildkit() {
? ? # 獲取最新穩定版
? ? LATEST_BUILDKIT=$(curl -s https://api.github.com/repos/moby/buildkit/releases/latest | jq -r '.tag_name')
? ? if [[ -z "$LATEST_BUILDKIT" || "$LATEST_BUILDKIT" == "null" ]]; then
? ? ? ? echo "獲取buildkit最新版本失敗,可能是網絡問題" >&2
? ? ? ? exit 1
? ? fi? ? # 識別架構
? ? ARCH=$(case $(uname -m) in
? ? ? ? x86_64) echo "amd64" ;;
? ? ? ? aarch64) echo "arm64" ;;
? ? ? ? *) echo "unsupported"; exit 1 ;;
? ? esac)
?? ?
? ? # 下載并安裝
? ? curl -fL "https://github.com/moby/buildkit/releases/download/${LATEST_BUILDKIT}/buildkit-${LATEST_BUILDKIT}.linux-${ARCH}.tar.gz" \
? ? ? ? | tar -xz -C /usr/local? ? # 創建buildkitd服務文件(優化openEuler集成)
? ? cat > /etc/systemd/system/buildkitd.service <<EOF
[Unit]
Description=BuildKit Daemon for openEuler 25.03
Documentation=https://github.com/moby/buildkit
After=network.target containerd.service
Requires=containerd.service[Service]
Type=simple
ExecStart=/usr/local/bin/buildkitd \
? ? --oci-worker=true \
? ? --containerd-worker=true \
? ? --containerd-worker-namespace=k8s.io \
? ? --addr=unix:///run/buildkit/buildkitd.sock \
? ? --oci-worker-snapshotter=overlayfs
Restart=on-failure
RestartSec=5
LimitNOFILE=1048576
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"[Install]
WantedBy=multi-user.target
EOF? ? # 啟動服務并配置開機自啟
? ? systemctl daemon-reload
? ? systemctl start buildkitd
? ? systemctl enable buildkitd
}# 主執行流程
echo "=== 檢測到openEuler 25.03系統,開始安裝 ==="echo "=== 1/4 安裝前置依賴 ==="
install_dependenciesecho "=== 2/4 安裝并配置containerd ==="
install_containerdecho "=== 3/4 安裝最新版nerdctl ==="
install_nerdctlecho "=== 4/4 安裝并配置buildkit ==="
install_buildkitecho "=== 安裝驗證 ==="
echo "nerdctl版本: $(nerdctl --version)"
echo "buildkitd狀態: $(systemctl is-active buildkitd)"
echo "containerd狀態: $(systemctl is-active containerd)"echo "安裝完成!nerdctl和buildkit已配置為開機啟動"
? ??