AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1285173
Accepted
godfool
godfool
Asked: 2020-10-20 14:45:08 +0800 CST2020-10-20 14:45:08 +0800 CST 2020-10-20 14:45:08 +0800 CST

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

  • 772

我正在尝试做这样的事情:如何将 GRUB 重新安装到 EFI 分区?

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

我的设置

运行带有第二个硬盘驱动器 ( ) 的 Ubuntu 18.04 系统,该硬盘驱动器 ( /dev/sdb) 具有两个分区:根分区 ( /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完成这项工作。

grub2 boot uefi ansible
  • 1 1 个回答
  • 531 Views

1 个回答

  • Voted
  1. Best Answer
    godfool
    2020-10-22T05:35:51+08:002020-10-22T05:35:51+08:00

    我终于让它工作了,如下(最后是完整的 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

    这不包括以后的vmlinuzorinitrd图像(假设一个空的引导分区),因此将它们复制过来:

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

    由于 UUID 不同,我需要更新/mnt/root/boot/grub/grub.cfgand /mnt/root/etc/fstab,并用新 UUID 替换旧 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
    
    • 0

相关问题

  • 如果旧版 grub 安装在引导分区扇区而不是 MBR 中,如何将 grub 更新为 grub2?

  • 更改 Wubi 的启动顺序

  • 如何在 Ubuntu 启动时显示或隐藏启动消息?

  • 如何避免启动时出现“S to Skip”消息?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve