forked from mdevctl/mdevctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdevctl.sbin
executable file
·161 lines (145 loc) · 4.97 KB
/
mdevctl.sbin
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
#!/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
usage() {
echo "Usage: $(basename $0) [options] {COMMAND} ..." >&2
echo >&2
echo "Available commands:" >&2
echo "{start-mdev|stop-mdev|remove-mdev} <mdev-UUID>: start, stop, or remove a" >&2
echo " saved mdev" >&2
echo "create-mdev <mdev UUID> <parent device> <mdev_type>: create mdev with optional" >&2
echo " start policy override" >&2
echo " options: [--auto] [--manual]" >&2
echo "set-start <mdev UUID>: change mdev start policy, if no option specified," >&2
echo " system default policy is used" >&2
echo " options: [--auto] [--manual]" >&2
echo "list-all: list all possible mdev types supported in the system" >&2
echo "list-available: list all mdev types currently available" >&2
echo "list-mdevs: list currently configured mdevs" >&2
echo "list-saved: list all persistent mdevs (including not currently active ones)" >&2
exit 1
}
while (($# > 0)); do
case ${1} in
--auto)
config[start]=auto
;;
--manual)
config[start]=manual
;;
start-mdev|stop-mdev|remove-mdev|set-start)
[ $# -ne 2 ] && usage
cmd=$1
uuid=$2
break
;;
create-mdev)
[ $# -ne 4 ] && usage
cmd=$1
uuid=$2
parent=$3
mdev_type=$4
break
;;
list-available|list-all|list-mdevs|list-saved)
[ $# -ne 1 ] && usage
cmd=$1
break
;;
*)
usage
;;
esac
shift
done
if [ -z "${cmd}" ]; then
usage
fi
case ${cmd} in
stop-mdev)
systemctl stop mdev@$uuid.service
;;
start-mdev)
systemctl start mdev@$uuid.service
;;
create-mdev)
mkdir -p $persist_base/$parent
echo "mdev_type=$mdev_type" > $persist_base/$parent/$uuid
echo "start=${config[start]}" >> $persist_base/$parent/$uuid
if [ ${config[start]} == "auto" ]; then
systemctl start mdev@$uuid.service
fi
;;
remove-mdev)
systemctl stop mdev@$uuid.service
find $persist_base -name $uuid -type f | xargs rm -f
;;
set-start)
file=$(find $persist_base -name $uuid -type f)
if [ -n "$file" ]; then
if grep -q ^start $file; then
sed -i "s/^start=.*/start=${config[start]}/g" $file
else
echo "start=${config[start]}" >> $persist_base/$parent/$uuid
fi
else
exit 1
fi
;;
list-mdevs)
for mdev in $(find $mdev_base/ -maxdepth 1 -mindepth 1 -type l); do
uuid=$(basename $mdev)
parent_type=$(readlink -f $mdev/mdev_type)
mdev_type=$(basename $parent_type)
parent=$(basename $(echo $parent_type | sed -e 's/\/mdev_supported_types.*//'))
echo "$uuid $parent $mdev_type"
done
;;
list-saved)
for parent in $(find $persist_base/ -maxdepth 1 -mindepth 1 -type d); do
for mdev in $(find $parent/ -maxdepth 1 -mindepth 1 -type f); do
uuid=$(basename $mdev)
parent=$(basename $parent)
read_config $mdev
mdev_type=${config[mdev_type]}
start=${config[start]}
echo "$uuid $parent $mdev_type $start"
done
done
;;
list-available|list-all)
if [ $cmd == "list-available" ]; then
exclude_nonavail="yes"
fi
for parent in $(find $parent_base/ -maxdepth 1 -mindepth 1 -type l); do
echo "$(basename $parent)"
for parent_type in $(find $parent/mdev_supported_types/ -maxdepth 1 -mindepth 1 -type d); do
avail=$(cat $parent_type/available_instances)
if [ $avail -eq 0 ] && [ "$exclude_nonavail" == "yes" ]; then
continue
fi
echo " $(basename $parent_type)"
echo " Available instances: $avail"
echo " Device API: $(cat $parent_type/device_api)"
if [ -e $parent_type/description ]; then
echo " Description: $(cat $parent_type/description | tr '\n' ' ')"
fi
done
done
;;
esac