我正在安装 grub 来引导系统。为什么我必须在内部和外部都执行grub-install
and ,否则无法进入界面,反而会进入界面?update-grub2
chroot
grub
efi shell
燃烧.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