GRUB is a program that boots the operating system. Its configuration file can identify a disk partition by either its device name or its Universally Unique Identifier (UUID). This guide explains how to modify the GRUB configuration file to use a UUID to identify a disk partition.
Background information
If you use a device name (such as /dev/vda1) to identify a disk partition in the GRUB configuration file, that device name can change after operations like importing an image to start an instance. If the specified device name no longer points to the correct partition, the kernel might fail to load the file system, which prevents the system from starting.
Procedure
To ensure the system can reliably identify the root file system disk partition, we recommend configuring GRUB to identify the partition by its UUID. Follow these steps.
Modifying the GRUB configuration is a high-risk operation. Incorrect modifications can prevent your instance from starting. Before you proceed, you must created a snapshot for your instance to ensure data security.
Check the GRUB version
The GRUB program has two major versions: GRUB2 and GRUB Legacy. GRUB Legacy is no longer in active development, except for bug fixes. Follow these steps to check your GRUB version.
Use one of the following methods to identify your
GRUBversion.GRUB2
Run the following command to check the structure of your system's
GRUBconfiguration directory.sudo ls /etc/grub.d/If the output includes files such as
00_headerand40_custom, you are usingGRUB2. Example output:00_header 05_debian_theme 10_linux 20_linux_xen 30_os-prober 30_uefi-firmware 40_custom 41_customGRUB Legacy
Run the following command to check the contents of the
/boot/grub/directory.sudo ls /boot/grub/If the
/boot/grubdirectory exists and contains amenu.lstfile, you are usingGRUB Legacy.
Locate the GRUB configuration file
The configuration files for GRUB2 and GRUB Legacy are in different locations.
GRUB2: The configuration file is typically located at/boot/grub/grub.cfgor/boot/grub2/grub.cfg.GRUB Legacy: The configuration file is typically located at/boot/grub/menu.lstor/boot/grub/grub.conf.
You can refer to the preceding example to go to the /boot directory and check the GRUB configuration file.
Get the current disk information
You need to obtain the disk partition information of the current instance to use in the GRUB configuration file.
Run the following command to view the disk partition information for the current instance:
sudo blkidExample output:
/dev/vda1: LABEL="/" UUID="e7e8xxxx-xxxx-xxxx-xxxx-xxxxb9263645" TYPE="ext4" PARTUUID="0efbxxxx-01"In this example, the
UUIDof the/dev/vda1device ise7e8xxxx-xxxx-xxxx-xxxx-xxxxb9263645.
Modify the GRUB configuration file
Run the following command to back up the current
GRUBconfiguration file:sudo cp /boot/grub2/grub.cfg /boot/grub2/grub.cfg.bakImportantTo restore the
GRUBconfiguration file, run the following command:sudo cp -f /boot/grub2/grub.cfg.bak /boot/grub2/grub.cfgRun the following command to open the
GRUBconfiguration file:sudo vim /boot/grub2/grub.cfgPress
ito enter insert mode. In the configuration file, replace all occurrences ofroot=/dev/vda1withroot=UUID=e7e8xxxx-xxxx-xxxx-xxxx-xxxxb9263645.linux /boot/vmlinuz-3.16.0-4-amd64 root=/dev/vda1 ro single debian-installer=en_US net.ifnames=0 vga=792 console=tty0 console=ttyS0,115200n8The modified line:
linux /boot/vmlinuz-3.16.0-4-amd64 root=UUID=e7e8xxxx-xxxx-xxxx-xxxx-xxxxb9263645 ro single debian-installer=en_US net.ifnames=0 vga=792 console=tty0 console=ttyS0,115200n8After you finish editing, press the
Esckey to exit insert mode, and then type:wqto save the file and exit.(Optional) Run the following command to view the modified configuration file. This ensures that all disk partition identifiers in
GRUBnow use theUUIDformat.sudo cat /boot/grub2/grub.cfg
Reference
To learn how to configure the fstab file to automatically mount a data disk, see Automatically mount a data disk using a UUID in /etc/fstab.