xref: /minix3/releasetools/mkboot (revision a5f47c23d5b23ae7b216ed3fb4442881c82c7cb8)
17f99f3a2SThomas Veerman#!/bin/sh
27f99f3a2SThomas Veerman#
3445f634aSpikpik#	mkboot 2.0 - make root device bootable, etc.
47f99f3a2SThomas Veerman#							Author: Kees J. Bot
57f99f3a2SThomas Veerman
67f99f3a2SThomas Veermanusage() {
7b0814fecSLionel Sambuc	echo "Usage: $0 [bootable | hdboot] [DESTDIR]" >&2
87f99f3a2SThomas Veerman	exit 1
97f99f3a2SThomas Veerman}
107f99f3a2SThomas Veerman
117f99f3a2SThomas Veermanrotate_oldest() {
127f99f3a2SThomas Veerman	base_dir="$1"
137f99f3a2SThomas Veerman	set -- `ls -t "$base_dir"`
147f99f3a2SThomas Veerman
157f99f3a2SThomas Veerman	case $# in
167f99f3a2SThomas Veerman	0|1|2|3)
177f99f3a2SThomas Veerman		# Not much there, do not remove a thing.
187f99f3a2SThomas Veerman		;;
197f99f3a2SThomas Veerman	*)
207f99f3a2SThomas Veerman		# Remove the third-newest image in /boot/$hdboot_t, but
217f99f3a2SThomas Veerman		# only if there's an older one (which is kept).
227f99f3a2SThomas Veerman		echo "rm $root:$base_dir/$3"
237f99f3a2SThomas Veerman		rm -rf "$base_dir/$3"
247f99f3a2SThomas Veerman	esac
257f99f3a2SThomas Veerman}
267f99f3a2SThomas Veerman
277f99f3a2SThomas Veerman
287f99f3a2SThomas Veermanmdec=/usr/mdec	# bootstraps
29b0814fecSLionel Sambuc# If no DESTDIR specified, then act on / or on the current chroot
30b0814fecSLionel SambucDESTDIR=
317f99f3a2SThomas Veerman# Check arguments.
327f99f3a2SThomas Veermancase "$#:$1" in
33445f634aSpikpik1:bootable | 1:hdboot )
34b0814fecSLionel Sambuc	# LSC Broken, if $# == 1, then $2,$3 are not set...
35b0814fecSLionel Sambuc	action=$1
36b0814fecSLionel Sambuc	;;
37b0814fecSLionel Sambuc2:bootable | 2:hdboot )
38b0814fecSLionel Sambuc	action=$1 DESTDIR=$2
397f99f3a2SThomas Veerman	;;
407f99f3a2SThomas Veerman*)	usage
417f99f3a2SThomas Veermanesac
427f99f3a2SThomas Veerman
437f99f3a2SThomas Veerman# Get the device table.
447f99f3a2SThomas VeermanFSTAB=/etc/fstab
457f99f3a2SThomas Veermantouch $FSTAB
46*a5f47c23SLionel Sambucroot="`awk <$FSTAB '{ if($2=="/") { print $1 } }'`"
477f99f3a2SThomas Veerman
487f99f3a2SThomas Veerman# The real root device may be the RAM disk.
497f99f3a2SThomas Veermanrealroot=`printroot -r`
507f99f3a2SThomas Veerman
517f99f3a2SThomas Veerman# If it's an initial fstab, pretend root is real root
527f99f3a2SThomas Veermanif [ "$root" = "/dev/ROOT" -o -z "$root" ]
537f99f3a2SThomas Veermanthen	root=$realroot
547f99f3a2SThomas Veermanfi
557f99f3a2SThomas Veerman
567f99f3a2SThomas Veermancase $action in
577f99f3a2SThomas Veermanbootable | hdboot)
587f99f3a2SThomas Veerman	# We need the root device.
597f99f3a2SThomas Veerman
607f99f3a2SThomas Veerman	if [ $realroot = $root ]
617f99f3a2SThomas Veerman	then
627f99f3a2SThomas Veerman		rootdir=
637f99f3a2SThomas Veerman	else
647f99f3a2SThomas Veerman		umount $root 2>/dev/null
657f99f3a2SThomas Veerman		mount $root /mnt || exit
667f99f3a2SThomas Veerman		rootdir=/mnt
677f99f3a2SThomas Veerman	fi
687f99f3a2SThomas Veermanesac
697f99f3a2SThomas Veerman
707f99f3a2SThomas Veermancase $action in
717f99f3a2SThomas Veermanhdboot)
7240b67629SAntoine Leca	version=`sh ../sys/conf/osrelease.sh`
737f99f3a2SThomas Veerman
747f99f3a2SThomas Veerman	# Retrieve the git revision; this only succeeds
757f99f3a2SThomas Veerman	# if git is available, it's a git checkout, *and*
767f99f3a2SThomas Veerman	# there are no uncommitted changes.
777f99f3a2SThomas Veerman	if git diff --quiet 2>/dev/null
787f99f3a2SThomas Veerman	then	gitrev="-`git describe --always`"
797f99f3a2SThomas Veerman	fi
807f99f3a2SThomas Veerman
817f99f3a2SThomas Veerman	revision=`cat revision 2>/dev/null`
827f99f3a2SThomas Veerman
837f99f3a2SThomas Veerman	oldrev=$revision
847f99f3a2SThomas Veerman
857f99f3a2SThomas Veerman	if [ -z "$revision" ]
867f99f3a2SThomas Veerman	then
877f99f3a2SThomas Veerman		revision=0
887f99f3a2SThomas Veerman		rrevision=""
8940b67629SAntoine Leca		gitrev=""
907f99f3a2SThomas Veerman	else
917f99f3a2SThomas Veerman		revision=`expr $revision + 1`
927f99f3a2SThomas Veerman		rrevision=r$revision
937f99f3a2SThomas Veerman	fi
947f99f3a2SThomas Veerman
957f99f3a2SThomas Veerman	target="${version}${rrevision}${gitrev}"
967f99f3a2SThomas Veerman
97b0814fecSLionel Sambuc	rotate_oldest "$DESTDIR/boot/minix"
987f99f3a2SThomas Veerman
997f99f3a2SThomas Veerman	# rotate system processes. We assume latest ones are in
1007f99f3a2SThomas Veerman	# /boot/modules/.temp and we maintain /boot/modules/ by ourselves.
101b0814fecSLionel Sambuc	[ -d $DESTDIR/boot/minix/.temp ] || exit 1
102b0814fecSLionel Sambuc	rm -rf $DESTDIR/boot/minix/"$target"/
103b0814fecSLionel Sambuc	mv $DESTDIR/boot/minix/.temp $DESTDIR/boot/minix/"$target"
104b0814fecSLionel Sambuc	rm -f $DESTDIR/boot/minix_latest
105b0814fecSLionel Sambuc	ln -s minix/"$target" $DESTDIR/boot/minix_latest
1067f99f3a2SThomas Veerman
1077f99f3a2SThomas Veerman	# Save the revision number.
1087f99f3a2SThomas Veerman	test "$revision" != "$oldrev" && echo $revision >revision
1097f99f3a2SThomas Veerman
1107f99f3a2SThomas Veerman	test $realroot != $root && umount $root
1117f99f3a2SThomas Veerman	echo "Done."
1127f99f3a2SThomas Veerman	;;
1137f99f3a2SThomas Veermanesac
1147f99f3a2SThomas Veermansync
1157f99f3a2SThomas Veermanexit 0
116