s-n-ushakov / rename-efi-entry

A Bash script to rename EFI boot entries
BSD 2-Clause "Simplified" License
84 stars 15 forks source link
bash efi linux uefi

// This document is authored using Asciidoctor: // https://asciidoctor.org/

rename-efi-entry

[.lead] A Bash script to rename EFI boot entries

Background

EFI{nbsp}/ UEFI, boot configurations

EFI{nbsp}/ https://uefi.org/specifications/[UEFI] is a specification that defines a software interface between an operating system and platform firmware. It is intended by hardware manufacturers to replace the legacy BIOS firmware interface.

In particular this specification allows to define OS boot configurations to be loaded{nbsp}/ executed upon system power on. These boot configurations specify disk partitions to boot from, and are typically identified by their text labels that are listed in "Startup"{nbsp}/ "Boot" EFI setup sections. These boot configurations may be optionally selectable upon system startup.

The problem

Operating system installation routines tend to have these boot configuration labels hard-coded and to create them in EFI PROM automatically. So in case a computer has, say, several Ubuntu instances installed, it is also likely to have several identical "ubuntu" boot configuration labels in its EFI menus, causing a kind of confusion.

One might further want to rename boot configurations, so to make them distinguishable, but unfortunately the standard https://github.com/rhboot/efibootmgr[`efibootmgr`] utility has no option for that. It is only possible to delete a boot configuration entry and to re-create it with a new label. Meanwhile, the data that is necessary for re-creating a boot configuration entry is not trivial and requires certain inquiries into EFI configuration and disk partition attributes.

The script

The rename-efi-entry script is designed to help renaming EFI boot configuration entries using efibootmgr utility for entry deletion and re-creation. It automates querying current EFI configuration and bootable partition data, and also shaping command line arguments for efibootmgr.

Usage

sudo ./rename-efi-entry.bash existing_efi_label new_efi_label [bootnum]

// a table with: // - automatic column width, // - 2 columns top-aligned, see https://blog.mrhaki.com/2014/11/awesome-asciidoctor-table-column-and.html // - title with no default numbered caption, see https://github.com/asciidoctor/asciidoctor/issues/1563 [%autowidth,cols=2.<,caption=] .Command line arguments |=== |existing_efi_label |existing EFI entry label to be renamed; + a special value of `''` is recognized as a placeholder for "any label", in particular to be used in cases of missing label

|new_efi_label |the new label value to be assigned

|bootnum |conditionally optional: the bootnum value of the EFI entry to be renamed; + required in case of multiple matching label values, so to avoid ambiguity |===

E.g.:

sudo ./rename-efi-entry ubuntu 'ubuntu 18.04'

or in case of multiple boot entries labeled as 'ubuntu':

sudo ./rename-efi-entry ubuntu 'ubuntu 18.04' 0001

or in case of missing boot entry label in EFI menu:

sudo ./rename-efi-entry '*' 'ubuntu 18.04' 0001

In case of doubt bootnum values may be clarified using efibootmgr --verbose command, see entries like Boot0001* in the first column:

[options="nowrap"] .... $ efibootmgr --verbose BootCurrent: 0001 Timeout: 0 seconds BootOrder: 0002,0001,0017,001B,0000,0016,0019,001A,0018 Boot0000 Windows Boot Manager HD(1,MBR,0x5092863d,0x3cde8c,0x1340)/File(\EFI\Microsoft\Boot\bootmgfw.efi)WINDOWS... Boot0001 ubuntu HD(1,GPT,2ffcc127-f6ce-40f0-9932-d1dfd14e9462,0x800,0x100000)/File(\EFI\ubuntu\shimx64.efi) Boot0002 ubuntu HD(1,GPT,cba13b09-d754-4d31-9719-369fa60928d1,0x800,0x100000)/File(\EFI\ubuntu\shimx64.efi) Boot0010 ... ....

Required bootnum values (e.g. 0001 from Boot0001*) may be further identified by checking corresponding uuid values (e.g. 2ffcc127-f6ce-40f0-9932-d1dfd14e9462) against values provided by sfdisk -d command, e.g.:

[options="nowrap"] .... $ sudo sfdisk -d /dev/sda label: gpt label-id: 0560DAA2-3010-47A2-B083-047C813F0A04 device: /dev/sda unit: sectors first-lba: 34 last-lba: 1953525134

/dev/sda1 : start= 2048, size= 1048576, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=2FFCC127-F6CE-40F0-9932-D1DFD14E9462, name="EFI System Partition" /dev/sda2 : start= 1050624, size= 102400000, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=A06790C2-3818-4F57-84EF-4D1B9FFB417E, name="SSD system" /dev/sda3 : start= 103450624, size= 1850073088, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=67A374C2-081E-477E-945C-78BE129A2044, name="SSD data" ....

Implementation details

What the script does under the hood, is:

Limitations

This script was designed to rename EFI boot entries that are related to Linux. It is likely to ignore the other ones. Still it is https://github.com/FroggMaster/EFIRename/blame/603f93f0a3ef9487896d9b6404a1f07eef4ce53a/README.adoc#L109-L110[reported] to deal with Windows 10 entries successfully too.

For an EFI boot entry to be renamed, the related device needs to be attached{nbsp}/ known to the system currently running.

The following boot device types are recognized and honored (see https://wiki.archlinux.org/index.php/Device_file#Block_device_names[Arch Linux : Device file : Block device names]):

// a table with: // - automatic column width, // - 2 columns [%autowidth,cols=2] |=== |SCSI family |e.g. /dev/sda1

|NVMe |e.g. /dev/nvme0n1p1

|MMC family |e.g. /dev/mmcblk0p1 |===

Important usage notes

Feel free to use, but do it on your risk :)

It may be wise to review the efibootmgr commands before final execution...

Licenses

The project is issued and distributed under the following licenses:

// a collapsible block, still undocumented feature, // see: https://github.com/asciidoctor/asciidoctor/issues/1699#issuecomment-473773056 .More on LITL [%collapsible]