This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Desktop Software

Adding a UPS to a desktop Linux machine

By Ben Martin on February 29, 2008 (4:00:00 PM)

Share    Print    Comments   

An uninterruptible power supply (UPS) will allow your computer to continue to function for a period of time when mains power is lost. This can help you to smooth over short-term (1-5 minute) loss of power by running from the UPS battery. When the UPS battery is running low, the UPS can signal your computer to shut down cleanly. With a UPS you can avoid lengthly filesystem or RAID checks due to abrupt power loss. Here are some tips on UPSes in general and how to set one up to protect a Linux machine, even if the model you have lacks explicit Linux support.

Nowadays, you can acquire a UPS for your desktop system for $100-$200, depending on its features. Most cheaper UPSes will power a computer for only 4-5 minutes, so they're best used to smooth over of short-term power losses and brownouts and to allow a computer system to shut down properly in the event of a longer-term power loss. This later point is especially important when you're running RAID-5, because having the data on individual disks of such a RAID out of sync might lead to data loss if it's not properly addressed.

The main things to look for when investigating which UPS to get are the initial cost, the cost of replacement batteries and the frequency with which you'll have to replace them, the ability to manage and monitor the UPS from Linux, and the watts and volt-amps provided.

The batteries in a UPS degrade over time, resulting in a loss in the total power capacity of the UPS. You might have to replace the batteries in the UPS in three to five years. If you only need to run a machine for five minutes and have the choice of a UPS that can run a machine for seven minutes or one that can give you 10, you can get away without replacing the batteries in the larger capacity UPS for a longer time -- though the batteries for the larger UPS will likely be more expensive as well.

Working out the volt amps (VA) and watts that you require by hand is not exactly straightforward. You must ensure that the UPS can provide more VA and watts than you intend to draw from it. The easiest way to work out what VA and watts you need is to use this Web form offered by American Power Conversion. One downside to the APC Web form is that you cannot select which graphics card your system will be running. While you might not be planning to protect a machine running a 3-D game with a UPS, you might have a high-end graphics card in your Linux machine, and it would be nice to know that it is covered by your UPS.

You don't strictly need the ability to manage and monitor the UPS, but it can make things easier. If your UPS has no software support for Linux, you can use handcrafted methods to detect when the mains power has been lost. The following method was suggested to me by Robert Murphy, who had to use a UPS with no software support to protect a server. The solution in its original form was to attach a modem to the mains power and connect it to the Linux machine, then have the Linux machine monitor the data set ready (DSR) line on the modem. When the mains power is lost, the Linux machine will still be running because it is connected to the UPS. The modem, which is not connected to the UPS, will lose its power source. The Linux machine will detect that the modem is no longer powered the next time it polls the DSR line and start to shut down while the UPS battery still has power.

These days however, you should expect Linux support from your UPS. The software should be able to tell you the battery status and current load on the UPS. Such information is handy for setting up the machine to continue to run until there is only two minutes' worth of power left in the UPS, and for sending out messages that the system will shut down in N minutes unless power is restored.

You can use apcupsd for many APC UPS units, OpenUPSd for Belkin UPS, or Network UPS Tools to interact with many types of UPS. Network UPS Tools (NUT) has many clients, including one for KDE (shown in figure), another standalone client, and a GKrellM plugin. NUT can communicate with a wide range of UPS devices. Using NUT can make it convenient to add another UPS to a network because you can use the same software to interact with and monitor both units over the network. Another plus side of NUT is that your knowledge of UPS software setup and management is not locked in to a particular vendor. On the other hand, you may not be able to get at all the features and information for your particular UPS unit with NUT.

For this article I'll use NUT, and I'll be setting up an APC Smart-UPS SC620.

You can install NUT packages for Ubuntu Gutsy, Fedora 8, and openSUSE. The KDE NUT client, knutclient, is packaged only for Ubuntu and some releases of Mandriva. On a Fedora machine you might want to install the packages nut and nut-client.

Installing the UPS hardware involves connecting a single terminal on the UPS battery, plugging the UPS into the mains power, and connecting the UPS to the computer using either an RS-232 serial interface or USB. A conservative approach is to not immediately power the computer from the UPS but perform an off-line power failure simulation first. More on this shortly.

The NUT daemon has a collection of configuration files in either /etc/nut or /etc/ups, depending on how your NUT was packaged. Unfortunately you must make changes to four configuration files in order to use your UPS. All of the files are well commented, so I will only mention the sorts of changes that are needed.

The ups.conf file should contain a section with the driver name and port to use to communicate with your UPS. The main changes you will likely have to make to upsd.conf are to ensure that the ACLs allow you to connect to the NUT daemon, at least allowing connections from localhost. The upsd.users file is where you set up usernames and passwords and assign which actions each user can perform. You will need to have at least one user in upsd.users so that the monitoring daemon can connect. The fourth configuration file is upsmon.conf in which you tell NUT which UPS units to monitor. The upsmon.conf needs to use the monitoring username and password from upsd.users.

# vi /etc/ups/ups.conf ... [ups] driver = apcsmart port = /dev/ttyS0 # vi /etc/ups/upsd.conf ... ACL all 0.0.0.0/0 ACL localhost 127.0.0.1/32 ACL localnet 192.168.7.0/24 ACCEPT localhost ACCEPT localnet REJECT all ... # vi /etc/ups/upsd.users ... [monuser] password = xxx allowfrom = localhost upsmon master ... # vi /etc/ups/upsmon.conf ... MONITOR ups@localhost 1 monuser xxx master ...

You would think that the packages could have supplied configuration files in a state such that the latter three files would not require changes. If the default were to monitor a UPS named ups on localhost, then the passwords, usernames, and ACLs could have been set up to allow localhost to monitor such a UPS. As you are not likely to ever want to manually use the password for the upsmonitor user, the packages could have just chosen a random string for that password and set up both upsd.users and upsmon.conf for you. As it stands there is a fair amount of busywork you must do beyond just selecting the device and port in ups.conf.

You will find mentions of /etc/killpower throughout the NUT documentation. The main reason for this file is so that just before the computer is about to switch itself off it can tell the UPS itself to shutdown. On Fedora the /etc/init.d/halt script is already set up to do the right thing if the /etc/killpower file exists. On other distributions, a quick grep in /etc/init.d or a similar directory for the killpower string may show that the presence of this file is already handled for you. If /etc/killpower is not handled in your shutdown scripts then the UPS will remain on when the computer shuts down. This is not critical but may cause the UPS to slowly continue to use battery power until it runs out.

With the configuration files in place you should be able to start the NUT daemon by executing service ups start. Assuming that the UPS hardware is on and connected to the computer and that you have named your ups simply "ups" as shown above, you can query its settings with upsc ups. Partial output is shown below. Note that I have changed the battery alarm to sound only when the UPS is running out of battery power. The default is for the UPS to sound an audible alert whenever you lose mains power.

# upsc ups battery.alarm.threshold: L ups.load: 049.4 ups.model: Smart-UPS SC620

The reason to not connect the computer to the UPS right from the start is so that you can conduct an off-line test, in which the computer power is still plugged directly into the wall but the computer is monitoring the UPS and is set to shut itself down cleanly when the UPS alerts the computer that it is running out of battery power. The UPS will alert the computer to shut down only when the UPS battery is running low, so you need to ensure that you have something that will draw power from the UPS during the off-line test -- perhaps an old CRT monitor or two. With a monitor running from the UPS, the UPS serial or USB cable connected to the computer, and the computer drawing its power not from the UPS you can start the off-line test. Make sure that the UPS daemon is running, and run tail -f /var/log/messages. Switch off the power to the UPS itself. You should see a message that the UPS has lost mains power and is running on battery. Now you can keep monitoring the battery.charge by running upsc ups periodically. You should see that value steadily dropping, compliments of the monitor drawing its power from the UPS battery. At a certain point the UPS will tell the computer that it has reached a low battery threshold. At this stage NUT should kick in and run shutdown -h now.

The /var/log/messages file should contain entries similar to the following during the off-line test:

... upsmon[29384]: UPS ups@localhost on battery ... ... upsmon[29384]: UPS ups@localhost battery is low ... upsmon[29384]: Executing automatic power-fail shutdown ... upsmon[29384]: Auto logout and shutdown proceeding ... shutdown[32512]: shutting down for system halt

The off-line test is invaluable for verifying that the computer and UPS are communicating correctly. It is comforting to know that the computer will properly shutdown at the UPS's request before connecting the computer power to the UPS. While the computer is still off after the off-line test is a great opportunity to connect the computer power to run off the UPS. Don't forget to ensure that the NUT daemon is run at boot by issuing the command chkconfig ups on.

A UPS can keep short-term power losses from affecting your computer. By employing a UPS and using these techniques, you can avoid filesystem checks and RAID-5 revalidations after a power-induced shutdown.

Ben Martin has been working on filesystems for more than 10 years. He completed his Ph.D. and now offers consulting services focused on libferris, filesystems, and search solutions.

Share    Print    Comments   

Comments

on Adding a UPS to a desktop Linux machine

Note: Comments are owned by the poster. We are not responsible for their content.

Adding a UPS to a desktop Linux machine

Posted by: Anonymous [ip: 196.206.19.83] on February 29, 2008 07:51 PM
Thank you !

#

Adding a UPS to a desktop Linux machine

Posted by: Anonymous [ip: 213.49.144.27] on March 01, 2008 01:15 PM
Small warning: I always heard that the degauss from old CRT monitors is not UPS-friendly. I could be wrong.

#

Adding a UPS to a desktop Linux machine

Posted by: Anonymous [ip: 69.39.102.36] on March 01, 2008 03:21 PM
To take the guesswork out of calculating power consumption, get a Killawatt. They are about 30 bucks and show power consumption in watts, VA, amps, etc.. Good for sizing of a UPS and calculating power costs. Good for lots of other things around the house too.

#

Adding a UPS to a desktop Linux machine

Posted by: Anonymous [ip: 85.210.172.193] on March 02, 2008 11:26 AM
Here in the north of Scotland ,UK we had numerous brief power cuts (also called brown-outs ) due to bird strikes during the migration season. A UPS was essential It is important that the UPS has a VA capacity in excess of that required for the computer equipment it protects.

Last year the 'brown-out' problem largely disappeared when the power company replaced the High Voltage (11kV) bare copper wires by insulated wires in local areas where birds rest during their migratory movements. Yet on occasions ,especially during winter storms, the UPS still provides excellent service ,especially in this rural area. When specifying a UPS check the power cut period it can/needs to cover (usually up to 15 minutes , depending on the capacity of the internal lead-acid batteries) .

Highland Ham

#

Brownouts...

Posted by: Anonymous [ip: 138.104.36.1] on March 05, 2008 01:39 PM
I'd just like to correct you here, a brownout is when the power levels *drop*, not when you lose power completely. Brownouts are particularly damaging as if you have some high-end equipment (audio equipment being particularly vulnerable) it will still draw a small amount of power - lots of dead speakers and amps when we had a few brownouts here.
Apart from that, great article, very useful. The other way I had read (possibly here...) was; if you have a second machine you can spare, set the main one on the UPS, the second on the mains, and run a script to periodically ping (or query in some fashion) the second computer (on mains power) to see if it's still up, and if it isn't to try again a couple of times, then gracefully shut down.

#

Adding a UPS to a desktop Linux machine

Posted by: Anonymous [ip: 121.222.148.231] on March 02, 2008 04:08 PM
well i used to live in Lebanon. there we have channaling of the power. it's rotated over the areas each six hours. well we use not a UPS but what we call there inverters which can last around 3 hours on a six wheel truck battery, and they does the same as the UPS. the problem with them is that they are nationally made and they dont have any intelligent controllers.

#

nitpicking on "packages"

Posted by: Michael Shigorin on March 03, 2008 01:38 PM
"The KDE NUT client, knutclient, is packaged only for Ubuntu and some releases of Mandriva." -- seems like the author considers his quite limited distro knowledge as universal. :) Of course, all of these "onlys" are funny. I don't run ubuntu or any release of mandriva but still can (occasionally do) just apt-get install knutclient. No, it's not Debian either (for those more knowledgeable). :) In our distro there's a system setup module for NUT, even...

#

Adding a UPS to a desktop Linux machine

Posted by: Anonymous [ip: 68.88.48.126] on March 03, 2008 07:59 PM
People who are looking for more power for the buck and don't mind missing out on 'intelligent' controllers might consider doing something like this: http://icrontic.com/articles/super_ups

Building your own out of deep cycle batteries means far better capacity, while sacraficing the fun of monitoring it via the PC.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya