forked from NVIDIA/mig-parted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.sh
171 lines (152 loc) · 4.34 KB
/
utils.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
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
#!/usr/bin/env bash
# Copyright (c) 2021, NVIDIA CORPORATION. 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
function nvidia-mig-manager::service::reverse_array() {
# first argument is the array to reverse
# second is the reversed array
local -n arr="${1}"
local -n rev="${2}"
for i in "${arr[@]}"; do
rev=("${i}" "${rev[@]}")
done
}
function nvidia-mig-manager::service::assert_module_loaded() {
local module="${1}"
cat /proc/modules | grep -e "^${module} "
if [ "${?}" == "0" ]; then
return 0
fi
return 1
}
function nvidia-mig-manager::service::assert_gpu_reset_available() {
local devices_path="/sys/bus/pci/devices"
for d in $(ls "${devices_path}"); do
local vendor="$(cat "${devices_path}/${d}/vendor")"
if [ "${vendor}" != "0x10de" ]; then
continue
fi
local class="$(cat "${devices_path}/${d}/class")"
if [ "${class}" != "0x030200" ]; then
continue
fi
if [ ! -f "${devices_path}/${d}/reset" ]; then
return 1
fi
done
return 0
}
function nvidia-mig-manager::service::reboot() {
local statedir="/var/lib/nvidia-mig-manager"
mkdir -p "${statedir}"
if [ ! -f "${statedir}/reboot_attempted" ]; then
touch "${statedir}/reboot_attempted"
reboot
return ${?}
fi
(set +x;
echo "Machine already rebooted once -- not attempting again"
echo "You must manually remove the following file to enable automatic reboots again:"
echo " ${statedir}/reboot_attempted")
return 1
}
function nvidia-mig-manager::service::clear_reboot_state() {
local statedir="/var/lib/nvidia-mig-manager"
rm -rf "${statedir}/reboot_attempted"
}
function nvidia-mig-manager::service::persist_config_across_reboot() {
local selected_config="${1}"
cat << EOF > /etc/systemd/system/nvidia-mig-manager.service.d/override.conf
[Service]
Environment="MIG_PARTED_SELECTED_CONFIG=${selected_config}"
EOF
systemctl daemon-reload
}
function nvidia-mig-manager::service::start_systemd_services() {
local -n __services="${1}"
for s in ${__services[@]}; do
systemctl list-unit-files --state=enabled,generated | grep -F "${s}"
if [ "${?}" != "0" ]; then
continue
fi
systemctl start "${s}"
if [ "${?}" != "0" ]; then
return 1
fi
done
return 0
}
function nvidia-mig-manager::service::stop_systemd_services() {
local -n __services="${1}"
for s in ${__services[@]}; do
systemctl -q is-active "${s}"
if [ "${?}" != "0" ]; then
continue
fi
systemctl stop "${s}"
if [ "${?}" != "0" ]; then
return 1
fi
done
return 0
}
function nvidia-mig-manager::service::kill_k8s_containers_via_docker_by_image() {
local images=()
local -n __image_names="${1}"
for i in ${__image_names[@]}; do
images+=("${i}")
images+=("$(docker images --format "{{.ID}} {{.Repository}}" | grep "${i}" | cut -d' ' -f1 | tr '\n' ' ')")
done
for i in ${images[@]}; do
local containers="$(docker ps --format "{{.ID}} {{.Image}}" | grep "${i}" | cut -d' ' -f1 | tr '\n' ' ')"
if [ "${containers}" != "" ]; then
docker kill ${containers}
if [ "${?}" != "0" ]; then
return 1
fi
sleep 10
docker rm ${containers}
if [ "${?}" != "0" ]; then
return 1
fi
fi
done
return 0
}
function nvidia-mig-manager::service::kill_k8s_containers_via_containerd_by_image() {
local images=()
local -n __image_names="${1}"
for i in ${__image_names[@]}; do
images+=("${i}")
images+=("$(ctr -n k8s.io image ls | grep "${i}" | tr -s ' ' | cut -d' ' -f1 | tr '\n' ' ')")
done
for i in ${images[@]}; do
local containers="$(ctr -n k8s.io container ls "image~=${i}" -q)"
if [ "${containers}" != "" ]; then
ctr -n k8s.io task kill -a -s SIGKILL ${containers} || true
if [ "${?}" != "0" ]; then
return 1
fi
sleep 10
ctr -n k8s.io task rm -f ${containers} || true
if [ "${?}" != "0" ]; then
return 1
fi
ctr -n k8s.io container rm ${containers}
if [ "${?}" != "0" ]; then
return 1
fi
fi
done
return 0
}