LoginSignup
10
7

More than 5 years have passed since last update.

rpm作成時%preunと%postunには気をつけよう

Last updated at Posted at 2018-11-20

何が起きたか?

// rpmをインストール
# rpm -ivh goblin-slayer-1.00-1.el7.noarch.rpm

// 牛飼娘も女神官もいる :)
# ll /etc/cow_girl.ini
-rw-r--r-- 1 root root 0 11月 20 22:48 /etc/cow_girl.ini
# ll /etc/priestess.ini
-rw-r--r-- 1 root root 0 11月 20 22:48 /etc/priestess.ini

// rpmをアップグレード
# rpm -Uvh goblin-slayer-1.00-2.el7.noarch.rpm

// いなくなってしまった!? :(
# ll /etc/cow_girl.ini
ls: cannot access /etc/cow_girl.ini: No such file or directory
# ll /etc/priestess.ini
ls: cannot access /etc/priestess.ini: No such file or directory

rpmのspecファイルは以下の最低限で、Releaseを1から2に変更しただけです。

goblin-slayer.spec
Name:     goblin-slayer
Version:  1.00
Release:  1%{?dist}
Summary:  GOBLIN SLAYER
License:  GPL+ or Artistic
BuildArch:  noarch

%description
%prep
%setup -T -c
%files

%pre
touch /etc/cow_girl.ini

%post
touch /etc/priestess.ini

%preun
rm -f /etc/cow_girl.ini

%postun
rm -f /etc/priestess.ini

原因

rpmのアップグレードは
%pre,%postだけでなく、%preun,%postunも呼ばれるためです。
このため、ファイルが消されてしまいました。

どうすればいいのか

引数$1に実行時の値が入ってくるのでそれを使って判定します。

Fedora Packaging Guidelines for RPM Scriptlets - Fedora Project Wiki
から表を引用します。

install upgrade uninstall
%pre $1 == 1 $1 == 2 (N/A)
%post $1 == 1 $1 == 2 (N/A)
%preun (N/A) $1 == 1 $1 == 0
%postun (N/A) $1 == 1 $1 == 0

整理するとこの順番、この引数の値で呼ばれます。

  • 初回インストール時
1 %pre $1 == 1
2 $post $1 == 1
  • アップグレード時
1 %pre $1 == 2 new rpm
2 $post $1 == 2 new rpm
3 %preun $1 == 1 old rpm
4 %postun $1 == 1 old rpm

※注意 %preunと%postunは古いパッケージの方のものが呼ばれる。しかも順番は後になる。

  • アンインストール時
1 %preun $1 == 0
2 $postun $1 == 0

修正

なので$1が0のときだけと判定するようにします。

goblin-slayer.spec
Name:     goblin-slayer
Version:  1.00
Release:  1%{?dist}
Summary:  GOBLIN SLAYER
License:  GPL+ or Artistic
BuildArch:  noarch

%description
%prep
%setup -T -c
%files

%pre
touch /etc/cow_girl.ini

%post
touch /etc/priestess.ini

%preun
if [ $1 -eq 0 ]; then
    rm -f /etc/cow_girl.ini
fi

%postun
if [ $1 -eq 0 ]; then
    rm -f /etc/priestess.ini
fi

参考

pre,post,preun,postunの$1の値 - Qiita

RPM SPEC scriptlet - ダメ出し Blog

10
7
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
10
7