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

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

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, ,
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
helpful.  Also, reading linux/init/do_mounts.c helped.

- Dan

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

# mkbe2gbf: make bare ext2 grub boot floppy
# by Dan Kegel, , 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
    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

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


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

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

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.
as patched by
# (See also for
alternate approach)
# <path>      <type> <mode>   <uid>   <gid>   <major> <minor> <start> <inc>  
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  

# 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
   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
   # 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
   # grub actually reads menu.lst, not grub.conf
   ln -s grub.conf $TMPDIR/boot/grub/menu.lst

# 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)


echo "Boot floppy image ready at $IMAGE"


Site Timeline