-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.sh
More file actions
204 lines (173 loc) · 5.64 KB
/
Copy pathtest.sh
File metadata and controls
204 lines (173 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# by https://github.com/oneclickvirt/lxd_images
# 2025.02.01
set -e
trap 'echo "发生错误,行号: $LINENO, 命令: $BASH_COMMAND"' ERR
if [[ $# -ne 1 ]]; then
echo "用法: $0 <完整镜像名称>"
exit 1
fi
image_name="$1"
is_kvm_image=false
if [[ "$image_name" == *_kvm.zip ]]; then
is_kvm_image=true
fi
# Determine the release tag for download URL
if $is_kvm_image; then
release_tag="kvm_images"
else
release_tag="${image_name%%_*}"
fi
if [[ "$image_name" == *"x86_64"* || "$image_name" == *"amd64"* ]]; then
fixed_images_file="x86_64_fixed_images.txt"
elif [[ "$image_name" == *"arm64"* ]]; then
fixed_images_file="arm64_fixed_images.txt"
else
echo "错误:无法识别镜像架构"
exit 1
fi
date=$(date)
{
echo "$date"
echo "------------------------------------------"
echo "测试镜像: $image_name"
} >> log
test_status_file=$(mktemp)
echo "success" > "$test_status_file"
cleanup() {
local exit_code=$?
echo "清理资源..."
lxc stop test 2>/dev/null || true
lxc delete -f test 2>/dev/null || true
lxc image delete myc 2>/dev/null || true
if [[ -f "$test_status_file" ]]; then
if [[ "$(cat "$test_status_file")" != "success" ]]; then
echo "测试失败,从列表中移除镜像"
escaped_name=$(echo "$image_name" | sed 's/[.]/\\./g')
sed -i "/^${escaped_name}$/d" "$fixed_images_file"
fi
else
echo "警告:test_status_file 丢失,跳过失败处理" | tee -a log
fi
rm -f "$test_status_file"
rm -f lxd.tar.xz rootfs.squashfs disk.qcow2 "$image_name"
echo "------------------------------------------" >> log
# 检查 systemd-resolved 是否被影响,并尝试恢复
if systemctl is-active --quiet systemd-resolved; then
echo "重启 systemd-resolved 以恢复 DNS"
systemctl restart systemd-resolved || true
fi
exit $exit_code
}
trap cleanup EXIT
echo "开始下载镜像..."
if ! curl -m 1800 -LO "https://github.com/oneclickvirt/lxd_images/releases/download/${release_tag}/$image_name"; then
echo "主下载失败,尝试备用下载源..."
if ! curl -m 1800 -LO "https://cdn.spiritlhl.net/https://github.com/oneclickvirt/lxd_images/releases/download/${release_tag}/$image_name"; then
echo "错误:所有下载源均失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
fi
if [ ! -f "$image_name" ] || [ ! -s "$image_name" ]; then
echo "错误:镜像文件不存在或为空" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
chmod 777 "$image_name"
if ! unzip -q "$image_name"; then
echo "错误:解压失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
echo "$image_name" >> "$fixed_images_file"
if $is_kvm_image; then
# KVM image import: uses disk.qcow2 instead of rootfs.squashfs
if ! lxc image import lxd.tar.xz disk.qcow2 --alias myc; then
echo "错误:KVM镜像导入失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
# Verify secureboot is disabled for KVM images
if ! tar -xOf lxd.tar.xz metadata.yaml 2>/dev/null | grep -q 'requirements.secureboot:[[:space:]]*"false"\|requirements.secureboot:[[:space:]]*false'; then
echo "错误:KVM 镜像缺少 requirements.secureboot=false" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
else
if ! lxc image import lxd.tar.xz rootfs.squashfs --alias myc; then
echo "错误:镜像导入失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
fi
init_args=(myc test)
if $is_kvm_image; then
init_args+=(--vm)
fi
if ! lxc init "${init_args[@]}"; then
echo "错误:实例初始化失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
if ! lxc start test; then
echo "错误:实例启动失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
sleep 10
ssh_test() {
local retries=0
local max_retries=5
while [ $retries -lt $max_retries ]; do
if lxc exec test -- lsof -i:22 2>/dev/null | grep -q ssh; then
echo "SSH服务正常" | tee -a log
return 0
fi
echo "等待SSH服务启动,尝试 $((retries + 1))/$max_retries..." | tee -a log
sleep $((2**retries))
((retries++))
done
echo "SSH服务异常" | tee -a log
return 1
}
network_test() {
lxc exec test -- cp /etc/resolv.conf /etc/resolv.conf.bak || true
lxc exec test -- sh -c 'echo "nameserver 8.8.8.8" > /etc/resolv.conf'
if lxc exec test -- curl -m 10 -skL https://cdn.spiritlhl.net/https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test | grep -q "success"; then
echo "网络连通正常" | tee -a log
lxc exec test -- mv /etc/resolv.conf.bak /etc/resolv.conf 2>/dev/null || true
return 0
else
echo "网络连接失败" | tee -a log
lxc exec test -- mv /etc/resolv.conf.bak /etc/resolv.conf 2>/dev/null || true
return 1
fi
}
if ! ssh_test; then
echo "fail" > "$test_status_file"
exit 1
fi
if ! network_test; then
echo "fail" > "$test_status_file"
exit 1
fi
echo "执行重启测试..." | tee -a log
if ! lxc stop test; then
echo "错误:实例停止失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
if ! lxc start test; then
echo "错误:实例重启失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
sleep 15
if ! network_test; then
echo "重启后网络测试失败" | tee -a log
echo "fail" > "$test_status_file"
exit 1
fi
echo "所有测试通过" | tee -a log
exit 0