Monday, July 15, 2013

Setup a UPS daemon for Cyberpower UPS Battery Backups

If you wish to use a Cyberpower UPS on a Linux system, then you'll want a monitoring daemon that alerts your PC when it's on battery power and does a proper shutdown before battery runs out. I have multiple cyberpower ups systems that I've verified this works on : CP1500AVR, CP1500AVRLCD, CP900AVR, PR1000LCDRTXL2Ua. It's likely it works on any cyberpower device. NOTE: this is for USB only, if you are using a rs232 serial connection - the below steps have not been verified.

You have a couple options to achieve interoperability, such as PowerPanel, upsmon, nut. The only one this article will explore at the moment is Cyberpower's Linux Power Panel Software. Although this article will most likely be updated with a NUT setup in the future.

Unless your distro comes with this package (Gentoo does not at the time of writing this), download the latest copy from their website : http://www.cyberpowersystems.com/products/management-software/ppl.html?selectedTabId=resources&imageI=#tab-box

Preferably the .tar package so you can build and customize as you see fit.



You can then verify connectivity with the following command :


 You will probably want to read through this config file before customizing your own:


 And the following helper scripts that come stock with this application:


The helper scripts are useful, but I started out by moving them to /usr/local/bin to maintain an orderly filesystem, then modified them as outlined below :


The following is my pwrstat daemon config with all notation removed.
Note: I don't have it setup to turn off my PC at the first power failure - as short power interruptions are common where I live - the below will only initiate a shutdown on low battery mode. Other notes are I have it setup to run until battery reaches 15% and then give 30s to launch script and another 60s to shutdown PC. Depending on the size of your UPS and power requirements, you might have to tweak those numbers to allot yourself enough time.
SIDE NOTE: I won't go into a huge recommendation and list of considerations, but generally speaking I measure my power usage for my PC and one monitor (other monitors, speakers, etc are all on the ups' surge protection only - not battery), and find a UPS wattage where I'd use roughly 60% of max UPS rated wattage. Most UPS' and power supplies have their peak efficiency around 50-60% utilization. Additionally I like to plan provide my desktop PC for around 20minutes of backup power time.



Afterwards you can restart the daemon and confirm the new settings have been applied :


Now we'll modify the helper scripts below...
First is pwrstatd-email.sh . If you wish to send an email when a power outtage has occured continue with this config, otherwise you may skip. A good thing to know is your cell phones SMS email address, which will send you a text message, very handy for instant PC alerts. If you don't know your carriers SMS address google it, for example ATT is : 10digitnumber@txt.att.net. You'll want to keep the subjects and message bodies short for txt alerts - in the below example I've added a secondary RECEIPT address and subject section for shortened text messaging purposes.

Note: this script depends on system email settings, using `mail`. I'll go through a basic setup to use gmail's smtp servers. I recommend having a gmail account used just for smtp email sending, so you don't have to keep your primary accounts password stored in a plain text file on your filesystem.


Modify the following with your login information...
Note: You do not need to include @gmail.com for AuthUser. Also for hostname you can comment out, or not include the domain if you are not a member of one.

You can test to see if the email works now : Replacing email@gmail.com with your TO: address.

Assuming that works, then next you can look over the pwrstatd-email.sh to confirm it will work with your setup:


We're going to use a centralized config file (pwrstatd-email.conf), as to not declare variables that may change inside a shell script:


Next we'll look at pwrstatd-powerfail.sh. This is the script that will run as soon as your UPS goes into on-battery mode (after whatever delay set in configuration).


Next we'll look at pwrstatd-lowbatt.sh. This is the script that will run as soon as your UPS goes into "low battery" mode, whatever battery percentage specified above, after the configured delay.

Note: This is where you might want to add any custom shutdown options for your PC (if it's setup to shutdown here). If you have a complicated shutdown procedure, IE certain daemons stopped, work saved, etc, it might be best to create a script that does it all (if you don't have one already) then simply call that script from the end of this one. Also to note if you have multiple PC's running off a UPS - like a rackmount server setup - you can setup ssh key auth and write a script that will remote into each machine to issue a shutdown command.

After all configuration is done, save all your work on your PC and start testing.

You can start by running the scripts directly to test email functionality:

If you get an error about command wall or beep not found - and you're using gentoo the following will resolve:
With other distro's you'll have to install the relevant packages or remove those commands from the scripts.

Next disconnect the power cord from the wall to your UPS. It should turn on the battery and start beeping. Depending on how you set it up above with a delay, email, and custom notification settings - you should see that go off. You can verify the status with the following command :



Finally, wait until power drains down below your set low-battery threshold - and you should see your PC do a normal/safe shutdown.

Saturday, February 16, 2013

UEFI on Gentoo w/ grub2 and GPT

I did this on a new build (new hard drive) on a current slave system. So you can adapt the techniques a bit if you're doing this off a livecd or just migrating a current system.

First you'll want to download a UEFI compatible livecd ( or make your own ). I recommend SysRecueCD http://www.sysresccd.org/Download based on Gentoo. Just download the iso and burn it to a cd, then  set it aside.

You'll want to build a EFI partition either using parted or gdisk (assuming this is a clean disk, add your other partitions into this string of commands like normal):

Add this line to your make.conf (or modify for your arch needs) :


Then emerge efi/grub2:


Create the filesystem (has to be fat):

If you want to have a legacy  boot loader on this drive then I'd recommend using a directory inside of /boot ( like /boot/efi ) for the efi partition. Otherwise you can use /boot.

You'll probably want the following options enabled in your kenerl :

Compile your new kernel, then copy over your vmlinuz/bzimage to /boot :
NOTE: kernels must be in the following format for grub2 to autodetect "kernel-(version)" or "vmlinuz-(version)"

Make the config file with grub2 :


Now if you're currently booted off a UEFI medium, then great. Otherwise you need to use the sysrescuecd linked above or another medium. Reboot and check your BIOS settings. I am running a Sabertooth 990fx r2.0 and changed:

That forced the board to only boot of UEFI so I knew that the livecd wouldn't be booted in legacy/mbr mode. After you're booted off a uefi medium you can verify by looking for the directory `/sys/firmware/efi` .

If it exists you should be good. If it doesn't then you either :
A) Aren't booted right off the UEFI medium - check bios settings
B) Check livecd you're using to make sure it supports efi bootmgr and the kernel
C) modprobe efivars - if the kernel built it in as a module

You shouldn't have to worry about B or C if using the sysrescd.

Get back in your new build environment if you had to reboot

Finally install the EFI grub bootmgr :

It should tell you if it was successful. If so - reboot off your new UEFI grub2 disk!

You may want to go back into your BIOS if you enabled UEFI only mode, and put it into mix mode in case you ever want to boot off a non-uefi medium.


Saturday, February 9, 2013

mrebuild.sh - rebuild modules after a kernel update

This script is used to rebuild all third party modules after a kernel update. modules-rebuild doesn't  always pull in all the packages you need, so this gives an easy config file to specify additional modules, packages, and commands. Making the rebuild of everything necessary after a kernel update a one-liner.

  • download mrebuild.sh and put in /usr/local/bin
  • download mrebuild.conf and put in /etc/conf.d
  • view and modify mrebuild.conf if necessary
  • `mrebuild.sh --go` will kick off the rebuild process

This script can also be downloaded http://pastebin.com/BBsZGZxV

mrebuild.sh



This config file can also be downloaded http://pastebin.com/iPs7hz1e

mrebuild.conf

killwindows.sh - simple wine/windows process terminator

If you run wine apps, or do any development with it, you'll undoubtedly have the need for this. Frequently a wine process will get hung up in the background, and may prevent you from doing what you need to.


  • Download the script and put it in a bin directory. 
  • view and modify blacklist and whitelist variables ( pipe delimited )
  • killwindows.sh --list     (will show you what processes are alive, and "would be" killed)
  • killwindows.sh --kill     (will show the what processes are alive then `kill` them)


You can also download the script here : http://pastebin.com/u2a3sG55




Friday, February 8, 2013

updateworld.sh - a simple way to manage world updates

This script manages and performs world updates for the Gentoo distro. It can perform the usual sync, emerge world, revdep, etc-update, plus options to add in whatever custom commands you want. It is maintained by a central .conf file to make it easy. The goal is to either give an end-user a simple, customized, and either quiet or verbose world update experience. On top of that it can be set to run autonomously, if you have many deployed Gentoo machines and have approved a branch to update to, pushing out this one line script should make it simple. This script has bash color logging, the default location is in `/var/log/portage`.

To view the made package page : http://code.google.com/p/updateworld
You can find the latest revision of the script on the page : https://code.google.com/p/updateworld/downloads/list

You will also find a basic wiki documentation highlighting the possible features.

The tar has these two important files:

To install :
  • copy updateworld.sh to /usr/local/bin
  • copy updateworld.conf to /etc/conf.d/
  • edit updateworld.conf - should read all the notes and options available

To run a quiet full update :
  • `updateworld.sh --full`

To run a verbose update adding in keep-going and force-sync :
  • `updateworld.sh --go --verbose --keep-going --force-sync`

You can also use the --help flag if you're looking for proper syntax and options.

g13lcd.sh - a simple app to load text or images on a g13

This script gives you a simple one-liner way to display specific text or images on the g13 LCD screen.
This relies on:
    ecraven's g13 https://github.com/ecraven/g13
    netpbm http://netpbm.sourceforge.net/
    imagemagick http://www.imagemagick.org/


netpbm and imagemagick are both in Portage for Gentoo.

  • emerge -av netpbm imagemagick
  • download and `make` g13 : `git clone https://github.com/ecraven/g13`
  • copy pbm2lpbm to a bin directory like `/usr/local/bin` 

Next download and copy this `g13lcd.sh` script to a bin directory. You can also find it: http://pastebin.com/NA9xzpaH





You will need to start the g13 daemon before being able to output to the LCD. Either use the `g13` command or see previous entry about the `g13.sh` script I wrote.

Afterwards you can execute this script by either:
`g13lcd.sh --text2lcd TEXT GOES HERE` or
`g13lcd.sh --image2lcd /somedirectory/someimage.jpg`

It should be able to convert most image types to display on the screen. But it's (as of yet) a very basic script, it ignores all aspect ratio, doesn't do specific padding, etc. You'll want to make sure the image you're converting is about the  same size pretty much.

g13.sh - using a g13 daemon and load presets

Getting g13 to work on Linux is more challenging then it should be. Unfortunately I've yet to get the g15daemon to fully work on the g13 pad (key binding issues). This way relies on ecraven's g13 app: https://github.com/ecraven/g13


  1. Download it by: `git clone https://github.com/ecraven/g13`
  2. Go into the downloaded directory and `make`
  3. Copy the g13, pbm2lpbm, clock.sh (if you desire) to a bin directory like `/usr/local/bin`

You should now be able to run `g13` and get the default functionality.

You can use the following `g13.sh` script to make a nice start up script for this g13 app and have all your saved features. It can change the RGB led color of the text, the led color of the MR buttons, apply bindings, and display a startup lcd image.  You can also download it : http://pastebin.com/prrCSAbY




This script relies on two config files.

/etc/conf.d/g13.conf (http://pastebin.com/AG66eDJ5)


/etc/conf.d/g13-binder.conf (http://pastebin.com/NGtMTRqA)


Follow the notes and modify the files as you see fit. The g13-binder.conf is optional - there is a default binding in the g13 daemon, this is just if you want to customize any key mapping.

Afterwards a simple `g13.sh --start` and `g13.sh --stop` should give you the desired results.

Note: If you copied over the clock.sh, running it will keep a somewhat accurate running clock on the lcd screen, much like the default for g15daemon.

Another neat g13 app on Linux is : http://code.google.com/p/linux-g13-driver/
It certainly makes the bindings easy with a Java UI and I believe uses the same ecraven g13 daemon. However it doesn't have the other functionality easily accessible - at least I haven't played around with it enough to discover.

Check out the g13lcd.sh script, gives a simplistic way to output text or images  to the g13 lcd screen. http://customlinux.blogspot.com/2013/02/g13lcdsh-simple-app-to-load-text-or.html

Thursday, February 7, 2013

pavolume.sh - control the active sink volume for pulseaudio

If you have multiple audio output devices, and you use keyboard bindings to control the master volume -> your system may get confused which master volume you want and select the first one by default. This script finds the current default active sink and changes the volume with it.

Copy it to a bin dir like `/usr/local/bin` execute with :
  • pavolume.sh --up
  • pavolume.sh --down
  • pavolume.sh --togmute  (this will change the mute status to the opposite of what it is currently - useful for a single key that does both)
  • pavolume.sh --sync   (this will sync all of your current apps using the current active/default output to the same volume level)

In the beggining of the script you'll see four useful settings/variables :
  1. The 'osd' variable. If you want to display an OSD message when changing volume set to yes. It's setup for kde and the KOSD package - so you might have to install that.
  2. The 'inc' variable. This is the increment that each volume up/down request will preform. 5 is default.
  3. The 'capvol' variable. This will cap the max volume to 100% (since pulseaudio by default allows you to go over this limit). Defaults to 'yes'.
  4. The 'maxvol' variable. This will cap the maximum volume to another percent (capvol overrides this if set to yes). Pulse will keep increasing the volume to several thousand percent, so here you can set a much more respectable number. Defaults to '200'.
  5. The 'autosync' variable. This will automatically sync all your sink-inputs (apps) with the current volume of your output (speakers) whenever you change the volume. This is useful if you manage multiple outputs and have issues with the app volumes becoming out of sync with the output (they should move up/down together). If you like keeping apps at different volumes then you should change this to no, and maybe bind a keystroke for manual sync if you so desire.

If you wish to use kosd it's in portage for Gentoo:
  • emerge -av kosd
  • After installation you have to restart kde to get it to load. 

If you wish to remove the volume cap (IE - have your max volume over 100%) you will need to apply the following patch to kosd. If you're using Gentoo (and you probably should be), all you need to do is put this patch in the `/etc/portage/patches/kde-misc/kosd/` directory.

You can also download the maxvol.patch here : http://pastebin.com/yCgVtmUZ




  • Afterwards go to systemsettings -> Shortcuts and Gestures -> Custom Shortcuts -> Add new shortcuts bound to the commands listed above.

You can also download the script here : http://pastebin.com/ZqWt3A5q

switchaudio.sh - switching pulseaudio output

A script if you use multiple audio output devices and Pulseaudio. For instance USB  headphones and a regular internal sound card. This script makes it easy to switch between the different cards, and update any current applications to use the new output device. 

To execute just copy to a bin directory like `/usr/local/bin` and run `switchaudio.sh --next`.

You can bind this script to a key shortcut in KDE : systemsettings -> Shortcuts and Gestures

You can also find it: http://pastebin.com/a6T3ezFs