0

What script is used for creating partition 3, (usbboot), and installing grub in mkusb.

I am trying to answer a question in ASK concerning building a multiboot USB from scratch, but hit a wall with installing grub2 and the boot partition and grub folder.

Most of the answers I can find in Ask concerning installing grub refer to legacy grub not grub2.

I tried looking at the source code of mkusb but all I saw was a haze.

A link to the shellscript would be highly appreciated.

C.S.Cameron
  • 20,590
  • 12
  • 78
  • 126

1 Answers1

1

Short answer

In mkusb I use the shellscript dus-persistent to create the persistent live system. It consists of several functions, and specifically the function grub_n_iso to create partitions and put things into them. (The name is inherited from an old system, where iso files were used.)

Longer answer

If you have already installed mkusb, you can read the file dus-persistent with less or with your favourite text editor.

less /usr/sbin/dus-persistent

If you have not installed it yet, you can read it via the following link,

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

Detailed answer

But I think you want more detailed information, that shows the details, where the third partition usbboot is created and filled with content (grub).

1. From the function grub_n_iso

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

In several versions of Ubuntu when installed, it is difficult to create grub for the 'other' boot mode, particularly to create grub for BIOS mode when booted in UEFI mode. For this reason there is an image grub.img, prepared like described below, that can be used.

When suitable, the partition is created with mkfs.vfat, and it will be labeled usbboot.

label="usbboot"
dosfslabel "$part" "$label"

2. From the function grub_n_iso

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

  • The command grub-install is used (in Ubuntu and Debian). It uses the option --efi-directory for the UEFI bootloader.

  • If the setting 'upefi' (usb-pack-efi) is selected or necessary, it will be extracted.

  • The command grub-install is used again. For obvious reasons it does not use the option --efi-directory for the BIOS bootloader.

3. From the function grub_n_iso

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 --------------------------------------

I do not add more detailed explanations now. Please specify exactly what needs to be explained, if you want more details.

sudodus
  • 47,864
  • Thanks I will try to work through this. Personally I am generally happy to use a mkusb install as a foundation, But I got an urge to try this from "scratch". – C.S.Cameron Oct 27 '19 at 14:07
  • @C.S.Cameron, A convenient alternative might be to make a template for your systems: to use or even extend the image grub.img so that you can use such an image instead of running the whole mkusb and then overwrite most of what it has created. – sudodus Oct 27 '19 at 15:44