`
lovnet
  • 浏览: 6921010 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

SysAdmin to SysAdmin: Make for system administrators

阅读更多

SysAdmin to SysAdmin: Make for system administrators
Wednesday December 01--2004 (08:00 AM GMT)

By: Michael McGlothlin

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.

分享到:
评论

相关推荐

    A SysAdmin’s Essential Guide to Linux Workstation Security

    A SysAdmin’s Essential Guide to Linux Workstation Security How to work from anywhere and keep your data, identity, and sanity By Konstantin Ryabitsev Director of IT Infrastructure Security at The ...

    Oracle EBS SYSADMIN -

    ### Oracle EBS SYSADMIN 关键知识点详述 #### 一、Oracle EBS系统安全管理 **概念解析:** Oracle EBS(Enterprise Business Suite)系统安全管理是确保企业资源规划(ERP)平台的安全性和可控性的核心组成部分。它...

    app-support-forum:基于 Meteor 的临床支持论坛

    username : 'sysadmin' , password : 'sysadmin' , email : 'sysadmin@test.org' role : 'Admin' , } { username : 'janedoe' , password : 'janedoe' , email : 'janedoe@test.org' ,

    test-your-sysadmin-skills:Linux Sysadmin测试问题和答案的集合。 通过这些质量检查测试您在不同领域的知识和技能

    test-your-sysadmin-skills:Linux Sysadmin测试问题和答案的集合。 通过这些质量检查测试您在不同领域的知识和技能

    oracle ebs sysadmin - 培训手册

    ### Oracle EBS Sysadmin - 培训手册知识点解析 #### 一、ORACLE应用系统安全管理 **单元培训目标:** - 理解应用系统安全管理的基本概念。 - 掌握如何定义应用用户的方法。 - 学会如何定义责任。 - 通过责任控制...

    sysadmin-tools:我们用来管理橘子的工具和脚本

    sysadmin工具 我们用来管理橘子的工具和脚本 签出项目,运行“ npm install”,设置config.json(参考config.json.sample),运行“ node tools.js”以开始使用

    linux-sysadmin-course, Linux系统管理 101.zip

    linux-sysadmin-course, Linux系统管理 101 linux-sysadmin-courseLinux系统管理 101这里 repo 包含课程的附加材料和演示文稿。它还包括所有 homeworks 。等级:两个测试,每个测试有 50个问题一个问题一点每个作业...

    javasnmp源码-awesome-sysadmin-cn:awesome-sysadmin-cn

    awesome-sysadmin 资源列表,我们将对各个资源项进行编译整理。 整理后的内容,将收录在。可参考已整理的内容: 《》 《》 如何参与本项目? 从下面的目录来看,本项目的工作量小不了,所以非常期待能有更多程序员一...

    sysadmws-formula:Sysadmin Workshop SaltStack公式

    Sysadmin Workshop SaltStack公式: 应用-将php-fpm,python或静态应用部署到Web服务器。 bulk_log,disk_alert,mysql_queries_log,mysql_replica_checker,notify_devilry,rsnapshot_backup,sysadmws-utils-...

    Serial 1.3.9 MAC

    Serial is a modern terminal emulator designed to make working with servers, network equipment, and embedded hardware easier for serialengineers and system administrators. For system administrators,...

    Linux for Developers

    * **Sysadmin Skills:** Presents a full section on performing basic sysadmin tasks that developers often need to handle, enhancing their ability to manage systems independently. * **Relevance:** These ...

    Linux for Developers: Jumpstart Your Linux Programming Skills

    Perform basic sysadmin tasks that developers often need to handle Compare Linux languages to choose the best one for each task Write Bash scripts that interact with users or other shell features ...

    prometheus_libvirt_exporter

    libvirt_cpu_stats_system_time_nanosecs{domain=""} libvirt_cpu_stats_user_time_nanosecs{domain=""} libvirt_cpu_stats_cpu_time_nanosecs{domain=""} libvirt_mem_stats_rss{domain=""} libvirt_mem_stats_...

    sysadmin-reading-list:幼虫期系统管理员和SRE的阅读清单

    sysadmin阅读列表 幼虫期sysadmin的阅读列表。 该列表主要针对UNIX操作系统家族,主要是因为这是我的专业领域,但是欢迎其他操作系统的PR。 目录 Ruby书 Perl Perl书籍 工具链 网页 模组 博客文章 工具 云 多平台 ...

Global site tag (gtag.js) - Google Analytics