grub-internals:The rescue mode
 

The Grub console



Tab : display available commands
help command : display help for that command if available ,
If not available "insmod command" is needed to load the command.mod
ex : "insmod date" then "help date"

grub2 switches between 2 modes of "shell console"

a sh:grub> activated by typing "reader.normal"
b grub_rescue> mode activated by typing "reader.rescue"

A 3rd lua.console> can be turned on from normal sh:grub
c lua:grub> mode activated by typing "parser.lua" ; use "grub.run("parser.sh") to switch back to sh:grub>

configfile switch to another config
source load another config -without changing config-

sh:grub requires the following modules : sh normal minicmd which come up with kernel.img

The rescue-mode

In the grub spirit ,the rescue mode helps troubleshooting . It's quite similar to falling into initramfs when root filesystem has not been found by linux kernel at boot,the same thing happens to grub when "normal" grub booting does not happen : it falls then into rescue mode. Let's artificially create this situation, then we will learn step by step grub2 !
Instead of creating normal_cdrescue ,let's add an embedded memdisk_img with almost nothing in it ,just a few modules .In the grub2_src , run the script :

rescuecd_withmemdisk.sh

#!/bin/sh
[ -e buildcd ] && rm -fr buildcd
mkdir buildcd buildcd/boot buildcd/boot/grub
cp *.mod command.lst moddep.lst fs.lst handler.lst parttool.lst buildcd/boot/grub
#voluntarily create memdisk_img to fall on rescue-mode. Had we copied modules to memdk/boot/grub everything would work both drives memdisk&cd
[ -e memdk ] && rm -fr memdk
mkdir memdk memdk/boot memdk/boot/grub
cp minicmd.mod normal.mod extcmd.mod boot.mod ls.mod command.lst memdk/boot
tar -C memdk -cf memdsk boot
./grub-mkimage -d . -m memdsk -o core.img biosdisk iso9660 memdisk tar
#embed memdsk into core.img then append cdboot to get "kindof" stage2_eltorito !
cat cdboot.img core.img > buildcd/boot/grub/stage2_eltorito
genisoimage -b boot/grub/stage2_eltorito -no-emul-boot -boot-load-size 4 -boot-info-table -o RescueModeStudy -r -J buildcd
#end script

qemu -cdrom RescueModeStudy

At boot the grub kernel loads memdisk ,so this overides the rescuecd_dir which is seen as a second drive ! Since the memdisk_modules are put in (memdisk)/boot ,they are not recognised at all so we fall into grub-rescue-mode . Had we put them into (memdisk)/boot/grub then we would get the right grub console normal mode with two drives (memdisk) (hd96) .

grub rescue>ls
(memdisk) (hd96)
The two recognised drives are embedded memdisk and the cd_iso.
grub rescue> ls -l              /* shows nothing */
Let's switch to cd drive
grub rescue> set prefix=(hd96)/boot/grub
grub rescue> set root=(hd96)
Now let's insert ls.mod
grub rescue>insmod ls
grub rescue>ls -l            /* show now the drives with their filesystem ! */
Device memdisk : filesystem type tarfs
Device hd96 : filesystem type iso9660 label cdrom uuid 2009-05-31-15-04-15-00

Type the "Tab" touch
grub rescue>Tab           /* show the commands available from normal.mod ,since ls.mod depends on normal.mod - Look at moddep.lst -*/
boot export insmod ls normal set unset

Add minicmd.mod
grub rescue>insmod minicmd
grub rescue>Tab           /* show cmnds from normal.mod & minicmd.mod */
boot cat dump exit export help insmod ls lsmod normal rmmod root set unset


To experiment more ,put different modules in memdk/boot/grub ,build the rescuemodeStudy and try Retrieve one by one the modules from the memdisk and you get some ideas how these modules get into play !