xref: /minix/releasetools/image.functions (revision 0a6a1f1d)
1
2#
3# spec file handling
4#
5
6#
7# Add a directory to a spec file
8#
9# $1 : directory to add
10# $2 : spec file
11add_dir_spec() {
12	echo "./$1 type=dir uid=0 gid=0 mode=0755" >> ${WORK_DIR}/$2
13}
14
15#
16# Add a file to a spec file
17#
18# $1 : file to add
19# $2 : spec file
20add_file_spec() {
21	echo "./$1 type=file uid=0 gid=0 mode=0755 size=$(wc -c < ${ROOT_DIR}/${1})" >> ${WORK_DIR}/$2
22}
23
24#
25# Add a symbolic link to a spec file
26#
27# $1 : symlink to add
28# $2 : link to
29# $3 : spec file
30add_link_spec() {
31	echo "./$1 type=link uid=0 gid=0 mode=0755 link=$2" >> ${WORK_DIR}/$3
32}
33
34#
35# workdir handling
36#
37
38#
39# Create the workdir (a directory where Minix is built using sets)
40# spec files are put in WORK_DIR, the file system created in ROOT_DIR
41#
42# $1 : sets to extract
43build_workdir() {
44	# Extract sets
45	mkdir -p ${ROOT_DIR}
46	for set in $1; do
47		if [ ! -e ${SETS_DIR}/${set}.tgz ]; then
48			echo "Missing ${SETS_DIR}/${set}.tgz, aborting"
49			echo "Are the release sets tarballs created?"
50			exit 1
51		fi
52		echo " * Extracting $set..."
53		(cd ${ROOT_DIR}; ${CROSS_TOOLS}/nbpax -rnz -f ${SETS_DIR}/${set}.tgz .)
54	done
55
56	# add rc (if any)
57	if [ -f ${RC} ]; then
58		cp ${RC} ${ROOT_DIR}/usr/etc/rc.local
59	fi
60
61	# Build login/password files
62	${CROSS_TOOLS}/nbpwd_mkdb -V 0 -p -d ${ROOT_DIR} ${ROOT_DIR}/etc/master.passwd
63
64	# Build specifications files
65	cp ${ROOT_DIR}/etc/mtree/set* ${WORK_DIR}
66	${ROOT_DIR}/usr/bin/MAKEDEV -s -m all >> ${WORK_DIR}/extra.dev
67}
68
69#
70# Add tarball sets to the workdir (for installation CD)
71#
72workdir_add_sets() {
73	# Add sets to the root
74	mkdir -p ${ROOT_DIR}/${ARCH}/binary/sets;
75	add_dir_spec "${ARCH}" extra.sets
76	add_dir_spec "${ARCH}/binary" extra.sets
77	add_dir_spec "${ARCH}/binary/sets" extra.sets
78
79	DEST_SETS_DIR="${ARCH}/binary/sets"
80	for set in ${SETS_DIR}/*.tgz; do
81		# Copy set itself
82		cp ${set} ${ROOT_DIR}/${DEST_SETS_DIR}
83		add_file_spec "${DEST_SETS_DIR}/$(basename ${set})" extra.sets
84
85		# Add file count
86		COUNT_SRC=$(echo $(basename ${set}) | sed -e "s/\(.*\)\.tgz/\set.\1/")
87		COUNT_NAME=$(echo $(basename ${set}) | sed -e "s/\.tgz/\.count/")
88		if [ -e "${DESTDIR}/etc/mtree/${COUNT_SRC}" ]
89		then
90			wc -l < ${DESTDIR}/etc/mtree/${COUNT_SRC} > ${ROOT_DIR}/${DEST_SETS_DIR}/${COUNT_NAME}
91		else
92			# Can't find mtree file, set bogus number
93			echo 1 > ${ROOT_DIR}/${DEST_SETS_DIR}/${COUNT_NAME}
94		fi
95		add_file_spec "${DEST_SETS_DIR}/${COUNT_NAME}" extra.sets
96
97		# Add file sizes
98		SIZE_NAME=$(echo $(basename ${set}) | sed -e "s/\.tgz/\.size/")
99		${CROSS_TOOLS}/nbpax -zvf ${set} . |grep -v 'bytes written in 1 secs [(]' | ${CROSS_TOOLS}/nbawk '{s+=$5} END{print s}' > ${ROOT_DIR}/${DEST_SETS_DIR}/${SIZE_NAME}
100		add_file_spec "${DEST_SETS_DIR}/${SIZE_NAME}" extra.sets
101	done
102
103	# Add checksums
104	cp ${SETS_DIR}/MD5 ${ROOT_DIR}/${DEST_SETS_DIR}
105	add_file_spec "${DEST_SETS_DIR}/MD5" extra.sets
106	cp ${SETS_DIR}/SHA512 ${ROOT_DIR}/${DEST_SETS_DIR}
107	add_file_spec "${DEST_SETS_DIR}/SHA512" extra.sets
108}
109
110#
111# Add CD boot files to the workdir
112#
113workdir_add_cdfiles() {
114	# Add boot monitor
115	cp ${DESTDIR}/usr/mdec/boot_monitor ${ROOT_DIR}/minixboot
116	add_file_spec "minixboot" extra.cdfiles
117
118	# Add README
119	cp releasetools/release/cd/README.TXT ${ROOT_DIR}/README.TXT
120	add_file_spec "README.TXT" extra.cdfiles
121}
122
123#
124# Extract kernel to designated directory
125#
126# $1: Directory where to extract
127workdir_add_kernel()
128{
129	(cd ${ROOT_DIR}; ${CROSS_TOOLS}/nbpax -rnz -f ${SETS_DIR}/minix-kernel.tgz .)
130
131	# Move kernel files to the correct directory
132	if [ ! -d ${ROOT_DIR}/boot/$1 ]
133	then
134		mkdir -p ${ROOT_DIR}/boot/$1
135		add_dir_spec "boot/$1" extra.kernel
136	fi
137
138	mv ${ROOT_DIR}/boot/minix/.temp/* ${ROOT_DIR}/boot/$1
139	rm -rf ${ROOT_DIR}/boot/minix/.temp
140	for i in $(cd ${ROOT_DIR}/boot/$1 && echo *)
141	do
142		add_file_spec "boot/$1/$i" extra.kernel
143	done
144}
145
146#
147# Read METALOG and use mtree to convert the user and group names into uid and gids.
148# Used as the reference mtree for building file systems.
149#
150create_input_spec()
151{
152	cat ${WORK_DIR}/set* ${WORK_DIR}/extra* | ${CROSS_TOOLS}/nbmtree -N ${ROOT_DIR}/etc -C -K device > ${WORK_DIR}/input
153
154	if [ ${ASR_HACK} -eq 1 ]
155	then
156		# Hacky workaround for ASR-randomized service binaries since they don't get nicely packaged in a tarball
157		# add any generated ASR-randomized service binaries (but not their root directory, which is already there)
158		# TODO: apply stricter file permissions for both these and the base /service binaries, against local attacks
159		(cd ${DESTDIR} && find ./usr/service/asr -type d | sed '1d;s/$/ type=dir uid=0 gid=0 mode=0755/') >> ${WORK_DIR}/input
160		(cd ${DESTDIR} && find ./usr/service/asr -type f | sed 's/$/ type=file uid=0 gid=0 mode=0755/') >> ${WORK_DIR}/input
161		cp -r ${DESTDIR}/usr/service/asr ${ROOT_DIR}/usr/service
162	fi
163}
164
165#
166# Split mtree into partitions and create proto files for nbmkfs.mfs
167#
168# $1 : partitions to create (example: usr home)
169create_protos()
170{
171	# build filter
172	FILTER_COMMAND="cat ${WORK_DIR}/input"
173	for i in $1
174	do
175		FILTER_COMMAND="$FILTER_COMMAND | grep -v \"^./$i/\" "
176	done
177
178	# fill root.img (skipping entries inside partitions while keeping partition mount points)
179	eval $FILTER_COMMAND | ${CROSS_TOOLS}/nbtoproto -b ${ROOT_DIR} -o ${WORK_DIR}/proto.root
180
181	# create proto files for partitions using toproto
182	for i in $1
183	do
184		cat ${WORK_DIR}/input | grep  "^\./$i/\|^. " | sed "s,\./$i,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${ROOT_DIR}/$i -o ${WORK_DIR}/proto.$i
185	done
186}
187
188#
189# Bundle packages (won't preinstall them)
190#
191# $1 : packages to bundle
192bundle_packages()
193{
194	if [ -z "$1" ]
195	then
196		return
197	fi
198
199	if [ -z $PACKAGE_DIR ]
200	then
201		echo "Error: PACKAGE_DIR is not set while trying to bundle packages."
202		echo "Please fetch binary packages to bundle and set PACKAGE_DIR to continue."
203		exit 1
204	fi
205
206	DESTPACKAGES="usr/packages/$RELEASE_VERSION/$ARCH/All"
207	RELEASEPACKAGE="${ROOT_DIR}/$DESTPACKAGES"
208	index=pkg_summary
209
210	# create directories
211	mkdir -p $RELEASEPACKAGE
212	add_dir_spec "usr/packages" extra.pkgsrc
213	add_dir_spec "usr/packages/$RELEASE_VERSION" extra.pkgsrc
214	add_dir_spec "usr/packages/$RELEASE_VERSION/$ARCH" extra.pkgsrc
215	add_dir_spec "usr/packages/$RELEASE_VERSION/$ARCH/All" extra.pkgsrc
216	add_link_spec "packages" "usr/packages" extra.pkgsrc
217	for pkgprefix in $1
218	do
219		realfn=$(echo $PACKAGE_DIR/${pkgprefix}*.tgz | cut -d' ' -f1)
220		if [ -f "$realfn" ]
221		then
222			# Copy package
223			p="$(basename $realfn)"
224			echo " * Bundling $p..."
225			cp "$realfn" "$RELEASEPACKAGE/$p"
226			add_file_spec "$DESTPACKAGES/$p" extra.pkgsrc
227		else
228			echo "Error: Can't find $pkgprefix in directory $PACKAGE_DIR for bundling package."
229			exit 1
230		fi
231	done
232
233	if [ -x "$(which $PKG_INFO)" ]
234	then
235		# Create packages index
236		echo " * Generating package index..."
237		indexname=$indexpath/$p.$index
238		$PKG_INFO -X $RELEASEPACKAGE/*.tgz >> $RELEASEPACKAGE/$index
239
240		# Compress index
241		echo " * Compressing index..."
242		bzip2 -f $RELEASEPACKAGE/$index
243		add_file_spec "$DESTPACKAGES/$index.bz2" extra.pkgsrc
244	else
245		echo " * Skipping package index generation."
246		echo "   PKG_INFO ("$(which $PKG_INFO)") not executable."
247	fi
248}
249
250#
251# stuff executed automatically to set up environment
252#
253
254usage() {
255	echo "Usage: $0 [options]"
256	echo "  -X xsrc       Build with X11 located in \"xsrc\" and extract its sets for image"
257	echo "                (do not automatically extract for installation CD)"
258	echo "  -b            Add ASR service binaries to the image"
259	echo "                (said binaries must be built beforehand)"
260	echo ""
261	echo "Environment variables:"
262	echo "  CREATE_IMAGE_ONLY     If set to 1, skip invocation of build.sh (default: 0)"
263	echo "  JOBS                  Number of CPUs to use for build.sh to use (default: 1)"
264	echo "  SETS                  Sets to extract for image (default: depends on script)"
265	echo "  BUILDVARS             Extra options passed to build.sh (default: none)"
266	echo ""
267	echo "  PACKAGE_DIR           Path to packages to bundle (default: none)"
268	echo "  BUNDLE_PACKAGES       List of packages to bundle (default: none)"
269	echo "  PKG_INFO              Path to 'pkg_info' for bundling (default: pkg_info)"
270}
271
272# parse options
273while getopts "iX:bh" c
274do
275	case "$c" in
276		i)	echo "This method of generating the ISO installation media is obsolete."
277			echo "Run ./releasetools/x86_cdimage.sh instead."
278			exit 1;;
279
280		X)	# we don't want to extract X sets by default for the installation CD
281			if ! echo "$0" | grep -q cdimage
282			then
283				SETS="$SETS xbase xcomp xetc xfont xserver"
284			fi
285			MKX11=yes
286			export MKX11
287			BUILDVARS="$BUILDVARS -X $OPTARG";;
288
289		b)      # bitcode build: increase partition sizes
290			ROOT_SIZE="$((${ROOT_SIZE} + 192*(2**20) / 512))"
291			USR_SIZE="$((${USR_SIZE} + 256*(2**20) / 512))"
292			ASR_HACK=1;;
293
294		h)	usage
295			exit 0;;
296
297		:)	usage
298			exit 2;;
299
300		\?)
301			usage
302			exit 2;;
303	esac
304done
305
306#
307# Are we going to build the minix sources?
308#
309
310if [ ${CREATE_IMAGE_ONLY} -eq 1 ]
311then
312	if [ ! -d ${DESTDIR} ]
313	then
314		echo "Minix source code doesn't appear to have been built."
315		echo "Please try with \$CREATE_IMAGE_ONLY set to 0."
316		exit 1
317	fi
318	if [ ! -d ${RELEASEDIR} ]
319	then
320		echo "Minix release tarball sets don't appear to have been created."
321		echo "Please try with \$CREATE_IMAGE_ONLY set to 0."
322		exit 1
323	fi
324	# FIXME: this won't change anything for tarballs
325	#${CROSS_TOOLS}/nbmake-i386 -C releasetools do-hdboot
326else
327	echo "Going to build Minix source code..."
328	#
329	# Remove the generated files to allow us call build.sh without '-V SLOPPY_FLIST=yes'.
330	#
331	rm -f ${FSTAB}
332
333	#
334	# Now start the build.
335	#
336	sh ${BUILDSH} -j ${JOBS} -m ${ARCH} -O ${OBJ} -D ${DESTDIR} ${BUILDVARS} -U -u release
337
338fi
339
340# sanity check
341if [ -d "${WORK_DIR}/.git" ]
342then
343	echo "WORK_DIR directory has a Git repository in it, abort!"
344	exit 1
345fi
346
347# clean working directory
348if [ -e "${WORK_DIR}" ]
349then
350	rm -rf "${WORK_DIR}"
351fi
352mkdir -p ${WORK_DIR}
353
354# get absolute paths to those directories
355CROSS_TOOLS=$(cd ${CROSS_TOOLS} && pwd)
356DESTDIR=$(cd ${DESTDIR} && pwd)
357OBJ=$(cd ${OBJ} && pwd)
358SETS_DIR=$(cd ${SETS_DIR} && pwd)
359WORK_DIR=$(cd ${WORK_DIR} && pwd)
360ROOT_DIR=${WORK_DIR}/fs
361