目标
在windows下把一个8GB的u盘制作为grub2引导的多启动U盘(可启动wepe,安装centos7,debian10等)
同时支持bios主板和uefi主板
工具准备
a.一个windows 10电脑
b.windows 10电脑安装好virtualbox,安装了一个linux虚拟机
c.一个U盘,这里的容量是8GB
d.一些软件
分区软件diskgenius https://www.diskgenius.cn/download.php
grub-2.04-for-windows https://ftp.gnu.org/gnu/grub/grub-2.04-for-windows.zip
grub4dos http://dl.grub4dos.chenall.net/grub4dos-0.4.6a-2021-05-16.7z
系统镜像
centos7 http://isoredirect.centos.org/centos/7/isos/x86_64/
debian10 https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/
debian10 安装需要的vmlinuz和initrd.gz两个文件
http://http.us.debian.org/debian/dists/Debian10.9/main/installer-amd64/current/images/hd-media/vmlinuz
http://http.us.debian.org/debian/dists/Debian10.9/main/installer-amd64/current/images/hd-media/initrd.gz
ubuntu 16.04 desktop https://releases.ubuntu.com/16.04/
微PE工具箱 http://www.wepe.com.cn/download.html
Gparted https://gparted.org/download.php
步骤简述
给u盘分区和格式化->安装grub2到u盘->放置文件,配置grub2.cfg->测试u盘启动效果
详细步骤
1.u盘分区和格式化
使用分区软件diskgenius将U盘分出来一个区,类型为GPT或者MBR,文件系统选择fat32格式。因为UEFI支持在fat32格式引导,
UEFI不支持ntfs格式的引导。 这里为了保证兼容BIOS和UEFI固件,选择了fat32格式的文件系统。
分区有风险,新手必须谨慎!U盘有数据,必须先备份。分清硬盘和U盘的区别,选择U盘操作,千万不要操作电脑的硬盘。
官方分区帮助网页 https://www.diskgenius.cn/help/index.php?from=dg
下图已经是我做了清除所有分区,新建分区,并且格式化为fat32后的U盘的截图。
可以看到分区成功后,u盘最前方有2048个保留扇区,刚好是1024KB,保证了grub2的成功写入。
2.安装grub2到u盘
下载好grub2-2.04-for-windows ,解压到d盘,例如在这里d:\tools\grub-2.04-for-windows
点击电脑左下角搜索按钮,输入cmd,点击“以管理员身份运行”。
在命令提示符里面输入diskmgmt.msc, 按回车键打开。查看U盘的编号和盘符。
这里的u盘显示磁盘1,说明编号是1。盘符是E。安装grub2的时候会用到,非常重要,请和下面的命令对上。
在准备安装grub2到u盘之前,先打开grub-2.04-for-windows的locale文件夹,把zh_CN文件夹里面的grub.mo复制到
上级的locale文件夹,并且改名为zh_CN.mo。
现在可以安装grub2了。
在命令提示符里面输入 cd /d d:\tools\grub-2.04-for-windows 按回车键后即进入grub2的目录
支持传统bios引导的安装命令如下。 e指的是u盘盘符e,//./PHYSICALDRIVE1指的是磁盘1。此处必须和上面磁盘管理对应。
grub-install.exe --force --no-floppy --target=i386-pc --themes=starfield --boot-directory=e:\boot //./PHYSICALDRIVE1 (请改为自己电脑的实际参数)
支持i386-efi引导的安装命令如下。 e指的是u盘盘符e。
grub-install.exe --force --removable --no-floppy --target=i386-efi --themes=starfield --boot-directory=e:\boot --efi-directory=e:\ (请改为自己电脑的实际参数)
支持x86_64-efi引导的安装命令如下。 e指的是u盘盘符e。
grub-install.exe --force --removable --no-floppy --target=x86_64-efi --themes=starfield --boot-directory=e:\boot --efi-directory=e:\ (请改为自己电脑的实际参数)
为了更好引导windows pe系统,这里也将grub4dos解压到u盘的boot文件夹。grub2把控制权转到grub4dos,grub4dos引导windows pe。提高成功率。
3.放置一些文件到u盘,配置grub.cfg
在u盘根目录的vmlinuz和initrd.gz是安装debian10需要的内核文件
在u盘新建iso文件夹,放置一些光盘镜像文件。
WePE_64_V2.1.iso是微PE工具箱运行后,点击"其他安装方式"里面的“光盘”图标,生成的C盘根目录的ISO文件。
用emeditor编辑器,在u盘的boot\grub文件夹,新建一个grub.cfg文件。
内容如下,其中需要注意的是U盘分区的uuid。
#使用lsblk命令得到u盘的fat32分区的uuid,这里是B267-7191,请改为实际U盘存放grub2的分区的UUID。
set fat32_data_uuid="B267-7191"
参考文章
安装windows版本的virtualbox和使用光盘镜像安装debian10 https://blog.csdn.net/shile/article/details/117201109
在windows下使用virtualbox的debian10虚拟机运行lsblk命令查看u盘的uuid https://blog.csdn.net/shile/article/details/117212778
#环境变量,用于选择菜单保存,注意对应菜单里需加入函数savedefault
if [ -s $prefix/grubenv ]; then
load_env
set default="${saved_entry}"
else
set default=0
fi
function savedefault {
if [ -s $prefix/grubenv ]; then
saved_entry="${chosen}"
save_env saved_entry
fi
}
#Boot automatically after xx secs.
if keystatus --shift; then
set timeout=30
else
set timeout=5
fi
insmod jpeg
insmod png
set theme=${prefix}/themes/starfield/theme.txt
#By default, boot the first entry.
set default=0
#Fallback to the second entry.
set fallback=1
# load_video
insmod gfxterm
insmod all_video
insmod font
if loadfont ${prefix}/fonts/unicode.pf2; then
set gfxmode=auto
set gfxpayload=keep
set gfxterm_font=unicode
set locale_dir=${prefix}/locale
set lang=zh_CN
terminal_output gfxterm
fi
#user and password
insmod password
#set superusers="grub"
#password grub grub
menuentry '进入微PE工具箱WePE_64_V2.1' --unrestricted {
savedefault
set g4d_dir="/boot/grub4dos-0.4.6a"
set cfgfile="find --set-root /iso/WePE_64_V2.1.iso;map --mem /iso/WePE_64_V2.1.iso (hd32);map --hook;chainloader (hd32); rootnoverify (hd32)"
linux16 $g4d_dir/grub.exe --config-file=$cfgfile
initrd16 /iso/WePE_64_V2.1.iso
}
#使用lsblk命令得到u盘的fat32分区的uuid,这里是B267-7191,请改为实际U盘的UUID
set fat32_data_uuid="B267-7191"
search --no-floppy --set=usb_iso_part --fs-uuid $fat32_data_uuid
menuentry '安装CentOS 7' --class gnu-linux --class gnu --class os {
savedefault
set isofile=/iso/CentOS-7-x86_64-Minimal-2009.iso
loopback loop ($usb_iso_part)$isofile
probe -u (loop) --set=loopuuid
linux (loop)/isolinux/vmlinuz iso-scan/filename=$isofile inst.repo=cdrom:UUID=$loopuuid rw rhgb quiet
initrd (loop)/isolinux/initrd.img
}
menuentry '安装Debian 10' --class gnu-linux --class gnu --class os {
savedefault
linux ($usb_iso_part)/vmlinuz
initrd ($usb_iso_part)/initrd.gz
}
menuentry '进入ubuntu 16.04桌面版' --class gnu-linux --class gnu --class os {
savedefault
set isofile=/iso/ubuntu-16.04.7-desktop-amd64.iso
loopback loop ($usb_iso_part)$isofile
linux (loop)/casper/vmlinuz file=/cdrom/preseed/ubuntu.seed boot=casper iso-scan/filename=$isofile quiet splash ---
initrd (loop)/casper/initrd
}
menuentry '安装ubuntu 16.04桌面版' --class gnu-linux --class gnu --class os {
savedefault
set isofile=/iso/ubuntu-16.04.7-desktop-amd64.iso
loopback loop ($usb_iso_part)$isofile
linux (loop)/casper/vmlinuz file=/cdrom/preseed/ubuntu.seed boot=casper iso-scan/filename=$isofile only-ubiquity quiet splash ---
initrd (loop)/casper/initrd
}
menuentry "GParted Live ISO" --class gnu-linux {
savedefault
set isofile="/iso/gparted-live-1.3.0-1-amd64.iso"
loopback loop ($usb_iso_part)$isofile
linux (loop)/live/vmlinuz boot=live config union=overlay username=user noswap noprompt ip=net.ifnames=0 toram=filesystem.squashfs nosplash findiso=${isofile}
initrd (loop)/live/initrd.img
}
menuentry "Reboot" --unrestricted {
reboot
}
menuentry "Shutdown" --unrestricted {
halt
}
保存好grub.cfg后 ,弹出U盘。
4.测试效果
将u盘插入一个电脑,开机进入bios,设置从u盘启动,查看具体效果。
参考资料
参考文章
- https://man7.org/linux/man-pages/man7/dracut.cmdline.7.html
- https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/8/html/performing_an_advanced_rhel_installation/kickstart-and-advanced-boot-options_installing-rhel-as-an-experienced-user
- https://www.cnblogs.com/f-ck-need-u/p/709469html#auto_id_34