Script to auto mount USBs on Boot/Reboot. Monitoring Multiple UPS
-
Hey, I'm not a huge poster, but I've been working on a short script to solve a problem I've been having.
Long story short, I have 5 CyberPower UPSs that I want to monitor over USB.
I know NUT exists, but I hear it's a hassle, and I like the trending/reporting that's available in the CyberPower Software.
Unfortunately, I can only monitor 1 UPS per instance of the software.
And on top of that, when/if the server reboots, all USB Passthrough is reset, as the USB UUID changes.
However I noticed that in "xe pusb-list" there is a static "Path" designation which I realized I could use to my advantageSo I built this script to run at startup (probably going to add a 1-2 minute delay on the front end, as sometimes the USB drivers take a minute to load, I'm learning). It grabs the UUID of a USB and saves it to a txt file based on the "Path". It then grabs the UUID of my designated VMs (UPS1-UPS5, just single core 1GB Ram ubuntu to run the software). Main reason I did this instead of hardcoding the UUID is in case I ever need to delete/restore the VM from backup and the UUID Changes. and Finally it starts the VM.
I haven't finished installing all of the UPSs, so I only have 3 of the 5 VMs programmed currently, but it's in a good enough state That I wanted to share it in case anyone else has been looking to solve a similar problem.
#set working directory cd /home/temp #remove old text files rm -f *.txt #create list of all pUSBs xe pusb-list > pusb.txt #split list per device and remove old list csplit -sz pusb.txt /uuid/ '{*}' rm -f pusb.txt #for all split files, if Vendor ID is CyberPower, create a txt file named by Serial Number, containing the USB uuid, and enable for passthrough for i in xx* do var1=$(awk '/vendor-id/ {print $4}' "$i") if [ "$var1" -eq 0764 ] then path=$(awk '/path/ {print $4}' "$i") awk '/uuid/ {print $5}' "$i" > "$path".txt xe pusb-param-set uuid=$(cat "$path".txt) passthrough-enabled=true xe usb-group-list PUSB-uuids=$(cat "$path".txt) > "$i"-2 awk '/uuid/ {print $5}' "$i"-2 > "$path"group.txt fi done #remove all csplit files rm -f xx* #create list of all VMs xe vm-list > vm.txt #split list per vm and remove old list csplit -sz vm.txt /uuid/ '{*}' rm -f vm.txt #for all split files, get UUID of needed VMs for f in xx* do vmname=$(awk '/name/ {print $4}' "$f") if [ "$vmname" == 'UPS1' ] || [ "$vmname" == 'UPS2' ] || [ "$vmname" == 'UPS3' ] || [ "$vmname" == 'UPS4' ] || [ "$vmname" == 'UPS5' ] then awk '/uuid/ {print $5}' "$f" > "$vmname".txt fi done #remove all csplit files rm -f xx* #if expected UPS is present, attach to expected VM if test -f 2-2.1group.txt then xe vusb-create usb-group-uuid=$(cat 2-2.1group.txt) vm-uuid=$(cat UPS1.txt) xe vm-start uuid=$(cat UPS1.txt) fi if test -f 2-2.2group.txt then xe vusb-create usb-group-uuid=$(cat 2-2.2group.txt) vm-uuid=$(cat UPS2.txt) xe vm-start uuid=$(cat UPS2.txt) fi if test -f 2-2.4group.txt then xe vusb-create usb-group-uuid=$(cat 2-2.4group.txt) vm-uuid=$(cat UPS5.txt) xe vm-start uuid=$(cat UPS5.txt) fi #remove leftover txt files rm -f *.txt
I am by no means an expert in scripting or linux... This has all been a learning endeavor for me... so if anyone has any suggestions on how to improve this script, I do welcome feedback.
-
That's interesting, I wonder why we have to reset all USB uuids after a reboot. Probably something to investigate when you have the time to do so
-
@olivierlambert Y'know, I thought I smelled a bit of sarcasm there, so I went and double checked... and i think now I'm more confused lmao
So I rebooted, UUIDs and Paths didn't change.
Then I unplugged, and moved one of the USBs I'm currently using.... and moving that one USB changed the Path and UUID of all 3 USBs that were attached.Guess the Paths weren't as static as I thought
-
Haha no it was really intriguing, but to be fair, it's "another question" in the pile of many others Something to take a look when we can, I wonder how does this work in a bare metal OS.
-
@olivierlambert The never ending struggle of development. always more questions.
while I did notice that the overall path changed, I did notice that some parts were consistent still...
IE, My Paths changed from 2-2.1, 2-2.2, and 2-2.4 to 1-2.1, 1-2.2, and 1-2.4 respectively.Ideally I would be using the Serial Number readout to sort these more efficiently, but for some reason my rack mounted CyberPower UPSs don't have Serial Numbers reporting even in the software when direct connected to my desktop... hence why I'm trying to find another way to "statically" identify my devices.
I'm going to keep working on this program, and will update at a later point when I make some more significant progress, maybe with some "Options" for people to swap in.
Next objective is for it to detect whether a running VM has its proper USB attached still, and if it became detached, to stop the VM, mount the USB, and then start it... main reason for this is in case a UPS gets unplugged, or is removed for maintenance.