-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.sh
More file actions
executable file
·246 lines (214 loc) · 6.26 KB
/
utils.sh
File metadata and controls
executable file
·246 lines (214 loc) · 6.26 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
#!/bin/sh
# script utilities
# safety
set -u
as_user() {
sudo -E -u "$MKROOTFS_USER" -g "$MKROOTFS_GROUP" "$@"
}
# makes fetching packages and stuff easier
get_arch() {
ARCH="$(uname -m)"
case $ARCH in
x86_64) echo "amd64" ;;
*) echo "$ARCH" ;;
esac
}
COLOR_BOLD_WHITE="\033[1;37m"
COLOR_BOLD_GREEN="\033[1;32m"
COLOR_BOLD_RED="\033[1;31m"
COLOR_RESET="\033[0m"
prestage_log() {
if [ -z "$MKROOTFS_NO_COLOR" ]; then
printf "${COLOR_BOLD_GREEN}%s${COLOR_RESET}\n" "$*"
else
echo "$@"
fi
}
poststage_log() {
prestage_log "$@"
}
stage_log() {
if [ -z "$MKROOTFS_NO_COLOR" ]; then
printf "${COLOR_BOLD_WHITE}${MKROOTFS_STAGE}:${COLOR_RESET} %s\n" "$*"
else
echo "${MKROOTFS_STAGE}: $@"
fi
}
stage_sublog() {
if [ -z "$MKROOTFS_NO_COLOR" ]; then
printf "${COLOR_BOLD_WHITE}-->${COLOR_RESET} %s\n" "$*"
else
echo "--> $@"
fi
}
error_log() {
if [ -z "$MKROOTFS_NO_COLOR" ]; then
printf "${COLOR_BOLD_RED}ERROR:${COLOR_RESET} %s, exitting...\n" "$*"
else
echo "ERROR: $@, exitting..."
fi
}
die_log() {
error_log "$1"
if [ $# -gt 1 ]; then
exit $2
else
exit 1
fi
}
silent() {
"$@" > /dev/null 2>&1
}
fetch_file() {
as_user wget "$1" -O "$2"
}
fetch_file_root() {
wget "$1" -O "$2"
}
run_hook() {
silent type "mkrootfs_${1}_hook"
if [ $? -eq 0 ]; then
"mkrootfs_${1}_hook"
fi
}
switch_dir() {
cd "$MKROOTFS_GENERATED" || die_log "could not switch directory"
}
MKROOTFS_CLEANUP_FUNCS=""
MKROOTFS_CLEANUP_ERROR_FUNCS=""
cleanup_cb_error() {
EXITCODE=$?
if [ $# -gt 0 ]; then
EXITCODE=$1
fi
trap - EXIT INT QUIT ABRT TERM
stage_log "cleaning up after error..."
for func in $(echo $MKROOTFS_CLEANUP_ERROR_FUNCS | tr ';' ' '); do
eval "$func"
done
exit $EXITCODE
}
cleanup_cb() {
EXITCODE=$?
if [ $EXITCODE -ne 0 ]; then
cleanup_cb_error $EXITCODE
fi
trap - EXIT INT QUIT ABRT TERM
stage_log "cleaning up after success..."
for func in $(echo $MKROOTFS_CLEANUP_FUNCS | tr ';' ' '); do
eval "$func"
done
exit 0
}
trap cleanup_cb EXIT
trap cleanup_cb_error INT QUIT ABRT TERM
add_cleanup_success() {
MKROOTFS_CLEANUP_FUNCS="${MKROOTFS_CLEANUP_FUNCS};$1"
}
add_cleanup() {
MKROOTFS_CLEANUP_ERROR_FUNCS="${MKROOTFS_CLEANUP_ERROR_FUNCS};$1"
}
export MKROOTFS_BINFMT_NAME="mkrootfs-aarch64"
export MKROOTFS_BINFMT_MAGIC="\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7"
export MKROOTFS_BINFMT_MASK="\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff"
register_binfmt() {
if [ "$MKROOTFS_TARGET_ARCH" != "$MKROOTFS_CURRENT_ARCH" ]; then
stage_sublog "registering binary format..."
test -d "/proc/sys/fs/binfmt_misc" || \
die_log "no binfmt_misc support in your kernel"
if [ ! -f "/proc/sys/fs/binfmt_misc/register" ]; then
mount -t binfmt_misc none /proc/sys/fs/binfmt_misc || \
die_log "could not mount binfmt_misc"
add_cleanup unregister_binfmt
add_cleanup_success unregister_binfmt
fi
if [ ! -f "/proc/sys/fs/binfmt_misc/${MKROOTFS_BINFMT_NAME}" ]; then
echo ":${MKROOTFS_BINFMT_NAME}:M::${MKROOTFS_BINFMT_MAGIC}:${MKROOTFS_BINFMT_MASK}:/${MKROOTFS_QEMU}:" \
> /proc/sys/fs/binfmt_misc/register
if [ $? -ne 0 ]; then
die_log "binfmt registration failed"
fi
fi
fi
}
unregister_binfmt() {
if [ "$MKROOTFS_TARGET_ARCH" != "$MKROOTFS_CURRENT_ARCH" ]; then
if [ -f "/proc/sys/fs/binfmt_misc/${MKROOTFS_BINFMT_NAME}" ]; then
echo -1 > "/proc/sys/fs/binfmt_misc/${MKROOTFS_BINFMT_NAME}"
fi
fi
}
prepare_binfmt() {
if [ "$MKROOTFS_TARGET_ARCH" != "$MKROOTFS_CURRENT_ARCH" ]; then
cp "../../bin/$MKROOTFS_QEMU" "$MKROOTFS_ROOT_DIR" || \
die_log "could not copy qemu"
fi
}
unprepare_binfmt() {
if [ "$MKROOTFS_TARGET_ARCH" != "$MKROOTFS_CURRENT_ARCH" ]; then
rm -f "${MKROOTFS_ROOT_DIR}/${MKROOTFS_QEMU}"
fi
}
mount_pseudo() {
stage_sublog "ounting pseudo-filesystems..."
mkdir -p "${MKROOTFS_ROOT_DIR}/dev" "${MKROOTFS_ROOT_DIR}/proc" \
"${MKROOTFS_ROOT_DIR}/sys"
mount --bind /dev "${MKROOTFS_ROOT_DIR}/dev"
mount --bind /sys "${MKROOTFS_ROOT_DIR}/sys"
mount --bind /proc "${MKROOTFS_ROOT_DIR}/proc"
add_cleanup umount_pseudo
add_cleanup_success umount_pseudo
}
umount_pseudo() {
silent umount "${MKROOTFS_ROOT_DIR}/dev"
silent umount "${MKROOTFS_ROOT_DIR}/sys"
silent umount "${MKROOTFS_ROOT_DIR}/proc"
}
prepare_net() {
silent cp /etc/resolv.conf "${MKROOTFS_ROOT_DIR}/etc"
if [ $? -eq 0 ]; then
add_cleanup unprepare_net
add_cleanup_success unprepare_net
fi
}
unprepare_net() {
rm -f "${MKROOTFS_ROOT_DIR}/etc/resolv.conf"
}
test_rootfs() {
test -d "$MKROOTFS_ROOT_DIR" || die_log "root directory does not exist"
}
make_rootfs() {
test ! -d "$MKROOTFS_ROOT_DIR" || die_log "root directory already exists"
mkdir "$MKROOTFS_ROOT_DIR" || die_log "could not create root directory"
chgrp "$MKROOTFS_ROOT_GID" "$MKROOTFS_ROOT_DIR" || \
die_log "could not set root directory permissions"
add_cleanup remove_rootfs
}
remove_rootfs() {
silent rm -rf "$MKROOTFS_ROOT_DIR"
}
in_rootfs() {
chroot "$MKROOTFS_ROOT_DIR" "$MKROOTFS_ENV_BIN" -i \
HOME="$MKROOTFS_ENV_HOME" TERM="$MKROOTFS_ENV_TERM" \
PATH="$MKROOTFS_ENV_PATH" SHELL="$MKROOTFS_ENV_SHELL" \
"$@"
}
archive_rootfs() {
ROOTBASE="${MKROOTFS_DISTRO}-$(date '+%Y%m%d')"
ROOTEXT="tar.xz"
TARARGS="cpJf"
if [ -f "../${ROOTBASE}.${ROOTEXT}" ]; then
I=2
while [ -f "../${ROOTBASE}_${I}.${ROOTEXT}"]; do
I=$(($I + 1))
done
ROOTBASE="${ROOTBASE}_${I}"
fi
ROOTNAME="${ROOTBASE}.${ROOTEXT}"
stage_sublog "creating archive ${ROOTNAME}..."
cd "${MKROOTFS_ROOT_DIR}" || die_log "could not enter root directory"
tar "$TARARGS" "../../${ROOTNAME}" . || \
die_log "could not create rootfs archive"
chown "${MKROOTFS_USER}:${MKROOTFS_GROUP}" "../../${ROOTNAME}"
stage_sublog "created archive: ${ROOTNAME}"
}