Version 9 (modified by Jinyang, on Nov 24, 2013 at 11:08:15 AM) (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 $ cd grub $ ./

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 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 > grub-setup -d $mnt/boot/grub -r '(fd0)' -m '(fd0)' rm -f umount $mnt


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 # This is a shell script to create a QEMU simulation environment # based on Grub2 for RTEMS.


# Quit when error occurs. set -e

create="yes" update="no"

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

usage() { cat <<EOF Usage: [OPTIONS] [FILE] Create/Update? a bootable hard-disk image with Grub2.

-h, --help printf this message and exit -c, --create create the disk image, default -u, --update mount the image to /mnt FILE the file you want to create or update, default ${image}


# Check the arguments while test $# -gt 0 do

option=$1 shift

case "$option" in # help -h | --help )

usage exit 0 ;;

# create the disk image -c | --create )

create="yes" ;;

# update the kernel/grub.cfg -u | --update )

update="yes" create="no" ;;

-* )

echo "Unrecognized option $option" 1>&2 echo 1>&2 usage exit 1 ;;

  • )

image="$option" ;;



create_image() {

# Create the actual disk image - 128MB dd if=/dev/zero of=${image} bs=512 count=32130 2>/dev/null chown ${user}:${group} ${image} echo "Create disk image ${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

echo "Create an msdos partion table, set bootable bit"

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

##### +++++++++++++++++ The other method, we need kpartx support ++++++++++++++++ # Notify the kernel about the new partion. Map the partition from the image file.

# kpartx -av ${lodev} &>/dev/null

# sleep a sec, wait for kpartx to create the device nodes.

# sleep 1

# Find the first unused loop device.

# lonxt=losetup -f

# Create a loop device for this partition, like /dev/sda and /dev/sda0 work.

# losetup ${lonxt} /dev/mapper/${loname}p1

##### +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# Make an ext2 filesystem on the first partition. mkfs.ext2 ${lonxt} &>/dev/null echo "Create new filesystem ext2" # 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

##### +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # --root-directory=/mnt equal to --boot-directory=/mnt/boot # --no-floppy or --allow-floppy

# grub-install --allow-floppy --boot-directory=/mnt/boot \ # --disk-module=biosdisk \ # --modules="ext2 part_msdos" --force ${lodev}

##### +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

##### +++++++++++++++++ The other method, we do it step by step +++++++++++++++++ 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}

##### +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ echo "Grub2 installed"

sync trap - INT TERM EXIT umount /mnt losetup -d ${lonxt} ##### +++++++++++++++++ The other method, we need kpartx support ++++++++++++++++

# kpartx -dv ${lodev} 2>/dev/null # sleep 1

##### +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ losetup -d ${lodev} echo "Done"


update_image() {

# You can use the following script update the image. start=fdisk -lu ${image} | grep "${image}1" | awk '{ print $3}' mount -o loop,offset=$(expr 512 \* ${start}) ${image} /mnt

exit 0

cp kernel /mnt cp grub.cfg /mnt/boot/grub sync umount mnt


if [ "${create}" = "yes" ]; then

echo "Create a bootable hard-disk image with Grub2" create_image


if [ "${update}" = "yes" ]; then

echo "Update the bootable hard-disk image" update_image