Skip to content

Instantly share code, notes, and snippets.

@dadatuputi
Last active March 4, 2024 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dadatuputi/f6a703e8bbf8e6842dd6a8daf6bc4593 to your computer and use it in GitHub Desktop.
Save dadatuputi/f6a703e8bbf8e6842dd6a8daf6bc4593 to your computer and use it in GitHub Desktop.
Proxmox script to update nvidia driver on system and in containers
#!/bin/bash
function usage() {
echo "Usage: $0 -u DRIVERURL -c CTID0,CTID1,..." 1>&2; exit 0;
}
while getopts ":hu:c:" opt; do
case "$opt" in
u) driverurl="$OPTARG";;
c) containers="$OPTARG";;
h | *) usage;;
esac
done
if [ -z "${driverurl}" ]; then
usage
fi
function logmsg() {
echo "######## $1 ########"
}
tmpfile=/tmp/nvidia.run
installcmd="$tmpfile --ui=none --no-questions"
# update system
function update_system_driver() {
logmsg "Updating system driver"
eval $installcmd
logmsg "Finished system driver install"
}
# update container
function update_container_driver() {
ct=$1
shift
# Initial container status
status=$(pct list | grep $ct | tr -s ' ' | cut -f2 -d' ')
if [ "$status" = "stopped" ]; then
pct start $ct
fi
logmsg "Updating driver in $ct"
logmsg "mounting $ct"
pct mount $ct
logmsg "copying $tmpfile to container"
cp $tmpfile /var/lib/lxc/$ct/rootfs$tmpfile
pct exec $ct -- bash -c "$@ --no-kernel-module"
rm /var/lib/lxc/$ct/rootfs$tmpfile
pct unmount $ct
logmsg "Finished in $ct"
pct stop $ct
# Start container if it was originally running
if [ "$status" = "running" ]; then
pct start $ct
logmsg "$ct was originally running, so restarting"
fi
}
# download the file
logmsg "Downloading driver from $driverurl to $tmpfile"
curl -o $tmpfile $driverurl
chmod +x $tmpfile
### Prep the system
## kill services in running containers
for i in ${containers//,/ }
do
# Stop possible GPU processes
pct exec $i -- bash -c "systemctl stop xxnetwork-cmix"
done
# update system driver
update_system_driver
# step through each container and update
for i in ${containers//,/ }
do
update_container_driver $i "$installcmd"
done
### Clean up the system
logmsg "Deleting tempfile $tmpfile"
rm $tmpfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment