从正在运行的系统在第二个磁盘的 EFI 分区上重新安装 GRUB

傻瓜

我正在尝试执行以下操作:如何将 GRUB 重新安装到 EFI 分区?

但是,我不知道如何运行,chroot因为它是一个自动化过程(使用 Ansible,但这无关紧要;如果可以在 Bash 中编写脚本,那对我有用),所以我在使用grub-install.

我的设置

运行/dev/sdb带有两个分区的第二个硬盘驱动器 ( )的 Ubuntu 18.04 系统/dev/sdb2安装在 中的根分区 ( ),安装在/mnt/root中的 EFI 分区 ( /dev/sdb1) /mnt/root/boot/efi我根据需要将正在运行的系统中的所有内容复制到这两个分区。

然后我尝试运行它来安装 grub 并使第二个硬盘驱动器可启动:

grub-install /dev/sdb1 --efi-directory=/mnt/root/boot/efi --boot-directory=/mnt/root/boot --target=x86_64-efi

我还尝试(另外)重新生成 grub.cfg:

grub-mkconfig -o /mnt/root/boot/grub/grub.cfg

我知道我可能需要弄乱 UUID 并告诉 GRUB 从哪个 HD 启动。目标是取出第二个硬盘驱动器并在另一台机器上自行引导它(因此 GRUB 可能首先知道它是(hd1),但很有可能它会(hd0)在新机器中。

对此有什么想法吗?

编辑:

我认为GRUB实际上安装成功。我被抛出一个grub>提示,可以从第二个硬盘驱动器手动启动。我想这意味着我只需要一个工作grub.cfg并且有可能/etc/fstab完成这项工作。

傻瓜

我终于让它工作了,如下(最后是完整的 Ansible 剧本)。

我将原始根分区复制到新的根分区,不包括一些特殊的挂载和(更重要的)/boot目录内容,所以我从一个干净的石板开始:

rsync -ahPHAXx --delete --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/boot/*,/mnt/*,/media/*,/lost+found/*} / /mnt/root

然后安装grub:

grub-install --efi-directory=/mnt/esp --boot-directory=/mnt/root/boot --removable --target=x86_64-efi

生成 grub 配置:

grub-mkconfig -o /mnt/root/boot/grub/grub.cfg

这不包括稍后使用vmlinuzinitrd图像(假设引导分区为空),因此将它们复制过来:

rsync -ahPHAXx --exclude="*/" --delete /boot/* /mnt/root/boot

由于 UUID 不同,我需要更新/mnt/root/boot/grub/grub.cfg/mnt/root/etc/fstab,并将旧 UUID 的每个实例替换为新实例。

我还需要编辑 ESP grub.cfg (in /mnt/esp/EFI/BOOT/grub.cfg) 来查看hd0而不是hd1.

我还从 中删除了对这些/dev/sdb安装的引用fstab,因此剩下的就是一条用于安装 ESP 的线,以及一条用于安装/.

为我做这件事的整个 Ansible 剧本在这里:

---
- hosts: 
  - deploy
  become: yes
  become_method: sudo
  vars:
    device: /dev/sdb
  tasks:
    - name: Unmount
      mount:
        path: "/mnt/{{ item }}"
        state: unmounted
      with_items:
        - esp
        - root

    - name: Delete partitions
      community.general.parted:
        device: "{{ device }}"
        number: "{{ item }}"
        state: absent
      with_items:
        - 1
        - 2

    - name: Create efi partition
      community.general.parted:
        device: "{{ device }}"
        number: 1
        label: gpt
        fs_type: fat32
        name: EFI System Partition
        flags: [ esp, boot ]
        part_start: "1MiB"
        part_end: "513MiB"
        state: present
      register: part

    - debug:
        var: part

    - name: Create root partition
      community.general.parted:
        device: "{{ device }}"
        number: 2
        name: root
        label: gpt # need to repeat the label here, otherwise the first partition gets overwritten
        part_start: "513MiB"
        part_end: "100%"
        state: present

    - name: Make root partition ext4
      filesystem:
        dev: "{{ device }}2"
        fstype: ext4

    - name: Make efi partition fat32
      shell: "mkfs.fat -F32 {{ device }}1"

    - name: "Get UUID for existing root"
      command: blkid -s UUID -o value /dev/sda2
      register: root_uuid
      changed_when: False

    - name: "Get UUID for existing efi"
      command: blkid -s UUID -o value /dev/sda1
      register: efi_uuid
      changed_when: False

    - name: "Get UUID for new root"
      command: blkid -s UUID -o value {{ device }}2
      register: new_root_uuid
      changed_when: False

    - name: "Get UUID for new efi"
      command: blkid -s UUID -o value {{ device }}1
      register: new_efi_uuid
      changed_when: False

    - debug:
        var: efi_uuid.stdout
    - debug:
        var: new_efi_uuid.stdout
    - debug:
        var: root_uuid.stdout
    - debug:
        var: new_root_uuid.stdout

    - name: Mount root from the other device
      mount:
        path: /mnt/root
        src: "{{ device }}2"
        fstype: ext4
        state: mounted

    - name: Mount efi from the other device
      mount:
        path: /mnt/esp
        src: "{{ device }}1"
        fstype: vfat
        state: mounted

    - name: copy root partition over
      shell: rsync -ahPHAXx --delete --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/boot/*,/mnt/*,/media/*,/lost+found/*} / /mnt/root
      args:
       executable: /bin/bash
       
    - name: copy boot images over
      shell: rsync -ahPHAXx --exclude="*/" --delete /boot/* /mnt/root/boot

    - name: install GRUB to make it bootable
      shell: "grub-install --efi-directory=/mnt/esp --boot-directory=/mnt/root/boot --removable --target=x86_64-efi"

    - name: Edit grub.cfg to point to hd0
      replace:
        path: /mnt/esp/EFI/BOOT/grub.cfg
        regexp: hd1
        replace: hd0

    - name: Generate grub.cfg
      shell: "grub-mkconfig -o /mnt/root/boot/grub/grub.cfg"

    - name: Edit UUID in boot/grub/grub.cfg
      replace:
        path: /mnt/root/boot/grub/grub.cfg
        regexp: "{{ root_uuid.stdout }}"
        replace: "{{ new_root_uuid.stdout }}"

    - name: Edit root UUID in etc/fstab
      replace:
        path: /mnt/root/etc/fstab
        regexp: "{{ root_uuid.stdout }}"
        replace: "{{ new_root_uuid.stdout }}"

    - name: Edit ESP UUID in etc/fstab
      replace:
        path: /mnt/root/etc/fstab
        regexp: "{{ efi_uuid.stdout }}"
        replace: "{{ new_efi_uuid.stdout }}"

    - name: Remove /dev/sdb references from fstab
      lineinfile:
        path: /mnt/root/etc/fstab
        state: absent
        regexp: '^/dev/sdb'

有这些要求:

---
collections:
  - community.general

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将Windows安装到单独的SSD后,BIOS不再在EFI引导分区上显示具有GRUB的其他磁盘

来自分类Dev

调试在QEMU上运行的GRUB2 EFI映像

来自分类Dev

如何修复/安装/重新安装grub?

来自分类Dev

在ubuntu分区中重新安装grub

来自分类Dev

需要重新安装GRUB2,但这需要我引导到不可引导的系统!

来自分类Dev

grub2-install:错误:/ boot / efi看起来不像是在Gentoo全新安装中的EFI分区

来自分类Dev

如何将GRUB重新安装到EFI分区?

来自分类Dev

如何重新安装GRUB2 EFI?

来自分类Dev

如何创建EFI分区并在第二个磁盘上安装grub2

来自分类Dev

无法为Lubuntu安装Grub / EFI

来自分类Dev

重新安装Windows后无法从Live CD重新安装grub

来自分类Dev

GRUB2可以与Windows共享EFI系统分区吗?

来自分类Dev

新主板,旧磁盘:“放弃等待根设备”,放入initramfs;grub重新安装没有帮助

来自分类Dev

两个带有Grub2的EFI磁盘

来自分类Dev

在旧模式下安装GRUB,如何在EFI模式下重新安装?

来自分类Dev

第二个操作系统从grub消失了

来自分类Dev

在使用适当的伪装重新安装后,为什么仍无法在NTFS分区上运行脚本?

来自分类Dev

在Grub上安装失败(只读EFI分区?)

来自分类Dev

如何在第二个磁盘上安装grub2并在第一个磁盘上使用EFI

来自分类Dev

重新安装Windows 7后重新安装grub后无法挂载/ home /分区

来自分类Dev

Windows仅在一个分区上安装后,在Ubuntu 16.04安装上出现Grub错误:“ grub-efi-amd64-signed失败”

来自分类Dev

在复制的系统上重新安装grub

来自分类Dev

grub-install:错误:找不到EFI目录。(使用双硬盘驱动器重新安装Grub Boot Loader Windows 10 / Kali Linux Dual Boot)

来自分类Dev

安全删除第二个 EFI 分区 - Windows 10

来自分类Dev

ubuntu 安装在分区上;想在同一个分区上重新安装

来自分类Dev

重新安装Ubuntu时没有找到EFI系统分区

来自分类Dev

移动引导加载程序或删除第二个驱动器中的 efi 分区

来自分类Dev

系统无法启动(grub:“没有这样的设备:<GUID>”+ 没有可启动的分区)在尝试将 Ubuntu 重新安装到同一分区时按后退

来自分类Dev

Winforms 上的 App.Config 正在重新安装

Related 相关文章

  1. 1

    将Windows安装到单独的SSD后,BIOS不再在EFI引导分区上显示具有GRUB的其他磁盘

  2. 2

    调试在QEMU上运行的GRUB2 EFI映像

  3. 3

    如何修复/安装/重新安装grub?

  4. 4

    在ubuntu分区中重新安装grub

  5. 5

    需要重新安装GRUB2,但这需要我引导到不可引导的系统!

  6. 6

    grub2-install:错误:/ boot / efi看起来不像是在Gentoo全新安装中的EFI分区

  7. 7

    如何将GRUB重新安装到EFI分区?

  8. 8

    如何重新安装GRUB2 EFI?

  9. 9

    如何创建EFI分区并在第二个磁盘上安装grub2

  10. 10

    无法为Lubuntu安装Grub / EFI

  11. 11

    重新安装Windows后无法从Live CD重新安装grub

  12. 12

    GRUB2可以与Windows共享EFI系统分区吗?

  13. 13

    新主板,旧磁盘:“放弃等待根设备”,放入initramfs;grub重新安装没有帮助

  14. 14

    两个带有Grub2的EFI磁盘

  15. 15

    在旧模式下安装GRUB,如何在EFI模式下重新安装?

  16. 16

    第二个操作系统从grub消失了

  17. 17

    在使用适当的伪装重新安装后,为什么仍无法在NTFS分区上运行脚本?

  18. 18

    在Grub上安装失败(只读EFI分区?)

  19. 19

    如何在第二个磁盘上安装grub2并在第一个磁盘上使用EFI

  20. 20

    重新安装Windows 7后重新安装grub后无法挂载/ home /分区

  21. 21

    Windows仅在一个分区上安装后,在Ubuntu 16.04安装上出现Grub错误:“ grub-efi-amd64-signed失败”

  22. 22

    在复制的系统上重新安装grub

  23. 23

    grub-install:错误:找不到EFI目录。(使用双硬盘驱动器重新安装Grub Boot Loader Windows 10 / Kali Linux Dual Boot)

  24. 24

    安全删除第二个 EFI 分区 - Windows 10

  25. 25

    ubuntu 安装在分区上;想在同一个分区上重新安装

  26. 26

    重新安装Ubuntu时没有找到EFI系统分区

  27. 27

    移动引导加载程序或删除第二个驱动器中的 efi 分区

  28. 28

    系统无法启动(grub:“没有这样的设备:<GUID>”+ 没有可启动的分区)在尝试将 Ubuntu 重新安装到同一分区时按后退

  29. 29

    Winforms 上的 App.Config 正在重新安装

热门标签

归档