heyheytower

日々のメモです。誰かのお役に立てれば幸いです。

"SysVinit" 起動の自作 deb パッケージを "systemd" 対応させる

likana.service のステータス
Pic.1 "systemd" 対応させた自作サービス (likana)

目次

目的

以前作成した deb パッケージが "SysVinit" 起動だったため、自分の環境が "Ubuntu16.04" になったこともあり "systemd" 対応を行った。

方法

"debhelper" の "systemd" 対応

下記記事の通り、debian/controldebian/rules に追記を行いました。

systemd対応¶

systemdの場合は、debhelperのadd onとして、dh-systemdが用意されています。 ですので、まず、 debian/control のBuild-Dependsに、dh-systemdを追記します。

Build-Depends: debhelper (>= 8.0.0), (snip), dh-systemd (>= 1.5), (snip)
次に、 debian/rules の%ターゲットのdhコマンドのオプションとして、systemdを追記します。

%:
dh $@ --with quilt,systemd
systemd用の設定ファイルは、upstartと同様テンプレートは用意されていません。debian/<パッケージ名>.serviceとして用意します。debian/yrmcds.service として作成します。

上記のみで、あとはdh-systemdが良きように設定してくれるのですが、これだけではpurge(apt-get purge)の時に残骸が残ります。 ですので、 postrm で下記のファイルを削除します。

/etc/systemd/system/yrmcds.service
/etc/systemd/system/multi-user.target.wants/yrmcds.service
/var/lib/systemd/deb-systemd-helper-enabled/yrmcds.service.dsh-also
/var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/yrmcds.service
/var/lib/systemd/deb-systemd-helper-masked/yrmcds.service


How to create a Debian package of support to sysvinit, upstart, systemd — ペンギンと愉快な機械の日々

deb パッケージの Ubuntu16.04(xenial) 対応

下記に記載がありますとおり、debian/changelog の対象ディストリビューション記載を変更します。

That format is a series of entries like this:

 package (version) distribution(s); urgency=urgency  
      [optional blank line(s), stripped]  
   * change details  
     more change details  
      [blank line(s), included in output of dpkg-parsechangelog]  
   * even more change details  
      [optional blank line(s), stripped]  
  -- maintainer name <email address>[two spaces]  date  

package and version are the source package name and version number.

distribution(s) lists the distributions where this version should be installed when it is uploaded - it is copied to the Distribution field in the .changes file. See Distribution, Section 5.6.14.

Debian Policy Manual - Source packages

実際は dch -v 1.5-2ppa などを実行して debian/changelog を編集します。

下記コードでは、"1.5-2ppa" のバージョンから "xenial" に変更されていることがご覧いただけるかと思います。

Code.1 debian/changelog

likana (1.5-2ppa) xenial; urgency=low

  * Set up a systemd.

 -- ito <maijou2501@gmail.com>  Thu, 15 Sep 2016 23:43:45 +0900

likana (1.4-1ppa) precise; urgency=low

  * Fix, typo and binary path.

 -- ito <maijou2501@gmail.com>  Fri, 16 Sep 2016 23:41:21 +0900

これで "PPA" にてバイナリが作成される際のディストリビューション指定が完了しました。

debian/package.service の作成

"systemd" の "Unit" を書く前にそもそも "systemd" の仕組みが分かっていなかったので、先ほどの記事や下記の記事で理解を深めた。

自分の場合でも、/etc/init.d/package の内容をもとに、"service" の "Unit" を作成した。

"systemd" での設定ファイルについて

設定ファイルは同じ /etc/default/package を用いて良いようだ。

SystemdForUpstartUsers - Ubuntu Wiki

Existing sysvinit style scripts read configuration in the form of variable assignments from a file under /etc/default. A policy decision is needed on whether systemd units will do the same.


systemd - Ubuntu Wiki

しかし下記の記事内容のとおりで変数の扱いが違うようで、/etc/default/package の中で変数の代入に変数を用いないようにした。

systemd の Environment / EnvironmentFile では変数展開できません - Qiita

"systemd" での /etc/default/package への配置を Makefile にて対応

"SysVinit" の場合、debian/package.default を利用することで、/etc/default/package へ配置できていたが, 自分の設定が足りないせいなのか "systemd" を用いた場合に配置がうまくいかなかった。

正しい対応ではないかもしれないが、Makefile の "install" ルールにて対応した。

Code.2 "Makefile" より抜粋

-snip-
install:
-snip-
        test -e ${DESTDIR}/etc/default && install -m 644 -g root -o root debian/${NAME}.default ${DESTDIR}/etc/default/${NAME}
-snip-

debian/postrm への追記

先ほどの記事を参考に、削除すべきファイルを設定した。

自分の場合は debian/package.udev を利用しているので、/etc/udev/rules.d/*package.rules を削除する設定を追加した。

"systemd" の "service" の実行を "input" グループで行う

自作のサービスでは /dev/input/event* をオープンするもので、"systemd" では "service" のグループを "input" に設定しないといけないようであった。

Users and groups - ArchWiki

Op.1 オーナーとパーミッションの確認

$ ls -l /dev/input/
合計 0
drwxr-xr-x 2 root root     100 10月 30 23:19 by-id
drwxr-xr-x 2 root root     100 10月 30 23:19 by-path
crw-rw---- 1 root input 13, 64 10月 30 23:19 event0
crw-rw---- 1 root input 13, 65 10月 30 23:19 event1
crw-rw---- 1 root input 13, 66 10月 30 23:19 event2
crw-rw---- 1 root input 13, 67 10月 30 23:19 event3
crw-rw---- 1 root input 13, 68 10月 30 23:19 event4
crw-rw---- 1 root input 13, 69 10月 30 23:19 event5
crw-rw---- 1 root input 13, 70 10月 30 23:19 event6
crw-rw---- 1 root input 13, 71 10月 30 23:19 event7
crw-rw---- 1 root input 13, 72 10月 30 23:19 event8
crw-rw---- 1 root input 13, 73 10月 30 23:19 event9
crw-rw-r-- 1 root input 13,  0 10月 30 23:19 js0
crw-rw---- 1 root input 13, 63 10月 30 23:19 mice
crw-rw---- 1 root input 13, 32 10月 30 23:19 mouse0

まとめ

目的のとおり、以前作成した deb パッケージ(likana)の "systemd" 対応を行うことができた。

※ "PPA" での公開 " likana : kyohei ito "