linux efi分区安装grub2,编译UEFI版本Grub2引导多系统文件efi
可以直接從這個人的源碼github中下載學習,項目地址:https://github.com/beatfan/UEFI_grub2
官網源碼地址
首先要從grub官網下在grub2,grub2中包含所有grub2相關的命令,可以用來生成grub2引導,這里著重介紹制作UEFI版本的grub2
UEFI啟動是fat/fat32分區/EFI/Boot/bootia32.efi或者/EFI/Boot/bootx64.efi,然后進程由UEFI交給efi程序,我們就是制作這個efi程序,使用grub-mkimage命令即可生成。
efi程序中需要內置配置文件,類似grub的grldr文件,一旦生成后就很難修改了,所以,我們需要一個類似與menu.lst的配置文件,所以我們指定的配置文件中指向外部的cfg配置文件
以下為64位efi生成,32位參考文件夾名稱,將x86_64-efi修改為i386-efi即可,一般是不需要,支持UEFI的基本都是64位的PC。
定制文件目錄方法
內置配置文件為:grub.cfg,內置配置文件搜索/EFI/grub/compile.cfg 文件,并將其設定為配置文件。
將其保存在grub2解壓的壓縮目錄下,內容如下:
search.file /EFI/grub/grub.cfg root
set prefix=($root)/EFI/grub
configfile ($root)/EFI/grub/grub.cfg
編譯64位efi文件Bootx64.efi
在grub2壓縮包下面解壓目錄下打開命令行,輸入以下命令:下面是編譯64的文件
grub-mkimage.exe -d x86_64-efi -c compile.cfg -p /EFI/grub -o Bootx64.efi -O x86_64-efi part_gpt part_msdos disk fat exfat ext2 ntfs xfs appleldr hfs iso9660 normal search_fs_file configfile linux linux16 chain loopback echo efi_gop efi_uga video_bochs video_cirrus file gfxmenu gfxterm gfxterm_background gfxterm_menu halt reboot help jpeg ls png true
編譯32位efi文件BOOTIA32.efi
grub-mkimage.exe -d i386-efi -c compile.cfg -p /EFI/grub -o BOOTIA32.efi -O i386-efi part_gpt part_msdos disk fat exfat ext2 ntfs xfs appleldr hfs iso9660 normal search_fs_file configfile linux linux16 chain loopback echo efi_gop efi_uga video_bochs video_cirrus file gfxmenu gfxterm gfxterm_background gfxterm_menu halt reboot help jpeg ls png true
命令解釋
-d 表示指定查找模塊目錄
-c 表示指定配置文件,這個配置文件會集成到efi文件內,就是我們剛剛編寫的x86_64-efi.cfg
-p 設置偏好文件夾,cfg文件中會調
-o 表示生成的目標文件
-O 表示集成的模塊
模塊這東西,有些命令不需要刻意不編譯進去,如果有些linux16命令沒編譯進去,就會找不到linux16.
以上生成完畢,在文件夾下會出現一個bootx64.efi文件,BOOTIA32.efi 文件夾,
將其和x86_64-efi、locale文件夾、unicode.pf2一起拷貝到第一個fat/fat32分區,并新建一個grub.cfg。
其中,x86_64-efi為模塊目錄,locale為地區語言,unicode.pf2為字體,grub.cfg為引導加載的配置文件
目錄如下:
FAT/FAT32
#########################
/EFI/Boot/bootx64.efi
/EFI/Boot/BOOTIA32.efi
/EFI/grub/grub.cfg
/EFI/grub/unicode.pf2
/EFI/grub/x86_64-efi/
/EFI/grub/locale/
#####################
x64.cfg內容示例:
[plain] view plain copy
function load_video {
if [ x$feature_all_video_module = xy ]; then
insmod all_video
else
insmod efi_gop
insmod efi_uga
insmod ieee1275_fb
insmod vbe
insmod vga
insmod video_bochs
insmod video_cirrus
fi
}
insmod part_gpt
insmod fat
set root='hd0,gpt1'
set font
font="/EFI/grub/unicode.pf2"
if loadfont
prefix/locale
set lang=zh_CN
insmod gettext
fi
adjust the screen resolution
terminal_output gfxterm
background
insmod jpeg
if background_image /EFI/grub/background.jpg; then
true
else
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
if background_color 255,255,155,0; then
clear
fi
fi
default select menu index
set default=0
timeout
set timeout_style=menu
set timeout=5
menuentry "啟動 delta win7" --class windows --class os {
insmod ntfs
set root='(hd0,gpt2)'
clear
echo "Start Windows"
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}
menuentry "local win7" --class windows --class os {
insmod ntfs
set root='(hd0,gpt3)'
clear
echo "Start Windows"
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}
menuentry "ubuntu16.04 x86" --class ubuntu --class os {
insmod ext2
set root='(hd0,gpt5)'
linux /vmlinuz ro root=/dev/sda5
initrd /initrd.img
echo "Start Ubuntu 16.04"
}
menuentry "ubuntu16.04 x64" --class ubuntu --class os {
insmod ext2
set root='(hd0,gpt6)'
linux /vmlinuz ro root=/dev/sda6
initrd /initrd.img
echo "Start Ubuntu 16.04"
}
menuentry "-------------------" --class ubuntu --class os{
set root=(hd0,gpt1)
}
menuentry "ubuntu-efi" --class ubuntu --class os {
insmod ext2
set root='(hd0,gpt5)'
chainloader /efi/grub.efi
}
menuentry "install ubuntu" --class ubuntu --class os {
insmod ext2
insmod loopback
set root=(hd0,gpt4)
set isofile=/OS/linux/ubuntu-16.04.2-desktop-amd64.iso
loopback loop
isofile
initrd (loop)/casper/initrd.lz
}
menuentry "-------------------" --class ubuntu --class os{
set root=(hd0,gpt1)
}
menuentry "reboot" --class windows --class os{
insmod reboot
reboot
}
menuentry "halt" --class windows --class os{
insmod halt
halt
}
參考文檔
官網源代碼
官網說明文檔
排錯教程
其他源代碼
命令參考文檔
grub2入門教程
測試配置是否生效
在進入grub界面如果出現
[Minima] BASH-like line editing is supported.For the first word,TAB
lists possible command completions. Anywhere else TAB lists the possible
completions of a device/filename. ESC at any time exits
問題出在引導配置文件沒有找到.
那么如何調試呢?
可以嘗試打印變量的方法,輸入C進入命令模式
輸入gettext $prefix
我們發現還是提示(hd0,gp1)/EFI/grub
說明目錄并沒有被更改,我們可以驗證一下放到此目錄在跑起來.
發現果然又可以了,后面原因就是便宜目錄的compile.cfg里面的目錄并沒有修改到根目錄。
文件語法錯誤,語法錯誤問題找起來比較難!
字體亂碼
打開cfg文件查看是不是配置了語言文件,但是目錄不存在
loadfont ($root)/grub/fonts/unicode.pf2
set locale_dir=($root)/grub/locale
如果依然亂碼,修改文件編碼為utf-8
題外話 編譯bios模式
echo 正在編譯bios 模式引導文件
grub-mkimage.exe -d i386-pc -c compile.cfg -p /grub -o core.img -O i386-pc biosdisk part_gpt part_msdos disk fat exfat ext2 ntfs xfs appleldr hfs iso9660 normal search_fs_file configfile linux linux16 chain loopback echo efi_gop efi_uga video_bochs video_cirrus file search gfxmenu gfxterm gfxterm_background gfxterm_menu halt reboot help jpeg ls png true blocklist cat date echo play memdisk tar
pause
在④步驟中,已經生成BIOS模式所需的內核文件Core.img,其大小是86.5 KB
生成的只是單單BIOS模式的內核文件,還無法引導Grub2,還需要個啟動文件Boot.img,該文件很小,只有512Byte,位于i386-pc文件夾,該文件的作用是啟動Grub2,然后加載內核文件Core.img
所以這里的最后壹步就是把啟動文件Boot.img和內核文件Core.img以二進制形式合并,合并后的文件我個人稱呼為扇區文件G2ldr(不知道這個文件有沒標準的名稱),因為可以直接導入到扇區,來引導啟動Grub2,也可以由GRUB4DOS直接加載這個扇區文件來啟動Grub2。
老樣子,先給出命令(如果命令行的路徑不是Grub2包所在的路徑,就先要修改命令行的路徑,前面有提到)
Copy /B i386-pc\Boot.img+Core.img G2ldr
導入g2ldr
用bootice 導入扇區
恢復扇區數改成63不能大于63的
之所以會超過是定制問題, 那么這個不會
grub-mkimage.exe -d I386-PC -p (hd0,1)/Grub2/BIOS -o Core. img -O i386-pc biosdisk part_msdos fat exfat ntfs
: qq5274202
我的cmd批處理定制
@echo off
echo 所有文件將移動到根目錄下的grub文件夾下
echo 正在編譯64位
grub-mkimage.exe -d x86_64-efi -c compile.cfg -p /grub -o Bootx64.efi -O x86_64-efi part_gpt part_msdos disk fat exfat ext2 ntfs xfs appleldr hfs iso9660 normal search_fs_file configfile linux linux16 chain loopback echo efi_gop efi_uga video_bochs video_cirrus file search gfxmenu gfxterm gfxterm_background gfxterm_menu halt reboot help jpeg ls png true blocklist cat date echo play memdisk tar search_fs_uuid
echo 正在編譯32位
grub-mkimage.exe -d i386-efi -c compile.cfg -p /grub -o BootIA32.efi -O i386-efi part_gpt part_msdos disk fat exfat ext2 ntfs xfs appleldr hfs iso9660 normal search_fs_file configfile linux linux16 chain loopback echo efi_gop efi_uga video_bochs video_cirrus file search gfxmenu gfxterm gfxterm_background gfxterm_menu halt reboot help jpeg ls png true blocklist cat date echo play memdisk tar search_fs_uuid
::grub-mkimage.exe -d I386-PC -p (hd0,1)/Grub2/BIOS -o Core.img -O i386-pc biosdisk part_msdos fat exfat ext2 ntfs normal iso9660 search_fs_file 定制bios模式生成了core.img
echo 正在編譯bios 模式引導文件
grub-mkimage.exe -d i386-pc -c compile.cfg -p /grub -o core.img -O i386-pc biosdisk part_gpt part_msdos disk fat exfat ext2 ntfs xfs hfs iso9660 normal search_fs_file configfile linux linux16 chain loopback echo video_bochs video_cirrus file search gfxmenu gfxterm gfxterm_background gfxterm_menu halt reboot help jpeg ls png true blocklist cat date echo play memdisk tar
::不是所有模塊其他編譯架構就有的不如efi開頭的,在bios里面肯定會找不到
copy /b i386-pc\boot.img+core.img g2ldr
echo 編譯完成
pause
那么bios加載方式如何啟動呢。
下是主要的命令示例,%%圍繞的變量自行替換成實際的路徑或名稱。
ECHO 生成bios啟動文件
start /wait /min %grubDir%\grub-mkimage.exe -d %grubDir%\i386-pc -p /grub2/bios -o %BootFileDir%\Core.img -O i386-pc biosdisk part_msdos iso9660 udf configfile search help reiserfs font linux chain search_fs_file
Copy /B %grubDir%\i386-pc\cdboot.img+%BootFileDir%\Core.img %BootFileDir%\G2ldr.bin
COPY /y %BootFileDir%\G2ldr.bin %ISODir%\G2ldr.bin
COPY /y %BootFileDir%\Core.img %ISODir%\grub2\bios\i386-pc\Core.img
ECHO 生成EFI 64啟動文件
start /wait /min %grubDir%\grub-mkimage.exe -d %grubDir%\x86_64-efi -p /grub2/uefi -o %EFIImgDir%\Bootx64.efi -O x86_64-efi search search_fs_file configfile help iso9660 fat part_gpt part_msdos disk exfat ext2 ntfs appleldr hfs normal reiserfs font linux chain
ECHO 生成EFI 32啟動文件
start /wait /min %grubDir%\grub-mkimage.exe -d %grubDir%\i386-efi -p /grub2/uefi -o %EFIImgDir%\Bootia32.efi -O i386-efi search search_fs_file configfile help iso9660 fat part_gpt part_msdos disk exfat ext2 ntfs appleldr hfs normal reiserfs font linux chain
ECHO 生成EFI啟動扇區
start /wait /min %ToolDir%\WinImage\WinImage.exe %ToolDir%\efisys.bin %BootFileDir%\efiImg /i /h /y
COPY /y %ToolDir%\efisys.bin %ISODir%\efisys.bin
IF "%1" neq "NoPause" PAUSE
ECHO 生成ISO鏡像
mkisofs.exe -v -d -N -R -J -boot-info-table -l -relaxed-filenames -gbk4dos-filenames -gbk4win-filenames -no-emul-boot -boot-load-size 4 -o %ISOName% -v -V "%ISOTitle%" -b %biosImg% %efi% %ISODir%
IF "%1" neq "NoPause" PAUSE
unknown filesystem排錯教程
出現這個錯誤,說明文件系統是不支持引導此分區,可以一個一個測試
輸入 ls
出現了(hd0) (hd0,msdos2) (hd0,msdos1)
(hd1)
出現了msdos的都可以引導bios,
嘗試輸入了ls (hd0,msdos2)顯示的是存儲盤的內容,
嘗試輸入(hd0,msdos1)顯示的是uefi分區內容.
而輸入 其他的都顯示未知的系統,也驗證了bios只識別mbr分區表.
于是我把grub移動到了uefi分區 也就是(hd0,msdos1) 然后輸入如下內容
后依次輸入如下內容:
set root=(hd0,msdos1)/boot/grub
set prefix=(hd0,msdos1)/grub
insmod normal
normal
成功加載菜單.
總結
以上是生活随笔為你收集整理的linux efi分区安装grub2,编译UEFI版本Grub2引导多系统文件efi的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 静态时序分析——Data to data
- 下一篇: 病毒分析报告模板