Inserting an USB drive can prevent x86 OpenWrt from booting in grub

If I have an USB drive plugged in when the grub menu displays, my x86 OpenWrt can fail with

error: disk 'hd0,gpt1' not found.

If the USB drive has not been plugged, then OpenWrt boots successfully.

It seems the USB drive assumes hd0 if it's plugged in and OpenWrt always boots from hd0.

The grub entry that boots OpenWrt is the vanilla content:

set params 'OpenWrt'

    linux /boot/vmlinuz root=PARTUUID=[uuid] rootwait    console=tty0 console=ttyS0,115200n8 noinitrd

My machines supports UEFI only, and both a Debian live cd USB drive that support UEFI or an empty USB drive that is formatted as FAT can prevent OpenWrt from booting.

How can I prevent the disk index from being messed up by an inserted USB drive, or make grub always boot from hard disk intelligently?

If anyone else finds this, remove the set root=(hd0,gpt1) line from the grub.cfg file. In general the (hdX,gptX) nomenclature should be avoided as it will cause issues like this. The $root variable seems to be set automatically by grub to the proper value. If you need another partition, you should use search --label <filesystem label> ... to locate fat formatted partitions or search --fs-uuid <partition UUID> ... to locate ext4 partitions.