better cpp
[minix3.git] / tools / mkboot
blob3c48fdeeed4725f7e8e87b9bb1708d7596c87841
1 #!/bin/sh
3 # mkboot 2.0 - make boot floppy, make root device bootable, etc.
4 # Author: Kees J. Bot
6 usage() {
7 echo "Usage: $0 [bootable | hdboot [minix or image] | fdboot [device]]" >&2
8 exit 1
11 rotate_oldest() {
12 base_dir="$1"
13 set -- `ls -t "$base_dir"`
15 case $# in
16 0|1|2|3)
17 # Not much there, do not remove a thing.
20 # Remove the third-newest $hdboot_t in /boot/$hdboot_t, but
21 # only if there's an older one (which is kept).
22 echo "rm $root:$base_dir/$3"
23 rm -rf "$base_dir/$3"
24 esac
28 trap 'e=$?; rm -f /tmp/mkb.$$; exit $e' 0 2
30 mdec=/usr/mdec # bootstraps
32 # Check arguments.
33 case "$#:$1" in
34 1:bootable | 2:hdboot | [12]:fdboot )
35 action=$1 dev=$2 size=$3
37 *) usage
38 esac
40 if [ "$1" = "hdboot" ]
41 then
42 if [ "$2" != "image" -a "$2" != "minix" ]
43 then usage
45 hdboot_t="$2"
48 # Get the device table.
49 FSTAB=/etc/fstab
50 touch $FSTAB
51 if grep -q "Poor man" $FSTAB
52 then . $FSTAB
53 else root="`awk <$FSTAB '{ if($2=="/") { print $1 } }'`"
56 # The real root device may be the RAM disk.
57 realroot=`printroot -r`
59 # If it's an initial fstab, pretend root is real root
60 if [ "$root" = "/dev/ROOT" -o -z "$root" ]
61 then root=$realroot
64 case $action in
65 bootable | hdboot)
66 # We need the root device.
68 if [ $realroot = $root ]
69 then
70 rootdir=
71 else
72 umount $root 2>/dev/null
73 mount $root /mnt || exit
74 rootdir=/mnt
76 esac
78 case $action in
79 hdboot)
80 # Install a new image on the root device.
81 if [ -e $rootdir/boot/$hdboot_t -a ! -d $rootdir/boot/$hdboot_t ]
82 then
83 # /boot/$hdboot_t is not yet a directory! Fix it.
84 su root -c \
85 "exec mv $rootdir/boot/$hdboot_t /M"
86 install -d $rootdir/boot/$hdboot_t
87 su root -c \
88 "exec mv $rootdir/M $rootdir/boot/$hdboot_t/`uname -r`"
91 sh tell_config OS_RELEASE . OS_VERSION >/tmp/mkb.$$
92 version=`sed 's/[" ]//g;/^$/d' </tmp/mkb.$$`
94 # Retrieve the git revision; this only succeeds
95 # if git is available, it's a git checkout, *and*
96 # there are no uncommitted changes.
97 if git diff --quiet 2>/dev/null
98 then gitrev="-`git describe --always`"
101 revision=`cat revision 2>/dev/null`
103 if [ -z "$revision" ]
104 then rrevision=""
105 gitrev=""
106 else rrevision=r$revision
109 oldrev=$revision
111 if [ -z "$revision" ]
112 then
113 revision=0
114 rrevision=""
115 else
116 revision=`expr $revision + 1`
117 rrevision=r$revision
120 target="${version}${rrevision}${gitrev}"
122 rotate_oldest "$rootdir/boot/$hdboot_t"
124 # rotate system processes. We assume latest ones are in
125 # /boot/modules/.temp and we maintain /boot/modules/ by ourselves.
126 if [ "$hdboot_t" = "minix" ]
127 then
128 [ -d /boot/minix/.temp ] || exit 1
129 rm -rf /boot/minix/"$target"/
130 mv /boot/minix/.temp /boot/minix/"$target"
131 rm -f /boot/minix_latest
132 ln -s minix/"$target" /boot/minix_latest
133 else
134 # Install the new image.
135 echo "install $hdboot_t $root:/boot/$hdboot_t/$target"
136 install -o root -m 600 $hdboot_t $rootdir/boot/$hdboot_t/$target || exit 1
138 # Tell bootloader which image is newest
139 ln -f $rootdir/boot/$hdboot_t/$target $rootdir/boot/${hdboot_t}_latest
142 # Save the revision number.
143 test "$revision" != "$oldrev" && echo $revision >revision
145 test $realroot != $root && umount $root
146 echo "Done."
148 esac
149 sync
150 exit 0