commit e58f7e17c9313085125a565ab1bcc101f47bf061
parent 71eacb8066f57296f8ee1300205aac1502243e35
Author: Natanael Copa <ncopa@alpinelinux.org>
Date: Tue, 1 Nov 2022 09:51:41 +0000
Merge branch 'add-dev-block-by-label-by-partlabel' into 'master'
Add /dev/block, /dev/disk/by-label, /dev/disk/by-partlabel symlinks
See merge request alpine/mdev-conf!2
Diffstat:
2 files changed, 135 insertions(+), 33 deletions(-)
diff --git a/persistent-storage b/persistent-storage
@@ -7,6 +7,20 @@ symlink_action() {
esac
}
+sanitise_file() {
+ sed -E -e 's/^\s+//' -e 's/\s+$//' -e 's/ /_/g' "$@" 2>/dev/null
+}
+
+sanitise_string() {
+ echo "$@" | sanitise_file
+}
+
+blkid_encode_string() {
+ # Rewrites string similar to libblk's blkid_encode_string
+ # function which is used by udev/eudev.
+ echo "$@" | sed -e 's| |\\x20|g'
+}
+
: ${SYSFS:=/sys}
# cdrom symlink
@@ -18,16 +32,41 @@ case "$MDEV" in
fi
esac
+
+# /dev/block symlinks
+mkdir -p block
+if [ -f "$SYSFS/class/block/$MDEV/dev" ]; then
+ maj_min=$(sanitise_file "$SYSFS/class/block/$MDEV/dev")
+ symlink_action ../$MDEV block/${maj_min}
+fi
+
+
# by-id symlinks
mkdir -p disk/by-id
-partition=$(cat $SYSFS/class/block/$MDEV/partition 2>/dev/null)
-case "$partition" in
- [0-9]*) partsuffix="-part$partition";;
-esac
+if [ -f "$SYSFS/class/block/$MDEV/partition" ]; then
+ # This is a partition of a device, find out its parent device
+ _parent_dev="$(basename $(${SBINDIR:-/usr/bin}/readlink -f "$SYSFS/class/block/$MDEV/.."))"
+
+ partition=$(cat $SYSFS/class/block/$MDEV/partition 2>/dev/null)
+ case "$partition" in
+ [0-9]*) partsuffix="-part$partition";;
+ esac
+ # Get name, model, serial, wwid from parent device of the partition
+ _check_dev="$_parent_dev"
+else
+ _check_dev="$MDEV"
+fi
+
+model=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/model")
+name=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/name")
+serial=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/serial")
+wwid=$(sanitise_file "$SYSFS/class/block/$_check_dev/wwid")
+: ${wwid:=$(sanitise_file "$SYSFS/class/block/$_check_dev/device/wwid")}
-wwid=$(cat $SYSFS/class/block/$MDEV/wwid 2>/dev/null)
-: ${wwid:=$(cat $SYSFS/class/block/$MDEV/device/wwid 2>/dev/null)}
+# Sets variables LABEL, PARTLABEL, PARTUUID, TYPE, UUID depending on
+# blkid output (busybox blkid will not provide PARTLABEL or PARTUUID)
+eval $(blkid /dev/$MDEV | cut -d: -f2-)
if [ -n "$wwid" ]; then
case "$MDEV" in
@@ -38,34 +77,41 @@ if [ -n "$wwid" ]; then
esac
fi
-serial=$(sed -E -e 's/^\s+//' -e 's/\s+$//' -e 's/ /_/g' \
- $SYSFS/class/block/$MDEV/device/serial 2>/dev/null)
-
-model=$(sed -E -e 's/^\s+//' -e 's/\s+$//' -e 's/ /_/g' \
- $SYSFS/class/block/$MDEV/device/model 2>/dev/null)
-
-if [ -n "$serial" ] && [ -n "$model" ]; then
- case "$MDEV" in
- nvme*) symlink_action ../../$MDEV disk/by-id/nvme-${model}_${serial}${partsuffix};;
- esac
-fi
-
-# virtio-blk
if [ -n "$serial" ]; then
+ if [ -n "$model" ]; then
+ case "$MDEV" in
+ nvme*) symlink_action ../../$MDEV disk/by-id/nvme-${model}_${serial}${partsuffix};;
+ sd*) symlink_action ../../$MDEV disk/by-id/ata-${model}_${serial}${partsuffix};;
+ esac
+ fi
+ if [ -n "$name" ]; then
+ case "$MDEV" in
+ mmcblk*) symlink_action ../../$MDEV disk/by-id/mmc-${name}_${serial}${partsuffix};;
+ esac
+ fi
+
+ # virtio-blk
case "$MDEV" in
vd*) symlink_action ../../$MDEV disk/by-id/virtio-${serial}${partsuffix};;
esac
fi
-# by-uuid, by-partuuid
-eval $(blkid /dev/$MDEV | cut -d: -f2-)
-if [ -n "$UUID" ]; then
- mkdir -p disk/by-uuid
- symlink_action ../../$MDEV disk/by-uuid/$UUID
+# by-label, by-partlabel, by-partuuid, by-uuid symlinks
+if [ -n "$LABEL" ]; then
+ mkdir -p disk/by-label
+ symlink_action ../../$MDEV disk/by-label/"$(blkid_encode_string "$LABEL")"
+fi
+if [ -n "$PARTLABEL" ]; then
+ mkdir -p disk/by-partlabel
+ symlink_action ../../$MDEV disk/by-partlabel/"$(blkid_encode_string "$PARTLABEL")"
fi
if [ -n "$PARTUUID" ]; then
mkdir -p disk/by-partuuid
- symlink_action ../../$MDEV disk/by-partuuid/$PARTUUID
+ symlink_action ../../$MDEV disk/by-partuuid/"$PARTUUID"
+fi
+if [ -n "$UUID" ]; then
+ mkdir -p disk/by-uuid
+ symlink_action ../../$MDEV disk/by-uuid/"$UUID"
fi
# backwards compatibility with /dev/usbdisk for /dev/sd*
diff --git a/tests/persistent-storage_test b/tests/persistent-storage_test
@@ -7,8 +7,11 @@ init_tests \
persistent_storage_sda_wwid \
persistent_storage_nvme_model_serial \
persistent_storage_virtio_serial \
- persistent_storage_by_uuid \
+ persistent_storage_block \
+ persistent_storage_by_label \
+ persistent_storage_by_partlabel \
persistent_storage_by_partuuid \
+ persistent_storage_by_uuid \
persistent_storage_usbdisk_symlink \
persistent_storage_usbdisk_partition_symlink
@@ -83,25 +86,57 @@ persistent_storage_virtio_serial_body() {
readlink disk/by-id/virtio-foobar
}
-persistent_storage_by_uuid_body() {
- local fs_uuid="2022-05-15-22-31-31-00"
+persistent_storage_block_body() {
+ mkdir -p sys/class/block/sda
+ echo '8:0' > sys/class/block/sda/dev
+
+ MDEV=sda ACTION=add atf_check \
+ sh $mdevscript
+ atf_check -o match:"sda" \
+ readlink block/8:0
+}
+
+persistent_storage_by_label_body() {
+ local fs_label="EFI System"
+ local fs_encoded_label="$(echo $fs_label | sed -e 's| |\\x20|g')"
cat <<-EOF > blkid
#!/bin/sh
# blkid mock
case \$1 in
- /dev/sr0)
- echo "/dev/sr0: BLOCK_SIZE=\"2048\" UUID=\"$fs_uuid\" LABEL=\"cidata\" TYPE=\"iso9660\""
+ /dev/sda1)
+ echo "/dev/sda1: UUID=\"00283cf5-01\" LABEL=\"$fs_encoded_label\" TYPE=\"ext4\""
;;
esac
EOF
chmod +x blkid
export PATH="$PWD:$PATH"
- MDEV=sr0 ACTION=add atf_check \
+ MDEV=sda1 ACTION=add atf_check \
sh $mdevscript
- atf_check -o match:"sr0" \
- readlink disk/by-uuid/"$fs_uuid"
+ atf_check -o match:"sda1" \
+ readlink disk/by-label/"$fs_encoded_label"
+}
+
+persistent_storage_by_partlabel_body() {
+ local partlabel="LUKS"
+
+ cat <<-EOF > blkid
+ #!/bin/sh
+ # blkid mock
+ case \$1 in
+ /dev/sda1)
+ echo "/dev/sda1: UUID=\"00283cf5-01\" LABEL=\"testing\" PARTLABEL=\"$partlabel\" TYPE=\"ext4\""
+ ;;
+ esac
+ EOF
+ chmod +x blkid
+ export PATH="$PWD:$PATH"
+
+ MDEV=sda1 ACTION=add atf_check \
+ sh $mdevscript
+ atf_check -o match:"sda1" \
+ readlink disk/by-partlabel/"$partlabel"
}
persistent_storage_by_partuuid_body() {
@@ -126,6 +161,27 @@ persistent_storage_by_partuuid_body() {
readlink disk/by-partuuid/"$partuuid"
}
+persistent_storage_by_uuid_body() {
+ local fs_uuid="2022-05-15-22-31-31-00"
+
+ cat <<-EOF > blkid
+ #!/bin/sh
+ # blkid mock
+ case \$1 in
+ /dev/sr0)
+ echo "/dev/sr0: BLOCK_SIZE=\"2048\" UUID=\"$fs_uuid\" LABEL=\"cidata\" TYPE=\"iso9660\""
+ ;;
+ esac
+ EOF
+ chmod +x blkid
+ export PATH="$PWD:$PATH"
+
+ MDEV=sr0 ACTION=add atf_check \
+ sh $mdevscript
+ atf_check -o match:"sr0" \
+ readlink disk/by-uuid/"$fs_uuid"
+}
+
persistent_storage_usbdisk_symlink_body() {
mkdir -p sys/class/block sys/block bin
ln -s ../../devices/pci0000:00/0000:00:14.0/usb2/2-10/2-10:1.0/host4/target4:0:0/4:0:0:0 sys/class/block/sdc