forked from proxmology/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoral2.sh
238 lines (208 loc) · 8.14 KB
/
coral2.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
YW=$(echo "\033[33m")
BL=$(echo "\033[36m")
RD=$(echo "\033[01;31m")
GN=$(echo "\033[1;92m")
CL=$(echo "\033[m")
BFR="\r\033[K"
CM="${GN}✓${CL}"
HOLD="-"
log() {
echo -e "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
msg_info() {
local msg="$1"
echo -ne " ${HOLD} ${YW}${msg}..."
}
msg_ok() {
local msg="$1"
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
}
msg_error() {
local msg="$1"
echo -e "${BFR} ${RD}✗ ${CL}${msg}"
}
NEED_REBOOT=false
# Validar la versión de Proxmox
validate_pve_version() {
if ! pveversion | grep -Eq "pve-manager/(8\\..+|[9-9])"; then
msg_error "Esta versión de Proxmox no es compatible. Requiere Proxmox VE 8.0 o superior."
exit 1
fi
}
# Verificar y configurar repositorios
verify_and_add_repos() {
msg_info "Verificando y configurando repositorios necesarios"
if ! grep -q "pve-no-subscription" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
echo "deb http://download.proxmox.com/debian/pve $(lsb_release -sc) pve-no-subscription" | tee /etc/apt/sources.list.d/pve-no-subscription.list
fi
if ! grep -q "non-free-firmware" /etc/apt/sources.list; then
echo "deb http://deb.debian.org/debian $(lsb_release -sc) main contrib non-free-firmware
deb http://deb.debian.org/debian $(lsb_release -sc)-updates main contrib non-free-firmware
deb http://security.debian.org/debian-security $(lsb_release -sc)-security main contrib non-free-firmware" | tee -a /etc/apt/sources.list
fi
apt-get update &>/dev/null
msg_ok "Repositorios configurados correctamente."
}
# Verificar si se requieren repositorios adicionales y añadirlos
add_coral_repos() {
msg_info "Verificando repositorios de Coral TPU"
if ! grep -q "coral-edgetpu-stable" /etc/apt/sources.list /etc/apt/sources.list.d/* 2>/dev/null; then
echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | tee /etc/apt/sources.list.d/coral-edgetpu.list
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/coral-edgetpu.gpg
apt-get update &>/dev/null
msg_ok "Repositorio de Coral añadido correctamente."
else
msg_ok "Repositorio de Coral ya configurado."
fi
}
# Instalar dependencias para Coral TPU
install_coral_dependencies() {
msg_info "Instalando dependencias para Coral TPU"
apt remove -y gasket-dkms &>/dev/null
apt install -y git devscripts dh-dkms dkms pve-headers-$(uname -r) &>/dev/null
cd /tmp
rm -rf gasket-driver
git clone https://github.com/google/gasket-driver.git &>/dev/null
cd gasket-driver/
debuild -us -uc -tc -b &>/dev/null
dpkg -i ../gasket-dkms_1.0-18_all.deb &>/dev/null
apt-get install -y libedgetpu1-max &>/dev/null
msg_ok "Dependencias de Coral TPU instaladas correctamente."
}
# Cambiar contenedor a privilegiado
ensure_privileged_container() {
CONFIG_FILE="/etc/pve/lxc/${CONTAINER_ID}.conf"
if grep -q "^unprivileged: 1" "$CONFIG_FILE"; then
msg_info "El contenedor ${CONTAINER_ID} es no privilegiado. Cambiando a privilegiado..."
sed -i "s/^unprivileged: 1/unprivileged: 0/" "$CONFIG_FILE"
STORAGE_TYPE=$(pct config "$CONTAINER_ID" | grep "^rootfs:" | awk -F, '{print $2}' | cut -d'=' -f2)
if [[ "$STORAGE_TYPE" == "dir" ]]; then
STORAGE_PATH=$(pct config "$CONTAINER_ID" | grep "^rootfs:" | awk '{print $2}' | cut -d',' -f1)
chown -R root:root "$STORAGE_PATH"
fi
msg_ok "Contenedor cambiado a privilegiado."
else
msg_ok "El contenedor ${CONTAINER_ID} ya es privilegiado."
fi
}
# Seleccionar contenedor usando whiptail
select_container() {
MENU_OPTIONS=()
while IFS= read -r line; do
CONTAINER_ID=$(echo "$line" | awk '{print $1}')
CONTAINER_NAME=$(echo "$line" | awk '{print $2}')
MENU_OPTIONS+=("$CONTAINER_ID" "$CONTAINER_NAME")
done <<< "$(pct list | awk 'NR>1 {print $1, $3}')"
CONTAINER_ID=$(whiptail --title "Seleccionar Contenedor" --menu "Selecciona un contenedor para configurar:" 15 60 5 "${MENU_OPTIONS[@]}" 3>&1 1>&2 2>&3)
if [ -z "$CONTAINER_ID" ]; then
msg_error "No se seleccionó ningún contenedor."
exit 1
fi
msg_ok "Contenedor seleccionado: $CONTAINER_ID"
}
# Configurar LXC para iGPU
configure_lxc_for_igpu() {
ensure_privileged_container
CONFIG_FILE="/etc/pve/lxc/${CONTAINER_ID}.conf"
sed -i '/^lxc\.cgroup2\.devices\.allow: c 226:/d' "$CONFIG_FILE"
sed -i '/^lxc\.mount\.entry: \/dev\/dri/d' "$CONFIG_FILE"
cat <<EOF >> "$CONFIG_FILE"
features: nesting=1
lxc.cgroup2.devices.allow: c 226:0 rwm
lxc.cgroup2.devices.allow: c 226:128 rwm
lxc.cgroup2.devices.allow: c 29:0 rwm
lxc.mount.entry: /dev/fb0 dev/fb0 none bind,optional,create=file
lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir
lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file
EOF
msg_info "Instalando drivers de iGPU en el contenedor ${CONTAINER_ID}"
pct exec "$CONTAINER_ID" -- bash -c "apt-get update && apt-get install -y vainfo intel-media-va-driver-non-free intel-gpu-tools && adduser root video && adduser root render" &>/dev/null
msg_ok "Drivers de iGPU instalados en el contenedor ${CONTAINER_ID}."
}
# Configurar LXC para NVIDIA
configure_lxc_for_nvidia() {
ensure_privileged_container
CONFIG_FILE="/etc/pve/lxc/${CONTAINER_ID}.conf"
NV_DEVICES=$(ls -l /dev/nv* | awk '{print $5,$6}' | sed 's/,/:/g')
sed -i '/^lxc\.cgroup2\.devices\.allow: c /d' "$CONFIG_FILE"
sed -i '/^lxc\.mount\.entry: \/dev\/nvidia/d' "$CONFIG_FILE"
for DEV in $NV_DEVICES; do
echo "lxc.cgroup2.devices.allow: c $DEV rwm" >> "$CONFIG_FILE"
done
cat <<EOF >> "$CONFIG_FILE"
lxc.mount.entry: /dev/nvidia0 dev/nvidia0 none bind,optional,create=file
lxc.mount.entry: /dev/nvidiactl dev/nvidiactl none bind,optional,create=file
lxc.mount.entry: /dev/nvidia-uvm dev/nvidia-uvm none bind,optional,create=file
lxc.mount.entry: /dev/nvidia-modeset dev/nvidia-modeset none bind,optional,create=file
lxc.mount.entry: /dev/nvidia-uvm-tools dev/nvidia-uvm-tools none bind,optional,create=file
EOF
msg_ok "Configuración de NVIDIA añadida al contenedor ${CONTAINER_ID}."
}
# Configurar LXC para Coral TPU
configure_lxc_for_coral() {
ensure_privileged_container
add_coral_repos
install_coral_dependencies
CONFIG_FILE="/etc/pve/lxc/${CONTAINER_ID}.conf"
cat <<EOF >> "$CONFIG_FILE"
lxc.cgroup2.devices.allow: c 189:* rwm
lxc.mount.entry: /dev/bus/usb dev/bus/usb none bind,optional,create=dir
lxc.cgroup2.devices.allow: c 29:0 rwm
lxc.mount.entry: /dev/apex_0 dev/apex_0 none bind,optional,create=file
EOF
msg_ok "Configuración de Coral TPU añadida al contenedor ${CONTAINER_ID}."
}
# Sugerir reinicio si es necesario
prompt_reboot() {
if $NEED_REBOOT; then
if whiptail --title "Reinicio Requerido" --yesno "Se recomienda reiniciar para aplicar los cambios. ¿Deseas reiniciar ahora?" 10 60; then
msg_info "Reiniciando el sistema..."
reboot
else
msg_ok "Por favor, reinicia el sistema más tarde para aplicar los cambios."
fi
fi
}
# Menú principal
main_menu() {
validate_pve_version
OPTIONS=(
"Añadir iGPU"
"Añadir NVIDIA"
"Añadir Coral TPU"
"Salir"
)
OPTION=$(whiptail --title "Menú Principal" --menu "Selecciona una opción:" 15 60 4 \
1 "Añadir iGPU" \
2 "Añadir NVIDIA" \
3 "Añadir Coral TPU" \
4 "Salir" 3>&1 1>&2 2>&3)
case "$OPTION" in
1)
select_container
configure_lxc_for_igpu
msg_ok "Configuración de iGPU completada. No es necesario reiniciar."
;;
2)
select_container
configure_lxc_for_nvidia
NEED_REBOOT=true
;;
3)
select_container
configure_lxc_for_coral
NEED_REBOOT=true
;;
4)
msg_ok "Saliendo del script."
exit 0
;;
*)
msg_error "Opción inválida. Intenta de nuevo."
main_menu
;;
esac
prompt_reboot
}
main_menu