Last active 1748426794

Init a server from cloud provider

anduin's Avatar anduin revised this gist 1748426793. Go to revision

1 file changed, 3 insertions, 1 deletion

init.sh

@@ -140,12 +140,14 @@ run_remote "sudo sed -i 's/PermitRootLogin yes/PermitRootLogin no/; \
140 140
141 141 # 9) Remove other non-system users
142 142 print_ok "Removing other users"
143 - others=$(run_remote "awk -F: -v skip='$NEWUSER' '\$3>=1000 && \$1!=skip {print \$1}' /etc/passwd")
143 + others=$(run_remote "awk -F: -v skip='$NEWUSER' '\$3>=1000 && \$1!=skip && \$1!=\"nobody\" && \$1!=\"nogroup\" {print \$1}' /etc/passwd")
144 +
144 145 for u in $others; do
145 146 print_warn "Deleting user $u"
146 147 run_remote "sudo pkill -u $u || true; sudo deluser --remove-home $u"
147 148 done
148 149
150 +
149 151 # 10) Reset machine-id
150 152 print_ok "Resetting machine-id"
151 153 run_remote "sudo rm -f /etc/machine-id /var/lib/dbus/machine-id && \

anduin's Avatar anduin revised this gist 1748425893. Go to revision

1 file changed, 2 insertions

mirror.sh

@@ -236,6 +236,8 @@ main() {
236 236 aptVersion=$(apt --version | head -n 1 | awk '{print $2}')
237 237 echo "Current APT version: $aptVersion"
238 238
239 + apt_major_version=$(echo "$aptVersion" | cut -d. -f1)
240 +
239 241 # If current APT version is 3.0 or higher, and using old format or none, convert to new format
240 242 # sudo apt modernize-sources
241 243 if [[ $apt_major_version -ge 3 && ( "$format" == "old" || "$format" == "none" ) ]]; then

anduin's Avatar anduin revised this gist 1748425443. Go to revision

1 file changed, 2 insertions, 2 deletions

mirror.sh

@@ -191,7 +191,7 @@ EOF
191 191 main() {
192 192 # Ensure required packages are installed
193 193 sudo apt update
194 - sudo apt install -y curl lsb-release bc
194 + sudo apt install -y curl lsb-release
195 195
196 196 # Get current source format
197 197 format=$(check_apt_format)
@@ -238,7 +238,7 @@ main() {
238 238
239 239 # If current APT version is 3.0 or higher, and using old format or none, convert to new format
240 240 # sudo apt modernize-sources
241 - if [[ $(echo "$aptVersion >= 3.0" | bc) -eq 1 && ( "$format" == "old" || "$format" == "none" ) ]]; then
241 + if [[ $apt_major_version -ge 3 && ( "$format" == "old" || "$format" == "none" ) ]]; then
242 242 echo "APT version is 3.0 or higher, converting to new format"
243 243 sudo apt modernize-sources
244 244 echo "APT sources converted to new format"

anduin's Avatar anduin revised this gist 1748425303. Go to revision

1 file changed, 1 insertion, 1 deletion

mirror.sh

@@ -191,7 +191,7 @@ EOF
191 191 main() {
192 192 # Ensure required packages are installed
193 193 sudo apt update
194 - sudo apt install -y curl lsb-release
194 + sudo apt install -y curl lsb-release bc
195 195
196 196 # Get current source format
197 197 format=$(check_apt_format)

anduin's Avatar anduin revised this gist 1748425230. Go to revision

1 file changed, 55 insertions, 55 deletions

mirror.sh

@@ -1,26 +1,26 @@
1 1 #!/usr/bin/env bash
2 2 set -euo pipefail
3 3
4 - # 判断当前 APT 源格式状态
4 + # Check current APT source format status
5 5 check_apt_format() {
6 6 local old_format=false
7 7 local new_format=false
8 8
9 - # 检查老格式 (.list)
9 + # Check old format (.list)
10 10 if [ -f "/etc/apt/sources.list" ]; then
11 11 if grep -v '^#' /etc/apt/sources.list | grep -q '[^[:space:]]'; then
12 12 old_format=true
13 13 fi
14 14 fi
15 15
16 - # 检查新格式中的 ubuntu.sources 文件
16 + # Check for ubuntu.sources file in new format
17 17 if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
18 18 if grep -v '^#' /etc/apt/sources.list.d/ubuntu.sources | grep -q '[^[:space:]]'; then
19 19 new_format=true
20 20 fi
21 21 fi
22 22
23 - # 返回状态
23 + # Return status
24 24 if $old_format && $new_format; then
25 25 echo "both"
26 26 elif $old_format; then
@@ -32,15 +32,15 @@ check_apt_format() {
32 32 fi
33 33 }
34 34
35 - # 寻找最快的镜像源
35 + # Find the fastest mirror
36 36 find_fastest_mirror() {
37 - # 所有要显示的内容都重定向到标准错误 >&2
38 - echo "正在测试镜像源速度..." >&2
37 + # Redirect all output to stderr
38 + echo "Testing mirror speeds..." >&2
39 39
40 - # 获取当前 Ubuntu 代号
40 + # Get current Ubuntu codename
41 41 codename=$(lsb_release -cs)
42 42
43 - # 定义潜在的镜像源列表
43 + # Define list of potential mirrors
44 44 mirrors=(
45 45 "https://archive.ubuntu.com/ubuntu/"
46 46 "https://mirror.aarnet.edu.au/pub/ubuntu/archive/" # Australia
@@ -68,15 +68,15 @@ find_fastest_mirror() {
68 68 "http://tw.archive.ubuntu.com/ubuntu/" # Taiwan
69 69 "https://mirror.twds.com.tw/ubuntu/" # Taiwan
70 70 "https://ubuntu.mirrors.uk2.net/ubuntu/" # United Kingdom
71 - "http://mirrors.ustc.edu.cn/ubuntu/" # 中国科学技术大学
72 - "http://ftp.sjtu.edu.cn/ubuntu/" # 上海交通大学
73 - "http://mirrors.tuna.tsinghua.edu.cn/ubuntu/" # 清华大学
74 - "http://mirrors.aliyun.com/ubuntu/" # 阿里云
75 - "http://mirrors.163.com/ubuntu/" # 网易
76 - "http://mirrors.cloud.tencent.com/ubuntu/" # 腾讯云
71 + "http://mirrors.ustc.edu.cn/ubuntu/" # USTC
72 + "http://ftp.sjtu.edu.cn/ubuntu/" # SJTU
73 + "http://mirrors.tuna.tsinghua.edu.cn/ubuntu/" # Tsinghua
74 + "http://mirrors.aliyun.com/ubuntu/" # Aliyun
75 + "http://mirrors.163.com/ubuntu/" # NetEase
76 + "http://mirrors.cloud.tencent.com/ubuntu/" # Tencent Cloud
77 77 "http://mirror.aiursoft.cn/ubuntu/" # Aiursoft
78 - "http://mirrors.huaweicloud.com/ubuntu/" # 华为云
79 - "http://mirrors.zju.edu.cn/ubuntu/" # 浙江大学
78 + "http://mirrors.huaweicloud.com/ubuntu/" # Huawei Cloud
79 + "http://mirrors.zju.edu.cn/ubuntu/" # Zhejiang University
80 80 "http://azure.archive.ubuntu.com/ubuntu/" # Azure
81 81 "https://mirrors.isu.net.sa/apt-mirror/" # Saudi Arabia
82 82 "https://mirror.team-host.ru/ubuntu/" # Russia
@@ -89,9 +89,9 @@ find_fastest_mirror() {
89 89
90 90 declare -A results
91 91
92 - # 测试单个镜像源的速度
92 + # Test speed of each mirror
93 93 for mirror in "${mirrors[@]}"; do
94 - echo "测试 $mirror ..." >&2
94 + echo "Testing $mirror ..." >&2
95 95 response="$(curl -o /dev/null -s -w "%{http_code} %{time_total}\n" \
96 96 --connect-timeout 1 --max-time 3 "${mirror}dists/${codename}/Release")"
97 97
@@ -100,14 +100,14 @@ find_fastest_mirror() {
100 100
101 101 if [ "$http_code" -eq 200 ]; then
102 102 results["$mirror"]="$time_total"
103 - echo " 成功: $time_total 秒" >&2
103 + echo " Success: $time_total seconds" >&2
104 104 else
105 - echo " 失败: HTTP 代码 $http_code" >&2
105 + echo " Failed: HTTP code $http_code" >&2
106 106 results["$mirror"]="9999"
107 107 fi
108 108 done
109 109
110 - # 按响应时间排序
110 + # Sort mirrors by response time
111 111 sorted_mirrors="$(
112 112 for url in "${!results[@]}"; do
113 113 echo "$url ${results[$url]}"
@@ -115,31 +115,31 @@ find_fastest_mirror() {
115 115 )"
116 116
117 117 echo >&2
118 - echo "=== 按响应时间排序的镜像源 (升序) ===" >&2
118 + echo "=== Mirrors sorted by response time (ascending) ===" >&2
119 119 echo "$sorted_mirrors" >&2
120 120 echo >&2
121 121
122 - # 选取最快的镜像源
122 + # Choose the fastest mirror
123 123 fastest_mirror="$(echo "$sorted_mirrors" | head -n 1 | awk '{print $1}')"
124 124
125 125 if [[ "$fastest_mirror" == "" || "${results[$fastest_mirror]}" == "9999" ]]; then
126 - echo "没有找到可用的镜像源,使用默认镜像源" >&2
126 + echo "No usable mirror found, using default mirror" >&2
127 127 fastest_mirror="http://archive.ubuntu.com/ubuntu/"
128 128 fi
129 129
130 - echo "找到最快的镜像源: $fastest_mirror" >&2
130 + echo "Fastest mirror found: $fastest_mirror" >&2
131 131 echo >&2
132 132
133 - # 只有这一行会返回给调用者,不带 >&2
133 + # Only this line will be returned to caller, without >&2
134 134 echo "$fastest_mirror"
135 135 }
136 136
137 - # 生成旧格式配置文件
137 + # Generate old format source list
138 138 generate_old_format() {
139 139 local mirror="$1"
140 140 local codename="$2"
141 141
142 - echo "生成旧格式配置文件 /etc/apt/sources.list"
142 + echo "Generating old format source list /etc/apt/sources.list"
143 143
144 144 sudo tee /etc/apt/sources.list >/dev/null <<EOF
145 145 deb $mirror $codename main restricted universe multiverse
@@ -148,15 +148,15 @@ deb $mirror $codename-backports main restricted universe multiverse
148 148 deb $mirror $codename-security main restricted universe multiverse
149 149 EOF
150 150
151 - echo "旧格式配置文件已更新"
151 + echo "Old format source list updated"
152 152 }
153 153
154 - # 生成新格式配置文件
154 + # Generate new format source list
155 155 generate_new_format() {
156 156 local mirror="$1"
157 157 local codename="$2"
158 158
159 - echo "生成新格式配置文件 /etc/apt/sources.list.d/ubuntu.sources"
159 + echo "Generating new format source list /etc/apt/sources.list.d/ubuntu.sources"
160 160
161 161 sudo tee /etc/apt/sources.list.d/ubuntu.sources >/dev/null <<EOF
162 162 Types: deb
@@ -184,67 +184,67 @@ Components: main restricted universe multiverse
184 184 Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
185 185 EOF
186 186
187 - echo "新格式配置文件已更新"
187 + echo "New format source list updated"
188 188 }
189 189
190 - # 主函数
190 + # Main function
191 191 main() {
192 - # 确保安装必要的包
192 + # Ensure required packages are installed
193 193 sudo apt update
194 194 sudo apt install -y curl lsb-release
195 195
196 - # 获取当前格式
196 + # Get current source format
197 197 format=$(check_apt_format)
198 - echo "当前 APT 源格式状态: $format"
198 + echo "Current APT source format status: $format"
199 199
200 - # 获取 Ubuntu 代号
200 + # Get Ubuntu codename
201 201 codename=$(lsb_release -cs)
202 - echo "Ubuntu 代号: $codename"
202 + echo "Ubuntu codename: $codename"
203 203
204 - # 获取最快的镜像源
205 - echo "正在寻找最快的镜像源..."
204 + # Find the fastest mirror
205 + echo "Searching for the fastest mirror..."
206 206 fastest_mirror=$(find_fastest_mirror)
207 207
208 - # 根据格式决定更新策略
208 + # Decide update strategy based on format
209 209 case "$format" in
210 210 "none")
211 - echo "未找到有效的 APT 源配置,将生成老格式配置文件"
211 + echo "No valid APT source found, generating old format source list"
212 212 generate_old_format "$fastest_mirror" "$codename"
213 213 ;;
214 214 "old")
215 - echo "系统使用传统格式,将更新老格式配置文件"
215 + echo "System uses traditional format, updating old format source list"
216 216 generate_old_format "$fastest_mirror" "$codename"
217 217 ;;
218 218 "new")
219 - echo "系统使用现代格式,将更新新格式配置文件"
219 + echo "System uses modern format, updating new format source list"
220 220 generate_new_format "$fastest_mirror" "$codename"
221 221 ;;
222 222 "both")
223 - echo "系统同时使用两种格式,将删除老格式,只保留新格式"
223 + echo "System uses both formats, old format will be removed, only new format will be retained"
224 224 sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
225 - echo "已备份旧格式文件到 /etc/apt/sources.list.bak"
225 + echo "Old format source list backed up to /etc/apt/sources.list.bak"
226 226 generate_new_format "$fastest_mirror" "$codename"
227 227 ;;
228 228 esac
229 229
230 - # 更新软件包列表
231 - echo "更新软件包列表..."
230 + # Update package list
231 + echo "Updating package list..."
232 232 sudo apt update
233 233
234 - echo "APT 源优化完成!"
234 + echo "APT source optimization completed!"
235 235
236 236 aptVersion=$(apt --version | head -n 1 | awk '{print $2}')
237 - echo "当前 APT 版本: $aptVersion"
237 + echo "Current APT version: $aptVersion"
238 238
239 239 # If current APT version is 3.0 or higher, and using old format or none, convert to new format
240 240 # sudo apt modernize-sources
241 241 if [[ $(echo "$aptVersion >= 3.0" | bc) -eq 1 && ( "$format" == "old" || "$format" == "none" ) ]]; then
242 - echo "当前 APT 版本为 3.0 或更高,将转换为新格式"
242 + echo "APT version is 3.0 or higher, converting to new format"
243 243 sudo apt modernize-sources
244 - echo "APT 源已转换为新格式"
244 + echo "APT sources converted to new format"
245 245 fi
246 246
247 247 }
248 248
249 - # 执行主函数
250 - main
249 + # Execute main function
250 + main

anduin's Avatar anduin revised this gist 1748425138. Go to revision

1 file changed, 236 insertions, 102 deletions

mirror.sh

@@ -1,116 +1,250 @@
1 1 #!/usr/bin/env bash
2 - # Step 1: Ensure required packages are installed
3 - sudo apt update
4 - sudo apt install -y curl apt-transport-https lsb-release
2 + set -euo pipefail
5 3
6 - function switchSource() {
7 - # Get current Ubuntu codename (e.g., jammy, focal, bionic)
8 - codename=$(lsb_release -cs)
9 -
10 - # Define a list of potential mirrors
11 - mirrors=(
12 - "https://archive.ubuntu.com/ubuntu/"
13 - "https://mirror.aarnet.edu.au/pub/ubuntu/archive/" # Australia
14 - "https://mirror.fsmg.org.nz/ubuntu/" # New Zealand
15 - "https://mirrors.neterra.net/ubuntu/archive/" # Bulgaria
16 - "https://mirror.csclub.uwaterloo.ca/ubuntu/" # Canada
17 - "https://mirrors.dotsrc.org/ubuntu/" # Denmark
18 - "https://mirrors.nic.funet.fi/ubuntu/" # Finland
19 - "https://mirror.ubuntu.ikoula.com/" # France
20 - "https://mirror.xtom.com.hk/ubuntu/" # Hong Kong
21 - "https://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu/" # India
22 - "https://ftp.udx.icscoe.jp/Linux/ubuntu/" # Japan
23 - "https://ftp.kaist.ac.kr/ubuntu/" # Korea
24 - "https://ubuntu.mirror.garr.it/ubuntu/" # Italy
25 - "https://ftp.uni-stuttgart.de/ubuntu/" # Germany
26 - "https://mirror.i3d.net/pub/ubuntu/" # Netherlands
27 - "https://mirroronet.pl/pub/mirrors/ubuntu/" # Poland
28 - "https://ubuntu.mobinhost.com/ubuntu/" # Iran
29 - "http://sg.archive.ubuntu.com/ubuntu/" # Singapore
30 - "http://ossmirror.mycloud.services/os/linux/ubuntu/" # Singapore
31 - "https://mirror.enzu.com/ubuntu/" # United States
32 - "http://jp.archive.ubuntu.com/ubuntu/" # Japan
33 - "http://kr.archive.ubuntu.com/ubuntu/" # Korea
34 - "http://us.archive.ubuntu.com/ubuntu/" # United States
35 - "http://tw.archive.ubuntu.com/ubuntu/" # Taiwan
36 - "https://mirror.twds.com.tw/ubuntu/" # Taiwan
37 - "https://ubuntu.mirrors.uk2.net/ubuntu/" # United Kingdom
38 - "http://mirrors.ustc.edu.cn/ubuntu/" # 中国科学技术大学
39 - "http://ftp.sjtu.edu.cn/ubuntu/" # 上海交通大学
40 - "http://mirrors.tuna.tsinghua.edu.cn/ubuntu/" # 清华大学
41 - "http://mirrors.aliyun.com/ubuntu/" # 阿里云
42 - "http://mirrors.163.com/ubuntu/" # 网易
43 - "http://mirrors.cloud.tencent.com/ubuntu/" # 腾讯云
44 - "http://mirror.aiursoft.cn/ubuntu/" # Aiursoft
45 - "http://mirrors.huaweicloud.com/ubuntu/" # 华为云
46 - "http://mirrors.zju.edu.cn/ubuntu/" # 浙江大学
47 - "http://azure.archive.ubuntu.com/ubuntu/" # Azure
48 - "https://mirrors.isu.net.sa/apt-mirror/" # Saudi Arabia
49 - "https://mirror.team-host.ru/ubuntu/" # Russia
50 - "https://labs.eif.urjc.es/mirror/ubuntu/" # Spain
51 - "https://mirror.alastyr.com/ubuntu/ubuntu-archive/" # Turkey
52 - "https://ftp.acc.umu.se/ubuntu/" # Sweden
53 - "https://mirror.kku.ac.th/ubuntu/" # Thailand
54 - "https://mirror.bizflycloud.vn/ubuntu/" # Vietnam
55 - )
56 -
57 - declare -A results
58 -
59 - # Function to test speed of a single mirror
60 - test_speed() {
61 - url="$1"
62 - # Attempt to do a quick GET and measure total time
63 - response="$(curl -o /dev/null -s -w "%{http_code} %{time_total}\n" \
64 - --connect-timeout 1 --max-time 2 "$url")"
65 -
66 - http_code=$(echo "$response" | awk '{print $1}')
67 - time_total=$(echo "$response" | awk '{print $2}')
68 -
69 - # If HTTP code == 200, mark the measured time; otherwise use a large value
70 - if [ "$http_code" -eq 200 ]; then
71 - results["$url"]="$time_total"
4 + # 判断当前 APT 源格式状态
5 + check_apt_format() {
6 + local old_format=false
7 + local new_format=false
8 +
9 + # 检查老格式 (.list)
10 + if [ -f "/etc/apt/sources.list" ]; then
11 + if grep -v '^#' /etc/apt/sources.list | grep -q '[^[:space:]]'; then
12 + old_format=true
13 + fi
14 + fi
15 +
16 + # 检查新格式中的 ubuntu.sources 文件
17 + if [ -f "/etc/apt/sources.list.d/ubuntu.sources" ]; then
18 + if grep -v '^#' /etc/apt/sources.list.d/ubuntu.sources | grep -q '[^[:space:]]'; then
19 + new_format=true
20 + fi
21 + fi
22 +
23 + # 返回状态
24 + if $old_format && $new_format; then
25 + echo "both"
26 + elif $old_format; then
27 + echo "old"
28 + elif $new_format; then
29 + echo "new"
72 30 else
73 - echo "Failed to access $url (HTTP code: $http_code)"
74 - results["$url"]="9999"
31 + echo "none"
75 32 fi
76 - }
33 + }
77 34
78 - echo "Testing all mirrors for Ubuntu '$codename'..."
79 - for mirror in "${mirrors[@]}"; do
80 - test_speed "$mirror"
81 - done
35 + # 寻找最快的镜像源
36 + find_fastest_mirror() {
37 + # 所有要显示的内容都重定向到标准错误 >&2
38 + echo "正在测试镜像源速度..." >&2
39 +
40 + # 获取当前 Ubuntu 代号
41 + codename=$(lsb_release -cs)
42 +
43 + # 定义潜在的镜像源列表
44 + mirrors=(
45 + "https://archive.ubuntu.com/ubuntu/"
46 + "https://mirror.aarnet.edu.au/pub/ubuntu/archive/" # Australia
47 + "https://mirror.fsmg.org.nz/ubuntu/" # New Zealand
48 + "https://mirrors.neterra.net/ubuntu/archive/" # Bulgaria
49 + "https://mirror.csclub.uwaterloo.ca/ubuntu/" # Canada
50 + "https://mirrors.dotsrc.org/ubuntu/" # Denmark
51 + "https://mirrors.nic.funet.fi/ubuntu/" # Finland
52 + "https://mirror.ubuntu.ikoula.com/" # France
53 + "https://mirror.xtom.com.hk/ubuntu/" # Hong Kong
54 + "https://mirrors.piconets.webwerks.in/ubuntu-mirror/ubuntu/" # India
55 + "https://ftp.udx.icscoe.jp/Linux/ubuntu/" # Japan
56 + "https://ftp.kaist.ac.kr/ubuntu/" # Korea
57 + "https://ubuntu.mirror.garr.it/ubuntu/" # Italy
58 + "https://ftp.uni-stuttgart.de/ubuntu/" # Germany
59 + "https://mirror.i3d.net/pub/ubuntu/" # Netherlands
60 + "https://mirroronet.pl/pub/mirrors/ubuntu/" # Poland
61 + "https://ubuntu.mobinhost.com/ubuntu/" # Iran
62 + "http://sg.archive.ubuntu.com/ubuntu/" # Singapore
63 + "http://ossmirror.mycloud.services/os/linux/ubuntu/" # Singapore
64 + "https://mirror.enzu.com/ubuntu/" # United States
65 + "http://jp.archive.ubuntu.com/ubuntu/" # Japan
66 + "http://kr.archive.ubuntu.com/ubuntu/" # Korea
67 + "http://us.archive.ubuntu.com/ubuntu/" # United States
68 + "http://tw.archive.ubuntu.com/ubuntu/" # Taiwan
69 + "https://mirror.twds.com.tw/ubuntu/" # Taiwan
70 + "https://ubuntu.mirrors.uk2.net/ubuntu/" # United Kingdom
71 + "http://mirrors.ustc.edu.cn/ubuntu/" # 中国科学技术大学
72 + "http://ftp.sjtu.edu.cn/ubuntu/" # 上海交通大学
73 + "http://mirrors.tuna.tsinghua.edu.cn/ubuntu/" # 清华大学
74 + "http://mirrors.aliyun.com/ubuntu/" # 阿里云
75 + "http://mirrors.163.com/ubuntu/" # 网易
76 + "http://mirrors.cloud.tencent.com/ubuntu/" # 腾讯云
77 + "http://mirror.aiursoft.cn/ubuntu/" # Aiursoft
78 + "http://mirrors.huaweicloud.com/ubuntu/" # 华为云
79 + "http://mirrors.zju.edu.cn/ubuntu/" # 浙江大学
80 + "http://azure.archive.ubuntu.com/ubuntu/" # Azure
81 + "https://mirrors.isu.net.sa/apt-mirror/" # Saudi Arabia
82 + "https://mirror.team-host.ru/ubuntu/" # Russia
83 + "https://labs.eif.urjc.es/mirror/ubuntu/" # Spain
84 + "https://mirror.alastyr.com/ubuntu/ubuntu-archive/" # Turkey
85 + "https://ftp.acc.umu.se/ubuntu/" # Sweden
86 + "https://mirror.kku.ac.th/ubuntu/" # Thailand
87 + "https://mirror.bizflycloud.vn/ubuntu/" # Vietnam
88 + )
89 +
90 + declare -A results
91 +
92 + # 测试单个镜像源的速度
93 + for mirror in "${mirrors[@]}"; do
94 + echo "测试 $mirror ..." >&2
95 + response="$(curl -o /dev/null -s -w "%{http_code} %{time_total}\n" \
96 + --connect-timeout 1 --max-time 3 "${mirror}dists/${codename}/Release")"
97 +
98 + http_code=$(echo "$response" | awk '{print $1}')
99 + time_total=$(echo "$response" | awk '{print $2}')
100 +
101 + if [ "$http_code" -eq 200 ]; then
102 + results["$mirror"]="$time_total"
103 + echo " 成功: $time_total 秒" >&2
104 + else
105 + echo " 失败: HTTP 代码 $http_code" >&2
106 + results["$mirror"]="9999"
107 + fi
108 + done
109 +
110 + # 按响应时间排序
111 + sorted_mirrors="$(
112 + for url in "${!results[@]}"; do
113 + echo "$url ${results[$url]}"
114 + done | sort -k2 -n
115 + )"
116 +
117 + echo >&2
118 + echo "=== 按响应时间排序的镜像源 (升序) ===" >&2
119 + echo "$sorted_mirrors" >&2
120 + echo >&2
121 +
122 + # 选取最快的镜像源
123 + fastest_mirror="$(echo "$sorted_mirrors" | head -n 1 | awk '{print $1}')"
124 +
125 + if [[ "$fastest_mirror" == "" || "${results[$fastest_mirror]}" == "9999" ]]; then
126 + echo "没有找到可用的镜像源,使用默认镜像源" >&2
127 + fastest_mirror="http://archive.ubuntu.com/ubuntu/"
128 + fi
129 +
130 + echo "找到最快的镜像源: $fastest_mirror" >&2
131 + echo >&2
132 +
133 + # 只有这一行会返回给调用者,不带 >&2
134 + echo "$fastest_mirror"
135 + }
82 136
83 - # Sort mirrors by time_total
84 - # Example of sorted_mirrors entry: "https://archive.ubuntu.com/ubuntu/ 0.034"
85 - sorted_mirrors="$(
86 - for url in "${!results[@]}"; do
87 - echo "$url ${results[$url]}"
88 - done | sort -k2 -n
89 - )"
137 + # 生成旧格式配置文件
138 + generate_old_format() {
139 + local mirror="$1"
140 + local codename="$2"
141 +
142 + echo "生成旧格式配置文件 /etc/apt/sources.list"
143 +
144 + sudo tee /etc/apt/sources.list >/dev/null <<EOF
145 + deb $mirror $codename main restricted universe multiverse
146 + deb $mirror $codename-updates main restricted universe multiverse
147 + deb $mirror $codename-backports main restricted universe multiverse
148 + deb $mirror $codename-security main restricted universe multiverse
149 + EOF
150 +
151 + echo "旧格式配置文件已更新"
152 + }
90 153
91 - echo
92 - echo "=== Sorted mirrors by response time (ascending) ==="
93 - echo "$sorted_mirrors"
94 - echo
154 + # 生成新格式配置文件
155 + generate_new_format() {
156 + local mirror="$1"
157 + local codename="$2"
158 +
159 + echo "生成新格式配置文件 /etc/apt/sources.list.d/ubuntu.sources"
160 +
161 + sudo tee /etc/apt/sources.list.d/ubuntu.sources >/dev/null <<EOF
162 + Types: deb
163 + URIs: $mirror
164 + Suites: $codename
165 + Components: main restricted universe multiverse
166 + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
95 167
96 - # Pick the top (fastest) mirror from the sorted list
97 - fastest_mirror="$(echo "$sorted_mirrors" | head -n 1 | awk '{print $1}')"
168 + Types: deb
169 + URIs: $mirror
170 + Suites: $codename-updates
171 + Components: main restricted universe multiverse
172 + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
98 173
99 - echo "Fastest mirror found: $fastest_mirror"
100 - echo "Updating /etc/apt/sources.list..."
174 + Types: deb
175 + URIs: $mirror
176 + Suites: $codename-backports
177 + Components: main restricted universe multiverse
178 + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
101 179
102 - # Update /etc/apt/sources.list with the fastest mirror
103 - sudo tee /etc/apt/sources.list >/dev/null <<EOF
104 - deb $fastest_mirror $codename main restricted universe multiverse
105 - deb $fastest_mirror $codename-updates main restricted universe multiverse
106 - deb $fastest_mirror $codename-backports main restricted universe multiverse
107 - deb $fastest_mirror $codename-security main restricted universe multiverse
180 + Types: deb
181 + URIs: $mirror
182 + Suites: $codename-security
183 + Components: main restricted universe multiverse
184 + Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg
108 185 EOF
186 +
187 + echo "新格式配置文件已更新"
188 + }
189 +
190 + # 主函数
191 + main() {
192 + # 确保安装必要的包
193 + sudo apt update
194 + sudo apt install -y curl lsb-release
195 +
196 + # 获取当前格式
197 + format=$(check_apt_format)
198 + echo "当前 APT 源格式状态: $format"
199 +
200 + # 获取 Ubuntu 代号
201 + codename=$(lsb_release -cs)
202 + echo "Ubuntu 代号: $codename"
203 +
204 + # 获取最快的镜像源
205 + echo "正在寻找最快的镜像源..."
206 + fastest_mirror=$(find_fastest_mirror)
207 +
208 + # 根据格式决定更新策略
209 + case "$format" in
210 + "none")
211 + echo "未找到有效的 APT 源配置,将生成老格式配置文件"
212 + generate_old_format "$fastest_mirror" "$codename"
213 + ;;
214 + "old")
215 + echo "系统使用传统格式,将更新老格式配置文件"
216 + generate_old_format "$fastest_mirror" "$codename"
217 + ;;
218 + "new")
219 + echo "系统使用现代格式,将更新新格式配置文件"
220 + generate_new_format "$fastest_mirror" "$codename"
221 + ;;
222 + "both")
223 + echo "系统同时使用两种格式,将删除老格式,只保留新格式"
224 + sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
225 + echo "已备份旧格式文件到 /etc/apt/sources.list.bak"
226 + generate_new_format "$fastest_mirror" "$codename"
227 + ;;
228 + esac
229 +
230 + # 更新软件包列表
231 + echo "更新软件包列表..."
232 + sudo apt update
233 +
234 + echo "APT 源优化完成!"
235 +
236 + aptVersion=$(apt --version | head -n 1 | awk '{print $2}')
237 + echo "当前 APT 版本: $aptVersion"
238 +
239 + # If current APT version is 3.0 or higher, and using old format or none, convert to new format
240 + # sudo apt modernize-sources
241 + if [[ $(echo "$aptVersion >= 3.0" | bc) -eq 1 && ( "$format" == "old" || "$format" == "none" ) ]]; then
242 + echo "当前 APT 版本为 3.0 或更高,将转换为新格式"
243 + sudo apt modernize-sources
244 + echo "APT 源已转换为新格式"
245 + fi
109 246
110 - # Final check
111 - sudo apt update
112 - echo "All done!"
113 247 }
114 248
115 - # Call the main function
116 - switchSource
249 + # 执行主函数
250 + main

anduin's Avatar anduin revised this gist 1748417744. Go to revision

1 file changed, 27 insertions, 19 deletions

init.sh

@@ -111,8 +111,8 @@ rm -f "$local_pass_file" 2>/dev/null || true
111 111 sshpass -p "$REMOTE_PASS" ssh -q -o StrictHostKeyChecking=no \
112 112 "$REMOTE_USER@$SERVER" "sudo cat /etc/$NEWUSER.pass" \
113 113 > "$local_pass_file"
114 - chmod 600 "$local_pass_file"
115 - chown "$USER:$USER" "$local_pass_file"
114 + sudo chown "$USER:$USER" "$local_pass_file"
115 + sudo chmod 600 "$local_pass_file"
116 116 print_ok "Password for $NEWUSER saved locally at $local_pass_file [DO NOT SHARE THIS FILE! IT CAN BE USED TO LOG IN VIA SERIAL CONSOLE OR RESCUE MODE!]"
117 117
118 118 # 7) Copy SSH key (only if absent)
@@ -198,26 +198,34 @@ print_ok "Checking HWE kernel package on remote"
198 198 run_remote <<'EOF'
199 199 set -euo pipefail
200 200
201 - HWE_PKG="$(apt search linux-generic-hwe- \
202 - | awk -F/ '/linux-generic-hwe-/{print $1; exit}')"
201 + # Try to find the HWE package
202 + HWE_PKG=$(apt search linux-generic-hwe- 2>/dev/null | grep -o 'linux-generic-hwe-[^/ ]*' | head -1)
203 203
204 - inst="\$(apt-cache policy \$HWE_PKG | awk '/Installed:/ {print \$2}')"
205 - cand="\$(apt-cache policy \$HWE_PKG | awk '/Candidate:/ {print \$2}')"
206 -
207 - if dpkg -s "\$HWE_PKG" &>/dev/null; then
208 - if [ "\$inst" != "\$cand" ]; then
209 - echo "[ OK ] Upgrading \$HWE_PKG from \$inst to \$cand"
210 - sudo apt-get update
211 - sudo apt-get install -y "\$HWE_PKG"
212 - echo reboot_required > /tmp/.reboot_flag
204 + if [ -z "$HWE_PKG" ]; then
205 + echo "[ OK ] No HWE kernel package found for this release, skipping"
206 + else
207 + inst=$(apt-cache policy "$HWE_PKG" | awk '/Installed:/ {print $2}')
208 + cand=$(apt-cache policy "$HWE_PKG" | awk '/Candidate:/ {print $2}')
209 +
210 + if dpkg -s "$HWE_PKG" &>/dev/null; then
211 + if [ "$inst" != "$cand" ] && [ "$cand" != "(none)" ]; then
212 + echo "[ OK ] Upgrading $HWE_PKG from $inst to $cand"
213 + sudo apt-get update
214 + sudo apt-get install -y "$HWE_PKG"
215 + echo reboot_required > /tmp/.reboot_flag
216 + else
217 + echo "[ OK ] $HWE_PKG is already at latest version ($inst), skipping"
218 + fi
213 219 else
214 - echo "[ OK ] \$HWE_PKG is already at latest version (\$inst), skipping"
220 + if [ "$cand" != "(none)" ]; then
221 + echo "[ OK ] Installing $HWE_PKG ($cand)"
222 + sudo apt-get update
223 + sudo apt-get install -y "$HWE_PKG"
224 + echo reboot_required > /tmp/.reboot_flag
225 + else
226 + echo "[ OK ] $HWE_PKG has no installation candidate, skipping"
227 + fi
215 228 fi
216 - else
217 - echo "[ OK ] Installing \$HWE_PKG (\$cand)"
218 - sudo apt-get update
219 - sudo apt-get install -y "\$HWE_PKG"
220 - echo reboot_required > /tmp/.reboot_flag
221 229 fi
222 230 EOF
223 231

anduin's Avatar anduin revised this gist 1748283478. Go to revision

1 file changed, 1 insertion

init.sh

@@ -224,6 +224,7 @@ EOF
224 224 # 16) Conditionally reboot & wait
225 225 if run_remote 'test -f /tmp/.reboot_flag'; then
226 226 print_ok "Rebooting server to apply new kernel"
227 + run_remote "rm -f /tmp/.reboot_flag"
227 228 run_remote "sudo reboot" || true
228 229 sleep 5
229 230 wait_ssh

anduin's Avatar anduin revised this gist 1748282311. Go to revision

1 file changed, 9 insertions

init.sh

@@ -106,6 +106,15 @@ else
106 106 print_ok "New password generated for $NEWUSER and persisted at /etc/$NEWUSER.pass. Please back it up! It can still be used to log in via serial console or rescue mode!"
107 107 fi
108 108
109 + local_pass_file="./password_${NEWUSER}_at_${SERVER}.txt"
110 + rm -f "$local_pass_file" 2>/dev/null || true
111 + sshpass -p "$REMOTE_PASS" ssh -q -o StrictHostKeyChecking=no \
112 + "$REMOTE_USER@$SERVER" "sudo cat /etc/$NEWUSER.pass" \
113 + > "$local_pass_file"
114 + chmod 600 "$local_pass_file"
115 + chown "$USER:$USER" "$local_pass_file"
116 + print_ok "Password for $NEWUSER saved locally at $local_pass_file [DO NOT SHARE THIS FILE! IT CAN BE USED TO LOG IN VIA SERIAL CONSOLE OR RESCUE MODE!]"
117 +
109 118 # 7) Copy SSH key (only if absent)
110 119 [ ! -f ~/.ssh/id_rsa.pub ] && run_local ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
111 120 PUBKEY=$(<~/.ssh/id_rsa.pub)

anduin's Avatar anduin revised this gist 1748281420. Go to revision

1 file changed, 60 insertions, 14 deletions

init.sh

@@ -37,7 +37,10 @@ run_local(){ print_ok "Local: $*"; "$@"; }
37 37 run_remote(){ sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$SERVER" "$*"; }
38 38 wait_ssh(){
39 39 print_ok "Waiting for SSH on $SERVER... (Running ssh $REMOTE_USER@$SERVER)"
40 - until sshpass -p "$REMOTE_PASS" ssh -q -o StrictHostKeyChecking=no "$REMOTE_USER@$SERVER" exit; do
40 + until sshpass -p "$REMOTE_PASS" ssh -q \
41 + -o StrictHostKeyChecking=no \
42 + -o ConnectTimeout=5 \
43 + "$REMOTE_USER@$SERVER" exit; do
41 44 print_warn "SSH not ready, retrying in 5s..."
42 45 sleep 5
43 46 done
@@ -181,15 +184,43 @@ print_ok "Selecting best mirror & updating"
181 184 run_remote "curl -s https://gist.aiursoft.cn/anduin/879917820a6c4b268fc12c21f1b3fe7a/raw/HEAD/mirror.sh | bash"
182 185 run_remote "sudo apt-get update"
183 186
184 - # 15) Install latest HWE kernel
185 - print_ok "Installing latest HWE kernel"
186 - run_remote "sudo apt-get install -y \$(apt search linux-generic-hwe- | awk -F/ '/linux-generic-hwe-/{print \$1}' | head -1)"
187 + # 15) Install or upgrade latest HWE kernel if needed
188 + print_ok "Checking HWE kernel package on remote"
189 + run_remote <<'EOF'
190 + set -euo pipefail
187 191
188 - # 16) Reboot & wait
189 - print_ok "Rebooting server"
190 - run_remote "sudo reboot" || true
191 - sleep 5
192 - wait_ssh
192 + HWE_PKG="$(apt search linux-generic-hwe- \
193 + | awk -F/ '/linux-generic-hwe-/{print $1; exit}')"
194 +
195 + inst="\$(apt-cache policy \$HWE_PKG | awk '/Installed:/ {print \$2}')"
196 + cand="\$(apt-cache policy \$HWE_PKG | awk '/Candidate:/ {print \$2}')"
197 +
198 + if dpkg -s "\$HWE_PKG" &>/dev/null; then
199 + if [ "\$inst" != "\$cand" ]; then
200 + echo "[ OK ] Upgrading \$HWE_PKG from \$inst to \$cand"
201 + sudo apt-get update
202 + sudo apt-get install -y "\$HWE_PKG"
203 + echo reboot_required > /tmp/.reboot_flag
204 + else
205 + echo "[ OK ] \$HWE_PKG is already at latest version (\$inst), skipping"
206 + fi
207 + else
208 + echo "[ OK ] Installing \$HWE_PKG (\$cand)"
209 + sudo apt-get update
210 + sudo apt-get install -y "\$HWE_PKG"
211 + echo reboot_required > /tmp/.reboot_flag
212 + fi
213 + EOF
214 +
215 + # 16) Conditionally reboot & wait
216 + if run_remote 'test -f /tmp/.reboot_flag'; then
217 + print_ok "Rebooting server to apply new kernel"
218 + run_remote "sudo reboot" || true
219 + sleep 5
220 + wait_ssh
221 + else
222 + print_ok "No new kernel installed, skipping reboot"
223 + fi
193 224
194 225 # 17) Final updates & cleanup
195 226 print_ok "Installing upgrades & cleanup"
@@ -203,14 +234,29 @@ run_remote "sudo apt-get install -y linux-tools-$(uname -r) cpupower && \
203 234
204 235 # 19) Remove snap
205 236 print_ok "Removing snapd"
206 - run_remote "sudo systemctl disable --now snapd && \
207 - dpkg -l snapd &>/dev/null && sudo apt-get purge -y snapd && \
208 - sudo rm -rf /snap /var/lib/snapd /var/cache/snapd && \
209 - sudo tee /etc/apt/preferences.d/no-snap.pref > /dev/null <<EOF
237 + run_remote <<'EOF'
238 + # 1) 如果 snapd.service 存在,就 disable 一下;否则跳过
239 + if systemctl list-unit-files | grep -q '^snapd\.service'; then
240 + sudo systemctl disable --now snapd || true
241 + else
242 + echo "[ OK ] snapd.service not found, skipping disable"
243 + fi
244 +
245 + # 2) 如果 dpkg 里检测到 snapd 包,就 purge 并清理数据目录
246 + if dpkg -l snapd &>/dev/null; then
247 + sudo apt-get purge -y snapd
248 + sudo rm -rf /snap /var/lib/snapd /var/cache/snapd
249 + else
250 + echo "[ OK ] snapd package not installed, skipping purge"
251 + fi
252 +
253 + # 3) 在所有机器都写上 no-snap 的 pin
254 + sudo tee /etc/apt/preferences.d/no-snap.pref > /dev/null <<EOP
210 255 Package: snapd
211 256 Pin: release a=*
212 257 Pin-Priority: -10
213 - EOF"
258 + EOP
259 + EOF
214 260
215 261 # 20) Final cleanup & benchmark
216 262 print_ok "Final autoremove & benchmark"
Newer Older