You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.7 KiB
76 lines
2.7 KiB
4 years ago
|
#!/bin/bash
|
||
|
#生成grub iso
|
||
|
#根据论坛http://wuyou.net/forum.php?mod=viewthread&tid=386651
|
||
|
#封装的命令,感谢rkr077大佬分享
|
||
|
|
||
|
|
||
|
if [ $# -ne 3 ];then
|
||
|
echo 参数校验失败
|
||
|
echo '命令用法:'
|
||
|
echo './createiso grub目录 输出iso文件名 回环设备'
|
||
|
echo '例如:./createiso grub test.iso /dev/loop3'
|
||
|
exit
|
||
|
fi
|
||
|
|
||
|
|
||
|
#回环设备
|
||
|
loop=$3
|
||
|
|
||
|
if [ `losetup -a|grep $loop|wc -l` -gt 0 ];then
|
||
|
echo 回环设备$loop已被占用
|
||
|
else
|
||
|
echo '1.生成GRUB2的内核文件(core.img)'
|
||
|
#先使用bootice的GRUB4DOS编辑器编写grub.cfg文件(以UTF-8编码保存),包含以下内容:
|
||
|
config_name=grub.cfg
|
||
|
grub_path=/boot/grub
|
||
|
core_img=core.img
|
||
|
iso_dir=iso
|
||
|
efi_dir=efi
|
||
|
efi_path=$efi_dir/boot
|
||
|
efi_img=cdefi.img
|
||
|
|
||
|
|
||
|
mnt_path=/mnt/image
|
||
|
grub2cd_img=grub2cd.img
|
||
|
bootx64=bootx64.efi
|
||
|
bootia32=bootia32.efi
|
||
|
cat << EOF > $config_name
|
||
|
search.file $grub_path/$config_name root
|
||
|
set prefix=$grub_path
|
||
|
normal
|
||
|
EOF
|
||
|
#然后执行以下命令:
|
||
|
#Legacy启动
|
||
|
grub-mkimage -c $config_name -p $grub_path -o $core_img -O i386-pc iso9660 udf fat exfat ntfs ext2 part_msdos part_gpt search disk biosdisk normal halt reboot
|
||
|
#64位UEFI启动
|
||
|
grub-mkimage -c $config_name -p $grub_path -o $bootx64 -O x86_64-efi iso9660 udf fat exfat ntfs ext2 part_msdos part_gpt search disk normal halt reboot
|
||
|
#32位UEFI启动
|
||
|
grub-mkimage -c $config_name -p $grub_path -o $bootia32 -O i386-efi iso9660 udf fat exfat ntfs ext2 part_msdos part_gpt search disk normal halt reboot
|
||
|
#将GRUB2安装目录下的i386-pc\cdboot.img文件复制到当前目录下,并用copy命令将cdboot.img和$core_img文件合并为$grub2cd_img
|
||
|
cat /usr/lib/grub/i386-pc/cdboot.img $core_img >$grub2cd_img
|
||
|
#2.生成EFI启动镜像
|
||
|
echo '2.生成EFI启动镜像'
|
||
|
mkdir -p $efi_path
|
||
|
cp $bootx64 $bootia32 $efi_path
|
||
|
#采用dd命令生成的镜像文件
|
||
|
dd if=/dev/zero of=$efi_img bs=1 count=1M
|
||
|
mkfs -t vfat $efi_img
|
||
|
losetup $loop $efi_img #映射loop设备
|
||
|
mount $loop $mnt_path #挂载loop设备
|
||
|
cp -r $efi_path $mnt_path
|
||
|
losetup -d $loop #解除loop映射
|
||
|
umount $mnt_path #卸载loop设备
|
||
|
#3.用genisoimage命令生成可启动iso文件
|
||
|
echo '3.用genisoimage命令生成可启动iso文件'
|
||
|
# #在iso文件夹下面新建boot\grub文件夹,将输入参数指定的grub目录拷贝到$iso_dir/$grub_path下
|
||
|
mkdir -p $iso_dir/$grub_path
|
||
|
cp -r $1/* $iso_dir/$grub_path
|
||
|
# #将grub2cd.img和cdefi.img复制到iso文件夹下面。
|
||
|
cp $efi_img $grub2cd_img $iso_dir
|
||
|
# #执行以下命令生成iso文件
|
||
|
genisoimage -J -l -allow-lowercase -R -V GRUB2 -joliet-long -no-emul-boot -boot-load-size 4 -boot-info-table -b $grub2cd_img -eltorito-alt-boot -b $efi_img -no-emul-boot -o $2 $iso_dir
|
||
|
rm -rf $core_img $bootx64 $bootia32 $config_name $grub2cd_img $efi_img $iso_dir $efi_dir
|
||
|
fi
|
||
|
|
||
|
|