-
Notifications
You must be signed in to change notification settings - Fork 3
/
lfs
executable file
·343 lines (303 loc) · 9.01 KB
/
lfs
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
set -e
prog="$(basename $0)"
cd "$(dirname $0)"
source ./env
stages="stage1a stage1b stage1c stage2 final"
for stage in $stages; do
images+="lfs-${stage} "
done
usage() {
cat <<EOF
Usage: $prog <command>
EOF
exit 2
}
assert-stage() {
if [ -z "$stage" ] ; then
echo "$prog: error: stage name should preceed command"
exit 1
fi
}
timestamp() {
date +"%Y-%m-%d %H:%m:%S"
}
lfs-env() {
env | grep -e "^lfs_" | sort
}
lfs-init() {
if [ "$stage" == "stage1a" ] ; then
build_arg="--build-arg lfs_host_image=${lfs_host_image}"
elif [ "$stage" == "final" ] ; then
build_arg="--build-arg lfs_init_user=${lfs_init_user}"
fi
set -x
bash -c "source ./env ; env | grep -e '^lfs_'" > eval.env
podman stop -t 0 lfs-$stage || true
podman rm -f lfs-$stage || true
podman build --tag lfs-$stage $build_arg containers/lfs-$stage
podman create \
--name lfs-$stage \
--hostname lfs-$stage \
--env-file eval.env \
--userns keep-id \
--volume "./build/sources:/build/rpmbuild/SOURCES:z" \
--volume "./build/${stage}/rpms:/build/rpmbuild/RPMS:z" \
--volume "./build/${stage}/srpms:/build/rpmbuild/SRPMS:z" \
--volume .:/build/lfs-rpm:z \
lfs-$stage
podman start lfs-$stage
}
lfs-spectool() {
(
set -x
./spectool -d "with_lfs_stage1 1" -d "lfs_version ${lfs_version}" -g -C build/sources $1
)
}
lfs-download() {
specs=$@
[ -z "$specs" ] && specs="$(ls specs/*.spec | grep -v '#' | sort)"
for spec in $specs; do
lfs-spectool $spec
done
}
lfs-build() {
method=$1
shift
specs=$*
[ -z "$specs" ] && specs="$(cat containers/lfs-$stage/$stage.pkg.txt)"
echo "" >> build/logs/build.log
for spec in $specs; do
if [ ! -f "$spec" ] ; then
echo "$prog: error: $spec: file not found"
continue
fi
package=$(basename "$spec" .spec)
if [ "$package" == "libstdc++" ]; then
rpmbuild_pkg_flags="--with lfs_gcc_libstdcpp_only"
elif [ "$package" == "gcc" ] && [ "$stage" == "stage1b" ] ; then
rpm_pkg_flags="--replacefiles"
fi
if [ "$method" == "--reinstall" ] ; then
rpm_pkg_flags="$rpm_pkg_flags --replacefiles"
fi
if [ ! -z "$*" ] || ! podman exec lfs-$stage rpm -q $package ; then
lfs-spectool "$spec"
echo "$stage:$package" > build/logs/last-started
echo "[$(timestamp)] start $stage:$package" >> build/logs/build.log
echo -e "\n----- building $stage:$package"
( set -e -x -o pipefail
podman exec lfs-$stage rpmbuild \
$rpmbuild_flags \
$rpmbuild_pkg_flags \
lfs-rpm/${spec} 2>&1 \
| tee build/logs/${stage}/${package}.log
podman exec --user root lfs-$stage rpm \
$method \
$rpm_flags \
$rpm_pkg_flags \
rpmbuild/RPMS/$lfs_arch/${package}-*.rpm 2>&1 \
| tee --append build/logs/${stage}/${package}.log
)
echo "$stage:$package" > build/logs/last-success
echo "[$(timestamp)] done $stage:$package" >> build/logs/build.log
fi
done
}
lfs-export() {
case $stage in
stage1a)
(
set -x
podman exec --user root -t lfs-${stage} tar -C /lfs -c -z -f /build/lfs-rpm/build/${stage}/lfs-${stage}.tar.gz .
cp build/${stage}/lfs-${stage}.tar.gz containers/lfs-${next_stage}
)
;;
stage1b)
(
set -x
podman exec --user root -t lfs-${stage} tar -C /lfs -c -z -f /build/lfs-rpm/build/${stage}/lfs-${stage}.tar.gz --exclude=./tools .
cp build/${stage}/lfs-${stage}.tar.gz containers/lfs-${next_stage}
)
;;
stage1c)
(
set -x
podman exec --user root -t lfs-$stage \
tar -C / -c -z -f /build/lfs-rpm/build/${stage}/lfs-$stage.tar.gz \
--exclude='./build' \
--exclude='./tmp/*' \
--exclude './home/*' \
--exclude './root/*' \
--exclude './dev/*' \
--exclude './proc/*' \
--exclude './sys/*' \
--exclude './var/lib/rpm/*' \
.
cp build/${stage}/lfs-${stage}.tar.gz containers/lfs-${next_stage}
)
;;
stage2)
(
set -x
podman exec --user root -t lfs-$stage \
tar -C / -c -z -f /build/lfs-rpm/build/${stage}/lfs-$stage.tar.gz \
--exclude='./build' \
--exclude='./tmp/*' \
--exclude './home/*' \
--exclude './root/*' \
--exclude './dev/*' \
--exclude './proc/*' \
--exclude './sys/*' \
.
rm -rf build/boot
podman cp "lfs-$stage:/boot" build/boot
cp build/${stage}/lfs-${stage}.tar.gz containers/lfs-${next_stage}
)
;;
final)
(
set -x
podman exec --user root -t lfs-$stage \
tar -C / -c -z -f /build/lfs-rpm/build/${stage}/lfs-$stage.tar.gz \
--exclude='./build' \
--exclude='./tmp/*' \
--exclude './dev/*' \
--exclude './proc/*' \
--exclude './sys/*' \
.
)
;;
*)
echo "export not defined for $stage"
exit 1
;;
esac
}
lfs-bootstrap() {
if [ "$stage" == "stage1c" ] ; then
lfs-spectool specs/cmake.spec
lfs-spectool specs/elfutils.spec
lfs-spectool specs/rpm.spec
podman exec --user root -t lfs-stage1c lfs-rpm/containers/lfs-stage1c/rpm-bootstrap.sh
else
echo "$prog: error: no bootstrap for this stage"
exit 1
fi
}
lfs-mkimage() {
set -x
rm -f "$lfs_root_img"
truncate -s $lfs_root_size "$lfs_root_img"
sudo mkfs -t ext4 "$lfs_root_img"
sudo mkdir -p /run/lfs
sudo mount "$lfs_root_img" /run/lfs
sudo tar xf build/final/lfs-final.tar.gz -C /run/lfs
sudo cp -r containers/lfs-final/overlay/* /run/lfs
sudo umount /run/lfs
}
lfs-install() {
set -x
sudo mkdir -p ${lfs_install_dir}
sudo cp ${lfs_root_img} ${lfs_install_dir}
sudo cp build/boot/vmlinuz ${lfs_install_dir}/lfs-${lfs_version}-vmlinuz
}
lfs-build-all() {
./lfs download
./lfs 1a init
./lfs 1a build
./lfs 1a export
./lfs 1b init
./lfs 1b build
./lfs 1b export
./lfs 1c init
./lfs 1c bootstrap
./lfs 1c build
./lfs 1c export
./lfs 2 init
./lfs 2 build
./lfs 2 export
./lfs final init
./lfs final export
}
lfs-clean() {
set -x
rm -rf
for stage in $stages; do
rm -rf "build/${stage}"
done
rm -rf build/logs
rm -rf containers/*/*.tar.gz
rm -f eval.env
podman stop --time 0 --ignore $images || true
podman rmi --force $images || true
}
lfs-dist-clean() {
set -x
lfs-reset
rm -rf build
}
mkdir -p build/{sources,logs}
for mkstage in $stages; do
mkdir -p build/$mkstage/{rpms,srpms}
mkdir -p build/logs/$mkstage
done
if [ "$with_check" != "1" ] ; then
rpm_nocheck="--nocheck"
fi
case $1 in
stage1a|1a)
shift
stage=stage1a
next_stage=stage1b
rpmbuild_flags="-bb $rpm_nocheck"
rpm_flags="--nodeps"
;;
stage1b|1b)
shift
stage=stage1b
next_stage=stage1c
rpmbuild_flags="-bb $rpm_nocheck"
rpm_flags="--nodeps"
;;
stage1c|1c)
shift
stage=stage1c
next_stage=stage2
rpmbuild_flags="-bb $rpm_nocheck"
rpm_flags="--nodeps"
;;
stage2|2)
shift
stage=stage2
next_stage=final
rpmbuild_flags="-ba $rpm_nocheck"
rpm_flags="--nodeps"
;;
final)
shift
stage=final
;;
esac
case $1 in
env) lfs-env ;;
download) shift ; lfs-download "$@" ;;
init) assert-stage && lfs-init ;;
start) assert-stage && podman start lfs-$stage ;;
stop) assert-stage && podman stop --timeout 0 lfs-$stage ;;
build) assert-stage && lfs-build --install ;;
rpm) assert-stage && shift ; lfs-build --reinstall "$@" ;;
shell) assert-stage && exec podman exec -it lfs-$stage /bin/bash ;;
root) assert-stage && exec podman exec --user root -it lfs-$stage /bin/bash ;;
export) assert-stage && lfs-export ;;
bootstrap) assert-stage && lfs-bootstrap ;;
mkimage) lfs-mkimage ;;
install) lfs-install ;;
clean) lfs-clean ;;
dist-clean) lfs-dist-clean ;;
build-all) lfs-build-all ;;
*)
echo "$prog: error: invalid command" 1>&2
usage
;;
esac