summaryrefslogtreecommitdiffstats
path: root/sd_links
blob: 240d2d7e8e1a321046807166b75aeab0ad0aa12f (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
#!/bin/sh

# NOTE: since mdev -s only provide $MDEV, don't depend on any hotplug vars.

# function to create a link to a disk (sd[a-z])
mklink_disk() {
	local last
	# find the last disk
	last=`ls usb[a-z] 2>/dev/null | sort | tail -n 1 | sed 's/usb\([a-z]\).*/\1/'`
	if [ "$last" ] ; then
		# get next char in alphabet
		next=`echo $last | tr 'abcdeghijklmnopqrstuvwxy' \
				'bcdefghijklmnopqrtuvwxyz'`
	else	
		# its the first
		next="a"
	fi

	DISKLINK=usb$next
	ln -sf $DISK $DISKLINK
}

# function to create a link to a partition (sd[a-z][0-9])
mklink_partition() {
	local num=${MDEV##sd[a-z]}
	
	for i in usb[a-z] ; do
		if [ "`readlink $i 2>/dev/null`" = $DISK ] ; then
			DISKLINK=$i
			break
		fi
	done

	# if there are no disk link then create one.
	[ "$DISKLINK" ] || mklink_disk

	# create the link to the partition
	ln -sf $MDEV $DISKLINK$num
}

# check if there already exist an usb link to this dev.
for i in usb[a-z] usb[a-z][0-9]* ; do
	if [ "`readlink $i 2>/dev/null`" = $MDEV ] ; then
		USBLINK=$i
		break
	fi
done
if [ "$USBLINK" ] ;then
	# hotplug remove action
	[ "$ACTION" = "remove" ] && rm $USBLINK
	
	# the link already exist or is not supposed to exist. We are done.
	exit
fi

# find out if its a disk or a partition
if [ -e /sys/block/$MDEV ] ; then
	TYPE=disk
	DISK=$MDEV
	SYSDEV=`readlink -f /sys/block/$MDEV/device`
elif [ -e /sys/block/*/$MDEV ] ; then
	TYPE=partition
	PARENT=`dirname /sys/block/*/$MDEV`
	DISK=${PARENT##*/}	# basename $PARENT
	SYSDEV=`readlink -f $PARENT/device`
else
	exit
fi

# if /sys device path contains '/usb[0-9]' then we assume its usb
if [ "${SYSDEV##*/usb[0-9]}" != "$SYSDEV" ]; then
	mklink_$TYPE
	exit
fi