home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 8 / CDACTUAL8.iso / docs / ups-howt < prev    next >
Encoding:
Text File  |  1996-07-11  |  63.6 KB  |  1,596 lines

  1.   The UPS Howto
  2.   Harvey J. Stein, hjstein@math.huji.ac.il, Berger Financial Research,
  3.   Ltd.
  4.   v1.3, 16 April 1995
  5.  
  6.   This document will help you connect an uninterruptable power supply to
  7.   a Linux box... if you're lucky...  Copyright (c) 1994 by Harvey J.
  8.   Stein.  You may use this document as you see fit, as long as it
  9.   remains intact.  In particular, this notice (along with the contribu¡
  10.   tions below) must remain untouched.
  11.  
  12.   1.  Introduction
  13.  
  14.   This HOWTO covers connecting a UPS to a PC running Linux.  The idea is
  15.   to connect the two in such a way that Linux can shutdown cleanly when
  16.   the power goes out.  To a large extent this document is reduntant,
  17.   because all the basic info is contained in the powerd man page that
  18.   comes with the SysVinit package.  None the less, there seems to
  19.   periodically be alot of discussion on the net regarding connecting
  20.   Linux PCs to UPSs (and the versions of Linux that I installed didn't
  21.   come with a powerd man page).  I figured having a HOWTO would be a
  22.   good idea because:
  23.  
  24.   ╖  A second source of information might help to understand how to
  25.      connect Linux to a UPS, even if it's just the same information
  26.      written differently.
  27.  
  28.   ╖  The HOWTO can serve as a repository for UPS specific data.
  29.  
  30.   ╖  The HOWTO contains additional details that aren't in the powerd man
  31.      page.
  32.  
  33.   None the less, this does not replace the powerd man page.  Hopefully,
  34.   after reading both, people will be able to deal with UPSs.
  35.  
  36.   1.1.  Contributors
  37.  
  38.   I am forever indebted to those from whom I've received help,
  39.   suggestions, and UPS specific data.  The list includes, in order of
  40.   appearance in this document:
  41.  
  42.   ╖  Miquel van Smoorenburg (miquels@cistron.nl.mugnet.org)
  43.  
  44.   ╖  Danny ter Haar (danny@caution.cistron.nl.mugnet.org)
  45.  
  46.   ╖  Hennus Bergman (hennus@sky.nl.mugnet.org)
  47.  
  48.   ╖  Tom Webster (webster@kaiwan.com)
  49.  
  50.   ╖  Marek Michalkiewicz (ind43@sun1000.ci.pwr.wroc.pl)
  51.  
  52.   ╖  Christian G. Holtje (docwhat@uiuc.edu)
  53.  
  54.   ╖  Ben Galliart (bgallia@orion.it.luc.edu)
  55.  
  56.   ╖  Lam Dang (angit@netcom.com)
  57.  
  58.   1.2.  Important disclaimer
  59.  
  60.   I really can't guarantee that any of this will work for you.
  61.   Connecting a UPS to a computer can be a tricky business.  One or the
  62.   other or both might burn out, blow up, catch fire, or start World War
  63.   Three.  Furthermore, I only have direct experience with the Advice
  64.   1200 A UPS, and I didn't have to make a cable.  So, BE CAREFUL.
  65.   GATHER ALL INFORMATION YOU CAN ON YOUR UPS.  THINK FIRST.  DON'T
  66.   IMPLICITLY TRUST ANYTHING YOU READ HERE.
  67.  
  68.   On the other hand, I managed to get everything working with my UPSes,
  69.   without much information from the manufacturer, and without burning
  70.   out anything, so it is possible.
  71.  
  72.   1.3.  Other documents
  73.  
  74.   This document does not cover the general features and capabilities of
  75.   UPSs.  For this type of information, you might turn to the UPS.faq,
  76.   which is available via anonymous FTP from navigator.jpl.nasa.gov
  77.   (128.149.23.82) in pub/doc/faq/UPS.faq, and can probably be found
  78.   somewhere on rtfm.mit.edu.  It is maintained by Nick Christenson
  79.   (npc@minotaur.jpl.nasa.gov).  In email to him, he'd like that you put
  80.   UPS or UPS FAQ or something along these lines in the Subject line of
  81.   the message.
  82.  
  83.   2.  What you need to do (summary)
  84.  
  85.   ╖  Plug the PC into the UPS.
  86.  
  87.   ╖  Connect the PC's serial port to the UPS with a special cable.
  88.  
  89.   ╖  Run powerd on the PC.
  90.  
  91.   ╖  Setup your initd to do something reasonable on powerfail and
  92.      powerok events (like start a shutdown and kill any currently
  93.      running shutdowns, respectively, for example).
  94.  
  95.   3.  How it's supposed to work
  96.  
  97.      UPS's job
  98.         When the power goes out, the UPS continues to power the PC and
  99.         signals that the power went out by throwing a relay or turning
  100.         on an opticoupler on it's control port.
  101.  
  102.      Cable's job
  103.         The cable is designed so that when the UPS throws said relay,
  104.         this causes a particular serial port control line (typically
  105.         DCD) to go high.
  106.  
  107.      Powerd's job
  108.         Powerd monitors the serial port.  Keeps raised/lowered whatever
  109.         serial port control lines the UPS needs to have raised/lowered
  110.         (typically, DTR must be kept high and whatever line shuts off
  111.         the UPS must be kept low).  When powerd sees the UPS control
  112.         line go high, it writes FAIL to /etc/powerfail and sends the
  113.         initd process a SIGPWR signal.  When the control line goes low
  114.         again, it writes OK to /etc/powerfail and sends initd a SIGPWR
  115.         signal.
  116.  
  117.      Initd's job (aside from everything else it does)
  118.         When it receives a SIGPWR, it looks at /etc/powerfail.  If it
  119.         contains FAIL it runs the powerfail entry from /etc/inittab.  If
  120.         it contains OK it runs the powerokwait entry from inittab.
  121.  
  122.   4.  Where to get the appropriate software
  123.  
  124.   When I last looked, the package to pick up was
  125.   /pub/Linux/system/Daemons/SysVinit-2.50.tgz from sunsite.unc.edu or a
  126.   mirror.  It includes a copy of powerd.c, shutdown.c, an initd that
  127.   understands what to do with SIGPWR and can handle powerfail and
  128.   powerokwait entries in the inittab file.
  129.  
  130.   Of course, by now, I'm sure much newer versions are available.
  131.   Whatever you get might differ from the description here, but will
  132.   probably adhere to the overall philosophy.
  133.  
  134.   5.  How to set things up
  135.  
  136.   The following presupposes that you have a cable that works properly
  137.   with powerd.c.  If you're not sure that your cable works (or how it
  138.   works), see section ``Reverse-engineering cables and hacking
  139.   powerd.c'' for information on dealing with poorly described cables and
  140.   reconfiguring powerd.c.  Sections ``Serial port pin assignments'' and
  141.   ``Ioctl to RS232 correspondence'' will also be useful.
  142.  
  143.   If you need to make a cable, see section ``How to make a cable'' for
  144.   the overall details, and the subsection of section ``Info on selected
  145.   UPSs'' that refers to your UPS.  The latter might also include
  146.   information on manufacturer supplied cables.  You'll probably want to
  147.   read all of section ``Info on selected UPSs'' because each section has
  148.   afew additional generally helpful details.
  149.  
  150.   ╖  Edit /etc/inittab.  Put in something like this:
  151.  
  152.      # What to do when power fails (Halt system & drain battery :):
  153.      pf::powerfail:/etc/powerfailscript +5
  154.  
  155.      # If power is back before shutdown, cancel the running shutdown.
  156.      pg:0123456:powerokwait:/etc/powerokscript
  157.  
  158.   ╖  Write scripts /etc/powerfailscript and /etc/powerokscript to
  159.      shutdown in 5 minutes (or whatever's appropriate) and kill any
  160.      existing shutdown, respectively.  Depending on the version of
  161.      shutdown that you're using, this will be either so trivial that
  162.      you'll dispense with the scripts, or be a 1 line bash script,
  163.      something along the lines of:
  164.  
  165.          kill `ps -aux | grep "shutdown" | grep -v grep | awk '{print $2}'`
  166.  
  167.   and you'll keep the scripts.  (In case it doesn't come out right, the
  168.   first single quote on the above line is a backquote, the second and
  169.   third are single quotes, and the last is also a backquote.)
  170.  
  171.   ╖  Tell initd to re-process the inittab file with the command:
  172.  
  173.          telinit q
  174.  
  175.   ╖  Edit rc.local so that powerd gets run upon startup.  The syntax is:
  176.  
  177.          powerd <line>
  178.  
  179.   Replace <line> with the serial port that the modem is connected, such
  180.   as /dev/cua1.
  181.  
  182.   ╖  Connect PC's serial port to UPS's serial port.  DO NOT PLUG PC INTO
  183.      UPS YET.
  184.  
  185.   ╖  Plug a light into the UPS.
  186.  
  187.   ╖  Turn on the UPS and the light.
  188.  
  189.   ╖  Run powerd.
  190.  
  191.   ╖  Test the setup:
  192.  
  193.   ╖  Yank the UPS's plug.
  194.  
  195.   ╖  Check that the light stays on.
  196.  
  197.   ╖  Check that /etc/powerfailscript runs.
  198.  
  199.   ╖  Check that shutdown is running.
  200.  
  201.   ╖  Plug the UPS back in.
  202.  
  203.   ╖  Check that the light stays on.
  204.  
  205.   ╖  Check that /etc/powerokscript runs.
  206.  
  207.   ╖  Check that /etc/powerfailscript is not running.
  208.  
  209.   ╖  Check that shutdown is no longer running.
  210.  
  211.   ╖  Yank the UPS's plug again.  Leave it out and make sure that the PC
  212.      shuts down properly in the proper amount of time.
  213.  
  214.   ╖  The Dangerous Part.  After everything seems to be proper, powerdown
  215.      the PC and plug it into the UPS.  Run a script that sync's the hard
  216.      disk every second or so.  Simultaneously run a second script that
  217.      keeps doing a find over your entire hard disk.  The first is to
  218.      make this a little safer and the second is to help draw lots of
  219.      power.  Now, pull the plug on the UPS, check again that shutdown is
  220.      running and wait.  Make sure that the PC shuts down cleanly before
  221.      the battery on the UPS gives out.  This is dangerous because if the
  222.      power goes out before the PC shuts down, you can end up with a
  223.      corrupt file system, and maybe even lose all your files.  You'll
  224.      probably want to do a full backup before this test, and set the
  225.      shutdown time extremely short to begin with.
  226.  
  227.   Congratulations!  You now have a Linux PC that's protected by a UPS
  228.   and will shutdown cleanly when the power goes out!
  229.  
  230.   6.  User Enhancements
  231.  
  232.   ╖  Hack powerd.c to monitor the line indicating that the batteries are
  233.      low.  When the batteries get low, do an immediate shutdown.
  234.  
  235.   ╖  Modify shutdown procedure so that if it's shutting down in a
  236.      powerfail situation, then it turns off the UPS after doing
  237.      everything necessary.
  238.  
  239.   7.  How to make a cable
  240.  
  241.   This section is just from messages I've seen on the net.  I haven't
  242.   done it so I can't write from experience.  If anyone has, please write
  243.   this section for me :).  See also the message about the GPS1000
  244.   contained in section ``GPS1000 from ACCODATA''
  245.  
  246.      >From miquels@caution.cistron.nl.mugnet.org Wed Jul 21 14:26:33 1993
  247.      Newsgroups: comp.os.linux
  248.      Subject: Re: UPS interface for Linux?
  249.      From: miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg)
  250.      Date: Sat, 17 Jul 93 18:03:37
  251.      Distribution: world
  252.      Organization: Cistron Electronics.
  253.  
  254.      In article <1993Jul15.184450.5193@excaliber.uucp>
  255.      joel@rac1.wam.umd.edu (Joel M. Hoffman) writes:
  256.      >I'm in the process of buying a UPS (Uninteruptable Power Supply), and
  257.      >notice that some of them have interfaces for LAN's to signal the LAN
  258.      >when the power fails.
  259.      >
  260.      >Is there such an interface for Linux?
  261.      >
  262.      >Thanks.
  263.      >
  264.      >-Joel
  265.      >(joel@wam.umd.edu)
  266.      >
  267.  
  268.      When I worked on the last versioon of SysVinit (Now version 2.4),
  269.      I temporarily had a UPS on my computer, so I added support for it.
  270.      You might have seen that in the latest <signal.h> header files there
  271.      is a #define SIGPWR 30 now :-). Anyway, I did not have such a special
  272.      interface but the output of most UPS's is just a relais that makes or breaks
  273.      on power interrupt. I thought up a simple way to connect this to the
  274.      DCD line of the serial port. In the SysVinit package there is a daemon
  275.      called 'powerd' that keeps an eye on that serial line and sends SIGPWR
  276.      to init when the status changes, so that init can do something (such as
  277.      bringing the system down within 5 minutes). How to connect the UPS to
  278.      the serial line is described in the source "powerd.c", but I will
  279.      draw it here for explanation:
  280.  
  281.                           +------------------------o  DTR
  282.                           |
  283.                         +---+
  284.                         |   | resistor
  285.                         |   | 10 kilo-Ohm
  286.                         |   |
  287.                         +---+                                To serial port.
  288.                           |
  289.             +-----o-------+------------------------o  DCD
  290.             |             |
  291.             o  UPS        |
  292.           \    relais     |
  293.            \              |
  294.             |             |
  295.             +-----o-------+------------------------o  GND
  296.  
  297.      Nice drawing eh?
  298.  
  299.      Hope this helps.
  300.      SysVinit can be found on sunsite (and tsx-11 probably) as
  301.      SysVinit2.4.tar.z
  302.  
  303.      Mike.
  304.  
  305.      --
  306.  
  307.      Miquel van Smoorenburg, <miquels@cistron.nl.mugnet.org>
  308.      Ibmio.com: cannot open CONFIG.SYS: file handle broke off.
  309.  
  310.      >From danny@caution.cistron.nl.mugnet.org Wed Jul 21 14:27:04 1993
  311.      Newsgroups: comp.os.linux
  312.      Subject: Re: UPS interface for Linux?
  313.      From: danny@caution.cistron.nl.mugnet.org (Danny ter Haar)
  314.      Date: Mon, 19 Jul 93 11:02:14
  315.      Distribution: world
  316.      Organization: Cistron Electronics.
  317.  
  318.      In article <9307174330@caution.cistron.nl.mugnet.org>
  319.      miquels@caution.cistron.nl.mugnet.org (Miquel van Smoorenburg) writes:
  320.      >How to connect the UPS to the serial line is described in the source
  321.      >"powerd.c", but I will draw it here for explanation:
  322.  
  323.      The drawing wasn't really clear, please use this one in stead !
  324.      >
  325.      >                     +------------------------o  DTR
  326.      >                     |
  327.      >                   +---+
  328.      >                   |   | resistor
  329.      >                   |   | 10 kilo-Ohm
  330.      >                   |   |
  331.      >                   +---+                                To serial port.
  332.      >                     |
  333.      >       +-----o-------+------------------------o  DCD
  334.      >       |
  335.      >       o  UPS
  336.      >     \    relais
  337.      >      \
  338.      >       |
  339.      >       +-----o--------------------------------o  GND
  340.      >
  341.  
  342.      The DTR is kept high, when the UPS's power input is gone it
  343.      will close the relais . The computer is monitoring
  344.      the DCD input port to go LOW . When this happens it will start a
  345.      shutdown sequence...
  346.  
  347.      _____
  348.      Danny
  349.  
  350.      --
  351.      <=====================================================================>
  352.      Danny ter Haar  <dannyth@hacktic.nl> or <danny@cistron.nl.mugnet.org>
  353.      Robins law #103: 'a couple of lightyears can't part good friends'
  354.  
  355.   8.  Reverse-engineering cables and hacking powerd.c
  356.  
  357.   Try to get documentation for the cables that your UPS seller supplies.
  358.   In particular find out:
  359.  
  360.   ╖  What lines need to be kept high.
  361.  
  362.   ╖  What line(s) turn off the UPS.
  363.  
  364.   ╖  What lines the UPS toggles to indicate that:
  365.  
  366.   ╖  Power is out.
  367.  
  368.   ╖  Battery is low.
  369.  
  370.   You then need to hack powerd.c appropriately.
  371.  
  372.   If you have trouble getting the above information, or just want to
  373.   check it (a good idea) the following program  might help.  It's a
  374.   hacked version of powerd.c.  It allows you to set the necessary port
  375.   flags from the command line and then monitors the port, displaying the
  376.   control lines every second.  I used it as "upscheck /dev/cua1 2" (for
  377.   example) to set the 2nd bit (DTR) and to clear the other bits.  The
  378.   number base 2 indicates which bits to set, so for example to set bits
  379.   1, 2 and 3, (and clear the others) use 7.  See the code for details.
  380.  
  381.   Here's the (untested) upscheck.c program.  It's untested because I
  382.   edited the version I originally used to make it clearer, and can't
  383.   test the new version at the moment.
  384.  
  385.   ______________________________________________________________________
  386.   /*
  387.    * upscheck     Check how UPS & computer communicate.
  388.    *
  389.    * Usage:       upscheck <device> <bits to set>
  390.    *              For example, upscheck /dev/cua4 4 to set bit 3 &
  391.    *              monitor /dev/cua4.
  392.    *
  393.    * Author:      Harvey J. Stein <hjstein@math.huji.ac.il>
  394.    *              (but really just a minor modification of Miquel van
  395.    *              Smoorenburg's <miquels@drinkel.nl.mugnet.org> powerd.c
  396.    *
  397.    * Version:     1.0 19940802
  398.    *
  399.    */
  400.   #include <sys/types.h>
  401.   #include <sys/ioctl.h>
  402.   #include <fcntl.h>
  403.   #include <errno.h>
  404.   #include <stdlib.h>
  405.   #include <unistd.h>
  406.   #include <stdio.h>
  407.   #include <signal.h>
  408.  
  409.   /* Main program. */
  410.   int main(int argc, char **argv)
  411.   {
  412.     int fd;
  413.  
  414.   /*  These TIOCM_* parameters are defined in <linux/termios.h>, which  */
  415.   /*  is indirectly included here.                                      */
  416.     int dtr_bit = TIOCM_DTR;
  417.     int rts_bit = TIOCM_RTS;
  418.     int set_bits;
  419.     int flags;
  420.     int status, oldstat = -1;
  421.     int count = 0;
  422.     int pc;
  423.  
  424.     if (argc < 2) {
  425.           fprintf(stderr, "Usage: upscheck <device> <bits-to-set>\n");
  426.           exit(1);
  427.     }
  428.  
  429.     /* Open monitor device. */
  430.     if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  431.       fprintf(stderr, "upscheck: %s: %s\n", argv[1], sys_errlist[errno]);
  432.       exit(1);}
  433.  
  434.     /* Get the bits to set from the command line. */
  435.     sscanf(argv[2], "%d", &set_bits);
  436.  
  437.     while (1) {
  438.       /* Set the command line specified bits (& only the command line */
  439.       /* specified bits).                                             */
  440.       ioctl(fd, TIOCMSET, &set_bits);
  441.       fprintf(stderr, "Setting %o.\n", set_bits);
  442.  
  443.       sleep(1);
  444.  
  445.       /* Get the current line bits */
  446.       ioctl(fd, TIOCMGET, &flags);
  447.       fprintf(stderr, "Flags are %o.\n", flags);
  448.  
  449.   /*  Fiddle here by changing TIOCM_CTS to some other TIOCM until    */
  450.   /*  this program detects that the power goes out when you yank     */
  451.   /*  the plug on the UPS.  Then you'll know how to modify powerd.c. */
  452.       if (flags & TIOCM_CTS)
  453.         {
  454.           pc = 0 ;
  455.           fprintf(stderr, "power is up.\n");
  456.         }
  457.       else
  458.         {
  459.           pc = pc + 1 ;
  460.           fprintf(stderr, "power is down.\n");
  461.         }
  462.       }
  463.  
  464.     close(fd);
  465.   }
  466.   ______________________________________________________________________
  467.  
  468.   9.  Serial port pin assignments
  469.  
  470.   The previous section presupposes knowledge of the correspondence
  471.   between terminal signals and serial port pins.  Here's a reference for
  472.   that correspondence, taken from David Tal's <GSRGAAO@TECHNION.BITNET>
  473.   ``Frequently Used Cables and Connectors'' document.  I'm including a
  474.   diagram illustrating the connectors, and a table listing the
  475.   correspondence between pin numbers and terminal line signals.
  476.  
  477.   |       |        |        |       |         |          |                                |
  478.   |DB-25  |  DB-9  |  Name  |  EIA  |  CCITT  |  DTE-DCE |  Description                   |
  479.   |Pin #  |  Pin # |        |       |         |          |                                |
  480.   |________|_________|_________|________|__________|___________|_______________                  |
  481.   |1      |        |  FG    |  AA   |  101    |  ---     | Frame Ground/Chassis GND       |
  482.   |2      |  3     |  TD    |  BA   |  103    |  --->    | Transmitted Data, TxD          |
  483.   |3      |  2     |  RD    |  BB   |  104    |  <---    |  Received Data, RxD            |
  484.   |4      |  7     |  RTS   |  CA   |  105    |  --->    | Request To Send                |
  485.   |5      |  8     |  CTS   |  CB   |  106    |  <---    |  Clear To Send                 |
  486.   |6      |  6     |  DSR   |  CC   |  107    |  <---    |  Data Set Ready                |
  487.   |7      |  5     |  SG    |  AB   |  102    |  ----    | Signal Ground, GND             |
  488.   |8      |  1     |  DCD   |  CF   |  109    |  <---    |  Data Carrier Detect           |
  489.   |9      |        |  --    |  --   |  -      |  -       | Positive DC test voltage       |
  490.   |10     |        |  --    |  --   |  -      |  -       | Negative DC test voltage       |
  491.   |11     |        |  QM    |  --   |  -      |  <---    |  Equalizer mode                |
  492.   |12     |        |  SDCD  |  SCF  |  122    |  <---    |  Secondary Data Carrier Detect |
  493.   |13     |        |  SCTS  |  SCB  |  121    |  <---    |  Secondary Clear To Send       |
  494.   |14     |        |  STD   |  SBA  |  118    |  --->    | Secondary Transmitted Data     |
  495.   |15     |        |  TC    |  DB   |  114    |  <---    |  Transmitter (signal) Clock    |
  496.   |16     |        |  SRD   |  SBB  |  119    |  <---    |  Secondary Receiver Clock      |
  497.   |17     |        |  RC    |  DD   |  115    |  --->    | Receiver (signal) Clock        |
  498.   |18     |        |  DCR   |  --   |  -      |  <---    |  Divided Clock Receiver        |
  499.   |19     |        |  SRTS  |  SCA  |  120    |  --->    | Secondary Request To Send      |
  500.   |20     |  4     |  DTR   |  CD   |  108.2  |  --->    | Data Terminal Ready            |
  501.   |21     |        |  SQ    |  CG   |  110    |  <---    |  Signal Quality Detect         |
  502.   |22     |  9     |  RI    |  CE   |  125    |  <---    |  Ring Indicator                |
  503.   |23     |        |  --    |  CH   |  111    |  --->    | Data rate selector             |
  504.   |24     |        |  --    |  CI   |  112    |  <---    |  Data rate selector            |
  505.   |25     |        |  TC    |  DA   |  113    |  <---    |  Transmitted Clock             |
  506.  
  507.       Pin Assignment for the Serial Port (RS-232C), 25-pin and 9-pin
  508.  
  509.   ______________________________________________________________________
  510.  
  511.           1                         13         1         5
  512.         _______________________________      _______________
  513.         \  . . . . . . . . . . . . .  /      \  . . . . .  /    RS232-connectors
  514.          \  . . . . . . . . . . . .  /        \  . . . .  /     seen from outside
  515.           ---------------------------          -----------      of computer.
  516.           14                      25            6       9
  517.  
  518.      DTE : Data Terminal Equipment (i.e. computer)
  519.      DCE : Data Communications Equipment (i.e. modem)
  520.      RxD : Data received; 1 is transmitted "low", 0 as "high"
  521.      TxD : Data sent; 1 is transmitted "low", 0 as "high"
  522.      DTR : DTE announces that it is powered up and ready to communicate
  523.      DSR : DCE announces that it is ready to communicate; low=modem hangup
  524.      RTS : DTE asks DCE for permission to send data
  525.      CTS : DCE agrees on RTS
  526.      RI  : DCE signals the DTE that an establishment of a connection is attempted
  527.      DCD : DCE announces that a connection is established
  528.   ______________________________________________________________________
  529.  
  530.   10.  Ioctl to RS232 correspondence
  531.  
  532.   Since you also might need to modify powerd.c to raise and lower the
  533.   correct lines, you might also need the numeric values of different
  534.   terminal signals.  The can be found in /usr/include/linux/termios.h,
  535.   but are reproduced here for reference.  Since they could change,
  536.   you're best off confirming these values against said file.
  537.  
  538.        ______________________________________________________________________
  539.        /* modem lines */
  540.        #define TIOCM_LE        0x001
  541.        #define TIOCM_DTR       0x002
  542.        #define TIOCM_RTS       0x004
  543.        #define TIOCM_ST        0x008
  544.        #define TIOCM_SR        0x010
  545.        #define TIOCM_CTS       0x020
  546.        #define TIOCM_CAR       0x040
  547.        #define TIOCM_RNG       0x080
  548.        #define TIOCM_DSR       0x100
  549.        #define TIOCM_CD        TIOCM_CAR
  550.        #define TIOCM_RI        TIOCM_RNG
  551.        ______________________________________________________________________
  552.  
  553.   Note that the 3rd column is in Hex.
  554.  
  555.   11.  Info on selected UPSs
  556.  
  557.   This section contains UPS specific information.  What I'd like is to
  558.   have the UPS control port information (what each pin does and needs to
  559.   have done), information on the manufacturer supplied cable (what it
  560.   connects where), and a hacked version of powerd.c which works with the
  561.   UPS.  What I currently have is fairly complete descriptions of setting
  562.   up each UPS.  I'd try to distill out the relevant information, but
  563.   since I can't test each UPS, it's hard to decide exactly what's
  564.   relevant.  Furthermore, each UPS seems to have some additional quirks
  565.   that are nicely described by the authors of each section.  So for now
  566.   I'm leaving everything in.  Makes for a hefty Howto.
  567.  
  568.   Please send me your experiences for inclusion here.
  569.  
  570.   11.1.  General Experiences.
  571.  
  572.   I've been saving peoples comments, but haven't gotten permission yet
  573.   to include them here.  Here's a general summary of what I've heard
  574.   from people.
  575.  
  576.   APC: Won't release info on their ``smart'' mode without your signature
  577.   on a non-disclosure agreement.  Thus, people are forced to run their
  578.   ``smart'' UPSes in the ``dumb'' mode as outlined above.
  579.  
  580.   Tripp Lite: One person reported that Tripp lite won't release info
  581.   either.
  582.  
  583.   Upsonic: One person reported that Upsonic has discussed technical
  584.   details over the phone, answered questions via fax and are generally
  585.   helpful.
  586.  
  587.   11.2.  Advice 1200 A
  588.  
  589.   UPS from Advice Electronics, Tel Aviv Israel (they stick their own
  590.   name on the things).
  591.  
  592.   UPS Control Port's pin specifications.
  593.  
  594.   ╖  2 - Power Fail.
  595.  
  596.   ╖  5 - Battery Low.
  597.  
  598.   ╖  6 - Shut Down UPS.
  599.  
  600.   ╖  4 - Common ground for pin 2, 5, 6.
  601.  
  602.   They also gave me the following picture which didn't help me, but may
  603.   help you if you want to build a cable yourself:
  604.  
  605.   ______________________________________________________________________
  606.            2 ----------+
  607.                        |
  608.                        \
  609.                         \|
  610.                          |--------------
  611.                         /|
  612.                       \/      <--- The "\/" here indicates the type of
  613.                       |            this transister.  I forget what
  614.                       |            denotes what, but this one points
  615.                    +-----+         away from the center line.
  616.                   /  /  /
  617.  
  618.            5 ----------+
  619.                        |
  620.                        \
  621.                         \|
  622.                          |--------------
  623.                         /|
  624.                       \/
  625.                       |
  626.                       |
  627.                    +-----+
  628.                   /  /  /
  629.  
  630.                          +-------------
  631.                          |
  632.                          /
  633.                 10K    |/
  634.            6 --\/\/\/--|
  635.                        |\
  636.                          \/
  637.                          |
  638.                          |
  639.                       +-----+
  640.                      /  /  /
  641.  
  642.            4 ----------+
  643.                        |
  644.                        |
  645.                     +-----+
  646.                    /  /  /
  647.  
  648.   ______________________________________________________________________
  649.  
  650.   Cable supplied
  651.  
  652.   They first gave me a cable that was part of a DOS UPS control package
  653.   called RUPS.  I used this for testing.  When I was satisfied, they
  654.   gave me a cable they use for Netware servers connected to UPSs.  It
  655.   functioned identically.  Here are the details:
  656.  
  657.   ╖  DTR - Powers cable (make powerd.c keep it high).
  658.  
  659.   ╖  CTS - Power out (stays high and goes low when power goes out).
  660.  
  661.   ╖  DSR - Battery low (stays high.  Goes low when battery does).
  662.  
  663.   ╖  RTS - Turns off UPS (keep it low.  Set it high to turn off UPS).
  664.  
  665.   (The powerd.c that comes with SysVinit set or left RTS high, causing
  666.   the UPS to shut off immediately when powerd was started up!)
  667.  
  668.   11.3.  GPS1000 from ACCODATA
  669.  
  670.      >From hennus@sky.nl.mugnet.org Thu Mar 10 15:10:22 1994
  671.      Newsgroups: comp.os.linux.help
  672.      Subject: Re: auto-shutdown with UPS
  673.      From: hennus@sky.nl.mugnet.org (Hennus Bergman)
  674.      Date: Tue, 1 Mar 1994 22:17:45 GMT
  675.      Distribution: world
  676.      Organization: The Organization For Removal Of On-Screen Logos
  677.  
  678.      In article <CRAFFERT.94Feb28125452@nostril.lehman.com>,
  679.      Colin Owen Rafferty <craffert@nostril.lehman.com> wrote:
  680.      >I am about to buy an Uninterruptable Power Supply for my machine, and
  681.      >I would like to get one that has the "auto-shutdown" feature.
  682.      >
  683.      I just got one of those real cheap :-)
  684.      It's a GPS1000 by ACCODATA. Anybody know how good the output
  685.      signal of these things is? [Don't have a scope myself :-(]
  686.  
  687.      >I assume that these each have some kind of serial connection that
  688.      >tells the system information about it.
  689.      >
  690.      I took it apart to find out how it worked. There were three optocouplers
  691.      (two output, one input) connected to a 9 pin connector at the back.
  692.      One turns on when the power fails, and goes off again when the power
  693.      returns. While the power is off, you can use the `input' to shut the
  694.      battery off. [It releases the power-relay.] The third one is some kind
  695.      of feedback to tell that it did accepted the `shut-down command'.
  696.      I think the interface for my UPS was designed to be connected to TTL-level
  697.      signals, but with some resistors it could be connected to serial port.
  698.      It's wired in such a way that using a RS-232 port you cannot use both
  699.      output optocouplers; but the shutdown feedback is not necessary anyway,
  700.      just use the important one. ;-)
  701.      [Note that it is possible to blow the transistor part in optocouplers
  702.      with RS-232 levels if you wire it the wrong way round ;-)]
  703.  
  704.      I was hoping I would be able to connect it to my unused game port,
  705.      but that doesn't have an output, does it?
  706.      I'll probably end up getting an extra printer port for this.
  707.  
  708.      Not all UPS' use optocouplers, some use simple relays, which are
  709.      less critical to connect, but of course not as `nice'.
  710.  
  711.      >Has anyone written a package that watches the UPS and does a shutdown
  712.      >(or something) when the power is off?
  713.      SysVinit-2.4 (and probably 2.5 as well) has a `powerd' daemon that
  714.      continually watches a serial port for presence of the CD (Carrier
  715.      Detect) line and signals init when it drops. Init then activates
  716.      shutdown with a time delay. If the power returns within a few minutes
  717.      the shutdown is cancelled. Very Nice.
  718.      The only problem I had with it is that it doesn't actually tell the
  719.      UPS to turn off when the shutdown is complete. It just sits there with
  720.      a root prompt. I'll probably write a small program to shut it down
  721.      >from /etc/brc. RSN.
  722.  
  723.      >    Colin Rafferty, Lehman Brothers <craffert@lehman.com>
  724.  
  725.      Hennus Bergman
  726.  
  727.   11.4.  TrippLite BC750LAN (Standby UPS)
  728.  
  729.   From: Tom Webster <webster@kaiwan.com>
  730.   To: hjstein@MATH.HUJI.AC.IL (Harvey J. Stein)
  731.   Subject: Re: Help - Powerd & UPS - Help
  732.   Date: Mon, 8 Aug 1994 12:26:09 -0700 (PDT)
  733.  
  734.   Harvey,
  735.  
  736.   First off, let me say that I enjoyed reading your HOWTO.  It is about
  737.   what I'd hoped my document might grow into.  I wrote my pseudo-HOWTO
  738.   late on night because I kept seeing the "Can I hook a UPS up to
  739.   Linux...." message, about once a month on c.o.l.*.  Mine deals
  740.   specifically with hooking one vendor's model of UPS (Tripplite's
  741.   BCxxx/LAN series) to a Linux box and making powerd work with it.
  742.  
  743.   It is in need of some upkeep, things have been a little hectic.  Now
  744.   that I have posted it three or four time in response to questions, I'm
  745.   finally getting some feedback (which catches these errors).  The
  746.   problems that I know it has so far are:
  747.  
  748.   The proper version of SysVinit is 2.4, not 2.04.
  749.  
  750.   There is some argument about whether one or more resistors are needed
  751.   in my cable.  The only place I really see that it might be needed is
  752.   in the inverter shutoff, to make sure that I don't send too much
  753.   voltage to the UPS.  For the sensor circuit I don't see why a DTE
  754.   device can't stand to have its signal looped back to it.  All that I'm
  755.   doing is connecting a line that is held high to the Carrier Detect
  756.   line.
  757.  
  758.   I should be a little clearer about how the UPS acts when it goes into
  759.   powerfail mode.  The Tripplites provide both an open and a closed
  760.   circuit on powerfail, two different pins and a common negative pin.
  761.   Thus all I have to do is wire a pin that it held high to the carrier
  762.   detect line and route this through the UPS's open on powerfail
  763.   circuit, to cause the carrier detect to drop.  I think that this
  764.   confuses some people who read Miquel van Smoorenburg's description of
  765.   a UPS that only provides only a closed circuit on powerfail, and
  766.   requires a much more complex cable.
  767.  
  768.   Well that is all I can think of for now.  I'm planning on seeing if my
  769.   cable will still operate if I insert resistors into all of the
  770.   circuits.  If it does then I'll make the changes to my document,
  771.   should find out this weekend.
  772.  
  773.   Tom
  774.  
  775.   ----------------
  776.  
  777.   Of Linux and Uninterruptible Power Supplies
  778.   [or How to connect a TrippLite BCXXXLAN UPS]
  779.  
  780.   by Tom Webster <webster@kaiwan.com>
  781.      05/20/94 (Version 1.0)
  782.  
  783.   1.0 Introduction
  784.   I struggled through connecting a TrippLite BC750LAN (Standby UPS) to
  785.   my Linux box about six months ago.  Since then I've seen several
  786.   requests for information on this subject, so I'm putting it in a
  787.   relatively stable format so I can just send this out when the question
  788.   reappears.
  789.  
  790.   1.1 The Results
  791.   When the power fails in my apartment, several things happen:
  792.  
  793.      1.  The UPS switches its inverter on and the computer starts
  794.          drawing off of the UPS.  The warning beeper also starts going
  795.          off.  If the power comes back on, the UPS shuts the inverter
  796.          down and switches back to line power.  Nothing else happens,
  797.          other than the beeper turns off.
  798.  
  799.      2.  If the power is off for ~15-30 seconds, the system will send
  800.          a message to the users (via wall) and initiate a shutdown
  801.          (to halt) in five minutes.  If the power comes back on, the
  802.          shutdown is canceled and a message stating this is sent
  803.          to the users (via wall) stating that the shutdown has been
  804.          canceled.
  805.  
  806.      3.  While the system is shutting itself down, its dying act is to
  807.          shut off the inverter on the UPS, killing power to the system.
  808.          This is done after the disks are unmounted, and is done to
  809.          prevent the halted system from draining power from the UPS.
  810.  
  811.      4.  When the line power comes back on, the system restarts normally.
  812.  
  813.   The BC750 has enough juice to keep my system going for quite some
  814.   time, so why do I only run it off of the UPS for five minutes?  The
  815.   answer rests in a couple of 'rules-of-thumb' (your mileage may vary):
  816.  
  817.      a.  If the power browns out or blacks out, 90% of the time it will
  818.          be out for 0-2 minutes.
  819.  
  820.      b.  If it is out for longer, it will be out for .5-3+ hours.
  821.  
  822.      c.  If it is out for a while (see b), the power will yo-yo at least
  823.          once while the power company is working on it.  (The power
  824.          will come up for 5-45 seconds, and then fail again.)
  825.  
  826.   So, I set my system up to cover the majority of the power outages I
  827.   get, without trying to cover the really long ones.  I also keep plenty
  828.   of reserve in the UPS to handle yo-yo situations.
  829.  
  830.   1.2  Disclaimer
  831.   I make no warranties or guarantees as to the suitability or sanity of
  832.   following my advice.  This is how my system is setup, and as far as I
  833.   can tell it works fine for me.  Your setup may need to be different to
  834.   fit your needs, especially if you are using different UPS hardware.
  835.  
  836.   2.0  Hardware
  837.   In the case of my UPS, I thought that the RS-232 interface was
  838.   something of a misnomer.  I was expecting the UPS to send and receive
  839.   data, like talking to your modem with Hayes "AT" commands.  This was
  840.   not the case.  It seems that it is called an RS-232 interface because
  841.   it stays within the voltage and signal limits of the RS-232 spec.  To
  842.   communicate with the UPS, you need to be able to sense changes in
  843.   state on certain lines and change the state of other lines.  The fact
  844.   that these lines may have nothing in common to the lines your system
  845.   might expect to use, if it were talking to say a modem or printer, is
  846.   probably why the UPS needs special cables to allow software (including
  847.   the manufacturer's) to communicate with the UPS.
  848.  
  849.   Through trial and error with a RS-232 patch panel, I was able to come
  850.   up with this cable diagram for the cable between the UPS and the
  851.   computer.  Please note that I did this without looking at the official
  852.   TrippLite cable and it may be different.
  853.  
  854.             UPS                System
  855.            DB-25               DB-25
  856.              1 <-------------->  1       Ground
  857.  
  858.              2 <-------------->  4       Power Fail
  859.              8 <-------------->  8       Sensing Circuit
  860.              3 <-------------->  2       Inverter Shutdown
  861.             20 <--------------> 22       Circuit
  862.  
  863.   Once you have the cable patched together, just hook the UPS side to
  864.   the UPS and the System side to a free serial port on your Linux box.
  865.   You will probably have to mess around with 9->25 and 25->9 adapters
  866.   to get your cable to fit, but you and a good computer store should
  867.   be able to handle this.
  868.  
  869.   3.0 Software
  870.   The software that I use is all available to Linux users and comes
  871.   with most distributions (SLS and Slackware at least).  This setup
  872.   has worked for me through Kernels .99.9, .99.14, and 1.00.
  873.  
  874.   3.1 System V Init
  875.   This package is needed to make the whole thing work.  If you are
  876.   still using the "Simple Init" package, perhaps it is time you looked
  877.   at upgrading.  The version I am using is 2.04, and I believe that
  878.   Miquel van Smoorenburg is the author of the package.
  879.  
  880.   3.2 powerd
  881.   powerd is the power daemon, by default is sits and watches for a
  882.   change in state on the DCD line and reports these changes to the
  883.   system via the signal mechanism.  The source for powerd is provided
  884.   in the System V Init package.  Compile it, move it to a binary
  885.   directory (I put it in /sbin on my system), and alter your rc.local
  886.   script to start the daemon.  The relevant part of my rc.local looks
  887.   like this:
  888.  
  889.       ----- snip -----
  890.       # Add support for the UPS
  891.       echo "Starting powerd daemon..."
  892.       if [ -x /sbin/powerd ]; then
  893.          /sbin/powerd /dev/cua4
  894.       fi
  895.       ----- snip -----
  896.  
  897.   3.3 inittab
  898.   Your inittab needs to be modified to properly handle the signals
  899.   that powerd will send if there is a power failure.  The relevant
  900.   lines of my inittab look like this:
  901.  
  902.      ----- snip -----
  903.      # What to do when power fails (shutdown to single user).
  904.      pf::powerfail:/sbin/shutdown -f +5 "THE POWER IS FAILING"
  905.  
  906.      # If power is back before shutdown, cancel the running shutdown.
  907.      pg:0123456:powerokwait:/sbin/shutdown -c "THE POWER IS BACK"
  908.  
  909.      # If power comes back in single user mode, return to multi user mode.
  910.      ps:S:powerokwait:/sbin/init 2
  911.      ----- snip -----
  912.  
  913.   3.4 rc.0 (brc)
  914.   Depending on how your system is setup either rc.0 or the brc script
  915.   is executed immediately prior to shutdown.  These scripts take care
  916.   of things like unmounting disks and any other last minute clean-up.
  917.  
  918.   The inverter shutdown circuit, is designed to signal an inverter
  919.   shutdown when data is sent out over the DTR line.  In my case,
  920.   I just cat a short file to the serial port (/etc/passwd - since I
  921.   know it will always be there).  My rc.0 is as follows, please note
  922.   that it is overly conservative, the sync can be removed and the
  923.   sleep times can probably be tightened, but it works so I haven't
  924.   messed with it.
  925.  
  926.     ----- snip -----
  927.     #! /bin/sh
  928.     #
  929.     # brc       This file is executed by init(8) when the system is being
  930.     #           shutdown (i.e. set to run at level 0).  It usually takes
  931.     #           care of un-mounting all unneeded file systems.
  932.     #
  933.     # Version:  @(#)/etc/brc            2.01    02/17/93
  934.     #
  935.     # Authors:  Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
  936.     #           Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  937.     #
  938.     # Modified: 01/15/94 - Inverter shutdown support added.
  939.     #           Tom Webster <webster@kaiwan.com>
  940.  
  941.     PATH=/sbin:/bin:/usr/sbin:/usr/bin
  942.     echo Unmounting file systems.....
  943.     sync
  944.     umount -a
  945.     sleep 2
  946.     cat /etc/passwd > /dev/cua4
  947.     sleep 5
  948.     echo Done.
  949.     ----- snip -----
  950.  
  951.   (On my UPS the inverter is only running when the line power is off,
  952.   so there is no harm in sending the shutdown signal at every shutdown.)
  953.  
  954.   4.0 Conclusion
  955.   Well that's how I hooked my TrippLite UPS up to my Linux box.
  956.   Feel free to drop me a line with the results of you attempts,
  957.   especially if you have any improvements.  :->
  958.  
  959.   From: Tom Webster <webster@kaiwan.com>
  960.   To: hjstein@MATH.HUJI.AC.IL (Harvey J. Stein)
  961.   Subject: Re: Help - Powerd & UPS - Help
  962.   Date: Thu, 11 Aug 1994 12:20:50 -0700 (PDT)
  963.  
  964.   Harvey,
  965.  
  966.   > Like I tried to say, powerd can just run shutdown directly in the
  967.   > event of a low battery, so that init doesn't need to deal with it &
  968.   > doesn't need to be hacked - no new signal necessary.  Although this
  969.   > violates the nice separation of labor between powerd & init, it's
  970.   > easier than adding another signal.  Or do you just mean another
  971.   > command?  Something like having powerok/powerfail/powerfailnow as
  972.   > commands in inittab which execute when SIGPWR is received &
  973.   > /etc/powerfail contains OK/FAIL/LOWBATT (respectively).  This would be
  974.   > cleanest, but having powerd execute shutdown -r now is trivial to do
  975.   > now, and might as well be done - the logic will be the same regardless
  976.   > of the action that powerd takes when it senses a low battery.  For now
  977.   > it can just run shutdown, & when init gets hacked it can write LOWBATT
  978.   > to /etc/powerfail (or whatever the hell the file is called) & give
  979.   > init a SIGPWR.
  980.  
  981.   I'd like to add the LOWBATT command, it would be the cleaner way to do
  982.   it.  I just need to take a look at the code and see how hard it would
  983.   be to add it.  Also need to look at my wiring, guess this may mean I've
  984.   got to run the system all the way down for a final test (once I think I
  985.   have it working.
  986.  
  987.   Tom
  988.  
  989.   From: Tom Webster <webster@kaiwan.com>
  990.   To: hjstein@MATH.HUJI.AC.IL (Harvey J. Stein)
  991.   Subject: Re: Help - Powerd & UPS - Help
  992.   Date: Mon, 15 Aug 1994 09:46:06 -0700 (PDT)
  993.  
  994.   Harvey,
  995.  
  996.   Well, I messed about all weekend taking readings with my multi-
  997.   tester and comparing it to the scant documentation that I have for
  998.   the UPS.  The only conclusions I came to were:
  999.  
  1000.   1.  My system has been working for about 8-9 months now.  If I was
  1001.   going to fry anything, it should have happened by now.
  1002.  
  1003.   2.  If my success is based on my serial hardware doing odd things
  1004.   (I'm using an STB 4COM board), there is no way I'm going to be
  1005.   able to find out on my system.  I'll have to leave that to other
  1006.   poor souls on the net.
  1007.  
  1008.   3.  As far as I can tell the only reason the 10kohm resistor was
  1009.   in Miquel's diagram, was to keep the line higher than DCD, even
  1010.   after the circuit had been shunted to ground.
  1011.  
  1012.   4.  The only circuit that is expected to do anything other that be
  1013.   switched by the UPS (the inverter shutdown circuit) has a 40kohm
  1014.   resistor built into it (inside the UPS).  That should take care of
  1015.   any worries there.
  1016.  
  1017.   5.  Miquel's circuit will work for power fail sensing, and might
  1018.   be extrapolated to include the low battery circuit.  If my circuit
  1019.   proves unviable for others, it would just require more soldering
  1020.   than I'd care to deal with in a cable.
  1021.  
  1022.   In other related news, I broke down this morning and ordered a
  1023.   cable from Tripplite.  I ordered the LanTastic/LAN Manager/Win NT
  1024.   cable, it's just a cable (9M-9F, no software).  This will set me
  1025.   back about $40 (the PC UNIX cable (w/ software) is about $140).
  1026.   The motivation for doing this was three part.
  1027.  
  1028.   (1)  Within a year I'll probably be running Win 4.0, or WinNT 3.5
  1029.   (I beta'ed 3.1) and both should support UPS monitoring (I know NT
  1030.   does and saw the power management icon on a Win4.0 desktop in one
  1031.   of the computer mags).
  1032.  
  1033.   (2)  My current cable isn't going to be compatible with any other
  1034.   monitoring software for other OS's (except by random chance),
  1035.   working to a known (and presumably soon to be common?) cable via a
  1036.   hacked powerd, should widen the audience.
  1037.  
  1038.   (3)  I can always reverse engineer the cable to see if Tripplite
  1039.   is indeed building any safety into their cables.
  1040.  
  1041.   It should be here in a week or so.  In the mean time, I'll start
  1042.   looking into hacking powerd for LOWBATT.
  1043.  
  1044.   Tom
  1045.  
  1046.   11.5.  APC Backup-UPS
  1047.  
  1048.   There seems to be some controversy as to the accuracy of the
  1049.   information here on APC Back-UPSes.  So, please be careful.  I'm
  1050.   prefacing this section with one message of caution I received.  It
  1051.   might not make alot of sense before the rest of this section is read,
  1052.   but this way, at least you're more likely to see it.  And again, since
  1053.   I don't have any APC UPS units, I can't verify the accruacy of either
  1054.   of these messages.
  1055.  
  1056.   11.5.1.  A message of caution
  1057.  
  1058.        From ind43@sun1000.ci.pwr.wroc.pl Sun Oct  9 11:00:42 1994
  1059.        Newsgroups: comp.os.linux.admin
  1060.        Subject: BUPS-HOWTO warning
  1061.        From: ind43@sun1000.ci.pwr.wroc.pl (Marek Michalkiewicz)
  1062.        Date: 6 Oct 1994 18:38:15 GMT
  1063.        Organization: Technical Univeristy of Wroclaw
  1064.        NNTP-Posting-Host: ci3ux.ci.pwr.wroc.pl
  1065.        X-Newsreader: TIN [version 1.2 PL2]
  1066.  
  1067.        If you want to connect the APC Back-UPS to your Linux box, this might
  1068.        be of interest to you.
  1069.  
  1070.        There is a good BUPS-HOWTO which describes how to do this. But it has
  1071.        one "bug".
  1072.  
  1073.        The RTS serial port signal is used to shut down the UPS. The UPS will
  1074.        shut down only if it operates from its battery. The manual says that
  1075.        the shutdown signal must be high for at least 0.5s. But few milliseconds
  1076.        is enough, at least for my APC Back-UPS 600.
  1077.  
  1078.        Using RTS to shut down the UPS can be dangerous, because the RTS goes
  1079.        high when the serial device is opened. The backupsd program then turns
  1080.        RTS off, but it is on (high) for a moment. This kills the power when
  1081.        backupsd is first started and there is a power failure at this time.
  1082.        This can happen for example when the UPS is shut down, unattended,
  1083.        and the power comes back for a while.
  1084.  
  1085.        Either start backupsd before mounting any filesystems for read-write,
  1086.        or (better) use TX (pin 3) instead of RTS (pin 7) to shut down the
  1087.        UPS (pin numbers are for 9-pin plug). Use ioctl(fd, TCSBRKP, 10);
  1088.        to make TX high for one second, for example. Using TX should be safe.
  1089.        Maybe I will post the diffs if time permits...
  1090.  
  1091.        -- Marek Michalkiewicz
  1092.        ind43@ci3ux.ci.pwr.wroc.pl
  1093.  
  1094.   11.5.2.  BUPS-HOWTO
  1095.  
  1096.   Luminated Software Group Presents
  1097.  
  1098.   HOWTO use Back-UPS (by APC) (to keep your linux box from frying)
  1099.  
  1100.   Version: 1.01  BETA
  1101.  
  1102.   Document by: Christian G. Holtje <docwhat@uiuc.edu> Cabling info and
  1103.   help: Ben Galliart <bgallia@orion.it.luc.edu>
  1104.  
  1105.   This document, under one condition, is placed in Public Domain. The
  1106.   one condition is that credit is given where credit is due.  Modify
  1107.   this as much as you want, just give some credit to us who worked.
  1108.  
  1109.   *******************************************************************************
  1110.   Warning!  I, nor any of us who have written or helped with this
  1111.   document, make and guarantees or claims for this text/source/hints.
  1112.   If anything is damaged, we take NO RESPONSIBILITY!  This works to the
  1113.   BEST OF OUR KNOWLEDGE, but we may have made mistakes.  So be careful!
  1114.   *******************************************************************************
  1115.  
  1116.   Al right, you just bought (or are going to buy) a Back-UPS from APC.
  1117.   (Other brands might be able to use this info, with little or no
  1118.   modification, but we don't know)  You've looked at the price of the
  1119.   Power-Chute software/cabling, and just are not sure it's worth the
  1120.   price.  Well, I made my own cable, and my own software and am using it
  1121.   to automatically shut off the power to my linux box when a power
  1122.   failure hits.  Guess what?  You can too!
  1123.  
  1124.   *** The Cable ***
  1125.  
  1126.   This was the hardest part to figure out (I know little about hardware,
  1127.   so Ben did the most work for this).  To build one, you need to buy
  1128.   from your local radio shack (or other part supplier) this stuff:
  1129.  
  1130.        1 9-Position Male D-Subminature Connector (solder-type)
  1131.                [Radio Shack cat. no. 276-1537c]
  1132.        1 9-Position Female D-Subminature Connector (solder-type)
  1133.                [Radio Shack cat. no. 276-1538c]
  1134.        2 casings for the above plugs (usually sold separately)
  1135.        Some stranded wire (wire made of strands, not solid wire)
  1136.  
  1137.   You also need, but may be able to borrow:
  1138.  
  1139.        1 soldering iron
  1140.        solder
  1141.  
  1142.   Okay...this is how you connect it up!
  1143.  
  1144.   These diagrams are looking into the REVERSE SIDE (the side where you
  1145.   solder the wire onto the plugs)  The letters G, R, and B represent the
  1146.   colors of the wires I used, and help to distinguish one line from the
  1147.   next.  (NOTE:  I'm use standard rs-232 (as near as we can tell)
  1148.   numbering.  The APC book uses different numbers.  Ignore them!  Use
  1149.   ours...I already changed the numbers for you!)
  1150.  
  1151.      ---------------------     Male Side! (This goes into the UPS)
  1152.       \  B   R  *  *  * /
  1153.         \  *  *  *  G  /
  1154.           ------------
  1155.  
  1156.      ---------------------     Female Side! (This goes into your COM port)
  1157.       \  R   *  *  *  G /
  1158.         \  *  B  *  *  /
  1159.           ------------
  1160.  
  1161.   For those who like the numbers better:
  1162.  
  1163.                Male            Female
  1164.        ---------------------------------------
  1165.                1               7               Black
  1166.                2               1               Red
  1167.                9               5               Green
  1168.  
  1169.   ---------Aside:  What the rs-232 pins are for!----------- Since we had
  1170.   to dig this info up anyway:
  1171.  
  1172.   >From the REAR (the soldering side) the pins are numbered so:
  1173.  
  1174.           ---------------------
  1175.            \  1   2  3  4  5 /
  1176.              \  6  7  8  9  /
  1177.                ------------
  1178.  
  1179.   The pins mean:
  1180.  
  1181.                Number  Name                    Abbr. (Sometimes written with D prefix)
  1182.                1       Carrier Detect          CD
  1183.                2       Receive Data            RD
  1184.                3       Transmit Data           TD(?)
  1185.                4       Data Terminal Ready     DTR
  1186.                5       Signal Ground           Gnd
  1187.                6       Data Set Ready          DSR
  1188.                7       Request to Send         RTS(?)
  1189.                8       Clear to Send           CS
  1190.                9       Ring Indicator          RI
  1191.  
  1192.   What we did is connect the UPS's RS-232 Line Fail Output to the CD,
  1193.   the UPS's chassis to Gnd, and the UPS's RS-232 Shut Down Input to RTS.
  1194.   Easy now that we told you, no?
  1195.  
  1196.   I have no idea if the software below will work, if you purchase the
  1197.   cable from APC.  It might, and it might not.
  1198.  
  1199.   *** The Software ***
  1200.  
  1201.   Okay, I use the SysVInit package by Miquel van Smoorenburg for Linux.
  1202.   (see end for file locations, credits, email addresses, etc.)  I don't
  1203.   know what would have to be changed to use someone elses init, but I
  1204.   know this code (following) will work with Miquel's stuff.  Just so I
  1205.   give credit where credit's due.  I looked at Miquel's code to figure
  1206.   out how ioctl()'s worked.  If I didn't have that example, I'd have
  1207.   been in trouble.  I also used the powerfail() routine (verbatim, I
  1208.   think), since it must interact with his init, I thought that he should
  1209.   know best.  The .c file is at the end of this document, and just needs
  1210.   to be clipped off.  To clip the file, edit away and extra '.sigs' and
  1211.   junk.  This document should end on the line /* End of File */.....cut
  1212.   the rest.
  1213.  
  1214.   This program can either be run as a daemon to check the status of the
  1215.   UPS and report it to init, or it can be run to send the kill-power
  1216.   command to the UPS.  The power will only be killed if there is a power
  1217.   problem, and the UPS is running off the battery.  Once the power is
  1218.   restored, it turns back on.
  1219.  
  1220.   To run as a daemon, just type: backupsd /dev/backups
  1221.  
  1222.   /dev/backups is a link to /dev/cua0 at the moment (COM 1, for you
  1223.   DOSers).  The niceness of the link is that I can just re-link the
  1224.   device if I change to com 2 or 3.
  1225.  
  1226.   Then, if the power dies init will run the commands for the powerwait.
  1227.   An example (This is from my /etc/inittab):
  1228.  
  1229.        #Here are the actions for powerfailure.
  1230.        pf::powerwait:/etc/rc.d/rc.power start
  1231.        po::powerokwait:/etc/rc.d/rc.power stop
  1232.  
  1233.   The powerwait will run, if the power goes down, and powerokwait will
  1234.   run if the power comes back up.
  1235.  
  1236.   Here is my entire rc.power:
  1237.  
  1238.   ______________________________________________________________________
  1239.   #! /bin/sh
  1240.   #
  1241.   # rc.power      This file is executed by init when there is a powerfailure.
  1242.   #
  1243.   # Version:      @(#)/etc/rc.d/rc.power   1.50    1994-08-10
  1244.   #
  1245.   # Author:       Christian Holtje, <docwhat@uiuc.edu>
  1246.   #
  1247.  
  1248.     # Set the path.
  1249.     PATH=/sbin:/etc:/bin:/usr/bin:/sbin/dangerous
  1250.  
  1251.     # Find out how we were called.
  1252.     case "$1" in
  1253.           start)
  1254.                   echo "Warning there is Power problems."  | wall
  1255.                   # Save current Run Level
  1256.                   ps | gawk '{ if (($5 == "init") && ($1 == "1")) print $6 }' \
  1257.                            | cut -f2 -d[ | cut -f1 -d] \
  1258.                            > /tmp/run.level.power
  1259.                   /sbin/shutdown -h +1m
  1260.                   ;;
  1261.           stop)
  1262.                   echo "Power is back up.  Attempting to halt shutdown." | wall
  1263.                   shutdown -c
  1264.                   ;;
  1265.           *)
  1266.                   echo "Usage:  $0 [start|stop]"
  1267.                   exit 1
  1268.                   ;;
  1269.     esac
  1270.   ______________________________________________________________________
  1271.  
  1272.   Pretty nifty, no?  Actually, there is a problem here...I haven't had
  1273.   time to figure it out...If there is a 'sh' wizard out there....
  1274.  
  1275.   There is one little detail left, that is having the UPS turn off the
  1276.   power if it was halted with the power out.  This is accomplished by
  1277.   adding this line into the end of your halt script:
  1278.  
  1279.          /sbin/backupsd /dev/backups killpower
  1280.  
  1281.   This will only kill the power if there is no power being supplied to
  1282.   your UPS.
  1283.  
  1284.   *** Testing the stuff ***
  1285.  
  1286.   This is just a short section saying this:
  1287.  
  1288.   BE CAREFUL!
  1289.  
  1290.   I recommend backing up your linux partitions, syncing several times
  1291.   before testing and just being careful in general.  Of course, I'm just
  1292.   recommending this.  I wasn't careful at all, and had to clean my
  1293.   partition several times testing my config.  But it works.  :)
  1294.   *** Where to Get It ***
  1295.  
  1296.   Miquel van Smoorenburg's SysVInit can be gotten at:
  1297.  
  1298.   sunsite.unc.edu:/pub/Linux/system/Daemons/SysVinit-2.50.tgz
  1299.  
  1300.   and a fix for some bash shells is right next-door as:
  1301.  
  1302.   sunsite.unc.edu:/pub/Linux/system/Daemons/SysVinit-2.50.patch1
  1303.  
  1304.   As to getting this HOWTO, you can email me.  docwhat@uiuc.edu  with
  1305.   the subject saying 'request' and the keyword 'backups' in body of the
  1306.   letter.  (I may automate this, and other stuff)
  1307.  
  1308.   *** Credit Where Credit's Due Dept. ***
  1309.  
  1310.   Thanks to Miquel van Smoorenburg <miquels@drinkel.nl.mugnet.org> for
  1311.   his wonderful SysVInit package and his powerd.c which helped me very
  1312.   much.
  1313.  
  1314.   Christian Holtje <docwhat@uiuc.edu> Documentation backupsd.c (what
  1315.   wasn't Miquel's) rc.power
  1316.  
  1317.   Ben Galliart <bgallia@orion.it.luc.edu> The cable Information for the
  1318.   RS-232 standard Lousy Jokes (none quoted here)
  1319.  
  1320.   ______________________________________________________________________
  1321.   /*  backupsd.c -- Simple Daemon to catch power failure signals from a
  1322.    *                Back-UPS (from APC).
  1323.    *
  1324.    *  Parts of the code are from Miquel van Smoorenburg's powerd.c
  1325.    *  Other parts are original from Christian Holtje <docwhat@uiuc.edu>
  1326.    *  I believe that it is okay to say that this is Public Domain, just
  1327.    *  give credit, where credit is due.
  1328.    *
  1329.    *  Disclaimer:  We make NO claims to this software, and take no
  1330.    *               resposibility for it's use/misuse.
  1331.    */
  1332.  
  1333.   #include <sys/types.h>
  1334.   #include <sys/ioctl.h>
  1335.   #include <fcntl.h>
  1336.   #include <errno.h>
  1337.   #include <stdlib.h>
  1338.   #include <unistd.h>
  1339.   #include <stdio.h>
  1340.   #include <signal.h>
  1341.  
  1342.   /* This is the file needed by SysVInit */
  1343.   #define PWRSTAT         "/etc/powerstatus"
  1344.  
  1345.   void powerfail(int fail);
  1346.  
  1347.   /* Main program. */
  1348.   int main(int argc, char **argv)
  1349.   {
  1350.     int fd;
  1351.     int killpwr_bit = TIOCM_RTS;
  1352.     int flags;
  1353.     int status, oldstat = -1;
  1354.     int count = 0;
  1355.  
  1356.     if (argc < 2) {
  1357.           fprintf(stderr, "Usage: %s <device> [killpower]\n", argv[0]);
  1358.           exit(1);
  1359.     }
  1360.  
  1361.     /* Open the the device */
  1362.     if ((fd = open(argv[1], O_RDWR | O_NDELAY)) < 0) {
  1363.           fprintf(stderr, "%s: %s: %s\n", argv[0], argv[1], sys_errlist[errno]);
  1364.           exit(1);
  1365.     }
  1366.  
  1367.     if ( argc >= 3  && (strcmp(argv[2], "killpower")==0) )
  1368.         {
  1369.             /* Let's kill the power! */
  1370.             fprintf(stderr, "%s: Attempting to kill the power!\n",argv[0] );
  1371.             ioctl(fd, TIOCMBIS, &killpwr_bit);
  1372.             /* Hmmm..... If you have a power outtage, you won't make it! */
  1373.             exit(0);
  1374.         }
  1375.     else
  1376.         /* Since we don't want to kill the power, clear the RTS. (killpwr_bit) */
  1377.         ioctl(fd, TIOCMBIC, &killpwr_bit);
  1378.  
  1379.   /* Become a daemon. */
  1380.     switch(fork()) {
  1381.     case 0: /* I am the child. */
  1382.                   setsid();
  1383.                   break;
  1384.     case -1: /* Failed to become daemon. */
  1385.                   fprintf(stderr, "%s: can't fork.\n", argv[0]);
  1386.                   exit(1);
  1387.     default: /* I am the parent. */
  1388.                   exit(0);
  1389.     }
  1390.  
  1391.     /* Now sample the DCD line. */
  1392.     while(1) {
  1393.         ioctl(fd, TIOCMGET, &flags);
  1394.         status = (flags & TIOCM_CD);
  1395.         /* Did DCD jumps to high? Then the power has failed. */
  1396.         if (oldstat == 0 && status != 0) {
  1397.             count++;
  1398.             if (count > 3) powerfail(0);
  1399.             else { sleep(1); continue; }
  1400.         }
  1401.         /* Did DCD go down again? Then the power is back. */
  1402.         if (oldstat > 0 && status == 0) {
  1403.             count++;
  1404.             if (count > 3) powerfail(1);
  1405.             else { sleep(1); continue; }
  1406.         }
  1407.         /* Reset count, remember status and sleep 2 seconds. */
  1408.         count = 0;
  1409.         oldstat = status;
  1410.         sleep(2);
  1411.     }
  1412.     /* Error! (shouldn't happen) */
  1413.     return(1);
  1414.   }
  1415.  
  1416.   /* Tell init the power has either gone or is back. */
  1417.   void powerfail(ok)
  1418.   int ok;
  1419.   {
  1420.     int fd;
  1421.  
  1422.     /* Create an info file needed by init to shutdown/cancel shutdown */
  1423.     unlink(PWRSTAT);
  1424.     if ((fd = open(PWRSTAT, O_CREAT|O_WRONLY, 0644)) >= 0) {
  1425.           if (ok)
  1426.                   write(fd, "OK\n", 3);
  1427.           else
  1428.                   write(fd, "FAIL\n", 5);
  1429.           close(fd);
  1430.     }
  1431.     kill(1, SIGPWR);
  1432.   }
  1433.  
  1434.   /* End of File */
  1435.   ______________________________________________________________________
  1436.  
  1437.   11.6.  APC Smart-UPS, Model 600
  1438.  
  1439.   Many people have APC Smart UPSes.  To the best of my knowledge, no one
  1440.   can run them in "smart" mode under Linux.  This is because APC refuses
  1441.   to release the protocol for the "smart" mode without a non-disclosure
  1442.   agreement.  Not very smart of them, I'd say :).
  1443.  
  1444.   The general consensus is to buy from a brand which does release the
  1445.   information.  I hear that Best is one such brand.
  1446.   If you are stuck with an APC Smart-UPS, you can still use it, but only
  1447.   in a dumb mode like all the other UPSes and as outlined above.
  1448.  
  1449.   Here's some info on how to make a cable for doing such.  You'll
  1450.   probably have to hack powerd.c as outlined in section ``Reverse-
  1451.   engineering cables and hacking powerd.c''
  1452.  
  1453.   From dangit@netcom.com Mon Aug 22 10:16:23 1994
  1454.   Newsgroups: comp.os.linux.misc
  1455.   Subject: UPS Monitoring Cable For APC
  1456.   From: dangit@netcom.com (Lam Dang)
  1457.   Date: Fri, 19 Aug 1994 11:56:28 GMT
  1458.   Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  1459.   X-Newsreader: TIN [version 1.2 PL1]
  1460.  
  1461.   [Didn't make it the first time.]
  1462.  
  1463.   A few netters have asked about UPS monitoring cables.  This is what I
  1464.   found when I made one for my APC Smart-UPS, Model 600.  A disclaimer is in
  1465.   order.  This is just an experimenter's report; use it at your own risks.
  1466.   Please read the User's Manual first, especially Section 6.4, Computer
  1467.   Interface Port.
  1468.  
  1469.   The cable is to run between a 9-pin female connector on the UPS and a
  1470.   25-pin male connector on the PC.  Since I cut off one end of a 9-pin
  1471.   cable and replaced it with a 25-pin connector, I had to be VERY
  1472.   CAREFUL ABOUT PIN NUMBERS.  The 25-pin hood is big enough to contain a
  1473.   voltage regulator and two resistors.  I got all the materials (listed
  1474.   below) from Radio Shack for less than 10 bucks.  As required by Windows NT
  1475.   Advanced Server 3.5 (Beta 2), the "interface" between the UPS connector
  1476.   and the PC connector is as follows:
  1477.  
  1478.           UPS (9-pin)              PC (25-pin)
  1479.  
  1480.           1 (Shutdown)             20 (DTR)
  1481.           3 (Line Fail)             5 (CTS)
  1482.           4 (Common)                7 (GND)
  1483.           5 (Low Battery)           8 (DCD)
  1484.           9 (Chassis Ground)        1 (Chassis Ground)
  1485.  
  1486.   This is pretty straightforward.  You can use UPS pin 6 instead of 3
  1487.   (they're the inverse of each other).  The complication is in pulling up
  1488.   UPS open collector pins 3 (or 6) and 5.
  1489.  
  1490.   This APC model provides an unregulated output of 24 Vdc at UPS pin 8. The
  1491.   output voltage is available all the time (at least until some time after
  1492.   Low Battery has been signalled).  The supply is limited to 40 mA.  To
  1493.   pull up, UPS pin 8 is input to a +5 Vdc voltage regulator.  The output of
  1494.   the regulator goes into two 4.7K resistors.  The other end of one
  1495.   resistor connects both UPS pin 3 (Line Fail) and PC pin 5 (CTS).  That
  1496.   of the other resistor connects both UPS pin 5 (Low Battery) and PC pin 8
  1497.   (DCD).  The two resistors draw about 2 mA when closed.
  1498.  
  1499.   Test your cable without connecting it to the PC.  When the UPS is on
  1500.   line, pins 5 (CTS) and 8 (DCD) at the PC end of the cable should be very
  1501.   close to 5 Vdc, and applying a high to pin 20 (DTR) for 5 seconds should
  1502.   have no effect.  Now pull the power plug to put the UPS on battery.  Pin
  1503.   5 (CTS) should go down to zero Vdc, pin 8 (DCD) should stay the same at 5
  1504.   Vdc, and applying a high to pin 20 (DTR), e.g., by shorting pins 8 and 20,
  1505.   should shut down the UPS after about 15 seconds.
  1506.  
  1507.   Keep the UPS on battery until Low Battery is lighted on its front panel.
  1508.   Now pin 8 (DCD) should go down to zero Vdc too.  Wait until the UPS
  1509.   battery is recharged.  Then connect your cable to the PC, disable the UPS
  1510.   option switches by turning all of them ON, and run your favorite UPS
  1511.   monitoring software.
  1512.  
  1513.   For those who want to run it with Windows NT Advanced Server, the UPS
  1514.   interface voltages are NEGATIVE for both power failure (using UPS pin 3)
  1515.   and low battery conditions, and POSITIVE for remote shutdown.  Serial
  1516.   line parameters such as baud rate don't matter.
  1517.  
  1518.   I haven't tested my cable with Linux powerd.  When you do, please let us
  1519.   know.  I run NT as often as Linux on the same PC.  I must conform to NT's
  1520.   UPS scheme.  Perhaps somebody can modify powerd to work with it and post
  1521.   the source code here.
  1522.  
  1523.   List of materials:
  1524.  
  1525.           1 shielded D-sub connector hood (Radio Shack 276-1510)
  1526.           1 25-pin female D-sub crimp-type connector (276-1430)
  1527.           1 7805 +5Vdc voltage regulator (276-1770)
  1528.           2 4.7K resistors
  1529.           1 component perfboard (276-148)
  1530.           1 cable with at least one 9-pin male connector.
  1531.  
  1532.   You'll need a multimeter, a soldering iron, and a couple of hours.
  1533.  
  1534.   Hope this helps.
  1535.  
  1536.   Regards,
  1537.  
  1538.   --
  1539.   Lam Dang
  1540.   dangit@netcom.com
  1541.  
  1542.   12.  How to shutdown other machines on the same UPS
  1543.  
  1544.   Some people (myself included) have several Linux PCs connected to one
  1545.   UPS.  One PC monitors the UPS and needs to get the other PCs to shut
  1546.   down when the power goes out.  There are a number of ways to do this,
  1547.   all are do-it-yourself currently, and most are just hypothetical.
  1548.  
  1549.   We assume the PCs can communicate over a network.  Call the PC that
  1550.   monitors the UPS the master & the other PCs the slaves.
  1551.  
  1552.   12.1.  UPS status port method
  1553.  
  1554.   Set up a port on the master which, when connected to, either sends
  1555.   "OK", "FAIL", or "BATLOW", the first when the power is ok, the second
  1556.   when the power has failed, and the third when the battery is low.
  1557.   Model this on port 13 (the time port) which one can telnet to &
  1558.   receive the local time.
  1559.  
  1560.   Have the slaves run versions of powerd that look at this port instead
  1561.   of checking a serial line.
  1562.  
  1563.   I think this is probably the best method, and intend to eventually
  1564.   upgrade my systems to use it.
  1565.  
  1566.   12.2.  Broadcast method
  1567.  
  1568.   Same as section ``UPS status port method'' except send an ethernet
  1569.   broadcast message that the power has just gone down.
  1570.  
  1571.   This might have security implications, since it could be spoofed.
  1572.  
  1573.   12.3.  Dummy login method
  1574.  
  1575.   Set up dummy logins on the slaves with login names powerok and
  1576.   powerfail, both with the same UID.  Make /etc/powerokscript the shell
  1577.   of the powerok user, and make /etc/powerfailscript the shell of the
  1578.   powerfail user.  On the master, have the /etc/powerokscript rlogin to
  1579.   each slave as user powerok and have the /etc/powerfailscript rlogin to
  1580.   each slave as user powerfail.  Put a .rhosts file on each slave in the
  1581.   home directory of powerok and powerfail to allow root from the master
  1582.   to login as user powerok and powerfail to each slave.
  1583.  
  1584.   This is the system I'm currently using.  Unfortunately, there are some
  1585.   difficulties in getting the remote logins to execute and return
  1586.   without hanging.  One probably wants the /etc/powerfailscript on the
  1587.   master to rsh to the slaves in background so that the
  1588.   /etc/powerfailscript doesn't hang.  However, I never managed to get
  1589.   logging in in background to work right.  I even tried arcane
  1590.   combinations such as having /etc/powerfailscript run foo in background
  1591.   and having foo log into the slave.  Whatever I did gave problems with
  1592.   the thing being stopped for tty input (or output, I can't remember).
  1593.  
  1594.   Also, this might create security holes.
  1595.  
  1596.