ESXi w/ USB UPS

Got tired of crappy PG&E power messing up my filesystems and got an APC UPS. Next step was getting it setup to shutdown all of the VMs and then the VMware host. I used an Ubuntu 18.04 VM. First thing is to pass through the APC USB connection. This can be done just from the web GUI. Also make sure ssh is enabled on you ESXi server. Once that’s done a lsusb on the VM will show you the IDs. In my case:

$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 004: ID 051d:0002 American Power Conversion Uninterruptible Power Supply

From this I needed to add the following line to my .vmx so that the APC is passed through every time. I had to use device1 as device0 was already take.

usb.autoConnect.device1 = "0x51d:0x2"

Next step is to setup ssh-keys. ESXi 6.7, at least, doesn’t support ed25519 so I went ahead with a 4096-bit rsa key. As root this looks like:

ssh-keygen -t rsa -b 4096

Your new key goes into /etc/ssh/keys-root/authorized_keys on your ESXi server. Make sure when you ssh as root from your VM to your ESXi host you can get in without a password.

Next step is to setup the tools you need on the Ubuntu VM. I used apcupsd and putty-tools. They install like this:

sudo apt-get install apcupsd putty-tools

Plink can only use private ssh keys in a .ppk format instead of the standard PEM as far as I can tell. To fix this use:

puttygen ~root/.ssh/id_rsa -O private -o ~root/.ssh/id_rsa.ppk

You can test if all of this is working like this:

/usr/bin/plink -i /root/.ssh/id_rsa.ppk root@<ESXi IP> "ls"

This should give you a badly formatted list of files from your ESXi server.

Next let’s setup apcupsd. We need to edit a few files. We will start with /etc/apcupsd/apcupsd.conf. We need to edit the following:

UPSNAME <someUPSname>
UPSCABLE usb
UPSTYPE usb
  # For USB UPSes, please leave the DEVICE directive blank.
DEVICE

Might as well adjust your BATTERYLEVEL, MINUTES and TIMEOUT variables at this point as well. I focused mainly on the last variable there, but YMMV. Make sure that /etc/default/apcupsdĀ hasĀ ISCONFIGURED=yes set. Let’s run a sudo service apcupsd restart at this point and then a apcaccess command. If everything was setup right you should see all of your UPS information. Let’s move on to setting up some actions now. In /etc/apcupsd/apccontrol you’ll want something like:

    doshutdown)
        echo "UPS ${2} initiated Shutdown Sequence" | ${WALL}
       # Shut down indirectly by triggering the ESXi host to do the
        # shutdown VMWare from ssh      
        /usr/bin/plink -i /root/.ssh/id_rsa.ppk root@<ESXi IP> "/sbin/shutdown.sh && /sbin/poweroff"
        ;;

Run another sudo service apcupsd restart and you can try to pull the plug!

Leave a Reply

Your email address will not be published. Required fields are marked *