init/init-apt.sh
Martin 7beb4ca57f
Some checks failed
Build Docker Image / Explore-Gitea-Actions (push) Has been cancelled
更新 apt docker
2025-09-07 14:27:11 +08:00

83 lines
2.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Alpine 相关处理
handle_alpine() {
echo "检测到 Alpine切换 apk 源..."
sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://reg.martin98.com/repository/alpine#g' /etc/apk/repositories
echo "安装 openntpd 和 vim..."
apk update
apk add --no-cache openntpd vim
# 启动并设置 openntpd 开机自启
rc-service openntpd restart
rc-update add openntpd default
# 配置 vim 粘贴
if ! grep -q "set pastetoggle=" ~/.vimrc; then
echo "set pastetoggle=" >> ~/.vimrc
fi
}
# Debian/Ubuntu 相关处理
handle_debian_ubuntu() {
APT_OPTIONS="-o Acquire::https::reg.martin98.com::Verify-Peer=false -o Acquire::https::reg.martin98.com::Verify-Host=false"
lsb_dist=$(cat /etc/*release | grep ^ID= | cut -d= -f2) # ubuntu or debian?
release=$(cat /etc/*release | grep VERSION_CODENAME | cut -d= -f2) # ubuntu(jammy oracular) debian(bookworm)....
components=$([ "${lsb_dist=}" = "debian" ] && echo "main contrib non-free non-free-firmware" || ([ "${lsb_dist=}" = "ubuntu" ] && echo "main restricted universe multiverse"))
# 删除 DEB822 格式
if [ -f /etc/apt/sources.list.d/${lsb_dist}.sources ]; then
rm /etc/apt/sources.list.d/${lsb_dist}.sources
fi
# 配置镜像源
cat <<EOF > /etc/apt/sources.list
deb https://reg.martin98.com/repository/$lsb_dist $release $components
deb https://reg.martin98.com/repository/$lsb_dist $release-updates $components
deb https://reg.martin98.com/repository/$lsb_dist $release-backports $components
EOF
for RELEASE in "$@"; do
echo "deb https://reg.martin98.com/repository/$lsb_dist $RELEASE $components" >> /etc/apt/sources.list
done
# 关闭交互式弹窗
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
apt update $APT_OPTIONS && apt install $APT_OPTIONS -y ca-certificates curl && update-ca-certificates
apt update && apt upgrade -y
# 检查并安装 vim
if ! command -v vim >/dev/null 2>&1; then
apt install $APT_OPTIONS -y vim
fi
# NTP
# 启用阿里云 ntp
if pidof systemd >/dev/null 2>&1; then
apt install chrony -y
cat > /etc/chrony.conf <<EOF
server ntp4.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp5.aliyun.com iburst
driftfile /var/lib/chrony/drift
allow 127.0.0.1
EOF
timedatectl set-timezone Asia/Shanghai
systemctl restart chrony && systemctl enable chrony
else
echo "systemctl 不存在,跳过 chrony 的重启"
fi
# fix vim 粘贴
if ! grep -q "set pastetoggle=" ~/.vimrc; then
echo "set pastetoggle=" >> ~/.vimrc
fi
}
# 主流程
if grep -qi 'alpine' /etc/*release; then
handle_alpine
exit 0
else
handle_debian_ubuntu "$@"
fi