forked from awilliam/mdevctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdevctl.sbin
executable file
·96 lines (87 loc) · 2.8 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
#!/usr/bin/bash
persist_base=/etc/mdev.d
mdev_base=/sys/bus/mdev/devices
parent_base=/sys/class/mdev_bus
case ${1} in
start-mdev|stop-mdev|remove-mdev)
if [ $# -ne 2 ]; then
echo "Usage: $0 $1 <mdev UUID>" >&2
exit 1
fi
cmd=$1
uuid=$2
;;
create-mdev)
if [ $# -ne 4 ]; then
echo "Usage: $0 $1 <mdev UUID> <parent device> <mdev_type>" >&2
exit 1
fi
cmd=$1
uuid=$2
parent=$3
mdev_type=$4
;;
list-available|list-all|list-mdevs|list-saved)
if [ $# -ne 1 ]; then
echo "Usage: $0 $1" >&2
exit 1
fi
cmd=$1
;;
esac
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 > $persist_base/$parent/$uuid
systemctl start mdev@$uuid.service
;;
remove-mdev)
systemctl stop mdev@$uuid.service
find $persist_base -name $uuid -type f | xargs rm -f
;;
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)
mdev_type=$(cat $mdev)
parent=$(basename $parent)
echo "$uuid $parent $mdev_type"
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