首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于创建usbboot分区和在mkusb中安装grub的脚本是什么?

用于创建usbboot分区和在mkusb中安装grub的脚本是什么?
EN

Ask Ubuntu用户
提问于 2019-10-27 11:17:16
回答 1查看 603关注 0票数 0

用于创建分区3 (usbboot)和在mkusb.

中安装grub的脚本。

我试图回答一个关于从头开始构建多引导USB的问题,但是在安装grub2、引导分区和grub文件夹时遇到了麻烦。

我在Ask中找到的大多数关于安装grub的答案都涉及到遗留的grub,而不是grub2。

我试着看mkusb的源代码,但我看到的只是一片迷雾。

一个到外壳脚本的链接将被高度赞赏。

EN

回答 1

Ask Ubuntu用户

回答已采纳

发布于 2019-10-27 12:59:20

短答案

在mkusb中,我使用system脚本dus-persistent来创建持久的活动系统。它由几个函数组成,特别是用于创建分区并将内容放入其中的函数grub_n_iso。(该名称是从使用iso文件的旧制度继承的。)

更长的答案

如果您已经拥有安装mkusb,您可以使用less或您最喜欢的文本编辑器读取文件dus-persistent

代码语言:javascript
复制
less /usr/sbin/dus-persistent

如果您还没有安装它,可以通过以下链接读取它,

phillw.net/isos/linux-tools/mkusb/dus-persistent

详细答案

但我认为您需要更详细的信息,其中显示了详细信息,其中创建了第三个分区usbboot并填充了内容(grub)。

1.来自函数grub_n_iso

代码语言:javascript
复制
echo "item 30"
echo "30
# make partitions: preparing $part" >> "$tailfile"

if $grubimg
then
 /bin/echo -e "$inversvid using grub.img: keeping file system of $part: $resetvid "
else
 echo "preparing $part  ------------------------------------------------"
 dd if=/dev/zero of="$part" bs=1024 count=1024 2>&1
 sync
 umount  "$part" 2>&1
 sync
 sleep 1
 mkfs.vfat -v -F 32 "$part"
 echo " "
 sync
 sleep 0.5
fi

在安装Ubuntu的几个版本中,很难为“其他”引导模式创建grub,特别是在UEFI模式下启动时为BIOS模式创建grub。由于这个原因,有一个图像grub.img,准备如下所述,可以使用。

适当时,使用mkfs.vfat创建分区,并将其标记为usbboot

代码语言:javascript
复制
label="usbboot"
dosfslabel "$part" "$label"

2.来自函数grub_n_iso

代码语言:javascript
复制
echo "item 70"
echo "70
# installing bootloaders" >> "$tailfile"

if [ "$distr" == "opensuse" ]
then
 grub_inst=grub2-install
else
 grub_inst=grub-install
fi

if $grubimg
then
 /bin/echo -ne "$inversvid UEFI Bootloader: $resetvid "
 $grub_inst --force --removable --no-floppy --boot-directory="$targ1"/boot \
 --efi-directory="$targ1"/EFI/BOOT "$2" 2>&1
fi

if $biosbl
then
 /bin/echo -ne "$inversvid UEFI Bootloader: $resetvid "
 $grub_inst --force --removable --no-floppy --boot-directory="$targ1"/boot \
 --efi-directory="$targ1"/EFI/BOOT "$2" 2>&1
elif ! test -f "$looper"/boot/grub/grub.cfg
then
 if test -f "$mkusb_sdir"/usb-pack_efi.tar.gz
 then
  if ! $upefi
  then
   echo "$inversvid Select 'upefi' to use 'usb-pack-efi' $resetvid"
  fi
 else
  /bin/echo -e "$inversvid No 'grub.cfg' for the UEFI Bootloader found in the iso file $resetvid
${version% *} needs the package 'usb-pack-efi' with the file
'usb-pack_efi.tar.gz' to make a persistent live system with
this iso file boot in UEFI mode."
  if test -f /usr/bin/apt-get
  then
   echo "You can use the following command lines in a terminal window.

sudo add-apt-repository ppa:mkusb/ppa
sudo apt-get update
sudo apt-get install usb-pack-efi"
  else
   echo "
Use search string 'ubuntu dus-plus' for the tarball 'dus-plus' which contains
'usb-pack-efi'. Be prepared to tweak the system manually afterwards. It may be
difficult to create a persistent live system in your linux distro."
   echo "$separator"
  fi
 fi
 if $grubimg
 then
  /bin/echo -e "$inversvid BIOS Bootloader via 'grub.img': $resetvid "
 else
  /bin/echo -ne "$inversvid BIOS Bootloader: $resetvid "
  $grub_inst --force --removable --no-floppy --boot-directory="$targ1"/boot "$2" 2>&1
 fi
fi
  • 使用命令grub-install (在Ubuntu和Debian中)。它为UEFI引导程序使用了选项--efi-directory
  • 如果设置'upefi‘(Usb Efi)被选中或必要,它将被提取。
  • 再次使用命令grub-install。由于明显的原因,它没有为BIOS引导程序使用选项--efi-directory

3.来自函数grub_n_iso

代码语言:javascript
复制
echo "item 80"
echo "80
# copying files ..." >> "$tailfile"

error=""

# start copy/extract boot files (not upefi) ----------------------------

if ! $upefi && test -f "$looper"/boot/grub/grub.cfg
then

# copy the boot files from the iso file

/bin/echo -e "$inversvid 64-bit bootloader: copy the boot files from the iso file $resetvid"
echo "looper=$looper"
echo "targ1=$targ1"

rsync -Ha --exclude=filesystem.squashfs "$looper/" "$targ1"
symlink_error_comment="rsync:$inversvid  don't worry, $resetvid symlink errors are *expected*
because of the target file system."
/bin/echo -e "$symlink_error_comment"
#if [ "$disk_name_type" == "debian" ] && [ "$release" == "stretch" ]
#then
# mount -o loop "$looper"/boot/grub/efi.img "$loopefi"
# rsync -Ha "$loopefi/efi" "$targ1"
# umount  "$loopefi"
# ls -l "$targ1"|grep ^d    ###
# find "$targ1/efi"         ###
# read -p "after rsync efi" ###
#fi

is_file "$targ1"/boot/grub/grub.cfg
if [ $? -ne 0 ]
then
 echo "'$targ1/boot/grub/grub.cfg': file not found"
 echo "This way to use ${version% *} needs 'grub.cfg' from the source iso file,"
 echo "and it is available in Debian and Ubuntu family *amd64* iso files"
 clear_grn "$pid" "$looper" "$targ1" "$tailfile" "$usbdata"
 exit
fi

# tweak 1 grub.cfg

sed "$targ1"/boot/grub/grub.cfg \
-e '/menuentry/a \ set root=(hd0,4)' \
-e '/linux/s#/#($root)/#' \
-e '/initrd/s#/#($root)/#' \
> "$targ1"/boot/grub/grub.cfg0
sync
menu_entry_1 "$targ1"/boot/grub/grub.cfg0 "$targ1"/boot/grub/grub.cfg
if [ $? -ne 0 ]
then
 error="$error - menu_entry_1: tweaking grub.cfg"
fi
rm "$targ1"/boot/grub/grub.cfg0

else  # ... copy/extract boot files (upefi) ----------------------------

# extract files originally from Andre's zip-file (except grub.cfg grub4dos/ ini/)

if $grubimg
then
 /bin/echo -e "$inversvid Bootloaders via 'grub.img', skipping 'usb-pack_efi': $resetvid "
 rm "$targ1"/boot/grub/grub.cfg
else
 if test -f "$mkusb_sdir"/usb-pack_efi.tar.gz
 then
  echo "using usb-pack_efi.tar.gz"
  tar -xvzf "$mkusb_sdir"/usb-pack_efi.tar.gz --directory "$targ1"
  if [ $? -ne 0 ]
  then
   error="- tar: extracting usb-pack_efi"
  fi
 fi
fi

# tweak 2 grub.cfg

if ! test -f "$targ1"/boot/grub/grub.cfg
then
 < "$mkusb_sdir"/grub.cfg sed '2'q > "$targ1"/boot/grub/grub.cfg  # copy two first lines
 if [ $? -ne 0 ]
 then
  error="$error - sed: copying start of grub.cfg"
 fi
fi
sync
sleep 0.5
cat "$targ1"/boot/grub/grub.cfg

# append the rest of the file (after two first lines)

menu_name

< "$mkusb_sdir"/grub.cfg sed -e '1,2'd \
-e '/menuentry "ubuntu/a \ set root=(hd0,4)' \
-e '/menuentry "memtest/a \  set root=(hd0,3)' \
-e '/loopback/d' -e 's#iso-scan/filename=/ubuntu.iso ##' \
-e 's/loop/$root/' \
-e "s%ubuntu.iso%$menuname%g" \
-e "s/vmlinuz/$vmlinuz/" \
-e "s/casper/$persist/g" \
-e "s/initrd.lz/$initrd/" \
-e '/menuentry/s/"./\U&/' \
>> "$targ1"/boot/grub/grub.cfg

#read -p "at 'append the rest of the file (after two first lines)'
#disk_name_type=$disk_name_type"

if [ "$disk_name_type" == "debian" ] \
|| [ "$disk_name_type" == "torios-debian" ] \
|| [ "$disk_name_type" == "9w-debian-wheezy" ]
then
 sed -i 's/persistent --/persistence --/' "$targ1"/boot/grub/grub.cfg
fi

if [ $? -ne 0 ]
then
 error="$error - sed: appending grub.cfg"
fi

fi  # end copy/extract boot files --------------------------------------

我现在没有补充更详细的解释。如果你想要更多的细节,请具体说明需要解释的内容。

票数 1
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1184161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档