forked from mdevctl/mdevctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdevctl.libexec
executable file
·214 lines (187 loc) · 5.61 KB
/
mdevctl.libexec
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/bash
persist_base=/etc/mdevctl.d
mdev_base=/sys/bus/mdev/devices
parent_base=/sys/class/mdev_bus
defaults=/etc/mdevctl.conf
declare -A config
config=([start]="auto")
read_config() {
if [ -s ${1} ]; then
while read line; do
if echo $line | grep -F = &>/dev/null; then
varname=$(echo "$line" | cut -d '=' -f 1)
config[$varname]=$(echo "$line" | cut -d '=' -f 2-)
fi
done < ${1}
fi
}
read_config $defaults
valid_uuid () {
if [[ $1 =~ ^\{?[A-F0-9a-f]{8}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{4}-[A-F0-9a-f]{12}\}?$ ]]; then
echo $1
fi
}
# The mdev device shows up before the link back to the type, wait for it
wait_for_mdev_type () {
for i in $(seq 1 10); do
if [ -L $mdev_base/$1/mdev_type ]; then
break;
fi
sleep 0.5
done
if [ -L $mdev_base/$1/mdev_type ]; then
readlink -f $mdev_base/$1/mdev_type
fi
}
# udev can trigger start-mdev when the parent appears, give it some time
# to register with mdev
wait_for_supported_types () {
for i in $(seq 1 10); do
if [ -d $parent_base/$1/mdev_supported_types ]; then
break;
fi
sleep 0.5
done
if [ -d $parent_base/$1/mdev_supported_types ]; then
echo "$parent_base/$1/mdev_supported_types"
fi
}
case ${1} in
start-mdev|stop-mdev)
if [ $# -ne 2 ]; then
echo "Usage: $0 $1 <mdev UUID>" >&2
exit 1
fi
cmd=$1
uuid=$2
;;
add-mdev)
if [ $# -ne 2 ]; then
echo "Usage: $0 $1 <mdev UUID>" >&2
exit 1
fi
cmd=$1
uuid=$2
;;
get-mdevs)
if [ $# -ne 2 ]; then
echo "Usage: $0 $1 <parent device>" >&2
exit 1
fi
cmd=$1
parent=$2
;;
*)
echo "Unknown option $1" >&2
exit 1
esac
case ${cmd} in
stop-mdev)
if [ -z "$(valid_uuid $uuid)" ]; then
echo "Invalid UUID $uuid" >&2
exit 1
fi
if [ ! -L $mdev_base/$uuid ]; then
echo "No device named $uuid found" >&2
exit 1
fi
echo 1 > $mdev_base/$uuid/remove
if [ $? -ne 0 ]; then
echo "Error removin device $uuid" >&2
exit 1
fi
;;
start-mdev)
if [ -z "$(valid_uuid $uuid)" ]; then
echo "Invalid UUID $uuid" >&2
exit 1
fi
if [ -L $mdev_base/$uuid ]; then
echo "Device $uuid already exists" >&2
exit 1
fi
file=$(find $persist_base -name $uuid -type f)
if [ -z "$file" ]; then
echo "No configuration found for $uuid" >&2
exit 1
fi
read_config $file
if [ -z "${config[mdev_type]}" ]; then
echo "Invalid configuration for $uuid, no mdev_type" >&2
exit 1
fi
parent=$(basename $(echo $file | sed -s "s/\/$uuid//"))
supported_types=$(wait_for_supported_types $parent)
if [ -z "$supported_types" ]; then
echo "Parent $parent not enabled for mdev" >&2
exit 1
fi
if [ ! -d $supported_types/${config[mdev_type]} ]; then
echo "Parent $parent does not support mdev type ${config[mdev_type]}" >&2
exit 1
fi
avail=$(cat $supported_types/${config[mdev_type]}/available_instances)
if [ $? -ne 0 ] || [ $avail -eq 0 ]; then
echo "No available instances of ${config[mdev_type]} on $parent" >&2
exit 1
fi
echo $uuid > $supported_types/${config[mdev_type]}/create
if [ $? -ne 0 ]; then
echo "Error creating mdev type ${config[mdev_type]} on $parent" >&2
exit 1
fi
;;
add-mdev)
if [ -z "$(valid_uuid $uuid)" ]; then
echo "Invalid UUID $uuid" >&2
exit 1
fi
parent_type=$(wait_for_mdev_type $uuid)
if [ -z "$parent_type" ]; then
echo "Type of device $uuid unavailable" >&2
exit 1
fi
mdev_type=$(basename $parent_type)
parent=$(basename $(echo $parent_type | sed -e 's/\/mdev_supported_types.*//'))
file=$(find $persist_base -name $uuid -type f)
if [ -n "$file" ]; then
if [ $file == $persist_base/$parent/$uuid ]; then
orig=${config[start]}
read_config $file
if [ ${config[mdev_type]} != $mdev_type ]; then
config[start]=$orig
rm -f $file
fi
else
rm -f $file
fi
fi
if [ -z "$file" ] || [ ! -e $file ]; then
if [ ! -d $persist_base/$parent ]; then
mkdir -p $persist_base/$parent
fi
echo "mdev_type=$mdev_type" > $persist_base/$parent/$uuid
echo "start=${config[start]}" >> $persist_base/$parent/$uuid
fi
;;
get-mdevs)
if [ ! -d $persist_base/$parent ]; then
exit 1
fi
for file in $(find $persist_base/$parent/ -maxdepth 1 -mindepth 1 -type f); do
uuid=$(basename $file)
if [ -n "$(valid_uuid $uuid)" ]; then
orig=${config[start]}
read_config $file
if [ ${config[start]} == "auto" ]; then
result+="mdev@$uuid.service "
fi
config[start]=$orig
fi
done
if [ -z "$result" ]; then
exit 1
fi
echo $result
;;
esac