簡単なDebianパッケージを作成する方法
1. 概要
Debianパッケージは、Debianベースのディストリビューションでソフトウェアを配布する最も簡単で効率的な方法です。 依存関係の管理を処理し、インストール/アップグレード/アンインストール操作のための優れたインターフェースを提供します。
パッケージを作成する公式の方法には、多くのステップとプロセスが含まれます。 このチュートリアルでは、これらのパッケージを作成する簡単な方法を見ていきます。 ただし、パッケージ作成の公式な方法は理想的であり、本番環境に推奨されます。
2. ファイルの準備
2.1. .debパッケージの構造
Debianベースのディストリビューションでは、アプリケーションをインストールする方法の1つは、 .debパッケージファイルをダウンロードし、dpkgコマンドを使用してインストールすることです。 この「debパッケージ」は、ソフトウェアアプリケーションに関連付けられたバイナリと構成ファイルのアーカイブです。 アーカイブ内のすべてのファイルは、特定のフォルダー構造に保持されます。
インストール中、ターゲットマシンでは、バイナリと構成ファイルがルートフォルダーから同様のフォルダー構造になります。
2.2. アプリケーション関連ファイル
より明確に理解するために例を見てみましょう。 まず、ビルド用の作業フォルダーを作成します。
bluelake@pacific:~/Documents/deb/build$ mkdir test
パスに注意してください。この演習を進めると、フォルダが変更されます。
次に、アプリケーション用のファイルを準備します。 簡単にするために、2つのファイルを用意しましょう。
- test.sh実行可能ファイル
- test.conf構成ファイル
これらのファイルについては、この演習の内容を以下のように保持します。
bluelake@pacific:~/Documents/deb/build/test$ cat test.conf
NAME=Test
bluelake@pacific:~/Documents/deb/build/test$ cat test.sh
#!/bin/bash
. test.conf
echo "Hi $NAME"
Debianの実行可能ファイルは/bin フォルダーに、構成ファイルは /etcフォルダーに保存されていることがわかっています。 規則に従って、test.shファイルを/binフォルダーに保持し、test.confを/etcフォルダーに保持します。
したがって、現在のディレクトリにbinとetcの2つのフォルダを作成します。
bluelake@pacific:~/Documents/deb/build/test$ mkdir {bin,etc}
bluelake@pacific:~/Documents/deb/build/test$ ls
bin etc test.conf test.sh
次に、実行可能ファイルと構成ファイルをそれぞれのフォルダーに移動します。
bluelake@pacific:~/Documents/deb/build/test$ mv test.conf etc/
bluelake@pacific:~/Documents/deb/build/test$ mv test.sh bin/
bluelake@pacific:~/Documents/deb/build/test$ ls
bin etc
これにより、ファイルをフォルダー構造に配置しました。これは、ターゲットマシンにインストールした場合と同じになります。
2.3. コントロールファイル
最後に、パッケージのメタデータを含む制御ファイルが必要です。 これは、フィールドと説明を含むテキストファイルです。 サポートされているフィールドは次のとおりです。
- パッケージ(必須):パッケージの名前
- バージョン(必須):パッケージのバージョン
- メンテナ(必須):メンテナの名前とメールアドレス
- アーキテクチャ(必須):サポートされているターゲットマシンアーキテクチャ
- 説明(必須):パッケージの簡単な説明
- セクション: admin 、 database 、 kernel 、utilsなどのパッケージ分類
- 優先度:パッケージがオプションまたは必須のどちらであるか
- 必須:パッケージが常に必要かどうか
- 依存: libc6 (> = 2.2.4-4)のような他の依存パッケージを一覧表示します
- ホームページ:パッケージに関連付けられているWebサイトのURL
- Package-Type:たとえば、debやudebなどのタイプを示します
上記のように、一部のフィールドは必須であり、他のフィールドはオプションです。
この情報を使用して、必須フィールドといくつかの推奨フィールドを含むcontrolファイルを作成しましょう。 このファイルはDEBIANという名前のフォルダにあります。
bluelake@pacific:~/Documents/deb/build/test$ ls
bin DEBIAN etc
bluelake@pacific:~/Documents/deb/build/test$ cat DEBIAN/control
Package: test
Version: 1.0-1
Section: utils
Priority: optional
Architecture: all
Maintainer: Baeldung <[email protected]>
Description: This is a test application
for packaging
3. パッケージの構築
これで、パッケージを作成する準備が整いました。 そのために、dpkg-debコマンドを使用できます。
bluelake@pacific:~/Documents/deb/build$ dpkg-deb --root-owner-group --build test
dpkg-deb: building package 'test' in 'test.deb'.
bluelake@pacific:~/Documents/deb/build$ ls
test test.deb
ご覧のとおり、test.debファイルが正常に作成されています。 これで、パッケージの準備がほぼ整いました。
4. パッケージを確認する
4.1. 内容を確認する
パッケージが作成されたら、 dpkg コマンドを実行して、ファイルがパッケージに存在するかどうかを確認できます。
bluelake@pacific:~/Documents/deb/build$ dpkg -c test.deb
drwxr-xr-x thinkpalm/thinkpalm 0 2021-12-23 12:09 ./
drwxr-xr-x thinkpalm/thinkpalm 0 2021-12-23 11:30 ./bin/
-rwxr-xr-x thinkpalm/thinkpalm 40 2021-12-23 11:15 ./bin/test.sh
drwxr-xr-x thinkpalm/thinkpalm 0 2021-12-23 11:30 ./etc/
-rw-r--r-- thinkpalm/thinkpalm 10 2021-12-23 11:15 ./etc/test.conf
結果から、予想されるパスに追加したファイルが含まれていることがわかります。
4.2. リンティング
Debianのパッケージ化規則にさらに準拠するために、パッケージをリントすることができます。 このために、lintianと呼ばれるツールを使用します。
このツールの使用方法を見てみましょう。
bluelake@pacific:~/Documents/deb/build$ lintian test.deb
E: test: debian-changelog-file-missing
E: test: file-in-etc-not-marked-as-conffile etc/test.conf
E: test: no-copyright-file
W: test: script-with-language-extension bin/test.sh
W: test: binary-without-manpage bin/test.sh
コマンドを実行した後、EとしてマークされたいくつかのエラーとWとしてマークされた警告を見ることができます。
これらを1つずつ修正する方法を確認してみましょう。
変更ログファイルがありません:
このエラーを修正するには、changelogファイルを追加します。
bluelake@pacific:~/Documents/deb/build$ cat changelog.Debian
test (1.0-2) stable; urgency=low
[ Baeldung ]
* Changes to installer
-- Baeldung <[email protected]> Thu, 23 Dec 2021 11:30:00 +0100
test (1.0-1) stable; urgency=low
[ Baeldung ]
* Wonderful program to print the name ;)
-- Baeldung <[email protected]> Thu, 23 Dec 2021 11:00:00 +0100
bluelake@pacific:~/Documents/deb/build$ gzip --best -n changelog.Debian
bluelake@pacific:~/Documents/deb/build$ mkdir -p test/usr/share/doc/test
bluelake@pacific:~/Documents/deb/build$ cp changelog.Debian.gz test/usr/share/doc/test/
ファイルを会議ファイルとしてマークする:
このエラーを修正するために、conffileをDEBIANフォルダーに追加できます。
bluelake@pacific:~/Documents/deb/build$ cat test/DEBIAN/conffiles
/etc/test.conf
著作権ファイルの追加:
これを修正するには、copyrightファイルを追加します。
bluelake@pacific:~/Documents/deb/build$ cat test/usr/share/doc/test/copyright
test
Copyright: 2020 Baeldung <[email protected]>
2020-12-23
The entire code base may be distributed under the terms of the GNU General
Public License (GPL), which appears immediately below. Alternatively, all
of the source code as any code derived from that code may instead be
distributed under the GNU Lesser General Public License (LGPL), at the
choice of the distributor. The complete text of the LGPL appears at the
bottom of this file.
See /usr/share/common-licenses/(GPL|LGPL)
バイナリファイルの拡張子の削除:
このエラーを修正するために、スクリプトファイルの拡張子を削除しましょう。
bluelake@pacific:~/Documents/deb/build$ mv test/bin/test.sh test/bin/test
バイナリの男性エントリの追加:
このエラーを修正するためにmanファイルを追加しましょう。
bluelake@pacific:~/Documents/deb/build$ cat test.1
.\" Hey, EMACS: -*- nroff -*-
.\" (C) Copyright 2020 Baeldung <[email protected]>
.\"
.TH TEST 1
.SH NAME
test \- test application
.SH SYNOPSIS
.B test.sh
.SH DESCRIPTION
The
.B test
prints the name.
.SH SEE ALSO
.BR echo (1).
.SH AUTHORS
The
.B test
script was written by
Baeldung <[email protected]>
.PP
This document was written by Baeldung <[email protected]> for Debian.
bluelake@pacific:~/Documents/deb/build$ gzip --best -n test.1
bluelake@pacific:~/Documents/deb/build$ ls
build.sh changelog.Debian.gz changelog.gz test test_1.0-1.deb test.1.gz
bluelake@pacific:~/Documents/deb/build$ mkdir -p test/usr/share/man/man1
bluelake@pacific:~/Documents/deb/build$ cp test.1.gz test/usr/share/man/man1/
最終的なフォルダ構造は次のようになります。
bluelake@pacific:~/Documents/deb/build$ tree test
test
├── bin
│ └── test
├── DEBIAN
│ ├── conffiles
│ └── control
├── etc
│ └── test.conf
└── usr
└── share
├── doc
│ └── test
│ ├── changelog.Debian.gz
│ └── copyright
└── man
└── man1
└── test.1.gz
9 directories, 7 files
これをもう一度作成しましょう:
bluelake@pacific:~/Documents/deb/build$ dpkg-deb --root-owner-group --build test
dpkg-deb: building package 'test' in 'test.deb'.
bluelake@pacific:~/Documents/deb/build$ mv test.deb test_1.0-2.deb
bluelake@pacific:~/Documents/deb/build$ lintian test_1.0-2.deb
bluelake@pacific:~/Documents/deb/build$
これで、すべてのエラーと警告が修正されたを確認できます。
5. 結論
このチュートリアルでは、最小限の手順でdebパッケージを作成するプロセスを実行しました。 これがDebianパッケージの作成を学ぶための良い出発点になることを願っています。