Score:0

Debian12 live "grub-install:error:cannot find EFI directory"

hu flag

I have now installed the Debian 12 live environment on the USB drive and installed my own assembled Linux system (root file system compiled through busybox, grub.cfg created by myself, and Linux kernel compiled by myself) on the hard drive/dev/sda using the live environment. I encountered a problem when executing: sudo grub-install --target=x86_64-efi --root-directory=/mnt --boot-directory=/mnt/boot /dev/sda --force --recheck, the following message is reported:

grub install: error: cannot find EFI directory

I saw a similar question (Ubuntu 18.04 "grub-install: error: cannot find EFI directory"), but after reading its answer, I didn't solve my own problem.

Complete shell script:

#!/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
}
Jaromanda X avatar
ru flag
your command line looks 10 kinds of wrong
ABeginner avatar
hu flag
@Jaromanda X Sorry, I am a beginner. Can you tell me where the problem is?
Jaromanda X avatar
ru flag
1, the command is `grub-install` not `grub` ... 2, `-- target=x86_ When 64 efi` makes zero sense, it should be `--target=x86_64-efi` ... 3, `-- root directory=/mnt` should be `--root-directory=/mnt` ... 4, `-- boot directory=/mnt/boot/dev` should be `--boot-directory=/mnt/boot/dev` ... 5, `-- force` should be `--force` ... 6, `-- recheck` should be `--recheck` ... 7, you haven't specified a `--efi-directory=` ... OK, so that's only 7 mistakes not 10
ABeginner avatar
hu flag
@JaromandaX Sorry, it was copied from another location, so there is a formatting issue. I have made the necessary modifications.
Jaromanda X avatar
ru flag
so, where's the EFI directory?
ABeginner avatar
hu flag
@JaromandaX I missed the creation of the efi directory in my burning script, and I have updated the burning script.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.