December 1, 2004

SysAdmin to SysAdmin: Make for system administrators

Author: Preston St. Pierre

Custom software installation is a common task for sysadmins. Compiling the software is often a complex process involving many steps that must be done in the correct order. Figuring
out these steps once is difficult enough, but repeating these steps exactly is even more difficult when you need to rebuild your software in the future. The make program can make this task easier by letting you script complex installs. By using a single control file, you can perform a number of tasks, including downloading, building, and installing needed programs; uninstalling the same programs; and backing up the program's configuration files.

A simple example would be building and managing the Apache Web server. You can download Apache as a prepackaged binary for most platforms, but administrators often want more control over the process, in the form of a custom compile. Using make for the task also lets you uninstall the program and back up the Apache configuration files.

First, let us take a look at how make works. The make program uses
control files called makefiles to tell it what to do. Usually there is
only one makefile in a directory, which is named Makefile. To
use the makefile, change to the directory containing the file and run
the make program:

cd myDir
make myTarget

Makefiles can contain both variables and rules. Variables let you define
information that you might use throughout the makefile; rules let you
define the logic of what you are trying to accomplish. Typically you'll have a choice of which rule in the
makefile you want to run. To run the desired rule rather than the
default, you must name that rule's target as an argument to make. You may also
include comments as needed to document what you are doing.

You define comments by beginning a line with the # character. After that character you can write whatever you want and it will not affect the running of the makefile.

# This is a sample comment.

A variable is defined as a line where name = value. For example,
apache = httpd-2.0.52 could be a variable that contains the name and
version of an Apache package that we will be working with. To reference
a variable we've defined, use the $ character, as in:

$(name)

Using the previous example of the variable we defined for Apache, the reference would be:

$(apache)

A rule is defined as one or more lines of the form:

target : prerequisites
    commands

For example:

install : $(apache)
    ( cd $(apache) && make install )

This rule specifies a target named install which checks the example Apache
variable to see if a file or directory of the same name
exists in the current directory. If that file does exist, then the rule's
commands will be executed. If the file doesn't exist, and there is no rule
specified as to how that file should be created, then the install rule
will stop and its commands will not be run. If the file doesn't
exist but there is a rule on how to create the file, then that rule will be
run first, and when it is finished the install rule will run. In this
example the install rule changes the working directory to that of the
Apache source files and runs the make install command. Notice that this command is only run if the working directory is successfully changed.

There are a couple of special rules that you need to know. The first sets
the default rule for your makefile. If you run make without specifying
a target, this is the rule that will be run. This rule has a target of
.DEFAULT as in the example below. The rules you specify as the prerequisites will be run as the default.

.DEFAULT: install

The other special rule that you need to know defines phony targets.
Usually when a rule is run, make checks to see if its target is a file
that exists. If it exists, then the rule will not run. Phony targets are
"convenience" targets, such as install. We want install to run even if there is a file in the directory named install, so we need to specify install as a phony target by giving it the target name .PHONY. The rules you specify as the prerequisites will be flagged as phony targets.

There are a few "convenience" rules that are usually included as a matter
of user-friendly design. At minimum these should include build,
install, clean, and uninstall. build configures and compiles a program; install installs a program; clean removes build files for a program; and uninstall removes the installed program. I like to also include help, download, all, and mostlyclean. help prints useful information to the
user about how to use your makefile; download downloads a program's
source packages; all means to build and install the program; and
mostlyclean removes the build files but leaves certain files that may be
needed later.

As a simple real world example, we'll examine a makefile for Apache.
First, we will define our variables. It is a good idea to define
variables that point to all the programs you will be running in your
rules. This makes it easy to change the location and parameters of these
programs later if you need to.

RM = /bin/rm -f
RMDIR = /bin/rm -Rf
UNTAR = /bin/tar -xzf
MAKE = /usr/bin/make
WGET = /usr/bin/wget

In this makefile we will need to know the basename of the package, where
we can download the package, and where we'll install the package to. We'll
set these bits of information in variables also:

apache = httpd-2.0.52
apache-download = http://linux.cs.lewisu.edu/apache/httpd/$(apache).tar.gz
apache-prefix = /services/apache

Now that all our variables are set, we can define our rules. We'll start
by defining our special rules:

.PHONY : all download build install mostlyclean clean uninstall help backup
.DEFAULT : all

Then we'll move on to defining our convenience rules. We'll do the easy
ones first (the @ character tells make to not echo the commands to the screen):

all: build install
help:
    @echo Targets:
    @echo -e " all\t\tDownloads, installs, and cleans up build files."
    @echo -e " download\tDownloads source files."
    @echo -e " build\tConfigure and compile."
    @echo -e " install\tInstalls."
    @echo -e " mostlyclean\tDeletes build files."
    @echo -e " clean\t\tDeletes build files and source files."
    @echo -e " uninstall\tDeletes installed files."
    @echo -e " backup\tBackup configuration files."
    @echo -e " help\t\tDisplays help."
download : $(apache).tar.gz

Now we can get into the nitty gritty of our rules. These have convenience
names but they are also the backbone of what our makefile is for.

The first of these major rules is build. When the source package for
Apache is extracted, it creates a directory that has the same name as the basename of the package. We'll check to see if that directory exists by making it a prerequisite for our build rule. If the
directory does exist, then we'll change directory to it and configure
Apache to be built. Once the build process is configured, we'll go ahead
and compile Apache. We'll use the variables we defined above during the
configuration process to tell Apache where it will be installed.

build: $(apache)
    ( cd $(apache) && ./configure --prefix=$(apache-prefix) \ --enable-ssl --enable-expires --enable-so && $(MAKE) )

After compiling Apache we'll need to install it, so we'll define the
install rule. Again, we check to make sure the build directory for Apache
exists. If it does, then we'll change to that directory and run the
command to install Apache. After we've installed the program, we'll copy our own Apache configuration file into our new installation of Apache.

install : $(apache)
    ( cd $(apache) && $(MAKE) install )
    cp httpd.conf $(apache-prefix)/conf/httpd.conf

Once Apache is installed, we'll want to clean up our build files. We have
two options for this: we can clean up all the build files and delete the source packages we downloaded, or we can clean up while leaving the source packages intact. To leave the source packages, we will use the mostlyclean rule. This only deletes the directory we extracted and compiled Apache in.

mostlyclean :
    $(RMDIR) $(apache)

If we also want to delete the source packages, we'll use the clean rule.
mostlyclean is a prerequisite to clean, so anything we remove with
mostlyclean will be removed by clean also.

clean : mostlyclean
    $(RM) $(apache).tar.gz

Eventually we may want to uninstall what we have installed. Either we
might just not need the program anymore, or maybe we want to remove it so
that we can do a fresh install or an upgrade. To do this, we use the uninstall rule, which deletes everything in the directory we installed the program into. If you decide to use a directory that has other files to install into, then you will need a longer and more careful uninstall rule.

uninstall:
    $(RMDIR) $(apache-prefix)

Other than these "convenience" rules, there are a couple of "glue" rules we'll use
to make everything run smoothly. These exist so that they can be
prerequisites to our earlier rules. If the file that is the target
already exists, then these rules will not run; otherwise, the rule runs
and creates the needed files. This first rule checks to see if Apache's
source package exists, and if not, uses wget to download it:

$(apache).tar.gz :
    $(WGET) $(apache-download)

This rule checks to see if Apache's source package has been extracted.
If it hasn't, then it extracts it. The prerequisite will download
the package if it doesn't already exist.

$(apache) : $(apache).tar.gz
    $(UNTAR) $(apache).tar.gz

Finally, we want to be able to back up Apache's configuration file when
needed. This configuration file will be restored when we install Apache
again.

backup :
    cp $(apache-prefix)/conf/httpd.conf .

Put it all together and run make, and presto -- you'll have Apache
installed:

# Programs we use to do the install.
RM = /bin/rm -f
RMDIR = /bin/rm -Rf
UNTAR = /bin/tar -xzf
MAKE = /usr/bin/make
WGET = /usr/bin/wget

# Settings that decide what gets installed where.
apache = httpd-2.0.52
apache-download =
http://linux.cs.lewisu.edu/apache/httpd/$(apache).tar.gz
apache-prefix = /services/apache

# Setup our main targets.
.PHONY : all download build install mostlyclean clean uninstall help backup

.DEFAULT : all

all: build install

help:
    @echo Targets:
    @echo -e " all\t\tDownloads, installs, and cleans up build files."
    @echo -e " download\tDownloads source files."
    @echo -e " build\tConfigure and compile."
    @echo -e " install\tInstalls."
    @echo -e " mostlyclean\tDeletes build files."
    @echo -e " clean\t\tDeletes build files and source files."
    @echo -e " uninstall\tDeletes installed files."
    @echo -e " backup\tBackup configuration files."
    @echo -e " help\t\tDisplays help."

download : $(apache).tar.gz

$(apache).tar.gz :
    $(WGET) $(apache-download)

$(apache) : $(apache).tar.gz
    $(UNTAR) $(apache).tar.gz

build: $(apache)
    ( cd $(apache) && ./configure --prefix=$(apache-prefix) \ --enable-ssl --enable-expires --enable-so && $(MAKE) )

install :
    ( cd $(apache) && $(MAKE) install )
    cp httpd.conf $(apache-prefix)/conf/httpd.conf

mostlyclean :
    $(RMDIR) $(apache)

clean : mostlyclean
    $(RM) $(apache).tar.gz

uninstall:
    $(RMDIR) $(apache-prefix)

backup :
    cp $(apache-prefix)/conf/httpd.conf .

This is just a start. It's easy to modify this base to add PHP,
mod_perl, MySQL, or just about anything else. Using make allows you to
preserve all the steps needed, so that repeating or altering them is a
snap. Practically anything you can do with regular script files can be
done more easily with make. make is a good way to keep track of the logic needed to perform all the tasks related to a given service or program.

Click Here!