Debian12 live“grub-install:错误:找不到 EFI 目录”

Debian12 live“grub-install:错误:找不到 EFI 目录”

我现在已经在U盘上安装了Debian 12 live环境,并使用live环境在硬盘/dev/sda上安装了自己组装的Linux系统(通过busybox编译出来的根文件系统,自己创建的grub.cfg,自己编译的Linux内核)。在执行时遇到了问题:sudo grub-install --target=x86_64-efi --root-directory=/mnt --boot-directory=/mnt/boot /dev/sda --force --recheck,报如下信息:

grub install: error: cannot find EFI directory

我看到过类似的问题(Ubuntu 18.04“grub-install:错误:找不到 EFI 目录”),但看完它的回答,我并没有解决自己的问题。

完整shell脚本:

#!/usr/bin/bash

current_path=$(pwd)

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}/"
    
    execute_command "sudo chroot ${root_path}"
}


foo(){
    copy_tool
    sudo mkdir -p ${root_path}
    #clear

    ls /dev/sda > ./tmp.txt
    hdstr=`awk 'NR==1 {print}' ./tmp.txt`
    echo "hdstr="${hdstr}
    rm ./tmp.txt

    execute_command "sudo fdisk -l /dev/sda"

    sudo fdisk ${hdstr} <<EOT 1>/dev/null 2>/dev/null || exit 1
    d
    1
    d
    2
    d
    3
    d
    4

    n
    1
    
    +200M
    Y
    n
    2
    
    +8G
    Y
    n
    3
    
    
    Y
    t
    1
    1
    w
EOT
    echo ""
    execute_command "sudo fdisk -l /dev/sda"
    execute_command "sudo partx ${hdstr} 1>/dev/null"
    echo "begin================================================================"
    #execute_command "sudo parted ${hdstr} set 1 boot off"
    #execute_command "sudo parted ${hdstr} set 1 bios_grub on"
    echo "end================================================================"


    hdstr1=`printf "%s1" ${hdstr}`
    echo ${hdstr1}
    execute_command "sudo fdisk -l /dev/sda"
    
    execute_command "sudo /sbin/mkfs.fat -F32 ${hdstr1} 1>/dev/null"
    
    execute_command "sudo fdisk -l /dev/sda"
    hdstr2=`printf "%s2" ${hdstr}`
    echo ${hdstr2}

    execute_command "sudo /sbin/mkfs.ext4 ${hdstr2} 1>/dev/null"


    execute_command "sudo fdisk -l /dev/sda"


    hdstr3=`printf "%s3" ${hdstr}`
    echo ${hdstr3}
    execute_command "sudo /sbin/mkfs.ext4 ${hdstr3} 1>/dev/null"


    execute_command "sudo mkdir ${boot_path} -p"
    execute_command "sudo mkdir ${efi_path} -p"
    execute_command "sudo fdisk -l /dev/sda"

    echo ${hdstr1}
    execute_command "sudo mount ${hdstr1} ${efi_path} 1>/dev/null"
    execute_command "sudo mount ${hdstr2} ${boot_path} 1>/dev/null"
    execute_command "sudo fdisk -l /dev/sda"

    execute_command "sudo mkdir -p ${grub_path}"
    echo "*********************Generate grub.cfg***************************************"
    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 mount ${hdstr3} ${root_path}"
    execute_command "sudo cp \"${current_path}/bzImage\" ${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"
    execute_command "sudo fdisk -l /dev/sda"

    change_root
    execute_command "sudo grub-install --target=x86_64-efi --root-directory=${root_path} --boot-directory=${boot_path} ${hdstr} --force --recheck"
    execute_command "sudo update-grub2 ${hdstr}"
    execute_command "sudo fdisk -l /dev/sda"

    execute_command "sync"      
}

foo

grub.cfg:

#Begin cfg
set default=0
set timeout=4

set root=(hd0,1)

menuentry "GNU/Linux, Test-6.4.0-rt" {
    linux /bzImage rw root=/dev/sda2
}

相关内容