0

I am installing grub to boot the system. Why do I have to execute grub-install and update-grub2 both internally and externally in chroot, otherwise I won't be able to enter the grub interface and will instead enter the efi shell interface?

enter image description here

Burning.sh:

#!/usr/bin/bash

current_path=$(pwd)

hdstr=/dev/sda
hdstr1=${hdstr}1
hdstr2=${hdstr}2
hdstr3=${hdstr}3

root_path=/mnt
boot_path=${root_path}/boot
grub_path=${boot_path}/grub
efi_path=${boot_path}/efi

lib64_path=${root_path}/lib64
bin_path=${root_path}/bin
lib_path=${root_path}/lib


    
execute_command() {
    local command=$1
    eval "$command"
    local status=$?
    if [ $status -eq 0 ]; then
        echo "run the cmd:$command success"
    else
        echo "failed to run the cmd:$command" 
        exit 1
    fi
}

copy_tool() {
    execute_command "sudo cp ${current_path}/grub-install /sbin/"
    execute_command "sudo chmod 777 /sbin/grub-install"
    execute_command "sudo cp ${current_path}/update-grub /sbin/"
    execute_command "sudo chmod 777 /sbin/update-grub"
    execute_command "sudo cp ${current_path}/update-grub2 /sbin/"
    execute_command "sudo chmod 777 /sbin/update-grub2"
}

change_root() {
    execute_command "sudo mount --rbind /dev  /mnt/dev"
    execute_command "sudo mount --rbind /proc /mnt/proc"
    execute_command "sudo mount --rbind /sys  /mnt/sys"

    execute_command "sudo mkdir -p ${bin_path}"
    execute_command "sudo mkdir -p ${lib_path}"
    execute_command "sudo mkdir -p ${lib64_path}"
    execute_command "sudo cp /lib/x86_64-linux-gnu/libtinfo.so.6 ${lib_path}/"
    execute_command "sudo chmod 777 ${lib_path}/libtinfo.so.6"
    
    execute_command "sudo cp /lib/x86_64-linux-gnu/libdl.so.2 ${lib_path}/"
    execute_command "sudo chmod 777 ${lib_path}/libdl.so.2"
    
    execute_command "sudo cp /lib/x86_64-linux-gnu/libc.so.6 ${lib_path}/"
    
    execute_command "sudo cp /lib64/ld-linux-x86-64.so.2 ${lib_path}/"
    
    
    execute_command "sudo ln -s ${lib_path} ${lib64_path}"
    execute_command "sudo cp ${lib_path}/* ${lib64_path}/ -ra"
    
    execute_command "sudo cp /bin/bash ${bin_path}/"
    
    cat << EOF | sudo chroot ${root_path}
    grub-install --target=x86_64-efi /dev/sda --force --recheck --efi-directory=/boot/efi 
    update-grub2 /dev/sda   
    exit 0
EOF
}

create_fstab() {
    UUID1=$(sudo blkid | grep '^/dev/sda1' | awk -F 'UUID="' '{print $2}' | awk -F '"' '{print $1}')
    UUID2=$(sudo blkid | grep '^/dev/sda2' | awk -F 'UUID="' '{print $2}' | awk -F '"' '{print $1}')
    UUID3=$(sudo blkid | grep '^/dev/sda3' | awk -F 'UUID="' '{print $2}' | awk -F '"' '{print $1}')
    
    devName="\/dev\/sda1"
    UUIDStr="UUID=$UUID1\t"
    sed -i "/${devName}/ s/^/${UUIDStr}/"  /mnt/etc/fstab
    
    devName="\/dev\/sda2"
    UUIDStr="UUID=$UUID2\t"
    sed -i "/${devName}/ s/^/${UUIDStr}/"  /mnt/etc/fstab
    
    devName="\/dev\/sda3"
    UUIDStr="UUID=$UUID3\t"
    sed -i "/${devName}/ s/^/${UUIDStr}/"  /mnt/etc/fstab
}

partition_format_mount() {
    sudo /sbin/parted ${hdstr} <<EOT 1>/dev/null 2>/dev/null || exit 1
    rm 1
    rm 2
    rm 3
    rm 4
    mkpart primary fat32 1MiB 200MiB
    set 1 esp on
    mkpart primary ext4 200MiB 8200MiB
    mkpart primary ext4 8200MiB 100%
    quit
EOT
    echo ""

    execute_command "sudo partx ${hdstr} 1>/dev/null"
    execute_command "sudo /sbin/mkfs.fat -F32 ${hdstr1} 1>/dev/null"
    execute_command "sudo /sbin/mkfs.ext4 ${hdstr2} 1>/dev/null"
    execute_command "sudo /sbin/mkfs.ext4 ${hdstr3} 1>/dev/null"
    
    execute_command "sudo mkdir -p ${root_path}"
    execute_command "sudo mount ${hdstr3} ${root_path} 1>/dev/null"
    execute_command "sudo mkdir ${boot_path} -p"
    execute_command "sudo mount ${hdstr2} ${boot_path} 1>/dev/null"
    execute_command "sudo mkdir ${efi_path} -p"
    execute_command "sudo mount ${hdstr1} ${efi_path} 1>/dev/null"
}

install_os(){
    execute_command "sudo mkdir -p ${grub_path}"
    execute_command "sudo cp ${current_path}/grub.cfg ${grub_path}/grub.cfg"


    execute_command "sudo cp ${current_path}/x86_64-efi  /usr/lib/grub/  -raf"
    execute_command "sudo cp \"${current_path}/bzImage\" ${root_path}"
    execute_command "sudo cp \"${current_path}/initrd\" ${root_path}"
    execute_command "sudo cp \"${current_path}/rootfs.tar.gz\" ${root_path}"
    cd ${root_path}
    execute_command "sudo tar -vxf rootfs.tar.gz"
    execute_command "echo \"y\" | sudo rm rootfs.tar.gz"
     
    change_root
    create_fstab
    sudo grub-install --target=x86_64-efi  /dev/sda --force --recheck --efi-directory=/boot/efi
    sudo update-grub2 /dev/sda
    execute_command "sync" 
}

mainFunc(){
    copy_tool
    partition_format_mount
    install_os    
}

mainFunc

replay oldfred: enter image description here

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
ABeginner
  • 15
  • 5
  • why do you think you have to? what is this script anyway? something you wrote? – Jaromanda X Aug 28 '23 at 10:04
  • @JaromandaX I tested it myself, and it wouldn't work if there was only one grub install.This script was all written by me. – ABeginner Aug 28 '23 at 10:08
  • so, if you did either one alone, it didn't work? that is odd – Jaromanda X Aug 28 '23 at 10:08
  • @JaromandaX Yes, that's my question. Why do i need both? In fact, I think there should be no problem with only the first one, but the test results are problematic. – ABeginner Aug 28 '23 at 10:10
  • probably because your script is massively over-complicated. some of the worst problems are: 1. you're using curly braces around variable as a substitute for double quotes. 2. instead of just using `set -e` and `set -x` to turn on exit-on-error and execution tracing, you've written your own `execute_command` function which uses `eval`. 3. you've use command substitution several times for things where simple assignment would work. 4. if you want to script partitioning a disk, use `sfdisk` or `parted`, not fdisk with a heredoc. – cas Aug 28 '23 at 11:13
  • 5. you run every command with sudo instead of just running the entire script with sudo. 6. what's that weird business with `ls /dev/sda > ./tmp.txt` supposed to be? why not just `hdstr=/dev/sda`? – cas Aug 28 '23 at 11:15
  • BTW, see [$VAR vs ${VAR} and to quote or not to quote](https://unix.stackexchange.com/q/4899) and [Why does my shell script choke on whitespace or other special characters?](https://unix.stackexchange.com/q/131766) – cas Aug 28 '23 at 11:18
  • UEFI chroot, must include ESP - efi system partition http://askubuntu.com/questions/53578/can-i-install-in-uefi-mode-with-the-alternate-installer/57380#57380 chroot with UEFI, LVM, encryption on NVMe drive https://ubuntuforums.org/showthread.php?t=2349833&p=13602088#post13602088 – oldfred Aug 28 '23 at 14:03
  • @cas Thank you very much for your detailed suggestions. I have optimized the script to the best of my ability. – ABeginner Aug 29 '23 at 10:19
  • @oldfred I added partition information to the question. – ABeginner Aug 30 '23 at 07:47
  • In Ubuntu update-grub2 is just a link to update-grub. And update-grub has 3 lines `cat /usr/sbin/update-grub`, primarily `exec grub-mkconfig -o /boot/grub/grub.cfg "$@"` and some distributions do not have update-grub but require you to run the grub-mkconfig line. – oldfred Aug 30 '23 at 13:56

0 Answers0