source: LFS/master.sh@ 7b33f40

ablfs-more legacy trunk
Last change on this file since 7b33f40 was 9a4c530, checked in by Pierre Labastie <pierre@…>, 4 years ago

Clean LFS/master.sh from unused
functions

  • Property mode set to 100644
File size: 15.4 KB
Line 
1#!/bin/bash
2
3# $Id$
4
5###################################
6### FUNCTIONS ###
7###################################
8
9
10#############################################################
11
12
13#-------------------------#
14chapter_targets() { #
15#-------------------------#
16# $1 is the chapter number. Pad it with 0 to the left to obtain a 2-digit
17# number:
18 printf -v dir chapter%02d $1
19
20# $2 contains the build number if rebuilding for ICA
21 if [[ -z "$2" ]] ; then
22 local N=""
23 else
24 local N=-build_$2
25 local CHROOT_TGT=""
26 mkdir ${dir}$N
27 cp ${dir}/* ${dir}$N
28 for script in ${dir}$N/* ; do
29 # Overwrite existing symlinks, files, and dirs
30 sed -e 's/ln *-sv/&f/g' \
31 -e 's/mv *-v/&f/g' \
32 -e 's/mkdir *-v/&p/g' -i ${script}
33 # Rename the scripts
34 mv ${script} ${script}$N
35 done
36 # Remove Bzip2 binaries before make install (LFS-6.2 compatibility)
37 sed -e 's@make install@rm -vf /usr/bin/bz*\n&@' -i ${dir}$N/*-bzip2$N
38 # Remove openssl-<version> from /usr/share/doc (LFS-9.x), because
39 # otherwise the mv command creates an openssl directory.
40 sed -e 's@mv -v@rm -rfv /usr/share/doc/openssl-*\n&@' \
41 -i ${dir}$N/*-openssl$N
42 fi
43
44 echo "${tab_}${GREEN}Processing... ${L_arrow}Chapter $1$N${R_arrow}"
45
46 for file in ${dir}$N/* ; do
47 # Keep the script file name
48 this_script=`basename $file`
49
50 # Some scripts need peculiar actions:
51 # - glibc chap 5: fix locales creation when running chapter05 testsuites
52 # - Stripping at the end of system build: lfs.xsl does not generate
53 # correct commands if the user does not want to strip, so skip it
54 # in this case
55 # - do not reinstall linux-headers when rebuilding
56 # - grub config: must be done manually; skip it
57 # - handle fstab and .config. Skip kernel if .config not supplied
58 case "${this_script}" in
59 5*glibc) [[ "${TEST}" = "3" ]] && \
60 sed -i 's@/usr/lib/locale@/tools/lib/locale@' $file ;;
61 *strippingagain) [[ "${STRIP}" = "n" ]] && continue ;;
62 *linux-headers*) [[ -n "$N" ]] && continue ;;
63 8*grub) (( nb_chaps == 5 )) && continue ;;
64 10*grub) continue ;;
65 *fstab) [[ -z "${FSTAB}" ]] ||
66 [[ ${FSTAB} == $BUILDDIR/sources/fstab ]] ||
67 cp ${FSTAB} $BUILDDIR/sources/fstab ;;
68 *kernel) [[ -z ${CONFIG} ]] && continue
69 [[ ${CONFIG} == $BUILDDIR/sources/kernel-config ]] ||
70 cp ${CONFIG} $BUILDDIR/sources/kernel-config ;;
71 esac
72 # Grab the name of the target
73 name=`echo ${this_script} | sed -e 's@[0-9]\{3,4\}-@@' \
74 -e 's@-pass[0-9]\{1\}@@' \
75 -e 's@-libstdc++@@' \
76 -e 's,'$N',,'`
77
78 # Find the name of the tarball and the version of the package
79 # If it doesn't exist, we skip it in iterations rebuilds (except stripping
80 # and revisedchroot, where .a and .la files are removed).
81 pkg_tarball=$(sed -n 's/tar -xf \(.*\)/\1/p' $file)
82 pkg_version=$(sed -n 's/VERSION=\(.*\)/\1/p' $file)
83
84 if [[ "$pkg_tarball" = "" ]] && [[ -n "$N" ]] ; then
85 case "${this_script}" in
86 *stripping*|*revised*) ;;
87 *) continue ;;
88 esac
89 fi
90
91 # Append the name of the script to a list. The name of the
92 # list is contained in the variable Makefile_target. We adjust this
93 # variable at various points. Note that it is initialized to "SETUP"
94 # in the main function, before calling this function for the first time.
95 case "${this_script}" in
96 *settingenvironment) Makefile_target=LUSER_TGT ;;
97 *changingowner ) Makefile_target=SUDO_TGT ;;
98 *creatingdirs ) Makefile_target=CHROOT_TGT ;;
99 *bootscripts ) Makefile_target=BOOT_TGT ;; # case of sysv book
100 *network ) Makefile_target=BOOT_TGT ;; # case of systemd book
101 esac
102 eval $Makefile_target=\"\$$Makefile_target ${this_script}\"
103
104 #--------------------------------------------------------------------#
105 # >>>>>>>> START BUILDING A Makefile ENTRY <<<<<<<< #
106 #--------------------------------------------------------------------#
107
108 # Drop in the name of the target on a new line, and the previous target
109 # as a dependency. Also call the echo_message function.
110 case $Makefile_target in
111 CHROOT_TGT) CHROOT_wrt_target "${this_script}" "$PREV" "$pkg_version" ;;
112 *) LUSER_wrt_target "${this_script}" "$PREV" "$pkg_version" ;;
113 esac
114
115 # If $pkg_tarball isn't empty, we've got a package...
116 if [ "$pkg_tarball" != "" ] ; then
117 # Touch timestamp file if installed files logs shall be created.
118 # But only for the final install chapter and not when rebuilding it
119 if [ "${INSTALL_LOG}" = "y" ] &&
120 (( 1+nb_chaps <= $1 )) &&
121 [ "x$N" = x ] ; then
122 CHROOT_wrt_TouchTimestamp
123 fi
124 # Always initialize the test log file, since the test instructions may
125 # be "uncommented" by the user
126 case $Makefile_target in
127 CHROOT_TGT) CHROOT_wrt_test_log "${this_script}" "$pkg_version" ;;
128 LUSER_TGT ) LUSER_wrt_test_log "${this_script}" "$pkg_version" ;;
129 esac
130
131 # If using optimizations, write the instructions
132 case "${OPTIMIZE}$1${nb_chaps}${this_script}${REALSBU}" in
133 0* | *binutils-pass1y | 15* | 167* | 177*) ;;
134 *kernel*) wrt_makeflags "$name" ;; # No CFLAGS for kernel
135 *) wrt_optimize "$name" && wrt_makeflags "$name" ;;
136 esac
137 fi
138
139# Some scriptlet have a special treatment; otherwise standard
140 case "${this_script}" in
141 *addinguser)
142(
143cat << EOF
144 @if [ -f luser-id ]; then \\
145 function useradd() { true; }; \\
146 function groupadd() { true; }; \\
147 export -f useradd groupadd; \\
148 fi; \\
149 export LFS=\$(MOUNT_PT) && \\
150 \$(CMDSDIR)/`dirname $file`/\$@ >> \$(LOGDIR)/\$@ 2>&1; \\
151 \$(PRT_DU) >>logs/\$@
152 @chown \$(LUSER):\$(LGROUP) envars
153 @chmod -R a+wt $JHALFSDIR
154 @chmod a+wt \$(SRCSDIR)
155EOF
156) >> $MKFILE.tmp
157 ;;
158 *settingenvironment)
159(
160cat << EOF
161 @cd && \\
162 function source() { true; } && \\
163 export -f source && \\
164 \$(CMDSDIR)/`dirname $file`/\$@ >> \$(LOGDIR)/\$@ 2>&1 && \\
165 sed 's|/mnt/lfs|\$(MOUNT_PT)|' -i .bashrc && \\
166 echo source $JHALFSDIR/envars >> .bashrc
167 @\$(PRT_DU) >>logs/\$@
168EOF
169) >> $MKFILE.tmp
170 ;;
171 *fstab) if [[ -n "$FSTAB" ]]; then
172 CHROOT_wrt_CopyFstab
173 else
174 CHROOT_wrt_RunAsRoot "$file"
175 fi
176 ;;
177
178 *)
179 # Insert date and disk usage at the top of the log file, the script
180 # run and date and disk usage again at the bottom of the log file.
181 case "${Makefile_target}" in
182 SETUP_TGT | SUDO_TGT) wrt_RunAsRoot "$file" "$pkg_version" ;;
183 LUSER_TGT) LUSER_wrt_RunAsUser "$file" "$pkg_version" ;;
184 CHROOT_TGT | BOOT_TGT) CHROOT_wrt_RunAsRoot "$file" "$pkg_version" ;;
185 esac
186 ;;
187 esac
188
189 # Write installed files log and remove the build directory(ies)
190 # except if the package build fails.
191 if [ "$pkg_tarball" != "" ] ; then
192 if [ "${INSTALL_LOG}" = "y" ] &&
193 (( 1+nb_chaps <= $1 )) &&
194 [ "x${N}" = "x" ] ; then
195 CHROOT_wrt_LogNewFiles "$name"
196 fi
197 fi
198
199 # Include a touch of the target name so make can check
200 # if it's already been made.
201 wrt_touch
202 #
203 #--------------------------------------------------------------------#
204 # >>>>>>>> END OF Makefile ENTRY <<<<<<<< #
205 #--------------------------------------------------------------------#
206
207 # Keep the script file name for Makefile dependencies.
208 PREV=${this_script}
209 # Set "system_build" var for iteration targets
210 if [ -z "$N" ] && (( 1+nb_chaps == $1 )); then
211 system_build="$system_build $this_script"
212 fi
213
214 done # end for file in $dir/*
215 # Set "system_build" when rebuilding: note the CHROOT_TGT is local
216 # in that case.
217 if [ -n "$N" ]; then
218 system_build="$CHROOT_TGT"
219 fi
220}
221
222#----------------------------#
223build_Makefile() { #
224#----------------------------#
225
226 echo "Creating Makefile... ${BOLD}START${OFF}"
227
228 cd $JHALFSDIR/${PROGNAME}-commands
229
230 # Start with empty files
231 >$MKFILE
232 >$MKFILE.tmp
233
234 # Ensure the first dependency is empty
235 unset PREV
236
237 # We begin with the SETUP target; successive targets will be assigned in
238 # the chapter_targets function.
239 Makefile_target=SETUP_TGT
240
241 # We need to know the chapter numbering, which depends on the version
242 # of the book. Use the number of subdirs to know which version we have
243 chaps=($(echo *))
244 nb_chaps=${#chaps[*]} # 5 if classical version, 7 if new version
245# DEBUG
246# echo chaps: ${chaps[*]}
247# echo nb_chaps: $nb_chaps
248# end DEBUG
249
250 # Make a temporary file with all script targets
251 for (( i = 4; i < nb_chaps+4; i++ )); do
252 chapter_targets $i
253 if (( i == nb_chaps )); then : # we have finished temporary tools
254 # Add the save target, if needed
255 [[ "$SAVE_CH5" = "y" ]] && wrt_save_target $Makefile_target
256 fi
257 if (( i == 1+nb_chaps )); then : # we have finished final system
258 # Add the iterations targets, if needed
259 [[ "$COMPARE" = "y" ]] && wrt_compare_targets $i
260 fi
261 done
262 # Add the CUSTOM_TOOLS targets, if needed
263 [[ "$CUSTOM_TOOLS" = "y" ]] && wrt_CustomTools_target
264
265 # Add a header, some variables and include the function file
266 # to the top of the real Makefile.
267 wrt_Makefile_header
268
269 # Add chroot commands
270 CHROOT_LOC="`whereis -b chroot | cut -d " " -f2`"
271 i=1
272 for file in ../chroot-scripts/*chroot* ; do
273 chroot=`cat $file | \
274 perl -pe 's|\\\\\n||g' | \
275 tr -s [:space:] | \
276 grep chroot | \
277 sed -e "s|chroot|$CHROOT_LOC|" \
278 -e 's|\\$|&&|g' \
279 -e 's|"$$LFS"|$(MOUNT_PT)|'`
280 echo -e "CHROOT$i= $chroot\n" >> $MKFILE
281 i=`expr $i + 1`
282 done
283
284 # Store virtual kernel file systems commands:
285 devices=`cat ../kernfs-scripts/devices.sh | \
286 sed -e 's|^| |' \
287 -e 's|mount|sudo &|' \
288 -e 's|mkdir|sudo &|' \
289 -e 's|\\$|&&|g' \
290 -e 's|\$|; \\\\|' \
291 -e 's|then|& :|' \
292 -e 's|\$\$LFS|$(MOUNT_PT)|g'`
293 teardown=`cat ../kernfs-scripts/teardown.sh | \
294 sed -e 's|^| |' \
295 -e 's|umount|sudo &|' \
296 -e 's|\$LFS|$(MOUNT_PT)|'`
297 teardownat=`cat ../kernfs-scripts/teardown.sh | \
298 sed -e 's|^| |' \
299 -e 's|umount|@-sudo &|' \
300 -e 's|\$LFS|$(MOUNT_PT)|'`
301 # Drop in the main target 'all:' and the chapter targets with each sub-target
302 # as a dependency.
303(
304 cat << EOF
305
306all: ck_UID ck_terminal mk_SETUP mk_LUSER mk_SUDO mk_CHROOT mk_BOOT create-sbu_du-report mk_BLFS_TOOL mk_CUSTOM_TOOLS
307$teardownat
308 @sudo make do_housekeeping
309EOF
310) >> $MKFILE
311if [ "$INITSYS" = systemd ]; then
312(
313 cat << EOF
314 @/bin/echo -e -n \\
315 NAME=\\"Linux From Scratch\\"\\\\n\\
316 VERSION=\\"$VERSION\\"\\\\n\\
317 ID=lfs\\\\n\\
318 PRETTY_NAME=\\"Linux From Scratch $VERSION\\"\\\\n\\
319 VERSION_CODENAME=\\"$(whoami)-jhalfs\\"\\\\n\\
320 > os-release && \\
321 sudo mv os-release \$(MOUNT_PT)/etc && \\
322 sudo chown root:root \$(MOUNT_PT)/etc/os-release
323EOF
324) >> $MKFILE
325fi
326(
327 cat << EOF
328 @echo $VERSION > lfs-release && \\
329 sudo mv lfs-release \$(MOUNT_PT)/etc && \\
330 sudo chown root:root \$(MOUNT_PT)/etc/lfs-release
331 @/bin/echo -e -n \\
332 DISTRIB_ID=\\"Linux From Scratch\\"\\\\n\\
333 DISTRIB_RELEASE=\\"$VERSION\\"\\\\n\\
334 DISTRIB_CODENAME=\\"$(whoami)-jhalfs\\"\\\\n\\
335 DISTRIB_DESCRIPTION=\\"Linux From Scratch\\"\\\\n\\
336 > lsb-release && \\
337 sudo mv lsb-release \$(MOUNT_PT)/etc && \\
338 sudo chown root:root \$(MOUNT_PT)/etc/lsb-release
339 @\$(call echo_finished,$VERSION)
340
341ck_UID:
342 @if [ \`id -u\` = "0" ]; then \\
343 echo "--------------------------------------------------"; \\
344 echo "You cannot run this makefile from the root account"; \\
345 echo "--------------------------------------------------"; \\
346 exit 1; \\
347 fi
348
349ck_terminal:
350 @stty size | ( read L C; \\
351 if (( L < 24 )) || (( C < 80 )) ; then \\
352 echo "--------------------------------------------------"; \\
353 echo "Terminal too small: \$\$C columns x \$\$L lines";\\
354 echo "Minimum: 80 columns x 24 lines";\\
355 echo "--------------------------------------------------"; \\
356 exit 1; \\
357 fi )
358
359mk_SETUP:
360 @\$(call echo_SU_request)
361 @sudo make save-luser
362 @sudo make BREAKPOINT=\$(BREAKPOINT) SETUP
363 @touch \$@
364
365mk_LUSER: mk_SETUP
366 @\$(call echo_SULUSER_request)
367 @\$(SU_LUSER) "make -C \$(MOUNT_PT)/\$(SCRIPT_ROOT) BREAKPOINT=\$(BREAKPOINT) LUSER"
368 @sudo make restore-luser
369 @touch \$@
370
371mk_SUDO: mk_LUSER
372 @sudo rm -f envars
373 @sudo make BREAKPOINT=\$(BREAKPOINT) SUDO
374 @touch \$@
375
376mk_CHROOT: mk_SUDO
377 @\$(call echo_CHROOT_request)
378 @( sudo \$(CHROOT1) -c "cd \$(SCRIPT_ROOT) && make BREAKPOINT=\$(BREAKPOINT) CHROOT")
379 @touch \$@
380
381mk_BOOT: mk_CHROOT
382 @\$(call echo_CHROOT_request)
383 @( sudo \$(CHROOT2) -c "cd \$(SCRIPT_ROOT) && make BREAKPOINT=\$(BREAKPOINT) BOOT")
384 @touch \$@
385
386mk_BLFS_TOOL: create-sbu_du-report
387 @if [ "\$(ADD_BLFS_TOOLS)" = "y" ]; then \\
388 \$(call sh_echo_PHASE,Building BLFS_TOOL); \\
389 (sudo \$(CHROOT2) -c "make -C $BLFS_ROOT/work"); \\
390 fi;
391 @touch \$@
392
393mk_CUSTOM_TOOLS: mk_BLFS_TOOL
394 @if [ "\$(ADD_CUSTOM_TOOLS)" = "y" ]; then \\
395 \$(call sh_echo_PHASE,Building CUSTOM_TOOLS); \\
396 sudo mkdir -p ${BUILDDIR}${TRACKING_DIR}; \\
397 (sudo \$(CHROOT2) -c "cd \$(SCRIPT_ROOT) && make BREAKPOINT=\$(BREAKPOINT) CUSTOM_TOOLS"); \\
398 fi;
399 @touch \$@
400
401devices: ck_UID
402$devices
403EOF
404) >> $MKFILE
405if [ "$INITSYS" = systemd ]; then
406(
407 cat << EOF
408 sudo mkdir -pv \$(MOUNT_PT)/run/systemd/resolve
409 sudo cp -v /etc/resolv.conf \$(MOUNT_PT)/run/systemd/resolve
410EOF
411) >> $MKFILE
412fi
413(
414 cat << EOF
415
416teardown:
417$teardown
418
419chroot1: devices
420 sudo \$(CHROOT1)
421 \$(MAKE) teardown
422
423chroot: devices
424 sudo \$(CHROOT2)
425 \$(MAKE) teardown
426
427SETUP: $SETUP_TGT
428LUSER: $LUSER_TGT
429SUDO: $SUDO_TGT
430EOF
431) >> $MKFILE
432if [ "$INITSYS" = systemd ]; then
433(
434 cat << EOF
435 mkdir -pv \$(MOUNT_PT)/run/systemd/resolve
436 cp -v /etc/resolv.conf \$(MOUNT_PT)/run/systemd/resolve
437
438EOF
439) >> $MKFILE
440fi
441(
442 cat << EOF
443CHROOT: SHELL=\$(filter %bash,\$(CHROOT1))
444CHROOT: $CHROOT_TGT
445BOOT: $BOOT_TGT
446CUSTOM_TOOLS: $custom_list
447
448create-sbu_du-report: mk_BOOT
449 @\$(call echo_message, Building)
450 @if [ "\$(ADD_REPORT)" = "y" ]; then \\
451 sudo ./create-sbu_du-report.sh logs $VERSION $(date --iso-8601); \\
452 \$(call echo_report,$VERSION-SBU_DU-$(date --iso-8601).report); \\
453 fi
454 @touch \$@
455
456save-luser:
457 @\$(call echo_message, Building)
458 @LUSER_ID=\$\$(grep '^\$(LUSER):' /etc/passwd | cut -d: -f3); \\
459 if [ -n "\$\$LUSER_ID" ]; then \\
460 if [ ! -d \$(LUSER_HOME).XXX ]; then \\
461 mv \$(LUSER_HOME){,.XXX}; \\
462 mkdir \$(LUSER_HOME); \\
463 chown \$(LUSER):\$(LGROUP) \$(LUSER_HOME); \\
464 fi; \\
465 echo "\$\$LUSER_ID" > luser-id; \\
466 echo User \$(LUSER) exists with ID \$\$LUSER_ID; \\
467 else \\
468 rm -f luser-id; \\
469 echo User \$(LUSER) does not exist; \\
470 echo It will be created with book instructions.; \\
471 fi
472 @\$(call housekeeping)
473
474restore-luser:
475 @\$(call echo_message, Building)
476 @if [ -f luser-id ]; then \\
477 rm -rf \$(LUSER_HOME); \\
478 mv \$(LUSER_HOME){.XXX,}; \\
479 rm luser-id; \\
480 else \\
481 userdel \$(LUSER); \\
482 groupdel \$(LGROUP); \\
483 rm -rf \$(LUSER_HOME); \\
484 fi
485 @\$(call housekeeping)
486
487do_housekeeping:
488 @-rm /tools
489
490EOF
491) >> $MKFILE
492
493 # Bring over the items from the Makefile.tmp
494 cat $MKFILE.tmp >> $MKFILE
495 rm $MKFILE.tmp
496 echo "Creating Makefile... ${BOLD}DONE${OFF}"
497}
Note: See TracBrowser for help on using the repository browser.