-
Notifications
You must be signed in to change notification settings - Fork 516
/
add_udev_rules.sh
executable file
·146 lines (128 loc) · 4.33 KB
/
add_udev_rules.sh
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
#!/bin/bash
#
# Copyright 2015-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You may
# not use this file except in compliance with the License. A copy of the
# License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#
set -x
source /tmp/sdk_root_env.exp
set +x
rm -f /tmp/sdk_root_env.exp
mkdir -p /opt/aws/bin
DBDFs=`lspci -Dn | grep -Ew "1d0f:1042|1d0f:1041" | awk '{print $1}' | sed ':x;N;$!bx;s/\n/ /g'`
if [[ $AWS_FPGA_SDK_OTHERS ]]; then
# Allow all users
# Make a script that will be run to change permissions everytime
# udev rule for the DBDF is matched
echo "Installing permission fix script for udev"
cat >/opt/aws/bin/change-fpga-perm.sh<<EF
#!/bin/bash
set -x
setperm () {
chmod g=u \$1
chmod a=u \$1
}
setfpgaperm () {
for f in \$1/*; do
setperm \$f;
done
}
devicePath=/sys/bus/pci/devices/\$1
grep -q "0x058000" \$devicePath/class && setfpgaperm "\$devicePath"
setperm /sys/bus/pci/rescan all
EF
echo "Adding udev rule: 9999-presistent-fpga.rules"
cat >/etc/udev/rules.d/9999-presistent-fpga.rules<<EF
ATTR{vendor}=="0x1d0f", ATTR{device}=="0x1041", RUN+="/opt/aws/bin/change-fpga-perm.sh %k"
ATTR{vendor}=="0x1d0f", ATTR{device}=="0x1041", ACTION=="add", RUN+="/opt/aws/bin/change-fpga-perm.sh %k"
ATTR{vendor}=="0x1d0f", ATTR{device}=="0x1042", RUN+="/opt/aws/bin/change-fpga-perm.sh %k"
ATTR{vendor}=="0x1d0f", ATTR{device}=="0x1042", ACTION=="add", RUN+="/opt/aws/bin/change-fpga-perm.sh %k"
EF
else
# Allow group only
echo "Creating group ${AWS_FPGA_SDK_GROUP}"
getent group ${AWS_FPGA_SDK_GROUP} >/dev/null 2>&1
if [[ $? -eq 0 ]] ; then
if [ -z ${AWS_FPGA_SDK_OVERRIDE_GROUP} ] ; then
echo "Group ${AWS_FPGA_SDK_GROUP} already exists. Please export a non existent group name to AWS_FPGA_SDK_GROUP or export AWS_FPGA_SDK_OVERRIDE_GROUP=y"
exit 1
fi
echo "${AWS_FPGA_SDK_GROUP} already exists, will grant FPGA resource access to this group"
else
groupadd ${AWS_FPGA_SDK_GROUP}
if [[ $? -ne 0 ]] ; then
echo "Could not group ${AWS_FPGA_SDK_GROUP}"
exit 1
fi
fi
echo "Adding user ${SDK_NON_ROOT_USER} into group ${AWS_FPGA_SDK_GROUP}"
getent group ${AWS_FPGA_SDK_GROUP} | grep -qw ${SDK_NON_ROOT_USER}
if [[ $? -eq 0 ]] ; then
echo "${SDK_NON_ROOT_USER} is already in group ${AWS_FPGA_SDK_GROUP}"
else
usermod -a -G ${AWS_FPGA_SDK_GROUP} ${SDK_NON_ROOT_USER}
if [[ $? -ne 0 ]] ; then
echo "Could not add user ${SDK_NON_ROOT_USER} to group ${AWS_FPGA_SDK_GROUP}"
exit 1
fi
fi
# Fail on any unsucessful command
set -e
# Make a script that will be run to change permissions everytime
# udev rule for the DBDF is matched
echo "Installing permission fix script for udev"
cat >/opt/aws/bin/change-fpga-perm.sh<<EF
#!/bin/bash
set -x
setperm () {
chmod g=u \$1
if [[ x\$2 != "x" ]] ; then
chmod a=u \$1
fi
}
setgrp() {
chown root:${AWS_FPGA_SDK_GROUP} \$1
}
setfpgaperm () {
for f in \$1/*; do
setperm \$f;
setgrp \$f
done
}
devicePath=/sys/bus/pci/devices/\$1
grep -q "0x058000" \$devicePath/class && setfpgaperm "\$devicePath"
setperm /sys/bus/pci/rescan all
EF
echo "Adding udev rule: 9999-presistent-fpga.rules"
rm -f /tmp/9999-presistent-fpga.rules
for d in $DBDFs ; do
echo "KERNEL==\"*${d}*\", RUN+=\"/opt/aws/bin/change-fpga-perm.sh '${d}'\"" >> /tmp/9999-presistent-fpga.rules
done
for d in $DBDFs ; do
echo "KERNEL==\"*${d}*\", ACTION==\"add\", RUN+=\"/opt/aws/bin/change-fpga-perm.sh '${d}'\"" >> /tmp/9999-presistent-fpga.rules
done
echo "Adding udev rule: 9999-presistent-fpga.rules"
cp /tmp/9999-presistent-fpga.rules /etc/udev/rules.d/9999-presistent-fpga.rules
fi
chmod 544 /opt/aws/bin/change-fpga-perm.sh
## Test the rules for any issues
for d in $DBDFs ; do
udevadm test --action="add" /sys/bus/pci/devices/${d} >/dev/null 2>&1
done
#
## Make udev daemon aware of rule changes
udevadm control --reload
#
## Trigger the change for rules to avoid a reboot
for d in $DBDFs ; do
udevadm trigger --action="add" /sys/bus/pci/devices/${d}
done