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.

内容概要:本文档详细介绍了利用Google Earth Engine (GEE) 平台对指定区域(位于中国广东省某地)进行遥感影像处理的一系列操作。首先,定义了研究区边界,并选取了 Landsat 8 卫星2023年8月至10月期间的数据,通过去云处理、归一化等预处理步骤确保数据质量。接着,基于预处理后的影像计算了地表温度(LST)、归一化植被指数(NDVI)、湿度指数(WET)、建筑指数(NDBSI)四个关键指标,并进行了主成分分析(PCA),提取出最重要的信息成分。为了进一步优化结果,还应用了像素二元模型对主成分分析的第一主成分进行了条件规范化处理,生成了最终的环境状态评估指数(RSEI)。最后,利用JRC全球表面水体数据集对水体区域进行了掩膜处理,保证了非水体区域的有效性。所有处理均在GEE平台上完成,并提供了可视化展示及结果导出功能。 适合人群:具备地理信息系统基础知识,对遥感影像处理有一定了解的研究人员或技术人员。 使用场景及目标:① 对特定区域的生态环境状况进行定量评估;② 为城市规划、环境保护等领域提供科学依据;③ 掌握GEE平台下遥感影像处理流程和技术方法。 其他说明:本案例不仅展示了如何使用GEE平台进行遥感影像处理,还涵盖了多种常用遥感指标的计算方法,如LST、NDVI等,对于从事相关领域的科研工作者具有较高的参考价值。此外,文中涉及的代码可以直接在GEE代码编辑器中运行,便于读者实践操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值