Home -- Michael's Blog :: Just Do IT -- Manuals

4.5. 使用Ports Collection

下面的章节提供了使用ports collection来安装或卸载程序的基本用法。

4.5.1. 获得Ports Collection

在您能使用ports之前, 您必须先获得ports collection--本质上是/usr/ports目录下的一堆 Makefiles,补丁, 和描述文件。

在您安装FreeBSD系统的时候,sysinstall会询问您是否需要安装ports collection. 如果您选择no,那您可以根据下面的指令来安装ports collection:

Sysinstall 方式

这种方式使用sysinstall再次手动安装ports collection。

  1. root用户, 运行 /stand/sysinstall命令,像下面显示的:

    # /stand/sysinstall
    
  2. 选择Configure, 按Enter

  3. 选择Distributions, 按 Enter

  4. 选择ports, 按Space

  5. 选择Exit, 按Enter

  6. 选择您的安装介质,就像CDROM,FTP,等等

  7. 选择Exit并按Enter

  8. X键退出sysinstall

您可以使用CVSup,这个程序让您获得和更新 ports collection 。CVSup需要使用 /usr/share/examples/cvsup/ports-supfile这个配置文件。 如果您想获得关于CVSup这个应用程序的更多信息,请看使用 CVSup (第 A.5 节) 和本文件。

CVSup 方法

使用CVSup获得 ports collection ,是一个非常快捷的方法。 如果您想保持您的 ports 树的最新状态,或学习更多关于CVSup的内容您可以阅读刚才提及的内容。

  1. 安装net/cvsup port。详情请参阅CVSup 安装 (第 A.5.2 节) 。

  2. root 用户,复制 /usr/share/examples/cvsup/ports-supfile 到一个新的地方,比如 /root 和您的家目录。

  3. 编辑 ports-supfile

  4. 修改 CHANGE_THIS.FreeBSD.org 到离您比较近的CVSup 服务器。请看 CVSup 镜像 (第 A.5.7 节) 服务器的完整列表。

  5. 运行 cvsup:

    # cvsup -g -L 2 /root/ports-supfile
    
  6. 运行这个命令之后将下载和应用所有变化的ports系统,除了在您的系统中重建ports系统。

4.5.2. 安装 Ports

当提到Ports collection时,第一个要说明的就是何谓“``skeleton''”。简单地说,一个port skeleton让一个程序在FreeBSD上简洁地编译并安装的所需文件的最小组合。每个port skeleton包含:

一些ports还有些其它的文件,就像 pkg-message。ports系统会在特殊的情况下使用这些文件。如果您想知道这些文件更多的细节以及ports的概要,请参阅 FreeBSD Porter's Handbook.

port里面包含着如何编译源代码的指令,但不包含真正的源代码。您可以在网上或CD-ROM上获得源代码。 源代码无论被开发者发布成什么格式。一般就是一个被tar 和gzip过的文件,或者是被一些其他的工具压缩或未压缩的文件。ports中这个程序源代码标示文件叫``distfile'',安装 FreeBSD port的方法还不止这两种。

注: 您必须使用 root 用户登录后安装ports.

警告在安装任何 port 之前, 应该首先确保已经更新到了最新的 ports collection, 并检查 http://vuxml.freebsd.org/ 中是否有与那个 port 有关的安全问题。

在安装应用程序之前, 可以使用 portaudit 来自动地检查是否存在已知的安全问题。 这个工具同样可以在 ports collection (security/portaudit) 中找到。 在安装新的 port 之前, 可以考虑先运行一下 portaudit -F 来抓取最新的漏洞数据库。 在每天的周期性系统安全检察时, 数据库会被自动更新, 并且会在这之后实施安全审计。 欲了解进一步的情况,请参阅 portaudit(1)periodic(8)

4.5.2.1. 从光盘安装 Ports

FreeBSD项目中官方的光盘文件不再包含distfiles。他们提供了许多更多的更好的使用编译好的软件包。如果您从 FreeBSD Mall 购买了像FreeBSD PowerPak这样的光盘产品,里面就包含了distfiles。下面的章节假设您已经有了一张FreeBSD的安装光盘。

把FreeBSD的光盘放入驱动器。Mount到/cdrom。(如果您使用了不同的 mount点,这个安装将不会工作。) 首先,改变目录到您要安装的port所在的目录:

# cd /usr/ports/sysutils/lsof

一旦进入了 lsof 的目录,您将会看到这个port的结构。下一步就是 make,或 ``build'',这个port。只需要在命令行简单地输入make命令。 做好之后,您可以看到下面的信息:

# make
>> lsof_4.57D.freebsd.tar.gz doesn't seem to exist in /usr/ports/distfiles/.
>> Attempting to fetch from file:/cdrom/ports/distfiles/.
===>  Extracting for lsof-4.57
...
[extraction output snipped]
...
>> Checksum OK for lsof_4.57D.freebsd.tar.gz.
===>  Patching for lsof-4.57
===>  Applying FreeBSD patches for lsof-4.57
===>  Configuring for lsof-4.57
...
[configure output snipped]
...
===>  Building for lsof-4.57
...
[compilation output snipped]
...
#

注意,一旦编译完成,你就会回到命令行。下一步安装port,要安装它只需要在 make 命令后跟上一个单词install 即可:

# make install
===>  Installing for lsof-4.57
...
[installation output snipped]
...
===>   Generating temporary packing list
===>   Compressing manual pages for lsof-4.57
===>   Registering installation for lsof-4.57
===>  SECURITY NOTE: 
      This port has installed the following binaries which execute with
      increased privileges.
#

一旦您返回到提示符,您就可以运行您刚刚安装的程序了。因为lsof是一个赋予特殊权限的程序,因此显示了一个安全警告。在编译和安装ports的时候 ,您可能要留意任何出现的警告。

注: 你可以运行一个命令 make install 来替换 makemake install这两个分离的步骤。

注: 在一些shell中,为了对这些程序做快速查找的操作,它对环境变量PATH指定的目录中的可执行命令有缓存。对于在基本系统中的一部分shell(就像 tcsh),和在port中的一些shell(举个例子, shells/zsh),如果您使用了这些shell中的一个,你必须在安装完一个port后执行 rehash 命令, 这样新安装的程序才能使用。

注: 有一小部分的port由于许可协议的问题不允许包含在光盘内。可能是因为在下载前需要注册一些信息或不允许再发布,或者其他理由。如果您想安装的port不在光盘中,为此你将需要在线操作来安装 (请看 下一章节)。

4.5.2.2. 从互联网安装Port

作为最后一节, 本节假定您有一个可用的互联网连接。如果您没有,您需要察看上一章节从光盘安装, 或者手动放一份distfile到/usr/ports/distfiles

实际上从互联网安装port和从光盘安装差不多,不同的是从互联网下载distfile,而不是从光盘读取。

相关步骤是一样的:

# make install
>> lsof_4.57D.freebsd.tar.gz doesn't seem to exist in /usr/ports/distfiles/.
>> Attempting to fetch from ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/.
Receiving lsof_4.57D.freebsd.tar.gz (439860 bytes): 100%
439860 bytes transferred in 18.0 seconds (23.90 kBps)
===>  Extracting for lsof-4.57
...
[extraction output snipped]
...
>> Checksum OK for lsof_4.57D.freebsd.tar.gz.
===>  Patching for lsof-4.57
===>  Applying FreeBSD patches for lsof-4.57
===>  Configuring for lsof-4.57
...
[configure output snipped]
...
===>  Building for lsof-4.57
...
[compilation output snipped]
...
===>  Installing for lsof-4.57
...
[installation output snipped]
...
===>   Generating temporary packing list
===>   Compressing manual pages for lsof-4.57
===>   Registering installation for lsof-4.57
===>  SECURITY NOTE: 
      This port has installed the following binaries which execute with
      increased privileges.
#

就像您看到的,唯一区别是有一行告知您系统正在从哪里获得port的distfiled的信息

ports系统使用 fetch(1) 去下载文件, 它有很多可以设置的环境变量,包含 FTP_PASSIVE_MODE, FTP_PROXY, and FTP_PASSWORD。 如果您在防火墙之后,或使用 FTP/HTTP代理,您就可能需要设置这些变量。 完整的说明请看 fetch(3)

当使用者不是所有时间都能连接上网络,这个make fetch命令选项就可以使用了。你只要在顶层目录(/usr/ports)下运行这个命令,所有需要的文件将被下载。这个命令也同样可以在下级类别目录中使用,例如: /usr/ports/net。 注意,如果一个port有一些依赖的库或其他port,它将 获取这些依赖的port的distfile文件,如果你想获取所有依赖的port的所有distfile,请用 fetch-recursive命令替换fetch命令。

注: 你可以在一个类别或在顶级目录编译所有的port,或者使用上述提到的make fetch命令。这样是非常危险的,因为有一些port不能并存。 或者有另一种可能,一些port会安装两个不同的文件,但是确是相同的文件名。

在一些罕见的例子中,用户可能需要在除了 MASTER_SITES 以外的一个站点(本地已经下载下来的文件)去获得一个文件包。 你可以用以下命令不使用 MASTER_SITES:

# cd /usr/ports/directory
# make MASTER_SITE_OVERRIDE= \
ftp://ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/ fetch

在这个例子中,我们改变了 MASTER_SITES这个选项为 ftp.FreeBSD.org/pub/FreeBSD/ports/distfiles/

注: Some ports allow (or even require) you to provide build options which can enable/disable parts of the application which are unneeded, certain security options, and other customizations. A few which come to mind are www/mozilla, security/gpgme, and mail/sylpheed-claws. A message will be displayed when options such as these are available.

4.5.2.3. Overriding the Default Ports Directories

Sometimes it is useful (or mandatory) to use a different distfiles and ports directory. The PORTSDIR and PREFIX variables can override the default directories. For example:

# make PORTSDIR=/usr/home/example/ports install

will compile the port in /usr/home/example/ports and install everything under /usr/local.

# make PREFIX=/usr/home/example/local install

will compile it in /usr/ports and install it in /usr/home/example/local.

And of course,

# make PORTSDIR=../ports PREFIX=../local install

will combine the two (it is too long to completely write on this page, but it should give you the general idea).

Alternatively, these variables can also be set as part of your environment. Read the manual page for your shell for instructions on doing so.

4.5.2.4. Dealing with imake

Some ports that use imake (a part of the X Window System) do not work well with PREFIX, and will insist on installing under /usr/X11R6. Similarly, some Perl ports ignore PREFIX and install in the Perl tree. Making these ports respect PREFIX is a difficult or impossible job.

4.5.3. Removing Installed Ports

Now that you know how to install ports, you are probably wondering how to remove them, just in case you install one and later on decide that you installed the wrong port. We will remove our previous example (which was lsof for those of you not paying attention). As with installing ports, the first thing you must do is change to the port directory, /usr/ports/sysutils/lsof. After you change directories, you are ready to uninstall lsof. This is done with the make deinstall command:

# cd /usr/ports/sysutils/lsof
# make deinstall
===>  Deinstalling for lsof-4.57

That was easy enough. You have removed lsof from your system. If you would like to reinstall it, you can do so by running make reinstall from the /usr/ports/sysutils/lsof directory.

The make deinstall and make reinstall sequence does not work once you have run make clean. If you want to deinstall a port after cleaning, use pkg_delete(1) as discussed in the Packages section of the Handbook.

4.5.4. Ports and Disk Space

Using the ports collection can defiantly eat up your disk space. For this reason you should always remember to clean up the work directories using the make clean option. This will remove the work directory after a port has been built, and installed. You can also remove the tar files from the distfiles directory, and remove the installed ports when their use has delimited.

Some users choose to limit the port categories by placing an entry in the refuse file. This way when they run the CVSup application, it will not download the files in that category.

4.5.5. Upgrading Ports

注: Once you updated your ports collection, before attempting a port upgrade, you should check the /usr/ports/UPDATING file. This file describes various issues and additional steps users may encounter and need to perform when updating a port.

Keeping your ports up to date can be a tedious job. For instance, to upgrade a port you would go to the ports directory, build the port, deinstall the old port, install the new port, and then clean up after the build. Imagine doing that for five ports, tedious right? This was a large problem for system administrators to deal with, and now we have utilities which do this for us. For instance the sysutils/portupgrade utility will do everything for you! Just install it like you would any other port, using the make install clean command.

Now create a database with the pkgdb -F command. This will read the list of installed ports and create a database file in the /var/db/pkg directory. Now when you run portupgrade -a, it will read this and the ports INDEX file. Finally, portupgrade will begin to download, build, backup, install, and clean the ports which have been updated. portupgrade comes with a lot of options for different use cases, the most important ones will be presented below.

If you want to upgrade only a certain application, not the complete database, use portupgrade pkgname, include the flags -r if portupgrade should act on all those packages depending on the given package as well, and -R to act on all packages required by the given packages. To use packages instead of ports for installation, provide -P and to just fetch distfiles without building or installing anything, use -F. For further information see portupgrade(1).

注: It is important to regularly update the package database using pkgdb -F to fix inconsistencies, especially when portupgrade asks you to. Do not abort portupgrade while it is updating the package database, this will leave you an inconsistent database.

Other utilities exist which will do this, check out the ports/sysutils directory and see what you come up with.

Michael's Blog :: Just Do IT -- manuals