#!/bin/sh
# This script is based on /etc/hotplug.d/block/20_disk_by_partlabel script
# but adapted to be run during the preinit phase.
# Create symlink in /dev/disk/by-partlabel/ to link devices label to their /dev/ entry.

do_disk_by_partlabel() {
  mkdir -p /dev/disk/by-partlabel

  # Process blkid output line by line
  for DEVNAME_PATH in /dev/mmcblk[0-9]p*; do
    # Extract device, label, and optional UUID
    PARTLABEL=`blkid --match-tag PARTLABEL -o value "$DEVNAME_PATH" 2> /dev/null || true`

    [ -z "$PARTLABEL" ] && continue

    # Add a symlink for each entry with a valid device and label
    ln -snf "$DEVNAME_PATH" "/dev/disk/by-partlabel/$PARTLABEL"
  done
}

boot_hook_add preinit_main do_disk_by_partlabel
