maketars revision 1.90
1#!/bin/sh
2#
3# $NetBSD: maketars,v 1.90 2019/10/02 11:16:00 maya Exp $
4#
5# Make release tar files for some or all lists.  Usage:
6# maketars [-b] [-x] [-i installdir] [-a arch] [-m machine] [-s setsdir]
7#	[-M metalog] [-N etcdir] [-F setlistsdir] [-d destdir]
8#	[-t tardir] [-U] [setname ...]
9#
10# The default sets are "base comp debug etc games man misc rescue tests text"
11# The X sets are "xbase xcomp xdebug xetc xfont xserver"
12# The extsrc sets are "extbase extcomp extetc"
13#
14# If '-i installdir' is given, copy the given sets to installdir
15# (using pax -rw ...) instead of creating tar files.
16# In this case, remove "etc", "xetc", and "extetc" from the list of default sets.
17#
18
19prog="${0##*/}"
20rundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
21. "${rundir}/sets.subr"
22
23# set defaults
24lists=
25tars="${RELEASEDIR}"
26dest="${DESTDIR}"
27metalog=
28installdir=
29etcdir=
30setlistdir=
31timestamp=
32setfilesonly=false
33quiet=false
34skipkeys=time,md5,sha1,sha384,sha512,rmd160,cksum
35preserve="-pe"
36
37usage()
38{
39	cat 1>&2 <<USAGE
40Usage: ${prog} [-L base,x,ext] [-b] [-x] [-y] [-i idir] [-a arch] [-m machine]
41	    [-s setsdir] [-S] [-M metalog] [-N etcdir] [-F setlistdir]
42	    [-d dest] [-t targetdir] [setname ...]
43	-L base,x,ext	Make specified lists
44	-b		Make both netbsd and x11 lists
45	-x		Only make x11 lists
46		[Default: make netbsd lists]
47	-y		Only make extsrc lists
48		[Default: make netbsd lists]
49	-i idir		Install sets to idir instead of creating tar files
50	-a arch		Set arch (e.g, m68k, mipseb, mipsel, powerpc) [${MACHINE_ARCH}]
51	-m machine	Set machine (e.g, amiga, i386, macppc) [${MACHINE}]
52	-q		Quiet operation
53	-s setsdir	Directory to find sets [${setsdir}]
54	-F setlistdir	output directory for generated set lists [${dest}/etc/mtree/]
55	-S		Exit after creating set files ${dest}/etc/mtree/set.*
56	-M metalog	metalog file
57	-N etcdir	etc dir for metalog use [${dest}/etc]
58	-U		do not preserve file permissions (with -i ..)
59	-d dest		\${DESTDIR}	[${dest}]
60	-t targetdir	\${RELEASEDIR}	[${tars}]
61	-T timestamp	Timestamp to set for all the files in the tar.
62	[setname ...]	Sets to build 	[${lists}]
63USAGE
64	exit 1
65}
66
67msg()
68{
69	$quiet || echo $*
70}
71
72# handle args
73while getopts L:bxyi:a:m:qs:F:SM:N:Ud:t:T: ch; do
74	case ${ch} in
75	L)
76		save_IFS="${IFS}"
77		IFS=,
78		for _list in ${OPTARG}; do
79			case $_list in
80			base)	lists="${lists} ${nlists}" ;;
81			x)	lists="${lists} ${xlists}" ;;
82			ext)	lists="${lists} ${extlists}" ;;
83			esac
84		done
85		IFS="${save_IFS}"
86		;;
87	# backward compat
88	b)
89		lists="${nlists} ${xlists}"
90		;;
91	x)
92		lists="${xlists}"
93		;;
94	y)
95		lists="${extlists}"
96		;;
97	i)
98		installdir="${OPTARG}"
99		;;
100	a)
101		MACHINE_ARCH="${OPTARG}"
102		MACHINE_CPU="$(arch_to_cpu "${OPTARG}")"
103		;;
104	m)
105		MACHINE="${OPTARG}"
106		;;
107	q)
108		quiet=true
109		;;
110	s)
111		setsdir="${OPTARG}"
112		;;
113	F)
114		setlistdir="${OPTARG}"
115		;;
116	S)
117		setfilesonly=true
118		;;
119	M)
120		metalog="${OPTARG}"
121		;;
122	N)
123		etcdir="${OPTARG}"
124		;;
125	U)
126		preserve=
127		;;
128	d)
129		dest="${OPTARG}"
130		;;
131	t)
132		tars="${OPTARG}"
133		;;
134	T)
135		timestamp="--timestamp $OPTARG"
136		;;
137	*)
138		usage
139		;;
140	esac
141done
142shift $((${OPTIND} - 1))
143if [ -n "${installdir}" ]; then	# if -i, remove etc + xetc + extetc from the default list
144	lists="$(echo ${lists} | ${SED} -e 's/ etc / /;s/ xetc / /;s/ extetc / /')"
145fi
146if [ -n "$*" ]; then
147	lists="$*"
148fi
149
150if [ -z "${tars}" -a -z "${installdir}" ]; then
151	echo >&2 "${prog}: \${RELEASEDIR} must be set, or -i must be used"
152	exit 1
153fi
154
155if [ -z "${dest}" ]; then
156	echo >&2 "${prog}: \${DESTDIR} must be set"
157	exit 1
158fi
159: ${etcdir:="${dest}/etc"}
160
161SDIR="$(${MKTEMP} -d "/tmp/${prog}.XXXXXX")"
162TMPFILES=
163
164: ${setlistdir:="${dest}/etc/mtree"}
165
166cleanup()
167{
168	es=$?
169	/bin/rm -rf "${SDIR}" ${TMPFILES}
170	trap - 0
171	exit ${es}
172}
173trap cleanup 0 2 3 13		# EXIT INT QUIT PIPE
174
175#
176# build the setfiles
177#
178
179for setname in ${lists}; do
180	${HOST_SH} "${setsdir}/makeflist" -a "${MACHINE_ARCH}" -m "${MACHINE}" \
181	    -s "${setsdir}" "${setname}" > "${SDIR}/flist.${setname}" \
182	    || exit 1
183	if [ ! -s "${SDIR}/flist.${setname}" ]; then
184		echo >&2 "makeflist output is empty for ${setname}"
185		exit 1
186	fi
187	${setfilesonly} && msg "Creating ${setlistdir}/set.${setname}"
188	if [ -n "${metalog}" ]; then
189		${AWK} -f "${rundir}/getdirs.awk" "${SDIR}/flist.${setname}" \
190		    > "${SDIR}/flist.${setname}.full" \
191		    || exit 1
192		(
193			echo "/set uname=root gname=wheel"
194			${AWK} -f "${rundir}/join.awk" \
195				"${SDIR}/flist.${setname}.full" "${metalog}"
196			echo "./etc/mtree/set.${setname} type=file mode=0444"
197		) | ${MTREE} -CS -k all -R "${skipkeys}" -N "${etcdir}" \
198		    > "${setlistdir}/set.${setname}" \
199		    || exit 1
200		# We deliberately do not add set.${setname} to ${metalog},
201		# because we depend on it as an input.
202	else
203		${MTREE} -c -p "${dest}" -k all -R "${skipkeys}" \
204		    -N "${etcdir}" -O "${SDIR}/flist.${setname}" \
205		    | ${MTREE} -C -k all -N "${etcdir}" \
206		    > "${setlistdir}/set.${setname}"
207	fi
208done
209if ${setfilesonly}; then		# exit after creating the set lists
210	exit 0
211fi
212
213runpax() {
214	local s="$1"
215	shift
216	(cd "${dest}" && 
217	    ${PAX} -dOw ${timestamp} -N"${etcdir}" -M "$@" < "${setlistdir}/set.${s}")
218}
219
220#
221# now build the tarfiles
222#
223
224GZIP=-9n		# for pax -z
225export GZIP
226es=0
227
228for setname in ${lists:-${nlists}}; do
229	out="${setname}.${TAR_SUFF:-tgz}"
230	if [ -n "${installdir}" ]; then
231		msg "Copying set ${setname}"
232		runpax "${setname}" -r ${preserve} "${installdir}"
233	else
234		if [ -n "${metalog}" -a "${tars}/${out}" -nt "${metalog}" ]
235		then
236			msg "${out} is up to date"
237			continue
238		fi
239		msg "Creating ${out}"
240		rm -f "${tars}/${out}"
241		TMPFILES="${TMPFILES} ${tars}/${out}.tmp"
242		runpax "${setname}" -z --use-compress-program \
243		    ${COMPRESS_PROGRAM} > "${tars}/${out}.tmp" &&
244		mv "${tars}/${out}.tmp" "${tars}/${out}"
245	fi
246	es=$((${es} + $?))
247done
248if [ ${es} -gt 255 ]; then
249	es=255
250fi
251exit ${es}
252