ARCH LINUX ColorBoot-HowTo

Copyright © 2003 Dennis Herbrich  <dennis@archlinux.org>

Sat Aug 9 14:10:54 2003

Abstract:

This guide explains the steps necessary to customize the text displayed during the boot process of ARCHLINUX, especially how to emphasize the status messages ''BUSY'', ''DONE'' and ''FAILED'' with colors.


Contents

1 What do I need?

Not much. This guide and, if you don't want to do any work yourself but simply have nice colors right away, the patch from section 5.1, either copied or downloaded from the net.

2 Where do I need to start?

The interesting file is named functions and is kept in the directory /etc/rc.d/ together with the other initscripts. It's only a couple hundred bytes large and thus quite clear. However, you won't be left alone with the question what the general purpose of this file is after all.

3 What is the purpose of the /etc/rc.d/functions file?

This file is a kind if ''function library'' for the system start scripts, and defines special functions which fulfill a few important tasks when called. Among other things one can find the functions for textual representation of the status messages here, meaning that you can centrally modify the look of the boot process in this file.

The process is quite straight-forward; One of the start scripts (ie. for the SSH daemon) calls the function named ''stat_busy'' and supplies it with a short note about which service is being started (''Starting Secure Shell Daemon''). The ''stat_busy'' function then usually prints an open square bracket and the supplied parameter, followed by a mean looking ''awk'' command which does nothing special but output the correct amount of spaces depending on the lenght of the supplied parameter. Last but not least, the ''BUSY'' and a closed square bracket is printed. Depending on whether the daemon could be started successfully or not, the ''stat_done'' (or ''stat_fail'', respectively) function will move the cursor to the already printed ''BUSY'' and overwrite it with new text.

With this knowledge it's easy to use modified text or even customize the whole boot layout. An important tool is the ANSI ''standard'', which is unfortunately rather pathetically standardized.

4 ANSI codes

With the help of so called ANSI codes it has been commonplace for quite some time to send control signals to ''dumb terminals'', which could change several settings this way without needing dedicated serial lines. ANSI control codes are directly embedded into the normal text output and generally begin with the (non-printable) ASCII-Code 0x1B (or 033 octal), the ''escape sequence'', followed by an open square bracket and actual codes seperated by a semicolon which control the terminal behaviour. To input the escape sequence one can use \e or \033.

From the hundreds of available control codes, we're in this case only interested in those enabling us to directly manipulate the screen content, ie. change the text and background color or move the cursor to a different position.

4.1 Changing colors

You can choose a background and foreground color indepently from a pool of eight different colors, and intensify the text color with the ''Bold'' option if you want. You can choose from:

   0 -- black
   1 -- red
   2 -- green
   3 -- yellow
   4 -- blue
   5 -- magenta
   6 -- cyan
   7 -- white
   9 -- Default
The foreground color is prefixed with a ''3'', the background color with a ''4''. To activate the bold option, you have to send a ''1'' as a seperate control code. The whole ANSI sequence must be terminated with a ''m'', and the command is complete. To print ''Hello'' to the screen with red text on white background and reverting the settings back to the defaults afterwards, this command has to be used in a shell: echo -e "\e[31;47mHello\e[0m" As you can see, after the escape sequence and the obligatory square bracket the codes 31 and 47 are submitted and the command ended with the ''m''. After that the normal text is printed, followed by the code 0 which resets all changes. Note the ''-e'' option of the echo command, since without this option no control sequences will be evaluated!

In addition to the bold option, the codes 4 (underline), 5 (blink), 7 (invert) and 8 (hidden) are available, which are supported by only a few terminals. The framebuffer console seems to only support underlining and inversion, for example.

4.2 List of colors

To get a better impression of how the colors actually look, execute this little shell script. It prints a table of all possible color combinations to screen for you to gawk at and peruse in your scripts.

  #!/bin/bash
  # Display ANSI color table
  #
  e="\033["
  echo -n " _ _ _ _ _40 _ _ _ 41_ _ _ _42 _ _ _ 43"
  echo "_ _ _ 44_ _ _ _45 _ _ _ 46_ _ _ _47 _"
  for fg in `seq 30 37`; do
    line1="$fg  "
    line2="    "
    for bg in `seq 40 47`; do
      line1="${line1}${e}${bg};${fg}m Normal  ${e}0m"
      line2="${line2}${e}${bg};${fg};1m Bold    ${e}0m"
    done
    echo -e "$line1\n$line2"
  done

4.3 Cursor positioning

The list below has been blatantly ripped from the Bash-Prompt-HOWTO. These codes are embedded into the textflow as usual.

 - Cursor position:
   \033[<L>;<R>H
      or
   \033[<L>;<R>f
   positions the cursor in line L and row R.
 - Cursor N lines up:
   \033[<N>A
 - Cursor N lines down:
   \033[<N>B
 - Cursor N rows to the right:
   \033[<N>C
 - Cursor N rows to the left:
   \033[<N>D
 
 - Clear screen and move cursor to (0,0):
   \033[2J
 - Delete text from cursor to end of line
   \033[K
 
 - Save cursor position:
   \033[s
 - Restore cursor position:
   \033[u

5 An example (traffic lights)

This example will modify the boot output in a simple but effectful manner; The printed status messages ''BUSY'', ''DONE'' and ''FAILED'' will no longer be printed in a boring gray, but in yellow, green and red instead. The traffic light color scheme is often happily used, so it's got to play along here as well.

At the beginning of the opened /etc/rc.d/functions file a handful variables need to be added which centrally define the colors to be used. The four variables for the three different status messages and the ''normal'' text color are set to contain the matching ANSI sequences for red, yellow and green (all bold) and ''Default'' (normal).

The first thing done in the ''stat_busy'' Funktion is setting the text color back to normal, to avoid any lingering settings to mess up the display of the opened square bracket and the service name. Of course you can fiddle with the colors here as well, but I'll hold myself back here in this example. The ''BUSY'' at the end with a closed square bracket and the buffer of spaces is then split up to guarantee that the spaces and the bracket definitely will be displayed in normal text color, and only the ''BUSY'' itself will be shown in it's color. That prevents ugly spaces if someone decided to set a background color. Analogous to this, the ''stat_done'' and ''stat_fail'' functions are edited. Done.

  
5.1 The patch for the example

The interested reader with an internet connection may download this patch from

http://archlinux.veloxis.de/howtos/colorboot/downloads/functions-color.patch

and install it according to the instruction in section 5.2. As a reference and courtesy for all plain ASCII readers, the complete patch for the current initscripts 0.5-4 package is attached below.

 --- functions.orig      2003-08-09 06:10:07.000000000 +0200
 +++ functions   2003-08-09 06:07:11.000000000 +0200
 @@ -3,6 +3,15 @@
  # functions
  #
  
 +# ANSI code for failure color (red)
 +COLOR_FAIL="\033[1;31m"
 +# ANSI code for busy color (yellow)
 +COLOR_BUSY="\033[1;33m"
 +# ANSI code for success color (green)
 +COLOR_DONE="\033[1;32m"
 +# ANSI code for default color
 +COLOR_NORMAL="\033[0;39m"
 +
  STAT_COL=68
 
  deltext() {
 @@ -10,19 +19,26 @@
  }
 
  stat_busy() {
 +       echo -en "${COLOR_NORMAL}"
         echo -n "[ $1 "
         awk "BEGIN { for (j=length(\"$1\"); j<$STAT_COL; j++) printf \" \" }"
 -       echo -n "   BUSY ]"
 +       echo -n "   "
 +       echo -en "${COLOR_BUSY}BUSY"
 +       echo -en "${COLOR_NORMAL} ]"
  }
 
  stat_done() {
         deltext
 -       echo "   DONE ]"
 +       echo -en "${COLOR_NORMAL}   "
 +       echo -en "${COLOR_DONE}DONE"
 +        echo -e "${COLOR_NORMAL} ]"
  }
 
  stat_fail() {
         deltext
 -       echo " FAILED ]"
 +       echo -en "${COLOR_NORMAL} "
 +       echo -en "${COLOR_FAIL}FAILED"
 +       echo -e "${COLOR_NORMAL} ]"
  }
 
 stat_die() {

  
5.2 Patch installation

The usage of this patch shouldn't be too hard even for a total newbie. Simply follow these steps:

6 Feedback

Feedback is of course appreciated, even if it's only a short ''Works for me!'' message. If you have the time and inclination to document the advanced possibilities of the boot output manipulation with ANSI, you're cordially invited to do so! Please send any feedback to <dennis@archlinux.org>.



Dennis Herbrich 2003-08-09