2011年2月24日

关于RPM的几个技巧

《运行时选项判断》
在做RPM安装包时,需要判断用户是在【安装】或者【更新】还是【卸载】,如何来做呢?
可以使用【$1】这个系统变量来判定,在不同的情况下其值是不同的,具体如下:

rpm -irpm -Urpm -e
%pre12
%post12
%preun0
%postun0



《解包》
有些情况下,只是想看看RPM内的文件内容,并不想进行安装,此时候可以如下来解包。
【rpm2cpio yourrpmfilename.rpm | cpio --extract --make-directories】



《宏定义》
可以通过修改系统级【/etc/rpm/macros】,用户级【~/.rpmmacros】文件,添加宏定义,改变默认路径等配置。

%_topdir /tmp/RPM
%_tmppath /tmp

修改完成以后,可以用命令【rpm --eval %_topdir】来检测是否生效。
其他常用的还有下面的几个参数
rpm --eval %_host_vendor
rpm --eval %_target_platform
rpm --eval %_build_name_fmt
rpm --eval %_rpmdir
rpm --eval %_os
想看所有选项信息的话,使用【rpm --showrc】。

另外,spec文件中的宏定义也可以使用bash脚本,例如:
%define C2 `grep CentOS /etc/issue`
%define CMD_TEST `if (test -n "%{C2}") ; then \
echo "centos"; fi`

%define GETOS getOS() {                              \
  VERSION_FILE=/proc/version                         \
                                                     \
  if (test -r $VERSION_FILE) then                    \
    :                                                \
  else                                               \
    return                                           \
  fi                                                 \
                                                     \
  turbolinux=`grep Turbolinux $VERSION_FILE`         \
  if (test -n "$turbolinux") then                    \
    echo "turbolinux"                                \
    return                                           \
  fi                                                 \
                                                     \
  centos=`grep "centos" $VERSION_FILE`               \
  if (test -n "$centos") then                        \
    echo "centos"                                    \
    return                                           \
  fi                                                 \
                                                     \
  redhat=`grep "Red Hat" $VERSION_FILE`              \
  if (test -n "$redhat") then                        \
    echo "redhat"                                    \
    return                                           \
  fi                                                 \
                                                     \
  return                                             \
}

没有评论:

发表评论