Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

1.3. 创建第一个 rpm 软件包

创建 RPM 软件包可能会很复杂。下面是一个完整的、可正常工作的 RPM Spec 文件,其中包含一些跳过和简化的内容。

Name:       hello-world
Version:    1
Release:    1
Summary:    Most simple RPM package
License:    FIXME

%description
This is my first RPM package, which does nothing.

%prep
# we have no source, so nothing here

%build
cat > hello-world.sh <<EOF
#!/usr/bin/bash
echo Hello world
EOF

%install
mkdir -p %{buildroot}/usr/bin/
install -m 755 hello-world.sh %{buildroot}/usr/bin/hello-world.sh

%files
/usr/bin/hello-world.sh

%changelog
# let's skip this for now

将文件保存为 hello-world.spec

现在使用这些命令:

$ rpmdev-setuptree
$ rpmbuild -ba hello-world.spec

命令 rpmdev-setuptree 会创建几个工作目录。由于这些目录永久存储在 $HOME 中,因此不需要再次使用此命令。

命令 rpmbuild 创建实际的 rpm 软件包。这个命令的输出类似如下:

... [SNIP]
Wrote: /home/<username>/rpmbuild/SRPMS/hello-world-1-1.src.rpm
Wrote: /home/<username>/rpmbuild/RPMS/x86_64/hello-world-1-1.x86_64.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.wgaJzv
+ umask 022
+ cd /home/<username>/rpmbuild/BUILD
+ /usr/bin/rm -rf /home/<username>/rpmbuild/BUILDROOT/hello-world-1-1.x86_64
+ exit 0

文件 /home/<username>/rpmbuild/RPMS/x86_64/hello-world-1-1.x86_64.rpm 是您的第一个 RPM 软件包。它可以安装到系统中,并经过测试。