Ejemplo n.º 1
0
def old_grub2_install_efi(target):
    """Configure for EFI.

    First capture the currently booted loader (normally a network device),
    then perform grub installation (adds a new bootloader and adjusts the
    boot order), finally re-adjust the boot order so that the currently booted
    loader is set to boot first in the new order.
    """
    with util.RunInChroot(target) as in_chroot:
        # 显示 启动项详细信息
        stdout, _ = in_chroot(['efibootmgr', '-v'], capture=True)
        currently_booted = get_efibootmgr_value(stdout, 'BootCurrent')
        loaders = get_file_efi_loaders(stdout)
        if currently_booted in loaders:
            loaders.remove(currently_booted)
        for loader in loaders:
            in_chroot(['efibootmgr', '-B', '-b', loader], capture=True)
        in_chroot([
            'grub2-install', '--target=x86_64-efi', '--efi-directory',
            '/boot/efi', '--recheck'
        ])
        stdout, _ = in_chroot(['efibootmgr'], capture=True)
        currently_booted = get_efibootmgr_value(stdout, 'BootCurrent')
        boot_order = get_efibootmgr_value(stdout, 'BootOrder').split(',')
        if currently_booted in boot_order:
            boot_order.remove(currently_booted)
        boot_order = [currently_booted] + boot_order
        new_boot_order = ','.join(boot_order)
        # 设置新的启动顺序
        in_chroot(['efibootmgr', '-o', new_boot_order])
Ejemplo n.º 2
0
def install_efi(target, uefi_path):
    """Install the EFI data from /boot into efi partition."""
    # Create temp mount point for uefi partition.
    tmp_efi = os.path.join(target, 'boot', 'efi_part')
    os.mkdir(tmp_efi)
    util.subp(['mount', uefi_path, tmp_efi])

    # Copy the data over.
    try:
        efi_path = os.path.join(target, 'boot', 'efi')
        if os.path.exists(os.path.join(tmp_efi, 'EFI')):
            shutil.rmtree(os.path.join(tmp_efi, 'EFI'))
        shutil.copytree(os.path.join(efi_path, 'EFI'),
                        os.path.join(tmp_efi, 'EFI'))
    finally:
        # Clean up tmp mount
        util.subp(['umount', tmp_efi])
        os.rmdir(tmp_efi)

    # Mount and do grub install
    util.subp(['mount', uefi_path, efi_path])
    try:
        with util.RunInChroot(target) as in_chroot:
            in_chroot([
                'grub2-install', '--target=x86_64-efi', '--efi-directory',
                '/boot/efi', '--recheck'
            ])
    finally:
        util.subp(['umount', efi_path])
Ejemplo n.º 3
0
def grub_install(target, root):
    """Installs grub onto the root."""
    root_dev = root.split(',')[0] + ')'
    with util.RunInChroot(target) as in_chroot:
        data = '\n'.join([
            'root %s' % root,
            'setup %s' % root_dev,
            'quit',
        ]).encode('utf-8')
        in_chroot(['grub', '--batch'], data=data)
Ejemplo n.º 4
0
def grub2_install_efi(target, uefi_path):
    """Install the EFI data from /boot into efi partition."""
    # Create temp mount point for uefi partition.
    tmp_efi = os.path.join(target, 'boot', 'efi_part')
    os.mkdir(tmp_efi)
    util.subp(['mount', uefi_path, tmp_efi])

    # Copy the data over.
    try:
        efi_path = os.path.join(target, 'boot', 'efi')
        if os.path.exists(os.path.join(tmp_efi, 'EFI')):
            shutil.rmtree(os.path.join(tmp_efi, 'EFI'))
        # bug  does not have a dir  EFI
        #No such file or directory: '/tmp/tmpab9g_us0/target/boot/efi/EFI'
        #os.mkdir(os.path.join(tmp_efi, 'EFI'))
        if os.path.exists(os.path.join(efi_path, 'EFI')):
            shutil.copytree(os.path.join(efi_path, 'EFI'),
                            os.path.join(tmp_efi, 'EFI'))
        else:
            shutil.copytree(os.path.join(target, "yxp"),
                            os.path.join(tmp_efi, 'EFI'))
    finally:
        # Clean up tmp mount
        util.subp(['umount', tmp_efi])
        os.rmdir(tmp_efi)

    # Mount and do grub install
    mount_flag = True
    try:
        util.subp(['mount', uefi_path, efi_path])
    except Exception as e:
        try:
            util.subp(['umount', efi_path])
            util.subp(['mount', uefi_path, efi_path])
        except Exception as e:
            mount_flag = False
            print(e)
            print("cant not mount %s" % uefi_path)

    #  check the  EFI directory
    if not check_efi_dir(target):
        shutil.copytree(os.path.join(target, "yxp"),
                        os.path.join(efi_path, 'EFI'))
        print("check why dont have the file")

    try:
        with util.RunInChroot(target) as in_chroot:
            in_chroot([
                'grub2-install', '--target=x86_64-efi', '--efi-directory',
                '/boot/efi', '--recheck'
            ])
    finally:
        if mount_flag:
            util.subp(['umount', efi_path])
Ejemplo n.º 5
0
 def test_run_in_chroot_with_target(self, m_subp):
     my_stdout = "my output"
     my_stderr = "my stderr"
     cmd = ['echo', 'HI MOM']
     target = "/foo"
     m_subp.return_value = (my_stdout, my_stderr)
     with util.RunInChroot(target) as i:
         out, err = i(cmd)
     self.assertEqual(my_stdout, out)
     self.assertEqual(my_stderr, err)
     m_subp.assert_called_with(cmd, target=target)
Ejemplo n.º 6
0
def get_grub_root(target):
    """Extracts the grub root (hdX,X) from the grub command.

    This is used so the correct root device is used to install
    stage1/stage2 boot loader.

    Note: grub-install normally does all of this for you, but
    since the grub is older, it has an issue with the ISCSI
    target as /dev/sda and cannot enumarate it with the BIOS.
    """
    with util.RunInChroot(target) as in_chroot:
        data = '\n'.join([
            'find /boot/grub/stage1',
            'quit',
        ]).encode('utf-8')
        out, err = in_chroot(['grub', '--batch'], data=data, capture=True)
        regex = re.search('^\s+(\(.+?\))$', out, re.MULTILINE)
        return regex.groups()[0]
Ejemplo n.º 7
0
def grub2_mkconfig(target):
    """Writes the new grub2 config."""
    with util.RunInChroot(target) as in_chroot:
        in_chroot(['grub2-mkconfig', '-o', '/boot/grub2/grub.cfg'])
Ejemplo n.º 8
0
def grub2_install(target, root):
    """Installs grub2 to the root."""
    with util.RunInChroot(target) as in_chroot:
        in_chroot(['grub2-install', '--recheck', root])
Ejemplo n.º 9
0
 def test_run_in_chroot_with_target_slash(self):
     with util.RunInChroot("/") as i:
         out, err = i(['echo', 'HI MOM'], capture=True)
     self.assertEqual('HI MOM\n', out)