summaryrefslogtreecommitdiff
blob: 5e395b251a8e454150e3b16aa9c469d1b9ff9c23 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/sh
###############################################################################
# Determines the Open Firmware path based on the linux device name
#
# Joseph Jezak <josejx@gentoo.org> Copyright (C) 2010
# Rewrite of OFPath for newer kernels/scsi configurations
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
###############################################################################

### Set this to 1 to turn on debugging messages
DEBUG=0

### Find the device tree
if [ ! -e /proc/device-tree ]; then
	echo 1>&2 "ofpath: Cannot find the device tree!"
	exit 1
fi
DEV_TREE="/proc/device-tree"

### Check if sys is mounted
if ! (grep -q '.* .* sysfs ' /proc/mounts 2> /dev/null) ; then
	echo 1>&2 "ofpath: sysfs must be mounted for ofpath to support this system"
        exit 1
fi

### Get the sysfs mount point
SYS="$(m=`grep '.* .* sysfs ' /proc/mounts | head -n 1` ; echo `d=${m#* };echo ${d%% *}`)"
if [ -z "$SYS" -o ! -d "$SYS" ] ; then
	echo 1>&2 "ofpath: Unable to determine sysfs mountpoint"
	exit 1
fi


### Get the device from the user
### We dereference links to support devices like /dev/cdrom1
DEVICE=$1
if [ -z "$DEVICE" ]; then
	echo 1>&2 "ofpath: No device specified!"
	exit 1
fi
DEVICE=$(readlink -f "$DEVICE")
DEVICE=$(basename $DEVICE)
if [ -z "$DEVICE" ] || [ ! -e "/dev/$DEVICE" ]; then
	echo 1>&2 "ofpath: Invalid device: /dev/$DEVICE"
	exit 1
fi
[ "$DEBUG" = 1 ] && echo "Device is: $DEVICE"

### Get the partition if we have it
case ${DEVICE} in
	sd*) PARTITION="${DEVICE#sd?}" ;;
	### No partition for sr/sg devices
	sr*|sg*) PARTITION="${DEVICE#sr?}" ;;
	hd*) PARTITION="${DEVICE#hd?}" ;;
	*) echo "Unknown device string."; exit 1;;
esac
if [ ! -z "$PARTITION" ] && [ "$DEBUG" = 1 ]; then
	echo "Partition: $PARTITION"
fi

### Get the disk device name
DISK_NAME="${DEVICE%%${PARTITION}}"
[ "$DEBUG" = 1 ] && echo "Disk Name: $DISK_NAME"

### Find the devspec for the controller
DEVPATH=$(cd -P "$SYS/block/${DISK_NAME}/device" && pwd)
if [ -z "$DEVPATH" ]; then
	echo "Unable to determine device path!"
	exit 1
fi
[ "$DEBUG" = 1 ] && echo "Devpath is: $DEVPATH"

### Get the OF Path of the controller
case ${DISK_NAME} in
	sd*|sg*|sr*) CONTROLLER_PATH=$(cat ${DEVPATH}/../../../devspec) ;;
	hd*) CONTROLLER_PATH=$(cat ${DEVPATH}/../../devspec) ;;
	*) CONTROLLER_PATH="" ;;
esac
if [ -z "$CONTROLLER_PATH" ]; then
	echo "Unable to determine controller path!"
	exit 1
fi
[ "$DEBUG" = 1 ] && echo "Controller Path is: $CONTROLLER_PATH"

### Generate the disk number and partition info
case ${DISK_NAME} in
	sd*|sg*|sr*)
		DISK_NO="$(cd ${DEVPATH}/../; pwd)";
		DISK_NO="${DISK_NO##*:}";
		;;
	hd*)
		DISK_NO="$(cd ${DEVPATH}/../; pwd)";
		DISK_NO="${DISK_NO##*ide}";
		;;
	*) echo "Unsupported disk type!"; exit 1 ;;
esac
DISK_NO="disk@${DISK_NO}:"
[ "$DEBUG" = 1 ] && echo "Disk Number: ${DISK_NO}"

### We need to get the controller port path (if it has one)
if [ ! -d "$DEV_TREE/$CONTROLLER_PATH/disk" ] && [ ! -d "$DEV_TREE/$CONTROLLER_PATH/$DISK_NO" ]; then
	### FIXME I don't know if every scsi device uses the host nomenclature
	case ${DISK_NAME} in 
		sd*|sg*|sr*)
			PORT="$(cd ${DEVPATH}/../../; pwd)";
			PORT="${PORT##*host}";
			CTL_PORT="${CONTROLLER_PATH##*/}";
			CTL_PORT="${CTL_PORT%%-root*}";
			PORT="$CTL_PORT@$PORT"
			[ "$DEBUG" = 1 ] && echo "Port: $PORT"
			;;
		*) echo "Unsupported disk type!"; exit 1 ;;
	esac
fi

### Add the partition information if required
if [ ! -z $PARTITION ]; then
	DISK_NO="$DISK_NO$PARTITION"
fi

### Build the OF Path
if [ -z "$PORT" ]; then
	OFPATH="$CONTROLLER_PATH/$DISK_NO"
else
	OFPATH="$CONTROLLER_PATH/${PORT}/$DISK_NO"
fi

### Print out the ofpath
echo $OFPATH