wiki:Building/Grub

Version 10 (modified by Jinyang, on 11/24/13 at 11:14:33) (diff)

Building Grub

The following instructions built the image and were run on a Fedora Core 5 Linux host.

Download the latest release of version 2 the Grub boot loader from the ftp, another ftp or, you can check out the developer branch using bzr. Remember to run autogen when checking out the development branch...

$ sudo apt-get install bzr bison libdevmapper-dev libfreetype6-dev autogen $ bzr branch http://bzr.savannah.gnu.org/r/grub/trunk/grub $ cd grub $ ./autogen.sh

Build the package with the following configure command:

$ ../grub-1.94/configure --prefix=/tmp/g2/build $ make all install

where the prefix can be anything suitable for your machine. If configure fails with a missing LZO library check you have the lzo-devel package installed.

To make the floppy image follow the instructions in the Grub Wiki at http://grub.enbug.org/TestingOnX86. This script is adapted from the instructions:

#! /bin/sh -x grub=/tmp/g2/build workspace=/tmp/g2 mnt=$workspace/mnt/loop file=$workspace/rtems-boot.img export PATH=$grub/bin:$grub/sbin:$PATH mkdir -p $workspace cd $workspace grub-mkimage -o core.img _chain ls pc multiboot gpt fat boot reboot configfile cat help dd if=/dev/zero of=$file bs=512 count=2880 /sbin/mkdosfs $file mkdir -p $mnt mount -o loop -t vfat $file $mnt mkdir -p $mnt/boot/grub cp $grub/lib/grub/i386-pc/boot.img core.img $grub/lib/grub/i386-pc/*.mod $mnt/boot/grub echo "configfile (hd0,0)/rtems-grub.cfg" > $mnt/boot/grub/grub.cfg echo '(fd0)' $file > tmp_device.map grub-setup -d $mnt/boot/grub -r '(fd0)' -m tmp_device.map '(fd0)' rm -f tmp_device.map umount $mnt

Grub2

Most of method of the previous is mostly based on grub legacy. And most Linux distributions have updated to Grub2. So in the following, we just introduce how to build a i386 simulation environment based on Grub2 and QEMU.

This is tested on Ubuntu-13.04 and the Grub2 also has been installed.

#!/bin/bash
set -e

image="disk.img"
# Hack to make everything owned by the original user.
user=`who am i | awk '{print $1}'`
group=`groups $user | awk '{print $3}'`

# Create the actual disk image - 15MB
dd if=/dev/zero of=${image} bs=512 count=32130 2>/dev/null
chown ${user}:${group} ${image}
# Setup the loopback device, and reture the devie name.
lodev=`losetup -f --show ${image}`
trap 'losetup -d ${lodev}; exit $?' INT TERM EXIT
loname=${lodev##*/}
# Make the partition table, partition and set it bootable.
parted -a minimal --script ${lodev} mklabel msdos mkpart primary ext2 1M 100% \
    set 1 boot on 2>/dev/null

# Get the start sectors.
start=`fdisk -lu ${lodev} | grep "${loname}p1" | awk '{ print $3}'`
# Find the first unused loop device.
lonxt=`losetup -f`
# Create a loop device for this partition, like /dev/sda and /dev/sda0 work.
losetup -o $(expr 512 \* ${start}) ${lonxt} ${lodev}
trap 'losetup -d ${lonxt}; losetup -d ${lodev}; exit $?' INT TERM EXIT

# Make an ext2 filesystem on the first partition.
mkfs.ext2 ${lonxt} &>/dev/null
# Mount the filesystem via loopback.
mount ${lonxt} /mnt
trap 'umount /mnt; losetup -d ${lonxt}; losetup -d ${lodev}; exit $?' INT TERM EXIT
mkdir -p /mnt/boot/grub
echo 'source (hd1,msdos1)/grub.cfg'>/mnt/boot/grub/grub.cfg


mkdir -p /mnt/boot/grub/i386-pc
cp /usr/lib/grub/i386-pc/* /mnt/boot/grub/i386-pc

# Make a bootable image of GRUB.
grub-mkimage -d /usr/lib/grub/i386-pc -O i386-pc --output=core.img \
        --prefix=\(,msdos1\)/boot/grub ext2 part_msdos biosdisk search_fs_uuid
mv core.img /mnt/boot/grub/i386-pc
# Set up a device to boot using GRUB.
grub-bios-setup --allow-floppy  --force --directory=/mnt/boot/grub/i386-pc \
        --device-map= ${lodev}

sync
trap - INT TERM EXIT
umount /mnt
losetup -d ${lonxt}
losetup -d ${lodev}