Unlike UUID, label may contain spaces. AFAICT your code fails with spaces

Le dim. 10 sept. 2023, 11:31, darkpenguin <darkpeng...@posteo.de> a écrit :

> Specifying the boot device by its label rather than its UUID can be
> pretty useful in various situations (e.g. multiple test VMs).
>
> This might have to be adapted a little to meet the coding standards I'm
> not familiar with. Please feel free to improve it in any way you want.
>
> This patch works for me on an older version of grub2 (=2.06-13 in Debian
> Bookworm), but it's rather trivial, and only touches two parts that are
> otherwise untouched for ages.
>
> - Specify "GRUB_DISABLE_LINUX_UUID=LABEL" in /etc/default/grub
> - It will attempt to use a label instead of UUID
> - If a device with the necessary label is not found in
> /dev/disk/by-label/ , it falls back to using UUID
> - If you just assigned new labels, run 'partprobe' to populate
> /dev/disk/by-label/ properly
>
> The decision to reuse GRUB_DISABLE_LINUX_UUID was because:
> 1) This is more of an addition on top of UUID rather than "disabling"
> it, it still uses UUID internally, and it falls back to UUID
> 2) I could not come up with a better way to do it
> 3) I could not figure out how to source other variables from
> /etc/defaults/grub and why not all of them are there. :)
>
>
> ---
> diff --git a/util/grub-mkconfig_lib.in b/util/grub-mkconfig_lib.in
> index 0895328..bd43bc0 100644
> --- a/util/grub-mkconfig_lib.in
> +++ b/util/grub-mkconfig_lib.in
> @@ -158,14 +158,22 @@ prepare_grub_to_access_device ()
>    fi
>    if [ "x${GRUB_DISABLE_UUID}" != "xtrue" ] &&
> fs_uuid="`"${grub_probe}" --device $@ --target=fs_uuid 2> /dev/null`" ;
> then
>      hints="`"${grub_probe}" --device $@ --target=hints_string 2>
> /dev/null`" || hints=
> +
> +    if [ "x${GRUB_DISABLE_LINUX_UUID}" = "xLABEL" ] \
> +        && [ -b "/dev/disk/by-label/$(blkid -l -t UUID=${fs_uuid} -s
> LABEL -o value)" ]; then
> +      device="--label $(blkid -l -t UUID=${fs_uuid} -s LABEL -o value)"
> +    else
> +      device="--fs-uuid ${fs_uuid}"
> +    fi
> +
>      if [ "x$hints" != x ]; then
>        echo "if [ x\$feature_platform_search_hint = xy ]; then"
> -      echo "  search --no-floppy --fs-uuid --set=root ${hints} ${fs_uuid}"
> +      echo "  search --no-floppy --set=root ${hints} ${device}"
>        echo "else"
> -      echo "  search --no-floppy --fs-uuid --set=root ${fs_uuid}"
> +      echo "  search --no-floppy --set=root ${device}"
>        echo "fi"
>      else
> -      echo "search --no-floppy --fs-uuid --set=root ${fs_uuid}"
> +      echo "search --no-floppy --set=root ${device}"
>      fi
>    fi
>    IFS="$old_ifs"
> diff --git a/util/grub.d/10_linux.in b/util/grub.d/10_linux.in
> index cc393be..d29a004 100644
> --- a/util/grub.d/10_linux.in
> +++ b/util/grub.d/10_linux.in
> @@ -61,6 +61,9 @@ if ( [ "x${GRUB_DEVICE_UUID}" = "x" ] && [
> "x${GRUB_DEVICE_PARTUUID}" = "x" ] )
>  elif [ "x${GRUB_DEVICE_UUID}" = "x" ] \
>      || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ]; then
>    LINUX_ROOT_DEVICE=PARTUUID=${GRUB_DEVICE_PARTUUID}
> +elif [ "x${GRUB_DISABLE_LINUX_UUID}" = "xLABEL" ] \
> +    && [ -b "/dev/disk/by-label/$(blkid -l -t UUID=${GRUB_DEVICE_UUID}
> -s LABEL -o value)" ]; then
> +  LINUX_ROOT_DEVICE=LABEL="$(blkid -l -t UUID=${GRUB_DEVICE_UUID} -s
> LABEL -o value)"
>  else
>    LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
>  fi
>
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
>
_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to