-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmkrootfs.sh
More file actions
executable file
·292 lines (257 loc) · 6.66 KB
/
Copy pathmkrootfs.sh
File metadata and controls
executable file
·292 lines (257 loc) · 6.66 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/bin/bash
export WORKDIR=$(dirname $(readlink -f "$0"))
cd ${WORKDIR} && mkdir -p build
if [ $# -lt 1 ];then
echo "Usage: $0 dist [clean]"
echo "dist: focal"
exit 1
fi
dist=$1
if [ -f "./env/linux/${dist}.env" ];then
source ./env/linux/${dist}.env
else
echo "./env/linux/${dist}.env not exists!"
if [ -f "./env/linux/private/${dist}.env" ];then
echo "But the private environment file ${WORKDIR}/env/linux/private/${dist}.env has been found."
source ./env/linux/private/${dist}.env
else
echo "./env/linux/private/${dist}.env not exists!"
exit 1
fi
fi
# install debootstrap
if [ -f ${WORKDIR}/src/debootstrap.tar.gz ];then
(
echo "========================================================"
echo "Install bootstrap ..."
cd /tmp && \
tar xf ${WORKDIR}/src/debootstrap.tar.gz && \
cd /tmp/debootstrap && \
make install && \
cd /tmp && \
rm -rf /tmp/debootstrap
)
fi
if [ ! -x /usr/sbin/debootstrap ];then
echo "debootstrap is not exists, please install it first!"
exit 1
else
echo "The deboostrap version is:"
/usr/sbin/debootstrap --version
echo "========================================================"
echo
fi
host_arch=$(uname -m)
if [ "${host_arch}" == "aarch64" ];then
CROSS_FLAG=0
else
CROSS_FLAG=1
fi
if [ $CROSS_FLAG -eq 1 ] && [ ! -x "/usr/bin/qemu-aarch64-static" ];then
echo "/usr/bin/qemu-aarch64-static not found, please install qemu-aarch64-static!"
echo "example: sudo apt install qemu-user-static"
exit 1
fi
if [ ! -f "${CHROOT}" ];then
echo "The chroot script is not exists! [${CHROOT}]"
exit 1
fi
if [ -n "$OS_RELEASE" ];then
os_release=$OS_RELEASE
else
os_release=$dist
fi
if [ -n "$DIST_ALIAS" ];then
output_dir=build/${DIST_ALIAS}
else
output_dir=build/${dist}
fi
param=$2
if [ "$param" == "clean" ];then
rm -rf ${output_dir}
echo "${output_dir} cleaned"
exit 0
fi
function ask_force_pass() {
local stage_name="$1"
local ok_file="$2"
# 检查是否在交互式环境中
if [ -t 0 ] && [ -t 1 ]; then
# 交互式:提示用户
read -p "Stage ${stage_name} failed. Force mark as passed? (y/N): " -r reply
case "$reply" in
[yY])
touch "$ok_file"
echo "Forced stage ${stage_name} to pass."
return 0
;;
esac
else
# 非交互式(如 GitHub Actions):自动拒绝
echo "Non-interactive environment detected. Cannot force pass stage ${stage_name}."
fi
# 默认行为:不强制跳过
return 1
}
function bind() {
cd ${WORKDIR}
mount -o bind /dev ${output_dir}/dev
mount -o bind /dev/pts ${output_dir}/dev/pts
mount -o bind /proc ${output_dir}/proc
mount -o bind /run ${output_dir}/run
mount -o bind /sys ${output_dir}/sys
which lsmount && lsmount | grep "${output_dir}" && echo -e "\033[0m" && sleep 5
}
function unbind() {
local i
cd ${WORKDIR}
i=0
while [ $i -le 5 ];do
umount -l ${output_dir}/dev/pts 2>/dev/null || break
sleep 1
i=$((i++))
done
i=0
while [ $i -le 5 ];do
umount -l ${output_dir}/dev/pts 2>/dev/null || break
sleep 1
i=$((i++))
done
i=0
i=0
while [ $i -le 5 ];do
umount -l ${output_dir}/dev 2>/dev/null || break
sleep 1
i=$((i++))
done
i=0
while [ $i -le 5 ];do
umount -l ${output_dir}/proc 2>/dev/null || break
sleep 1
i=$((i++))
done
i=0
while [ $i -le 5 ];do
umount -l ${output_dir}/sys 2>/dev/null || break
sleep 1
i=$((i++))
done
i=0
while [ $i -le 5 ];do
umount -l ${output_dir}/run 2>/dev/null || break
sleep 1
i=$((i++))
done
}
function on_trap() {
unbind
exit 0
}
trap "on_trap" 2 3 15
# first stage
echo "Stage 1 ..."
if [ -f "${output_dir}/stage1-ok" ];then
echo "Stage 1 has been skipped."
else
if [ $CROSS_FLAG -eq 1 ];then
/usr/sbin/debootstrap --arch=arm64 --foreign ${os_release} ${output_dir} "$DEBOOTSTRAP_MIRROR"
ret=$?
else
/usr/sbin/debootstrap --arch=arm64 ${os_release} ${output_dir} "$DEBOOTSTRAP_MIRROR"
ret=$?
fi
if [ $ret -ne 0 ]; then
echo "Stage 1 failed! Exit code: $ret"
if ask_force_pass "1" "${output_dir}/stage1-ok"; then
if [ $CROSS_FLAG -eq 1 ];then
mkdir -p ${output_dir}/usr/bin && cp -fv /usr/bin/qemu-aarch64-static "${output_dir}/usr/bin/"
fi
else
exit 1
fi
else
touch "${output_dir}/stage1-ok"
fi
echo "Stage 1 pass"
fi
mkdir -p ${output_dir}
mkdir -p ${output_dir}/dev
mkdir -p ${output_dir}/dev/pts
mkdir -p ${output_dir}/proc
mkdir -p ${output_dir}/run
mkdir -p ${output_dir}/sys
bind
# second stage
echo "Stage 2 ... "
if [ $CROSS_FLAG -eq 0 ];then
echo "Stage 2 is not required in the native environment and has been skipped."
else
if [ -f "${output_dir}/stage2-ok" ];then
echo "Stage2 has been skipped."
else
chroot "${output_dir}" debootstrap/debootstrap --second-stage
ret=$?
if [ $ret -ne 0 ]; then
echo "Stage 2 failed! Exit code: $ret"
if ! ask_force_pass "2" "${output_dir}/stage2-ok"; then
exit 1
fi
else
touch "${output_dir}/stage2-ok"
fi
echo "Stage 2 pass"
fi
fi
# third stage
echo "Stage 3 ..."
if [ $CROSS_FLAG -eq 1 ];then
mkdir -p ${output_dir}/usr/bin && cp -fv /usr/bin/qemu-aarch64-static "${output_dir}/usr/bin/"
fi
# 阻止服务启动
cat > ${output_dir}/usr/sbin/policy-rc.d <<'EOF'
#!/bin/sh
exit 101
EOF
chmod +x ${output_dir}/usr/sbin/policy-rc.d
cp ${SOURCES_LIST_WORK} ${output_dir}/etc/apt/sources.list
env_file="${output_dir}/tmp/chroot.env"
cat > ${env_file} <<EOF
CHROOT_DEFAULT_ROOT_PASSWORD="${CHROOT_DEFAULT_ROOT_PASSWORD}"
ENABLE_EXT_REPO="${CHROOT_ENABLE_EXT_REPO}"
EXT_REPOS="${CHROOT_EXT_REPOS}"
NECESSARY_PKGS="${CHROOT_NECESSARY_PKGS}"
OPTIONAL_PKGS="${CHROOT_OPTIONAL_PKGS}"
CUSTOM_PKGS="${CHROOT_CUSTOM_PKGS}"
INSTALL_LOCAL_DEBS="${CHROOT_INSTALL_LOCAL_DEBS}"
LOCAL_DEBS_HOME="/tmp/debs/"
LOCAL_DEBS_LIST="${CHROOT_LOCAL_DEBS_LIST}"
HAS_GRAPHICAL_DESKTOP="${CHROOT_HAS_GRAPHICAL_DESKTOP}"
DISPLAY_MANAGER="${CHROOT_DISPLAY_MANAGER}"
EOF
if [ "${CHROOT_INSTALL_LOCAL_DEBS}" == "yes" ];then
if [ "${CHROOT_LOCAL_DEBS_HOME}" != "" ] && [ -d "${CHROOT_LOCAL_DEBS_HOME}" ];then
mkdir -p ${output_dir}/tmp/debs && \
cp -av ${CHROOT_LOCAL_DEBS_HOME}/* ${output_dir}/tmp/debs/
fi
fi
if [ -f "${CHROOT}" ];then
cp -v "${CHROOT}" "${output_dir}/tmp/chroot.sh"
else
echo "${CHROOT} not exists!"
unbind
exit 1
fi
if [ $CROSS_FLAG -eq 1 ];then
chroot ${output_dir} /usr/bin/qemu-aarch64-static /bin/bash /tmp/chroot.sh || \
{ echo "Stage 3 failed!"; unbind; exit 1; }
else
chroot ${output_dir} /bin/bash /tmp/chroot.sh || \
{ echo "Stage 3 failed!"; unbind; exit 1; }
fi
echo "umount ... "
unbind
echo 'done'
echo
[ $CROSS_FLAG -eq 1 ] && rm ${output_dir}/usr/bin/qemu-aarch64-static
[ -f ${SOURCES_LIST_ORIG} ] && cp -v ${SOURCES_LIST_ORIG} ${output_dir}/etc/apt/sources.list
exit 0