xref: /haiku/build/scripts/build_haiku_image (revision cbed190f71b8aff814bf95539c39a1bcfb953ed8)
1#!/bin/sh
2set -o errexit
3
4# The first argument is the shell script that initializes the variables:
5# sourceDir
6# outputDir
7# tmpDir
8# addBuildCompatibilityLibDir
9# The following are only for image types:
10# installDir
11# isImage
12# imagePath
13# imageSize
14# imageLabel
15# updateOnly
16# dontClearImage
17# isVMwareImage
18# optionalPackageDescriptions
19#
20# addattr
21# copyattr
22# rc
23# rmAttrs
24# unzip
25# The following are only for image types:
26# bfsShell
27# fsShellCommand
28# makebootable
29# resattr
30# vmdkimage
31# The following is only for cd types:
32# generate_attribute_stores
33# isCD
34#
35if [ $# -gt 0 ]; then
36	. $1
37	shift
38fi
39
40if [ ! $isCD ]; then
41	# If the haiku image path is a symlink resolve it now (makebootable needs the
42	# path of the actual device path under Linux).
43	normalizedImagePath=''
44	if readlink -f "$imagePath" > /dev/null 2>&1 ; then
45		normalizedImagePath=$(readlink -f "$imagePath")
46	elif realpath "$imagePath" > /dev/null 2>&1 ; then
47		normalizedImagePath=$(realpath "$imagePath")
48	elif greadlink -f "$imagePath" > /dev/null 2>&1 ; then
49		normalizedImagePath=$(greadlink -f "$imagePath")
50	fi
51	if [ -n "$normalizedImagePath" ]; then
52		imagePath="$normalizedImagePath"
53	fi
54fi
55
56# this adds the build library dir to LD_LIBRARY_PATH
57eval "$addBuildCompatibilityLibDir"
58
59# map the shell commands
60if [ $isCD ]; then
61	outputDir=$tmpDir/cdsource
62
63	sPrefix=
64	tPrefix="$outputDir/"
65	cd=cd
66	scd=:
67	cp="$copyattr -d"
68	copyAttrs="$copyattr"
69	ln=ln
70	mkdir=mkdir
71	rm=rm
72elif [ $isImage ]; then
73	# If FIFOs are used for the communication with the FS shell, prepare them.
74	if $fsShellCommand --uses-fifos; then
75		fifoBasePath=/tmp/build_haiku_image-$$-fifo
76		toFSShellFifo=${fifoBasePath}-to-shell
77		fromFSShellFifo=${fifoBasePath}-from-shell
78
79		rm -f $toFSShellFifo $fromFSShellFifo
80		mkfifo $toFSShellFifo $fromFSShellFifo
81
82		# Open the FIFOs such that they are ready for the fsShellCommand. This
83		# also makes sure that they remain open until this script exits. When we
84		# exit while the FS shell is still running and waiting for commands,
85		# closing of our file descriptors will break the FIFOs and the FS shell
86		# will exit, too.
87		# Note: A bit of trickery is needed since opening one end blocks until
88		# someone opens the other end.
89		sleep 3<$fromFSShellFifo 1 &
90		exec 6>$fromFSShellFifo 3<$fromFSShellFifo
91		sleep 5<$toFSShellFifo 1 &
92		exec 4>$toFSShellFifo 5<$toFSShellFifo
93
94		# Remove the FIFO files again -- we have the open FDs, so they can
95		# still be used and this makes sure they won't hang around any further.
96		rm -f $toFSShellFifo $fromFSShellFifo
97
98		# Remap the fsShellCommand and bfsShell such that they don't inherit the
99		# wrong FDs. For both fsShellCommand and bfsShell FD 3 is the input from
100		# the respectively other program, FD 4 is the output to it.
101		actualFSShellCommand="$fsShellCommand"
102		actualBFSShell="$bfsShell"
103
104		fsShellCommandWrapper()
105		{
106			$actualFSShellCommand 5>&- 6>&- "$@"
107		}
108
109		bfsShellWrapper()
110		{
111			$actualBFSShell 3>&5 4<&6 "$@"
112		}
113
114		fsShellCommand=fsShellCommandWrapper
115		bfsShell=bfsShellWrapper
116	fi
117
118	# set up the other commands
119	sPrefix=:
120	tPrefix=/myfs/
121	cd="$fsShellCommand cd"
122	scd="$fsShellCommand cd"
123	cp="$fsShellCommand cp -f"
124	copyAttrs="$fsShellCommand cp -a"
125	ln="$fsShellCommand ln"
126	mkdir="$fsShellCommand mkdir"
127	rm="$fsShellCommand rm"
128	mkindex="$fsShellCommand mkindex"
129else
130	sPrefix=
131	# TODO: This should come from the environment.
132	tPrefix="$installDir/"
133	cd=cd
134	scd=:
135	cp="$copyattr -d"
136	copyAttrs="$copyattr"
137	ln=ln
138	mkdir=mkdir
139	rm=rm
140	mkindex=mkindex
141fi
142
143
144extractFile()
145{
146	# extractFile <archive> <directory>
147	archiveFile=$1
148	targetExtractedDir=$2
149	extractedSubDir=$3
150
151	echo "Extracting $archiveFile ..."
152
153	extractDir=$tmpDir/extract
154	$rmAttrs -rf "$extractDir"
155	mkdir -p "$extractDir"
156
157	case "$archiveFile" in
158		*.zip)
159			$unzip -q -d "$extractDir" "$archiveFile"
160			;;
161		*.tgz|*.tar.gz)
162			tar -C "$extractDir" -xf "$archiveFile"
163			;;
164		*)
165			echo "Unhandled archive extension in build_haiku_image extractFile()"
166			exit 1
167			;;
168	esac
169
170	if [ -f $extractDir/.OptionalPackageDescription ]; then
171		cat $extractDir/.OptionalPackageDescription >> $copyrightsFile
172		echo >> $copyrightsFile
173		rm $extractDir/.OptionalPackageDescription
174	fi
175
176	$cp -r "${sPrefix}$extractDir/$extractedSubDir/." "${tPrefix}$targetExtractedDir"
177
178	$rmAttrs -rf "$extractDir"
179}
180
181
182mkdir -p $tmpDir
183copyrightsFile=$tmpDir/copyrights
184$rmAttrs -f $copyrightsFile
185if [ "$optionalPackageDescriptions" ]; then
186	cp "$optionalPackageDescriptions" $copyrightsFile
187fi
188
189if [ $isCD ]; then
190	# setup output dir
191	$rmAttrs -rf "$outputDir"
192	mkdir -p "$outputDir"
193fi
194
195# create the image and mount it
196if [ $isImage ]; then
197	echo
198
199	imageOffsetFlags=
200	if [ $isVMwareImage ]; then
201		imageOffsetFlags="--start-offset 65536"
202	fi
203
204	if [ ! $updateOnly ]; then
205		echo "Creating image ..."
206
207		imageFlags="-i${imageSize}M"
208		if [ ! "$dontClearImage" ]; then
209			imageFlags="$imageFlags -c"
210		fi
211
212		if [ $isVMwareImage ]; then
213			$vmdkimage -h 64k $imageFlags "$imagePath"
214		else
215			$createImage $imageFlags "$imagePath"
216		fi
217
218		$bfsShell --initialize $imageOffsetFlags "$imagePath" \
219			"$imageLabel" "block_size 2048"
220		$makebootable $imageOffsetFlags "$imagePath"
221	fi
222
223	$bfsShell -n $imageOffsetFlags "$imagePath" > /dev/null &
224	sleep 1
225
226	# Close FDs 5 and 6. Those represent the pipe ends that are used by the
227	# FS shell. Closing them in the shell process makes sure an unexpected death
228	# of the FS shell causes writing to/reading from the other ends to fail
229	# immediately.
230	exec 5>&- 6>&-
231
232	# bail out, if mounting fails
233	$cd .
234fi
235
236echo "Populating image ..."
237while [ $# -gt 0 ]; do
238	. $1
239	shift
240done
241
242
243# install MIME database
244# TODO: It should be possible to do that in the build system too.
245
246if [ ! $updateOnly ]; then
247	mimeDBSource=$sourceDir/src/data/beos_mime
248	mimeDBDest=${tPrefix}home/config/settings/beos_mime
249
250	echo "Deleting old MIME database ..."
251
252	$rm -rf $mimeDBDest
253	$mkdir -p $mimeDBDest
254	mimeTmpDir=$tmpDir/mime
255	mimeDBTmpDir=$tmpDir/mime/db
256	mimeTmpIndex=0
257	mimeTmpFile=$mimeTmpDir/mimedb$$.rsrc
258
259	# create tmp dir for the MIME conversion stuff
260	mkdir -p $mimeDBTmpDir
261
262	echo "Installing MIME database ..."
263
264	for inSuperFile in $mimeDBSource/*.super; do
265		superType=$(basename $inSuperFile .super)
266		tmpSuperDir=$mimeDBTmpDir/$superType
267
268		# compile rdef to rsrc file and the rsrc file to attributes
269		$rc -o $mimeTmpFile $inSuperFile
270		mkdir -p $tmpSuperDir
271		$resattr -O -o $tmpSuperDir $mimeTmpFile
272		$rmAttrs $mimeTmpFile
273
274		# iterate through the sub types
275		for inSubFile in $mimeDBSource/$superType/*; do
276			# check, if the type exists
277			if test -f $inSubFile && grep META:TYPE $inSubFile > /dev/null 2>&1 ; then
278				subType=$(basename $inSubFile)
279				tmpSubFile=$mimeDBTmpDir/$superType/$subType
280
281				# compile rdef to rsrc file and the rsrc file to attributes
282				$rc -o $mimeTmpFile $inSubFile
283				$resattr -O -o $tmpSubFile $mimeTmpFile
284				$rmAttrs $mimeTmpFile
285			fi
286		done
287	done
288
289	$cp -r ${sPrefix}$mimeDBTmpDir/. $mimeDBDest
290
291	# cleanup tmp dir
292	$rmAttrs -rf $mimeTmpDir
293fi	# ! updateOnly
294
295
296# add the concatenated copyrights as an attribute to AboutSystem
297
298if [ ! $updateOnly ]; then
299	if [ -f $copyrightsFile ]; then
300		copyrightAttrs=$tmpDir/copyrightAttrs
301		$rmAttrs -f $copyrightAttrs
302		touch $copyrightAttrs
303		$addattr -f $copyrightsFile COPYRIGHTS $copyrightAttrs
304		$copyAttrs ${sPrefix}$copyrightAttrs ${tPrefix}system/apps/AboutSystem
305	fi
306fi
307
308if [ $isCD ]; then
309	# generate the attribute stores
310	echo "Generating attribute stores ..."
311	$generate_attribute_stores "$tPrefix"
312
313	echo "Copying boot image ..."
314	$cp "$cdBootFloppy" "$outputDir"
315
316	# build the iso image
317	echo "Building CD image ..."
318	mkisofs -uid 0 -gid 0 -b `basename $cdBootFloppy` -R -V "$cdLabel" -o "$cdImagePath" "$tPrefix"
319
320	# cleanup output dir
321	$rmAttrs -rf "$outputDir"
322fi
323
324# unmount
325if [ $isImage ]; then
326	echo "Unmounting ..."
327	$fsShellCommand sync
328	$fsShellCommand quit
329fi
330