Script to create bootable floppy image using genext2fs and grub without root

From: Dan Kegel (dank-news_at_kegel.com)
Date: 10/06/03


Date: Mon, 06 Oct 2003 05:12:00 GMT


For what it's worth, I just wrote a little
script that shows how to generate a bootable
ext2 floppy using grub and genext2fs without
being root.

I wrote this mostly because Thierry Larode and Robert Milan's mkbimage script,
http://savannah.gnu.org/cgi-bin/viewcvs/grub/grub/util/mkbimage,
requires root privs, and I wanted to show that there is
no need for root privs (or 'fakeroot') to make a bootdisk,
at least for ext2.

It was fun to write, once the pain stopped.
Took me a long time to figure out a few things:
1) you have to enable EXT2 support when building the kernel :-)
2) you have to disable initrd support when building the kernel,
    or it'll prompt you for a second floppy
3) you have to say root=/dev/fd0 on the kernel commandline
    instead of root=fd0, which gets misinterpreted as a hexadecimal
    major/minor device number (!).
4) genext2fs seems to have an off-by-one error; if you ask it to generate a
    1440KB image, the resulting disk image doesn't *quite* fit on a floppy:
$ dd if=mkb.out of=/dev/fd0 bs=1
dd: writing `/dev/fd0': No space left on device
1474561+0 records in
1474560+0 records out
(I guess I'll have a look at that sometime.)
The disk booted anyway, so I guess nothing critical is on that last sector.

While writing this, I found the recipes at
http://rescuecd.sourceforge.net/288.html
helpful. Also, reading linux/init/do_mounts.c helped.

- Dan



#!/bin/sh
# mkbe2gbf: make bare ext2 grub boot floppy
# by Dan Kegel, http://kegel.com, Oct 2003
# Released under GPL

if test $# != 4; then
    echo "Script to demonstrate creating a bootable floppy image using GRUB and genext2fs without root."
        echo "Normally, one uses an initramdisk when booting from floppy, so this script is mostly for fun."
    echo "Usage:"
    echo " mkbe2gbf directory-to-pickle desired-output-file floppy-size-in-kb path-to-grub-files"
        echo "Note: genext2fs and grub must be on the PATH,"
        echo "and the kernel should be in /boot/bzImage, and have been compiled with EXT2 and without INITRD."
        exit 1
fi

DIR=$1; shift
IMAGE=$1; shift
SIZE=$1; shift
GRUBDIR=$1; shift

TMPDIR=/tmp/mkboot.dir.$$
TMPDEVLIST=/tmp/mkboot.devlist.$$

if test ! -f $GRUBDIR/stage1; then
   echo "Directory $GRUBDIR does not contain file stage1. Please specify correct path to GRUB files."
   exit 1
fi

if ! mkdir $TMPDIR; then
   echo "Failed to create temporary directory $TMPDIR!"
   exit 1
fi

set -x -e

# List devices genext2fs should create.
# This generic set of devices should do for most people; more can be created after boot if needed.
# I bet you could get by with a lot less.
# The format is for the newer genext2fs, i.e.
# http://ftp.debian.org/debian/pool/main/g/genext2fs/genext2fs-1.3.orig.tar.gz as patched by
# http://www.uclibc.org/cgi-bin/cvsweb/buildroot/sources/genext2fs.patch
# (See also http://groups.google.com/groups?selm=ucy95yoxrv.fsf%40habanero.picante.com for alternate approach)
# <path> <type> <mode> <uid> <gid> <major> <minor> <start> <inc> <count>
cat > $TMPDEVLIST << _EOF_
/dev/ram0 b 640 0 0 1 0
/dev/mem c 640 0 0 1 1
/dev/kmem c 640 0 0 1 2
/dev/null c 640 0 0 1 3
/dev/zero c 640 0 0 1 5
/dev/random c 640 0 0 1 8
/dev/urandom c 640 0 0 1 9
/dev/ttyS0 c 640 0 0 4 64
/dev/console c 640 0 0 5 1
/dev/ptmx c 640 0 0 5 2
/dev/pts/0 c 640 0 0 136 0
_EOF_

# Copy input directory to temporary directory and add grub files
(cd $DIR; find . -print0 | cpio -0pmd $TMPDIR)
mkdir -p $TMPDIR/dev
mkdir -p $TMPDIR/dev/pts
mkdir -p $TMPDIR/boot/grub
test $TMPDIR/boot/grub/stage1 || cp $GRUBDIR/stage1 $TMPDIR/boot/grub
test $TMPDIR/boot/grub/stage2 || cp $GRUBDIR/stage2 $TMPDIR/boot/grub

# This section's a bit silly; it just creates the demo grub boot menu.
if test ! -f $TMPDIR/boot/grub/grub.conf; then
   echo "Can't find boot/grub/grub.conf, so creating generic one"
   if test ! -f $TMPDIR/boot/bzImage; then
       echo "Can't find kernel. Try creating $DIR/boot/grub/grub.conf containing paths to kernel and linuxrc"
           exit 1
   fi
   if test ! -f $TMPDIR/linuxrc; then
       echo "Can't find linuxrc. Try creating $DIR/boot/grub/grub.conf containing paths to kernel and linuxrc"
           exit 1
   fi
   # Note: root=fd0 fails, it thinks it's a hex number, must use /dev/fd0!
   # OK, this is silly, nobody really uses a floppy as a root device, but hey, it's neat you can.
   cat > $TMPDIR/boot/grub/grub.conf <<_EOF_
title Generic boot floppy, for kernel with EXT2 but not INITRD
root (fd0)
kernel /boot/bzImage init=/linuxrc root=/dev/fd0
_EOF_
   # grub actually reads menu.lst, not grub.conf
   ln -s grub.conf $TMPDIR/boot/grub/menu.lst
fi

# Create the image and populate it using genext2fs
# Don't reserve any blocks for the superuser.
genext2fs -r 0 -d $TMPDIR -b $SIZE -f $TMPDEVLIST $IMAGE

# FIXME: use $GRUBDIR/grub ? We want one that runs on the build machine, not the target...
grub --device-map=/dev/null <<_EOF_
device (fd0) $IMAGE
root (fd0)
setup (fd0)
_EOF_

rm -rf $TMPDIR $TMPDEVLIST

echo "Boot floppy image ready at $IMAGE"



Relevant Pages

  • Re: My Machine Wont Boot
    ... I made a floppy and When I boot the floppy clicks and makes a tiny grind, so it is trying, but sadly, the device listing in the bios stays on the screen with no error message. ... I copied the grub directory to the root of the floppy, so the flopy has the contents of the grub directory in its root. ...
    (Fedora)
  • Re: Grub, latest adventure
    ... > I want from floppy. ... This looks like you are telling the grub that the grub boot floppy ... > root ...
    (Debian-User)
  • Re: FC4 Install GRUB Load Error
    ... >> LinuxForums.org Tutorial on GRUB about booting it up. ... >> Yet when I take out the floppy and just try and boot from the Hard ... > preferably not root at this point in time. ...
    (Fedora)
  • Re: GRUB Loading Stage2Read Error
    ... in the floppy, ... You get a grub prompt, ... >> Grub> root ... but suggests a problem with the disk. ...
    (comp.os.linux.setup)
  • Re: Floppy-Images erstellen + =?UTF-8?B?Ym9vdGbDpGhpZyBtYWNoZW4=?=
    ... als "echte" Floppy behandelt wird. ... Tutorials und dort "Using Grub" soll man das so machen. ... Diskette booten und GRUB per 'setup ' auf die zweite Floppy ...
    (de.comp.os.unix.linux.misc)