tencent cloud

Feedback

Setting the GRUB File Disk Identification Method to UUID

Last updated: 2024-01-08 09:37:00

    Operation Scenario

    To ensure the Linux system can correctly identify the disk when launching the file system, please inspect and correctly set the GRUB file disk identification method.
    The GRand Unified Bootloader (GRUB) serves as a bootloader for initiating the operating system. GRUB permits the utilization of device names (for instance, /dev/vda1, /dev/vdb1 and so forth) to identify disk partitions. However, these device names may change due to the change in the actual operating environment after an image is imported. To guarantee the correct booting of the system even when the device name changes, you can modify the disk identification method in the GRUB file to the Universally Unique Identifier (UUID).

    Setting the GRUB File Disk Identification Method to UUID

    Confirming the GRUB File Path

    There are two common versions of GRUB: GRUB (GRUB Legacy) and GRUB2. The configuration files for GRUB and GRUB2 are located in different paths.
    For GRUB, the configuration file is typically located in /boot/grub/menu.lst or /boot/grub/grub.conf.
    For GRUB2, the configuration file is commonly located in /boot/grub/grub.cfg or /boot/grub2/grub.cfg.
    If you find the menu.lst or grub.conf file in the /boot/grub directory, you are probably using GRUB (GRUB Legacy). If you find the grub.cfg file in the /boot/grub or /boot/grub2 directory, you are probably using GRUB2.

    Obtaining the UUID

    To obtain the UUID of a partition, the blkid command can be used. Running the blkid command will display the detailed information of all the available partitions, including the UUIDs. Run the following command in the terminal:
    sudo blkid
    The output similar to the following one indicates that the associated UUID of the device /dev/vda1 is c0b9ecd8-f922-4e5d-bccb-83fbc94ad23b.
    /dev/vda1: UUID="c0b9ecd8-f922-4e5d-bccb-83fbc94ad23b" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="bcfcb5cb-01"

    Modifying the GRUB configuration file

    This segment highlights an example where the modification of the GRUB2 configuration file located in the /boot/grub/grub.cfg directory is made. If you are using GRUB, or if the GRUB2 configuration file for distribution is located in the /boot/grub2/grub.cfg directory, you can adjust the configuration according to the actual situation.
    1. Back up the current /boot/grub/grub.cfg file to the /home directory.
    sudo cp /boot/grub/grub.cfg /home
    2. Use the vi editor to open the /boot/grub/grub.cfg file and confirm the root partition marked in the configuration file. In this case, the root partition is located on the /dev/vda1 device.
    sudo vi /boot/grub/grub.cfg
    # /boot/grub/grub.cfg
    ...
    echo 'Loading Linux 6.1.0-13-amd64 ...'
    linux /boot/vmlinuz-6.1.0-13-amd64 root=/dev/vda1 ro
    echo 'Loading initial ramdisk ...'
    ...
    3. Edit the configuration starting with a device name in the grub.cfg file, and change the root=/dev/vda1 device name to the root=UUID=xxx format. The content after root=UUID= is the UUID value corresponding to the device returned by running the blkid command. This configuration may appear for multiple times in the grub.cfg file. The modifcation is required for each configuration.
    # Before modification
    ...
    echo 'Loading Linux 6.1.0-13-amd64 ...'
    linux /boot/vmlinuz-6.1.0-13-amd64 root=/dev/vda1 ro
    echo 'Loading initial ramdisk ...'
    ...
    # After modification
    ...
    echo 'Loading Linux 6.1.0-13-amd64 ...'
    linux /boot/vmlinuz-6.1.0-13-amd64 root=UUID=c0b9ecd8-f922-4e5d-bccb-83fbc94ad23b ro
    echo 'Loading initial ramdisk ...'
    ...
    4. Press Esc to enter :wq. Press Enter to save the configuration and exit the editor.
    5. (Optional) Run the following command to ensure the modification has been successfully saved.
    sudo cat /boot/grub/grub.cfg
    ...
    linux /boot/vmlinuz-6.1.0-13-amd64 root=UUID=c0b9ecd8-f922-4e5d-bccb-83fbc94ad23b ro
    ...
    6. (Optional) Delete the grub.cfg backup file in the /home directory.
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support