操作系统开发 - 在disk image上安裝grub2

来源:互联网 发布:linux文件夹改名 编辑:程序博客网 时间:2024/06/04 18:44

網上很少關於grub2在disk image上的安裝方法,所以寫這編教程:


Makefile:

dd if=/dev/zero of=hd.img count=20 bs=1047675
./createPartition.sh
kpartx -a hd.img
mkfs.vfat /dev/mapper/loop0p1
mount /dev/mapper/loop0p1 1
mkdir -p 1/boot/grub
cp grub.cfg 1/boot/grub/grub.cfg
cp testing_kernel 1/boot



echo "(hd0) /dev/loop0" > device.map
echo "set prefix=(hd0,msdos1)/boot/grub" > mycfg.cfg
~/grub2/bin/grub-mkimage --config=mycfg.cfg -p /boot -O i386-pc -o 1/boot/grub/core.img loadenv chain biosdisk part_dvh part_msdos msdospart part_gpt exfat ext2 fat vbe vga ntfs echo test configfile minicmd hdparm normal
cp ~/grub2/lib/grub/i386-pc/boot.img 1/boot/grub/
~/grub2/sbin/grub-bios-setup -m device.map -d 1/boot/grub /dev/loop0
sleep 1
umount 1
kpartx -d hd.img


grub.cfg:

set timeout=15
set root=(hd0,msdos1)
#set default=0 # Set the default menu entry
 
menuentry "china ar" {
   insmod ext2
   set root=(hd0,msdos1)
   chainloader +1
#   multiboot /boot/testing_kernel   # The multiboot command replaces the kernel command
#   boot
}


menuentry "OS Name 2" {
   multiboot /boot/testing_kernel   # The multiboot command replaces the kernel command
   boot
}


creeatePartition.sh:

#!/bin/bash




cat <<EOM| fdisk hd.img
n
p
1




t
c
w
EOM


echo ok # you need this line