grub制作USB启动盘
一、使用 GParted 分区U盘:
第一个分区 500M , 并且设置标识 boot esp , fat32格式
第二个分区剩余全部,ntfs格式(可以读写大于4G的文件)
终端输入命令fdisk -l
查看结果:
终端输入命令blkid
查看结果:
二、扇区末尾1M空间使用命令 gdisk
打上标记 bios_grub
,格式化方式——未格式化
wmx@wmx-ubuntu:~$ sudo gdisk /dev/sdb <=========== 1步骤
[sudo] wmx 的密码:
GPT fdisk (gdisk) version 1.0.1
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): n <=========== 2步骤
Partition number (3-128, default 3): <=========== 3步骤 ,回车
First sector (34-30605278, default = 30603264) or {+-}size{KMGTP}: <=========== 回车
Last sector (30603264-30605278, default = 30605278) or {+-}size{KMGTP}: <=========== 回车
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): EF02 <=========== 4步骤
Changed type of partition to 'BIOS boot partition'
Command (? for help): p <=========== 5步骤
Disk /dev/sdb: 30605312 sectors, 14.6 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 961A84D4-79FF-47F1-990A-885ED8AD66AE
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 30605278
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 1026047 500.0 MiB EF00
2 1026048 30603263 14.1 GiB 0700
3 30603264 30605278 1007.5 KiB EF02 BIOS boot partition
Command (? for help): w <=========== 6步骤
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y <=========== 7步骤
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
使用GParted
查看结果
boot esp
标记支持 EFI 启动
bios_grub
标记支持 BIOS 启动
三、安装grub到U盘
1 挂载U盘分区 /dev/sdb1
到 /mnt
:
sudo mount /dev/sdb1 /mnt
2 grub安装到MBR
sudo grub-install --target=i386-pc --recheck --boot-directory=/mnt/boot /dev/sdb
3 grub安装到ESP,特别注意--removable
参数,安装到移动设备上一定要用这个参数
sudo grub-install --target x86_64-efi --efi-directory /mnt --boot-directory=/mnt/boot --removable
特别注意:
- Ubuntu的grub默认的target是i386-pc,这个target包含在grub-pc这个包,如果你的系统使用BIOS+MBR安装,这个包默认是存在的。x86_64-efi这个target包含在grub-efi这个包,只有你的系统使用UEFI+GPT方式安装这个包才会存在。如果某个target报错,错误信息类似于grub-install: error: /usr/lib/grub/x86_64-efi/modinfo.sh doesn’t exist. Please specify --target or --directory. 这样的话,就需要安装grub-pc或grub-efi之后再试。
- 安装到ESP要加–removable这个参数 这个参数专门针对于可移动设备,一定要加
- bios_grub标记的分区 如果按照上面的分区过程操作了,增加了这个标记的分区,安装grub的时候会自动识别这个标记的分区并成功安装grub,否则报错:
Installing for i386-pc platform.
grub-install: warning: this GPT partition label contains no BIOS Boot Partition; embedding won't be possible.
grub-install: warning: 无法嵌入。在此次安装中 GRUB 只能通过使用块列表安装。但是块列表是不可信赖的,不推荐使用。.
grub-install:错误: will not proceed with blocklists.
四、添加grub菜单
sudo grub-mkconfig -o /mnt/boot/grub/grub.cfg
然后基于你自己的电脑和U盘修改 /mnt/boot/grub/grub.cfg
如下是我的示例:
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by grub-mkconfig using templates
# from /etc/grub.d and settings from /etc/default/grub
#
### BEGIN /etc/grub.d/00_header ###
if [ -s $prefix/grubenv ]; then
set have_grubenv=true
load_env
fi
if [ "${next_entry}" ] ; then
set default="${next_entry}"
set next_entry=
save_env next_entry
set boot_once=true
else
set default="0"
fi
if [ x"${feature_menuentry_id}" = xy ]; then
menuentry_id_option="--id"
else
menuentry_id_option=""
fi
export menuentry_id_option
if [ "${prev_saved_entry}" ]; then
set saved_entry="${prev_saved_entry}"
save_env saved_entry
set prev_saved_entry=
save_env prev_saved_entry
set boot_once=true
fi
function savedefault {
if [ -z "${boot_once}" ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
}
###记录错误
function recordfail {
set recordfail=1
if [ -n "${have_grubenv}" ]; then if [ -z "${boot_once}" ]; then save_env recordfail; fi; fi
}
### 加载显示模块
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
}
### 查找字体
if [ x$feature_default_font_path = xy ] ; then
font=unicode
else
insmod part_gpt
insmod ext2
set root='hd0,gpt4'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt4 --hint-efi=hd0,gpt4 --hint-baremetal=ahci0,gpt4 7195511a-452c-42f9-82d1-e6806399e90f
else
search --no-floppy --fs-uuid --set=root 7195511a-452c-42f9-82d1-e6806399e90f
fi
font="/usr/share/grub/unicode.pf2"
fi
### 加载字体
if loadfont $font ; then
set gfxmode=auto
load_video
insmod gfxterm
set locale_dir=$prefix/locale
set lang=zh_CN
insmod gettext
fi
### 输出显示
terminal_output gfxterm
### 设置超时显示
if [ "${recordfail}" = 1 ] ; then
set timeout=30
else
if [ x$feature_timeout_style = xy ] ; then
set timeout_style=menu
set timeout=10
# Fallback normal timeout code in case the timeout_style feature is
# unavailable.
else
set timeout=10
fi
fi
### END /etc/grub.d/00_header ###
### 设置菜单外观
### BEGIN /etc/grub.d/05_debian_theme ###
set menu_color_normal=white/black
set menu_color_highlight=black/light-gray
if background_color 44,0,30,0; then
clear
fi
### END /etc/grub.d/05_debian_theme ###
### BEGIN /etc/grub.d/10_linux ###
###设置分辨率
function gfxmode {
set gfxpayload="${1}"
if [ "${1}" = "keep" ]; then
set vt_handoff=vt.handoff=7
else
set vt_handoff=
fi
}
### 根据配置,导出显示模式
if [ "${recordfail}" != 1 ]; then
if [ -e ${prefix}/gfxblacklist.txt ]; then
if hwmatch ${prefix}/gfxblacklist.txt 3; then
if [ ${match} = 0 ]; then
set linux_gfx_mode=keep
else
set linux_gfx_mode=text
fi
else
set linux_gfx_mode=text
fi
else
set linux_gfx_mode=keep
fi
else
set linux_gfx_mode=text
fi
export linux_gfx_mode
menuentry 'Ubuntu 16.04 boot from usb' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-7195511a-452c-42f9-82d1-e6806399e90f' {
### 记录错误
recordfail
### 加载显示驱动
load_video
### 配置显示模式
gfxmode $linux_gfx_mode
###
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
### 加载文件系统支持
insmod part_gpt
insmod ext2
### 设置 /boot 位置
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 936d36cb-f60a-4f19-a357-ca02dd506de1
else
search --no-floppy --fs-uuid --set=root 936d36cb-f60a-4f19-a357-ca02dd506de1
fi
### 加载内核 ,指定root(根位置 / )
linux /vmlinuz-4.15.0-50-generic root=UUID=7195511a-452c-42f9-82d1-e6806399e90f ro quiet splash $vt_handoff
### 初始化 RAM
initrd /initrd.img-4.15.0-50-generic
}
submenu 'Ubuntu 高级选项' $menuentry_id_option 'gnulinux-advanced-7195511a-452c-42f9-82d1-e6806399e90f' {
menuentry 'Ubuntu,Linux 4.15.0-50-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-50-generic-advanced-7195511a-452c-42f9-82d1-e6806399e90f' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 936d36cb-f60a-4f19-a357-ca02dd506de1
else
search --no-floppy --fs-uuid --set=root 936d36cb-f60a-4f19-a357-ca02dd506de1
fi
echo '载入 Linux 4.15.0-50-generic ...'
linux /vmlinuz-4.15.0-50-generic root=UUID=7195511a-452c-42f9-82d1-e6806399e90f ro quiet splash $vt_handoff
echo '载入初始化内存盘...'
initrd /initrd.img-4.15.0-50-generic
}
menuentry 'Ubuntu, with Linux 4.15.0-50-generic (upstart)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-50-generic-init-upstart-7195511a-452c-42f9-82d1-e6806399e90f' {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 936d36cb-f60a-4f19-a357-ca02dd506de1
else
search --no-floppy --fs-uuid --set=root 936d36cb-f60a-4f19-a357-ca02dd506de1
fi
echo '载入 Linux 4.15.0-50-generic ...'
linux /vmlinuz-4.15.0-50-generic root=UUID=7195511a-452c-42f9-82d1-e6806399e90f ro quiet splash $vt_handoff init=/sbin/upstart
echo '载入初始化内存盘...'
initrd /initrd.img-4.15.0-50-generic
}
menuentry 'Ubuntu, with Linux 4.15.0-50-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.15.0-50-generic-recovery-7195511a-452c-42f9-82d1-e6806399e90f' {
recordfail
load_video
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 936d36cb-f60a-4f19-a357-ca02dd506de1
else
search --no-floppy --fs-uuid --set=root 936d36cb-f60a-4f19-a357-ca02dd506de1
fi
echo '载入 Linux 4.15.0-50-generic ...'
linux /vmlinuz-4.15.0-50-generic root=UUID=7195511a-452c-42f9-82d1-e6806399e90f ro recovery nomodeset
echo '载入初始化内存盘...'
initrd /initrd.img-4.15.0-50-generic
}
}
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_linux_xen ###
### END /etc/grub.d/20_linux_xen ###
### BEGIN /etc/grub.d/20_memtest86+ ###
menuentry 'Memory test (memtest86+)' {
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 936d36cb-f60a-4f19-a357-ca02dd506de1
else
search --no-floppy --fs-uuid --set=root 936d36cb-f60a-4f19-a357-ca02dd506de1
fi
knetbsd /memtest86+.elf
}
menuentry 'Memory test (memtest86+, serial console 115200)' {
insmod part_gpt
insmod ext2
set root='hd0,gpt2'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt2 --hint-efi=hd0,gpt2 --hint-baremetal=ahci0,gpt2 936d36cb-f60a-4f19-a357-ca02dd506de1
else
search --no-floppy --fs-uuid --set=root 936d36cb-f60a-4f19-a357-ca02dd506de1
fi
linux16 /memtest86+.bin console=ttyS0,115200n8
}
### END /etc/grub.d/20_memtest86+ ###
### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###
### BEGIN /etc/grub.d/30_uefi-firmware ###
### END /etc/grub.d/30_uefi-firmware ###
### BEGIN /etc/grub.d/40_custom ###
# This file provides an easy way to add custom menu entries. Simply type the
# menu entries you want to add after this comment. Be careful not to change
# the 'exec tail' line above.
###
menuentry "usb ubuntu.iso" {
recordfail
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
search --no-floppy --fs-uuid --set=root 14993B1124BAFEAA
loopback loop ($root)/ubuntu.iso
echo '载入 vmlinuz ubuntu.iso ...'
linux (loop)/casper/vmlinuz.efi boot=casper iso-scan/filename=($root)/ubuntu.iso noeject noprompt quiet splash locale=zh_CN.UTF-8
echo '载入初始化内存盘...'
initrd (loop)/casper/initrd.lz
}
menuentry "USB Try Ubuntu without installing" {
### 记录错误
recordfail
### 加载显示驱动
load_video
### 配置显示模式
gfxmode $linux_gfx_mode
###
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
### 加载文件系统支持
insmod part_gpt
insmod ext2
search --no-floppy --fs-uuid --set=root 14993B1124BAFEAA
set gfxpayload=keep
linux ($root)/casper/vmlinuz.efi file=/cdrom/preseed/ubuntu.seed boot=casper quiet splash ---
initrd ($root)/casper/initrd.lz
}
###
### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ###
if [ -f ${config_directory}/custom.cfg ]; then
source ${config_directory}/custom.cfg
elif [ -z "${config_directory}" -a -f $prefix/custom.cfg ]; then
source $prefix/custom.cfg;
fi
### END /etc/grub.d/41_custom ###
五、提取ubuntu16.04lts.iso 镜像到 /dev/sdb2
然后重启电脑,选择U盘启动,大功告成