Show Table of Contents
32.7. 安装后脚本
可选择添加完成安装后立即在系统中运行的命令。这部分内容必须在 kickstart 的最后(如 第 32.4 节 “kickstart 选项” 所述),而且必须使用
%post 命令开头,%end 命令结尾。如果 kickstart 文件还包括 %pre 部分,那 %pre 和 %post 的顺序是没有关系的。
本节内容是关于安装其他软件和配置其他命名服务器的。
注意
如果使用静态 IP 信息和命名服务器配置网络,则可以在
%post 部分访问和解析 IP 地址。如果使用 DHCP 配置网络,当安装程序执行到 %post 部分时,/etc/resolv.conf 文件还没有准备好。此时可以访问网络,但是不能解析 IP 地址。因此,如果使用 DHCP,则必须在 %post 部分指定 IP 地址。
注意
安装后脚本是在 chroot 环境里执行的。因此,某些工作如从安装介质复制脚本或 RPM 将不能执行。
--nochroot- 允许指定要在 chroot 环境之外执行的命令。下例将
/etc/resolv.conf文件复制到刚安装的文件系统里。%post --nochroot cp /etc/resolv.conf /mnt/sysimage/etc/resolv.conf
--interpreter /usr/bin/python- 允许指定不同的脚本语言,如 Python。将 /usr/bin/python 替换成要使用的脚本语言。
--log /path/to/logfile- 记录后安装脚本输出结果。请注意:必须考虑到日志文件的路径,无论是否使用
--nochroot选项。例如:没有--nochroot:%post --log=/root/ks-post.log
使用nochroot%post --nochroot --log=/mnt/sysimage/root/ks-post.log
32.7.1. 示例
32.7.1.1. 注册然后挂载 NFS 共享
在 Red Hat Enterprise Subscription Management服务器中注册系统(在这个示例中是本地 Subscription Asset Manager 服务器):
%post --log=/root/ks-post.log /usr/sbin/subscription-manager register --username=admin@example.com --password=secret --serverurl=sam-server.example.com --org="Admin Group" --environment="Dev" %end
从 NFS 共享目录执行
runme 命令:
mkdir /mnt/temp mount -o nolock 10.10.0.2:/usr/new-machines /mnt/temp openvt -s -w -- /mnt/temp/runme umount /mnt/temp
kickstart 模式不支持 NFS 文件锁定,因此,当挂载 NFS 目录时必须使用
-o nolock 选项。
32.7.1.2. 采用传统 RHN 注册系统
rhnreg_ks 命令是在 Red Hat 网络中注册系统的程序,旨在用于非互动环境(例如:Kickstart 安装)。可在命令行或标准输入(stdin)中指定所有信息。应在生成激活码并要使用该激活码注册系统时使用这个命令。
有关使用
rhnreg_ks 自动注册系统的详情,请查看知识库文章 https://access.redhat.com/solutions/876433。
32.7.1.3. 将 subscription-manager 作为安装后脚本运行
subscription-manager 命令行脚本会在 Red Hat Enterprise Subscription Management服务器(客户门户网站订阅管理、Subscription Asset Manager 或者 CloudForms System Engine)中注册系统。这个脚本还可用来为系统自动分配或者附加与该系统最匹配的订阅。
在客户门户网站中注册时,请使用 Red Hat Network 登录证书。使用 Subscription Asset Manager 或者 CloudForms System Engine 注册时,请使用本地管理员生成的用户帐户。
注册命令中可使用附加选项设定系统的首选服务等级,以及对具体操作系统版本限制更新和勘误。
%post --log=/root/ks-post.log /usr/sbin/subscription-manager register --username=admin@example.com --password=secret --serverurl=sam-server.example.com --org="Admin Group" --environment="Dev" --servicelevel=standard --release="6.6" %end
有关使用 subscription-manager 的附加信息,请查看知识库文章 https://access.redhat.com/solutions/748313。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.