summaryrefslogtreecommitdiff
path: root/releasetools/mkboot
blob: b2abee8df1971efb27e5cf0ee3fd994c60a83b26 (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
#!/bin/sh
#
#	mkboot 2.0 - make root device bootable, etc.
#							Author: Kees J. Bot

usage() {
	echo "Usage: $0 [bootable | hdboot] [DESTDIR]" >&2
	exit 1
}

rotate_oldest() {
	base_dir="$1"
	set -- `ls -t "$base_dir"`

	case $# in
	0|1|2|3)
		# Not much there, do not remove a thing.
		;;
	*)
		# Remove the third-newest image in /boot/$hdboot_t, but
		# only if there's an older one (which is kept). 
		echo "rm $root:$base_dir/$3"
		rm -rf "$base_dir/$3"
	esac
}


mdec=/usr/mdec	# bootstraps
# If no DESTDIR specified, then act on / or on the current chroot
DESTDIR=
# Check arguments.
case "$#:$1" in
1:bootable | 1:hdboot )
	# LSC Broken, if $# == 1, then $2,$3 are not set...
	action=$1 
	;;
2:bootable | 2:hdboot )
	action=$1 DESTDIR=$2
	;;
*)	usage
esac

# Get the device table.
FSTAB=/etc/fstab
touch $FSTAB
root="`awk <$FSTAB '{ if($2=="/") { print $1 } }'`"

# The real root device may be the RAM disk.
realroot=`printroot -r`

# If it's an initial fstab, pretend root is real root
if [ "$root" = "/dev/ROOT" -o -z "$root" ]
then	root=$realroot
fi

case $action in
bootable | hdboot)
	# We need the root device.

	if [ $realroot = $root ]
	then
		rootdir=
	else
		umount $root 2>/dev/null
		mount $root /mnt || exit
		rootdir=/mnt
	fi
esac

case $action in
hdboot)
	version=`sh ../sys/conf/osrelease.sh`

	# Retrieve the git revision; this only succeeds
	# if git is available, it's a git checkout, *and*
	# there are no uncommitted changes.
	if git diff --quiet 2>/dev/null
	then	gitrev="-`git describe --always`"
	fi

	revision=`cat revision 2>/dev/null`

	oldrev=$revision

	if [ -z "$revision" ]
	then
		revision=0
		rrevision=""
		gitrev=""
	else
		revision=`expr $revision + 1`
		rrevision=r$revision
	fi

	target="${version}${rrevision}${gitrev}"

	rotate_oldest "$DESTDIR/boot/minix"

	# rotate system processes. We assume latest ones are in
	# /boot/modules/.temp and we maintain /boot/modules/ by ourselves.
	[ -d $DESTDIR/boot/minix/.temp ] || exit 1
	rm -rf $DESTDIR/boot/minix/"$target"/
	mv $DESTDIR/boot/minix/.temp $DESTDIR/boot/minix/"$target"
	rm -f $DESTDIR/boot/minix_latest
	ln -s minix/"$target" $DESTDIR/boot/minix_latest 

	# Save the revision number.
	test "$revision" != "$oldrev" && echo $revision >revision

	test $realroot != $root && umount $root
	echo "Done."
	;;
esac
sync
exit 0