summaryrefslogtreecommitdiff
path: root/aports/busybox/persistent-storage
blob: 4b821bc752b6ed3f2e0c69a0bf4793d8ee17c7a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh

symlink_action() {
	case "$ACTION" in
		add) ln -sf "$1" "$2";;
		remove) rm -f "$2";;
	esac
}

: ${SYSFS:=/sys}

# cdrom symlink
case "$MDEV" in
	sr*|xvd*)
		caps="$(cat $SYSFS/block/$MDEV/capability 2>/dev/null)"
		if [ $(( 0x${caps:-0} & 8 )) -gt 0 ]; then
			symlink_action $MDEV cdrom
		fi
esac

# 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

wwid=$(cat $SYSFS/class/block/$MDEV/wwid 2>/dev/null)
: ${wwid:=$(cat $SYSFS/class/block/$MDEV/device/wwid 2>/dev/null)}

if [ -n "$wwid" ]; then
	case "$MDEV" in
		nvme*) symlink_action ../../$MDEV disk/by-id/nvme-${wwid}${partsuffix};;
	esac
	case "$wwid" in
		naa.*) symlink_action ../../$MDEV disk/by-id/wwn-0x${wwid#naa.};;
	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
	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
fi
if [ -n "$PARTUUID" ]; then
	mkdir -p disk/by-partuuid
	symlink_action ../../$MDEV disk/by-partuuid/$PARTUUID
fi

# backwards compatibility with /dev/usbdisk for /dev/sd*
if [ "${MDEV#sd}" != "$MDEV" ]; then
	sysdev=$(readlink $SYSFS/class/block/$MDEV)
	case "$sysdev" in
		*usb[0-9]*)
			# require vfat for devices without partition
			if ! [ -e $SYSFS/block/$MDEV ] || [ TYPE="vfat" ]; then
				symlink_action $MDEV usbdisk
			fi
			;;
	esac
fi