summaryrefslogtreecommitdiff
path: root/releasetools/image.functions
blob: d2b89815a88065bab240abb4c69d839c65a1d6e1 (plain)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537

#
# spec file handling
#

#
# Add a directory to a spec file
#
# $1 : directory to add
# $2 : spec file
add_dir_spec()
{
	echo "./$1 type=dir uid=0 gid=0 mode=0755" >> ${WORK_DIR}/$2
}

#
# Add a file to a spec file
#
# $1 : file to add
# $2 : spec file
add_file_spec()
{
	echo "./$1 type=file uid=0 gid=0 mode=0755 size=$(wc -c < ${ROOT_DIR}/${1})" >> ${WORK_DIR}/$2
}

#
# Add a symbolic link to a spec file
#
# $1 : symlink to add
# $2 : link to
# $3 : spec file
add_link_spec()
{
	echo "./$1 type=link uid=0 gid=0 mode=0755 link=$2" >> ${WORK_DIR}/$3
}

#
# workdir handling
#

#
# Create the workdir (a directory where Minix is built using sets)
# spec files are put in WORK_DIR, the file system created in ROOT_DIR
#
# $1 : sets to extract
build_workdir()
{
	mkdir -p ${ROOT_DIR}

	# Don't do anything if we don't use sets
	if [ -n "$1" ]
	then
		# Extract sets
		for set in $1; do
			if [ ! -e ${SETS_DIR}/${set}.tgz ]; then
				echo "Missing ${SETS_DIR}/${set}.tgz, aborting"
				echo "Are the release sets tarballs created?"
				exit 1
			fi
			echo " * Extracting $set..."
			(cd ${ROOT_DIR}; ${CROSS_TOOLS}/nbpax -rnz -f ${SETS_DIR}/${set}.tgz .)
		done

		# add rc (if any)
		if [ -f ${RC} ]; then
			cp ${RC} ${ROOT_DIR}/usr/etc/rc.local
		fi

		# Build login/password files
		${CROSS_TOOLS}/nbpwd_mkdb -V 0 -p -d ${ROOT_DIR} ${ROOT_DIR}/etc/master.passwd

		# Build specifications files
		cp ${ROOT_DIR}/etc/mtree/set* ${WORK_DIR}
		${ROOT_DIR}/usr/bin/MAKEDEV -s -m all >> ${WORK_DIR}/extra.dev
	fi

	if [ ${BUNDLE_SETS} -eq 1 ]
	then
		echo " * Bundling sets..."
		workdir_add_sets
	fi
}

#
# Add tarball sets to the workdir (for installation CD)
#
workdir_add_sets()
{
	# Add sets to the root
	mkdir -p ${ROOT_DIR}/usr/${ARCH}/binary/sets;
	add_dir_spec "usr/${ARCH}" extra.sets
	add_dir_spec "usr/${ARCH}/binary" extra.sets
	add_dir_spec "usr/${ARCH}/binary/sets" extra.sets

	add_link_spec "${ARCH}" "usr/${ARCH}" extra.sets

	DEST_SETS_DIR="usr/${ARCH}/binary/sets"
	for set in ${SETS_DIR}/*.tgz; do
		# Copy set itself
		cp ${set} ${ROOT_DIR}/${DEST_SETS_DIR}
		add_file_spec "${DEST_SETS_DIR}/$(basename ${set})" extra.sets

		# Add file count
		COUNT_SRC=$(echo $(basename ${set}) | sed -e "s/\(.*\)\.tgz/\set.\1/")
		COUNT_NAME=$(echo $(basename ${set}) | sed -e "s/\.tgz/\.count/")
		if [ -e "${DESTDIR}/etc/mtree/${COUNT_SRC}" ]
		then
			wc -l < ${DESTDIR}/etc/mtree/${COUNT_SRC} > ${ROOT_DIR}/${DEST_SETS_DIR}/${COUNT_NAME}
		else
			# Can't find mtree file, set bogus number
			echo 1 > ${ROOT_DIR}/${DEST_SETS_DIR}/${COUNT_NAME}
		fi
		add_file_spec "${DEST_SETS_DIR}/${COUNT_NAME}" extra.sets

		# Add file sizes
		SIZE_NAME=$(echo $(basename ${set}) | sed -e "s/\.tgz/\.size/")
		${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}
		add_file_spec "${DEST_SETS_DIR}/${SIZE_NAME}" extra.sets
	done

	# Add checksums
	cp ${SETS_DIR}/MD5 ${ROOT_DIR}/${DEST_SETS_DIR}
	add_file_spec "${DEST_SETS_DIR}/MD5" extra.sets
	cp ${SETS_DIR}/SHA512 ${ROOT_DIR}/${DEST_SETS_DIR}
	add_file_spec "${DEST_SETS_DIR}/SHA512" extra.sets
}

#
# Add HDD files to the workdir
#
workdir_add_hdd_files()
{
	# create a fstab entry in /etc
	cat >${ROOT_DIR}/etc/fstab <<END_FSTAB
/dev/c0d0p1	/usr		mfs	rw			0	2
/dev/c0d0p2	/home		mfs	rw			0	2
none		/sys		devman	rw,rslabel=devman	0	0
none		/dev/pts	ptyfs	rw,rslabel=ptyfs	0	0
END_FSTAB
	add_file_spec "etc/fstab" extra.fstab

	# Add boot monitor
	cp ${DESTDIR}/usr/mdec/boot_monitor ${ROOT_DIR}/boot_monitor
	add_file_spec "boot_monitor" extra.boot
}

#
# Add CD boot files to the workdir
#
workdir_add_cd_files()
{
	# create a fstab entry in /etc
	cat >${ROOT_DIR}/etc/fstab <<END_FSTAB
none		/sys		devman	rw,rslabel=devman	0	0
none		/dev/pts	ptyfs	rw,rslabel=ptyfs	0	0
END_FSTAB
	add_file_spec "etc/fstab" extra.fstab

	# Add boot monitor
	cp ${DESTDIR}/usr/mdec/boot_monitor ${ROOT_DIR}/minixboot
	add_file_spec "minixboot" extra.cdfiles

	# Add README
	cp releasetools/release/cd/README.TXT ${ROOT_DIR}/README.TXT
	add_file_spec "README.TXT" extra.cdfiles
}

#
# Add ramdisk files to the workdir
#
workdir_add_ramdisk_files()
{
	# create a fstab entry in /etc
	cat >${ROOT_DIR}/etc/fstab <<END_FSTAB
none		/sys		devman	rw,rslabel=devman	0	0
none		/dev/pts	ptyfs	rw,rslabel=ptyfs	0	0
END_FSTAB
	add_file_spec "etc/fstab" extra.fstab

	# add early boot rc script
	cp minix/drivers/storage/ramdisk/rc ${ROOT_DIR}/etc/rc.ramdisk
	add_file_spec "etc/rc.ramdisk" extra.fstab

	# Add README
	cp releasetools/release/cd/README.TXT ${ROOT_DIR}/README.TXT
	add_file_spec "README.TXT" extra.cdfiles
}

#
# Extract kernel to designated directory
#
# $1: Directory where to extract
workdir_add_kernel()
{
	(cd ${ROOT_DIR}; ${CROSS_TOOLS}/nbpax -rnz -f ${SETS_DIR}/minix-kernel.tgz .)

	# Move kernel files to the correct directory
	if [ ! -d ${ROOT_DIR}/boot/$1 ]
	then
		mkdir -p ${ROOT_DIR}/boot/$1
		add_dir_spec "boot/$1" extra.kernel
	fi

	mv ${ROOT_DIR}/boot/minix/.temp/* ${ROOT_DIR}/boot/$1
	rm -rf ${ROOT_DIR}/boot/minix/.temp
	for i in $(cd ${ROOT_DIR}/boot/$1 && echo *)
	do
		add_file_spec "boot/$1/$i" extra.kernel
	done
}

#
# Read METALOG and use mtree to convert the user and group names into uid and gids.
# Used as the reference mtree for building file systems.
#
create_input_spec()
{
	if [ -n "$SETS" ]
	then
		cat ${WORK_DIR}/set* ${WORK_DIR}/extra* | ${CROSS_TOOLS}/nbmtree -N ${ROOT_DIR}/etc -C -K device > ${WORK_DIR}/input
	else
		cat ${WORK_DIR}/extra* | ${CROSS_TOOLS}/nbmtree -C > ${WORK_DIR}/input
	fi

	if [ ${ASR_HACK} -eq 1 ]
	then
		# Hacky workaround for ASR-randomized service binaries since they don't get nicely packaged in a tarball
		# add any generated ASR-randomized service binaries
		# TODO: apply stricter file permissions for both these and the base /service binaries, against local attacks
		(cd ${DESTDIR} && find ./usr/service/asr -type f | sed 's/$/ type=file uid=0 gid=0 mode=0755/') >> ${WORK_DIR}/input
		cp -r ${DESTDIR}/usr/service/asr ${ROOT_DIR}/usr/service
	fi
}

#
# Split mtree into partitions and create proto files for nbmkfs.mfs
#
# $1 : partitions to create (example: usr home)
create_protos()
{
	# build filter
	FILTER_COMMAND="cat ${WORK_DIR}/input"
	for i in $1
	do
		FILTER_COMMAND="$FILTER_COMMAND | grep -v \"^./$i/\" "
	done

	# fill root.img (skipping entries inside partitions while keeping partition mount points)
	eval $FILTER_COMMAND | ${CROSS_TOOLS}/nbtoproto -b ${ROOT_DIR} -o ${WORK_DIR}/proto.root

	# create proto files for partitions using toproto
	for i in $1
	do
		cat ${WORK_DIR}/input | grep  "^\./$i/\|^. " | sed "s,\./$i,\.,g" | ${CROSS_TOOLS}/nbtoproto -b ${ROOT_DIR}/$i -o ${WORK_DIR}/proto.$i
	done
}

#
# Clone grub repository and build efi boot binary
#
fetch_and_build_grub()
{
       if [ -d ${RELEASETOOLSDIR}/grub ]
       then
         echo grub is already checked out
       else
         git clone git://git.savannah.gnu.org/grub.git ${RELEASETOOLSDIR}/grub
         pushd ${RELEASETOOLSDIR}/grub
         # most recent known working commit at the time of writing
         git checkout a0bf403f66dbaca4edd8e667bfc397dd91c8d71c
         ./autogen.sh
         ./configure --with-platform=efi --target=i386
         make clean
         make -j ${JOBS}
         cd grub-core
         ../grub-mkimage -v -d . -o booti386.efi -O i386-efi -p /boot/efi normal part_msdos fat chain boot configfile multiboot minix3 gzio efi_uga
         ls -l booti386.efi
         popd
       fi
}

#
# Create grub.cfg for efi boot
#
create_grub_cfg()
{
	cat > ${EFI_DIR}/boot/efi/grub.cfg <<END_GRUBCFG

insmod serial
insmod minix3
insmod gzio
#insmod efi_uga
#insmod video_fb
insmod all_video

set timeout=30
set default=0

set gfxmode=text

menuentry "Minix Boot" {
        set root=(hd0,1)
        multiboot /boot/minix_default/kernel rootdevname=c0d0p0
        module /boot/minix_default/mod01_ds
        module /boot/minix_default/mod02_rs
        module /boot/minix_default/mod03_pm
        module /boot/minix_default/mod04_sched
        module /boot/minix_default/mod05_vfs
        module /boot/minix_default/mod06_memory
        module /boot/minix_default/mod07_tty
        module /boot/minix_default/mod08_mib
        module /boot/minix_default/mod09_vm
        module /boot/minix_default/mod10_pfs
        module /boot/minix_default/mod11_mfs
        module /boot/minix_default/mod12_init
}

menuentry "Minix Boot (serial)" {
        set root=(hd0,1)
        multiboot /boot/minix_default/kernel rootdevname=c0d0p0 cttyline=0 ttybaud=115200 console=tty00 consdev=com0
        module /boot/minix_default/mod01_ds
        module /boot/minix_default/mod02_rs
        module /boot/minix_default/mod03_pm
        module /boot/minix_default/mod04_sched
        module /boot/minix_default/mod05_vfs
        module /boot/minix_default/mod06_memory
        module /boot/minix_default/mod07_tty
        module /boot/minix_default/mod08_mib
        module /boot/minix_default/mod09_vm
        module /boot/minix_default/mod10_pfs
        module /boot/minix_default/mod11_mfs
        module /boot/minix_default/mod12_init
}
END_GRUBCFG
}

#
# Create ramdisk image from root directory
#
# $1 : size of ramdisk (optional)
create_ramdisk_image()
{
	PATH=$(cd ${CROSS_TOOLS}; pwd):$PATH
	if [ -z $1 ]
	then
		RAMSIZE="-x 5"
	else
		RAMSIZE="-b $(( $1 / 512 / 8))"
	fi

	# Build image
	_RAMDISKSIZE=$(${CROSS_TOOLS}/nbmkfs.mfs -d ${RAMSIZE} -I 0 ${WORK_DIR}/imgrd.mfs ${WORK_DIR}/proto.root)
	(cd ${WORK_DIR}; ${TOOLCHAIN_TRIPLET}objcopy -Ibinary -Bi386 -Oi586-elf32-minix imgrd.mfs imgrd.o)
	${TOOLCHAIN_TRIPLET}clang --sysroot=${DESTDIR} -L ${DESTDIR}/usr/lib -static -o ${WORK_DIR}/mod06_memory ${OBJ}/minix/drivers/storage/memory/memory.o ${WORK_DIR}/imgrd.o -nodefaultlibs -lblockdriver -lchardriver -lsys -lminc
}

#
# Bundle packages (won't preinstall them)
#
# $1 : packages to bundle
bundle_packages()
{
	if [ -z $PACKAGE_DIR ]
	then
		echo " * PACKAGE_DIR not set, skipping package bundling..."
		return
	fi

	if [ ! -x "$(which $PKG_INFO)" ]
	then
		echo "Error: PKG_INFO ("$(which $PKG_INFO)") not executable."
		echo "Can't create package index without pkg_info."
		echo "Bootstrap pkgsrc on host to get pkg_info."
		exit 1
	fi

	# Bundle all available packages if no list was given
	if [ -z "$1" ]
	then
		PACKAGES=$(for i in $(echo "$PACKAGE_DIR"/*.tgz); do basename $i | sed s/\.tgz//; done)
	else
		PACKAGES=$1
	fi

	DESTPACKAGES="usr/packages/$RELEASE_VERSION/$ARCH/All"
	RELEASEPACKAGE="${ROOT_DIR}/$DESTPACKAGES"
	index=pkg_summary

	# create directories
	mkdir -p $RELEASEPACKAGE
	add_dir_spec "usr/packages" extra.pkgsrc
	add_dir_spec "usr/packages/$RELEASE_VERSION" extra.pkgsrc
	add_dir_spec "usr/packages/$RELEASE_VERSION/$ARCH" extra.pkgsrc
	add_dir_spec "usr/packages/$RELEASE_VERSION/$ARCH/All" extra.pkgsrc
	add_link_spec "packages" "usr/packages" extra.pkgsrc
	for pkgprefix in $PACKAGES
	do
		realfn=$(echo $PACKAGE_DIR/${pkgprefix}*.tgz | cut -d' ' -f1)
		if [ -f "$realfn" ]
		then
			# Copy package
			p="$(basename $realfn)"
			echo " * Bundling $p..."
			cp "$realfn" "$RELEASEPACKAGE/$p"
			add_file_spec "$DESTPACKAGES/$p" extra.pkgsrc
		else
			echo "Error: Can't find $pkgprefix in directory $PACKAGE_DIR for bundling package."
			exit 1
		fi
	done

	# Create packages index
	echo " * Generating package index..."
	indexname=$indexpath/$p.$index
	$PKG_INFO -X $RELEASEPACKAGE/*.tgz >> $RELEASEPACKAGE/$index

	# Compress index
	echo " * Compressing index..."
	bzip2 -f $RELEASEPACKAGE/$index
	add_file_spec "$DESTPACKAGES/$index.bz2" extra.pkgsrc
}

#
# stuff executed automatically to set up environment
#

usage()
{
	echo "Usage: $0 [options]"
	echo "  -X xsrc       Build with X11 located in \"xsrc\""
	echo "  -x            Add X11 sets to extraction list"
	echo "  -b            Add ASR service binaries to the image"
	echo "                (said binaries must be built beforehand)"
	echo ""
	echo "Environment variables:"
	echo "  CREATE_IMAGE_ONLY     If set to 1, skip invocation of build.sh (default: 0)"
	echo "  JOBS                  Number of CPUs to use for build.sh to use (default: 1)"
	echo "  SETS                  Sets to extract for image (default: depends on script)"
	echo "  BUILDVARS             Extra options passed to build.sh (default: none)"
	echo ""
	echo "  PACKAGE_DIR           Path to packages to bundle (default: none)"
	echo "  BUNDLE_PACKAGES       List of packages to bundle (default: none)"
	echo "  PKG_INFO              Path to 'pkg_info' for bundling (default: pkg_info)"
	echo ""
	echo "  BUNDLE_SETS           If set to 1, bundle sets for setup (default: only for CD)"
}

# parse options
while getopts "ixX:bh" c
do
	case "$c" in
		i)	echo "This method of generating the ISO installation media is obsolete."
			echo "Run ./releasetools/x86_cdimage.sh instead."
			exit 1;;

		x)	SETS="$SETS xbase xcomp xetc xfont xserver";;

		X)	MKX11=yes
			export MKX11
			BUILDVARS="$BUILDVARS -X $OPTARG";;

		b)      # bitcode build: increase partition sizes
			ROOT_SIZE="$((${ROOT_SIZE} + 192*(2**20)))"
			USR_SIZE="$((${USR_SIZE} + 256*(2**20)))"
			ASR_HACK=1;;

		h)	usage
			exit 0;;

		:)	usage
			exit 2;;

		\?)
			usage
			exit 2;;
	esac
done

#
# Are we going to build the minix sources?
#

if [ ${CREATE_IMAGE_ONLY} -eq 1 ]
then
	if [ ! -d ${DESTDIR} ]
	then
		echo "Minix source code doesn't appear to have been built."
		echo "Please try with \$CREATE_IMAGE_ONLY set to 0."
		exit 1
	fi
	if [ ! -d ${RELEASEDIR} ]
	then
		echo "Minix release tarball sets don't appear to have been created."
		echo "Please try with \$CREATE_IMAGE_ONLY set to 0."
		exit 1
	fi
	# FIXME: this won't change anything for tarballs
	#${CROSS_TOOLS}/nbmake-i386 -C releasetools do-hdboot
else
	echo "Going to build Minix source code..."
	#
	# Remove the generated files to allow us call build.sh without '-V SLOPPY_FLIST=yes'.
	#
	rm -f ${FSTAB}

	#
	# Now start the build.
	#
	sh ${BUILDSH} -j ${JOBS} -m ${ARCH} -O ${OBJ} -D ${DESTDIR} ${BUILDVARS} -U -u release

fi

# sanity check
if [ -d "${WORK_DIR}/.git" ]
then
	echo "WORK_DIR directory has a Git repository in it, abort!"
	exit 1
fi

# clean working directory
if [ -e "${WORK_DIR}" ]
then
	rm -rf "${WORK_DIR}"
fi
mkdir -p ${WORK_DIR}

# get absolute paths to those directories
CROSS_TOOLS=$(cd ${CROSS_TOOLS} && pwd)
DESTDIR=$(cd ${DESTDIR} && pwd)
MODDIR=$(cd ${MODDIR} && pwd)
OBJ=$(cd ${OBJ} && pwd)
SETS_DIR=$(cd ${SETS_DIR} && pwd)
WORK_DIR=$(cd ${WORK_DIR} && pwd)
ROOT_DIR=${WORK_DIR}/fs

# get list of mods
mods="`( cd ${MODDIR}; echo mod* | tr ' ' ',' )`"