minix/tools/mkboot
Evgeniy Ivanov 8979450631 Rotate kernels/images/modules.
Kernels and system services are stored in a single directory in the
/boot/minix/ and rotated like /boot/images. /boot/minix_latest slink
is created automatically.
System serives are prefixed by "modNN_" to allow to easily load them
using "mod*" pattern.
2012-02-09 18:54:44 +01:00

202 lines
4.4 KiB
Bash
Executable file

#!/bin/sh
#
# mkboot 2.0 - make boot floppy, make root device bootable, etc.
# Author: Kees J. Bot
usage() {
echo "Usage: $0 [bootable | hdboot [minix or image] | fdboot [device]]" >&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 $hdboot_t 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
}
trap 'e=$?; rm -f /tmp/mkb.$$; exit $e' 0 2
mdec=/usr/mdec # bootstraps
# Check arguments.
case "$#:$1" in
1:bootable | 2:hdboot | [12]:fdboot )
action=$1 dev=$2 size=$3
;;
*) usage
esac
if [ "$1" = "hdboot" ]
then
if [ "$2" != "image" -a "$2" != "minix" ]
then usage
fi
hdboot_t="$2"
fi
# Get the device table.
FSTAB=/etc/fstab
touch $FSTAB
if grep -q "Poor man" $FSTAB
then . $FSTAB
else root="`awk <$FSTAB '{ if($2=="/") { print $1 } }'`"
fi
# 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
bootable)
# Install the boot monitor on the root device and make it bootable.
install -cs -m 644 $mdec/boot $rootdir/boot/boot || exit
sync
installboot_minix -device $root $mdec/bootblock /boot/boot || exit
test $realroot != $root && umount $root
;;
hdboot)
# Install a new image on the root device.
if [ -e $rootdir/boot/$hdboot_t -a ! -d $rootdir/boot/$hdboot_t ]
then
# /boot/$hdboot_t is not yet a directory! Fix it.
su root -c \
"exec mv $rootdir/boot/$hdboot_t /M"
install -d $rootdir/boot/$hdboot_t
su root -c \
"exec mv $rootdir/M $rootdir/boot/$hdboot_t/`uname -r`"
fi
sh tell_config OS_RELEASE . OS_VERSION >/tmp/mkb.$$
version=`sed 's/[" ]//g;/^$/d' </tmp/mkb.$$`
# 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`
if [ -z "$revision" ]
then rrevision=""
gitrev=""
else rrevision=r$revision
fi
oldrev=$revision
if [ -z "$revision" ]
then
revision=0
rrevision=""
else
revision=`expr $revision + 1`
rrevision=r$revision
fi
target="${version}${rrevision}${gitrev}"
rotate_oldest "$rootdir/boot/$hdboot_t"
# rotate system processes. We assume latest ones are in
# /boot/modules/.temp and we maintain /boot/modules/ by ourselves.
if [ "$hdboot_t" = "minix" ]
then
[ -d /boot/minix/.temp ] || exit 1
mv /boot/minix/.temp /boot/minix/"$target"
rm -f /boot/minix_latest
ln -s /boot/minix/"$target" /boot/minix_latest
else
# Install the new image.
echo "install $hdboot_t $root:/boot/$hdboot_t/$target"
install -o root -m 600 $hdboot_t $rootdir/boot/$hdboot_t/$target || exit 1
# Tell bootloader which image is newest
ln -f $rootdir/boot/$hdboot_t/$target $rootdir/boot/${hdboot_t}_latest
fi
# Save the revision number.
test "$revision" != "$oldrev" && echo $revision >revision
test $realroot != $root && umount $root
echo "Done."
;;
fdboot)
# fdboot: Make a boot floppy.
if [ -z "$dev" ]
then
echo -n \
"Finish the name of the floppy device to write (by default 'fd0'): /dev/";
read dev
case "$dev" in
'') dev=/dev/fd0
;;
/dev/*)
;;
*) dev=/dev/$dev
esac
fi
# Make a file system.
umount $dev 2>/dev/null
if mkfs -B 1024 -i 512 $dev
then :
else
echo "mkfs of $dev failed."
exit 1;
fi
# Install /dev, /boot/boot and /boot/image.
mount $dev /mnt || exit
mkdir -p /mnt/boot/image || exit
cpdir /dev /mnt/dev || exit
cp -p $mdec/boot /mnt/boot/boot || exit
cp -p image /mnt/boot/image/ || exit
umount $dev || exit
# Make bootable and copy the boot parameters.
installboot_minix -d $dev $mdec/bootblock /boot/boot || exit
pfile=fdbootparams
if [ -f $pfile ]
then echo "Using floppy boot parameters from file $pfile."
edparams $dev "`cat $pfile`" || exit
else echo "Copying floppy boot parameters from $root."
dd if=$root of=$dev skip=1 seek=1 count=1 conv=silent || exit
fi
edparams $dev 'main(){delay 2000;boot}; save' || exit
echo "Test kernel installed on $dev"
;;
esac
sync
exit 0