在此案例研究中,将在安装时创建一个数据库文件,并在删除软件包时保存数据库副本。
此案例研究展示以下技术:
使用类和类操作脚本对不同的对象集执行特殊操作
有关更多信息,请参见编写类操作脚本。
使用 space 文件通知 pkgadd 命令要正确安装此软件包需要额外空间
有关 space 文件的更多信息,请参见在目标系统上保留额外空间。
要为此案例研究在安装时创建一个数据库文件,并在删除时保存副本,您必须完成以下任务:
定义三个类。
在此案例研究中,软件包要求在 CLASSES 参数中定义以下三个类:
标准类 none,该类包含一组属于子目录 bin 的进程。
admin 类,该类包含可执行文件 config 和一个包含数据文件的目录。
cfgdata 类,该类包含一个目录。
使软件包可共同重定位。
请注意在 prototype 文件中,没有路径名以斜线或环境变量开头,这表明它们是可共同重定位的。
计算数据库文件需要的空间量,并创建一个要随软件包一起提供的 space 文件。此文件通知 pkgadd 命令软件包需要额外空间并指定空间量。
为 admin 类创建类操作脚本 (i.admin)。
该样例脚本使用属于 admin 类的数据文件初始化数据库。要执行此任务,该脚本执行以下操作:
将源数据文件复制到相应目标位置
创建一个名为 config.data 的空文件,并将其指定给 cfgdata 类
执行 bin/config 命令(与软件包一起提供并已安装)以使用属于 admin 类的数据文件填充 config.data 数据库文件
执行 installf -f 命令完成 config.data 的安装
由于在删除时 admin 类不需要特殊操作,因此不会创建删除类操作脚本。这意味着 admin 类中的所有文件和目录都会从系统中删除。
为 cfgdata 类创建删除类操作脚本 (r.cfgdata)。
删除脚本会在删除数据库文件之前创建该文件的副本。由于在安装时此类不需要特殊操作,因此不需要安装类操作脚本。
切记删除脚本的输入是要删除的路径名的列表。路径名始终按反向字母顺序显示。此删除脚本将文件复制到名为 $PKGSAV 的目录。当所有路径名都被处理后,脚本会返回并删除与 cfgdata 类关联的所有目录和文件。
此删除脚本的结果是将 config.data 复制到 $PKGSAV,然后删除 config.data 文件和数据目录。
PKG=krazy NAME=KrAzY Applications CATEGORY=applications BASEDIR=/opt ARCH=SPARC VERSION=Version 1 CLASSES=none cfgdata admin |
i pkginfo i request i i.admin i r.cfgdata d none bin 555 root sys f none bin/process1 555 root other f none bin/process2 555 root other f none bin/process3 555 root other f admin bin/config 500 root sys d admin cfg 555 root sys f admin cfg/datafile1 444 root sys f admin cfg/datafile2 444 root sys f admin cfg/datafile3 444 root sys f admin cfg/datafile4 444 root sys d cfgdata data 555 root sys |
# extra space required by config data which is # dynamically loaded onto the system data 500 1 |
# PKGINST parameter provided by installation service # BASEDIR parameter provided by installation service while read src dest do cp $src $dest || exit 2 done # if this is the last time this script will be executed # during the installation, do additional processing here. if [ "$1" = ENDOFCLASS ] then # our config process will create a data file based on any changes # made by installing files in this class; make sure the data file # is in class `cfgdata' so special rules can apply to it during # package removal. installf -c cfgdata $PKGINST $BASEDIR/data/config.data f 444 root sys || exit 2 $BASEDIR/bin/config > $BASEDIR/data/config.data || exit 2 installf -f -c cfgdata $PKGINST || exit 2 fi exit 0 |
这里举例说明了一个很少见的实例,其中 installf 适用于类操作脚本。由于 space 文件用于在特定文件系统上保留空间,因此即使该新文件没有包括在 pkgmap 文件中也可以被安全地添加。
# the product manager for this package has suggested that # the configuration data is so valuable that it should be # backed up to $PKGSAV before it is removed! while read path do # path names appear in reverse lexical order. mv $path $PKGSAV || exit 2 rm -f $path || exit 2 done exit 0 |