PROXMOX Maintenance Script
Let’s look at what we can do so that on your PROXMOX server you can update the entire system and also its virtual machines, what else we added that after all the updates the server will reboot and after each machine will boot at an interval of 15 seconds.
First, you can use the qm list
command to get a list of all virtual machines running on the Proxmox server. Then, you can use a loop to iterate through each virtual machine and update the operating system.
o create and start the script in the Proxmox console panel, you can follow these steps:
- Connect to the Proxmox server via SSH.
- Create a new file for the script using a text editor, for example:
nano update_vms.sh
3. Copy and paste the script into the file, then save and exit the text editor.
4. Make the script executable by running the following command:
chmod +x update_vms.sh
5. To start the script, you can run it directly by executing the following command:
./update_vms.sh
6. To schedule the script to run automatically, you can use the cron
utility. You can open the crontab by running the following command:
crontab -e
7. In the crontab editor, add a new line to schedule the script to run at a specific time, for example, if you want to schedule the script to run every day at 1 am:
0 1 * * * /path/to/update_vms.sh
Save and exit the crontab editor.
Please note that, if you want to run the script with root privilege, you can add it to the /etc/crontab file which is running with root privilege.
It’s recommended to test the script in a non-production environment before running it on a production server, and make sure you have a backup of all virtual machines and Proxmox configurations before running the script.
You can add updating Proxmox and all virtual machines’ security packages and modules, as well as restarting the entire Proxmox system, to the script.
You can add a delay of 15 seconds between the starting of each virtual machine after the Proxmox system has been rebooted, by adding a loop with a sleep command in the script. Here’s an example of how you might update the script:
An example script in Bash could be:
— — — — — — — — — — — — — — — — — — — — — — — — — —
#!/bin/bash
# Update Proxmox
apt-get update && apt-get upgrade -y
# Get list of all virtual machines
vm_list=$(pvesh get /cluster/resources -type vm)
# Iterate through each virtual machine
for vm in $vm_list; do
# Get the operating system of the virtual machine
os_type=$(pvesh get $vm -content general -output-format json | jq -r ‘.ostype’)
# Update the operating system based on the type
if [ $os_type = “l26” ]; then
# update linux os
qm updatevm $vm_id -new-ostype l26
# update security packages and modules
qm exec $vm_id — apt-get update && apt-get upgrade -y
elif [ $os_type = “w10” ]; then
# update windows os
qm updatevm $vm_id -new-ostype w10
# update security packages and modules
qm exec $vm_id — powershell.exe -command “& { wuauclt /detectnow }”
fi
done
# Restart Proxmox
reboot
# Wait for Proxmox to come back online
sleep 60
# Start each virtual machine one by one with a delay of 15 seconds
for vm in $vm_list; do
qm start $vm
sleep 15
done
— — — — — — — — — — — — — — — — — — — — — — — — — —
Note that the script uses pvesh
command to get the operating system type of each vm, and then uses qm updatevm
command to update the os and the vm_id is the unique id of the vm which you can get from the list of all vm by using the command qm list
It's recommended to test the script in a non-production environment before running it on a production server and make sure you have a backup of all virtual machines before running the script.
This script will first run apt-get update
and apt-get upgrade -y
to update Proxmox, then it will update the operating system, security packages and modules of each virtual machine based on their OS type, then it will reboot the entire Proxmox system, it will wait for 60 sec to give the Proxmox time to come back online, and finally it will start each virtual machine one by one with a delay of 15 seconds between each start.
It’s recommended to test the script in a non-production environment before running it on a production server, and also make sure you have a backup of all virtual machines and Proxmox configurations before running the script.
Please Claps if you like my content and i would like to hear what you think and what you can add to this script. Lets Go!!!!