LoginSignup
27
24

More than 5 years have passed since last update.

自作のコマンドをrpmパッケージ化 - specファイルの編集

Last updated at Posted at 2016-01-24

specファイルを編集して、
自作のコマンドをrpmパッケージにする方法をざっくりと調べました。

環境

  • fedora21 3.17.4-301.fc21.x86_64

事前準備

必要なもの:

  • rpmbuildコマンド
# sudo yum install rpm-build

でインストールします。

  • 自作のコマンド

今回はサンプルとして、以下を用意しました。
端末上に"hello, world!"を出力するプログラムです。

hello.c
#include <stdio.h>

main(void)
{
    printf("hello, world!\n");
}

コンパイルします。

$ gcc -o hello hello.c
$ ./hello
hello, world!

これでコマンドは完成です。(゚∀゚ノノ゙パチパチパチパチ

rpmパッケージ化

作成したコマンドをまずアーカイブ化します。
今回作ったコマンドのバージョンは1.0なので、
アーカイブ名はhello-1.0.tar.gzにしました。

$ tar czvf hello-1.0.tar.gz hello

作業するディレクトリを作成します。

$ mkdir -p rpmbuild/{SOURCES,SPECS}

先ほど作成したアーカイブを作業ディレクトリ配下のSOURCESに移します。

$ cp hello-1.0.tar.gz rpmbuild/SOURCES

カレントディレクトリを作業ディレクトリへと変更し...

$ cd rpmbuild

specファイルをざっくりと記述します。
各項目の意味は色々な所で解説されているので、ここでは割愛します。

$ vim SPECS/sample.spec
sample.spec
%define name hello
%define version 1.0
%define unmangled_version 1.0
%define release 1
%define _binaries_in_noarch_packages_terminate_build 0

Summary: sample hello world program
Name: %{name}
Version: %{version}
Release: %{release}
License: GPL2
Source0: %{name}-%{unmangled_version}.tar.gz
Group: Applications/File
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-buildroot
Prefix: %{_prefix}
BuildArch: noarch

%define INSTALLDIR %{buildroot}/usr/local/bin

%description
This is a sample program to learn how to make a rpm package.

%prep

%build

%install
rm -rf %{INSTALLDIR}
mkdir -p %{INSTALLDIR}
cp -R * %{INSTALLDIR}

%clean
rm -rf %{buildroot}

%files
/usr/local/bin
%defattr(-,root,root)

specファイル記述後、
rpmbuildコマンドを実行して、いざ、rpmパッケージを作成!

$ rpmbuild -bb SPECS/sample.spec

成功した場合、RPMSディレクトリ配下にrpmパッケージが作成されています。

ls RPMS/noarch/
hello-1.0-1.noarch.rpm

パッケージをインストールします。

$ sudo rpm -ivh RPMS/noarch/hello-1.0-1.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:hello-1.0-1                      ################################# [100%]

インストール後、試しに自作コマンドを実行します。

$ hello
hello, world!

無事、実行されました。

rpm -qaの一覧にもしっかりと表示されます。

$ rpm -qa | grep hello
hello-1.0-1.noarch

以上です。

27
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
24