關於「dpkg --unpack」和「dpkg --configure」

前提

做這個實驗前,先了解這篇『如何製作「deb檔(Debian Package)」』。

測試環境

以下測試是在「Xubuntu 14.04 64位元」。

執行步驟

準備工作環境

建立工作環境資料夾

$ mkdir ~/Templates/unpack-and-configure-package -p

切換到工作環境資料夾

$ cd ~/Templates/unpack-and-configure-package

建立套件最上層資料夾

建立一個資料夾「try」,這個是「套件最上層資料夾」,所有的檔案都要放在這裡面

$ mkdir try -p

建立「DEBIAN」資料夾

建立「try/DEBIAN」資料夾,這個資料夾是建立「Debian Package」的依據資料夾。

$ mkdir try/DEBIAN -p

建立「control」檔

建立「try/DEBIAN/control」檔,這個檔是建立「Debian Package」的依據檔案。

$ touch try/DEBIAN/control

撰寫「control」內容

執行執行下面的指令產生

echo 'Package: try' > try/DEBIAN/control
echo 'Version: 0.1' >> try/DEBIAN/control
echo 'Architecture: all' >> try/DEBIAN/control
echo 'Description: This is a test package.' >> try/DEBIAN/control
echo 'Maintainer: developer <developer@hell.heaven>' >> try/DEBIAN/control

確認觀看剛剛產生的內容

$ cat try/DEBIAN/control

顯示

Package: try
Version: 0.1
Architecture: all
Description: This is a test package.
Maintainer: developer <developer@hell.heaven>

建立「Maintainer Scripts」

建立「try/DEBIAN/preinst」

echo '#!/bin/sh -e' > try/DEBIAN/preinst
echo "echo 'preinst'" >> try/DEBIAN/preinst

確認觀看「try/DEBIAN/preinst」的內容

$ cat try/DEBIAN/preinst

顯示

#!/bin/sh -e
echo 'preinst'

建立「try/DEBIAN/postinst」

echo '#!/bin/sh -e' > try/DEBIAN/postinst
echo "echo 'postinst'" >> try/DEBIAN/postinst

確認觀看「try/DEBIAN/postinst」的內容

$ cat try/DEBIAN/postinst

顯示

#!/bin/sh -e
echo 'postinst'

觀看目錄結構

觀看一下剛剛建立的目錄結構

$ tree try

顯示

try
└── DEBIAN
    ├── control
    ├── postinst
    └── preinst

1 directory, 3 files

製作「Debian Package」

執行

$ dpkg -b try

會顯示

dpkg-deb: error: maintainer script `preinst' has bad permissions 664 (must be >=0555 and <=0775)

更改「try/DEBIAN/preinst」權限

$ chmod 755 try/DEBIAN/preinst

再次執行

$ dpkg -b try

會顯示

dpkg-deb: error: maintainer script `postinst' has bad permissions 664 (must be >=0555 and <=0775)

更改「try/DEBIAN/postinst」權限

$ chmod 755 try/DEBIAN/postinst

再次執行

$ dpkg -b try

會顯示

dpkg-deb: building package `try' in `try.deb'.

測試安裝

執行安裝

$ sudo dpkg -i try.deb

顯示

Selecting previously unselected package try.
(Reading database ... 947428 files and directories currently installed.)
Preparing to unpack try.deb ...
preinst
Unpacking try (0.1) ...
Setting up try (0.1) ...
postinst

執行

$ dpkg -s try

顯示

Package: try
Status: install ok installed
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Description: This is a test package.

觀看「/var/lib/dpkg/status」

$ grep 'Package: try' /var/lib/dpkg/status -A 5

顯示

Package: try
Status: install ok installed
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Description: This is a test package.

觀看「/var/lib/dpkg/available」

$ grep 'Package: try' /var/lib/dpkg/available -A 5

顯示

Package: try
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Size: 672
Description: This is a test package.

再次安裝

再次執行安裝

$ sudo dpkg -i try.deb

顯示

(Reading database ... 947428 files and directories currently installed.)
Preparing to unpack try.deb ...
preinst
Unpacking try (0.1) over (0.1) ...
Setting up try (0.1) ...
postinst

比對上面第一次安裝

Unpacking try (0.1) ...

變成了

Unpacking try (0.1) over (0.1) ...

移除

移除「try」這個套件

$ sudo dpkg -r try

顯示

(Reading database ... 947427 files and directories currently installed.)
Removing try (0.1) ...

執行

$ dpkg -s try

顯示

dpkg-query: package 'try' is not installed and no information is available
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.

觀看「/var/lib/dpkg/available」

$ grep 'Package: try' /var/lib/dpkg/available -A 5

顯示

Package: try
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Size: 672
Description: This is a test package.

觀看「/var/lib/dpkg/status」

$ grep 'Package: try' /var/lib/dpkg/status -A 5

沒有顯示訊息。

測試「dpkg --unpack」

執行

$ sudo dpkg --unpack try.deb

顯示

Selecting previously unselected package try.
(Reading database ... 947428 files and directories currently installed.)
Preparing to unpack try.deb ...
preinst
Unpacking try (0.1) ...

執行

$ dpkg -s try

顯示

Package: try
Status: install ok unpacked
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Description: This is a test package.

觀看「/var/lib/dpkg/status」

$ grep 'Package: try' /var/lib/dpkg/status -A 5

顯示

Package: try
Status: install ok unpacked
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Description: This is a test package.

比對上面第一次安裝

Status: install ok installed

變成了

Status: install ok unpacked

觀看「/var/lib/dpkg/available」

$ grep 'Package: try' /var/lib/dpkg/available -A 5

顯示

Package: try
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Size: 672
Description: This is a test package.

測試「dpkg --configure」

執行 (注意這裡後面是接package的名稱,而不是檔名)

$ sudo dpkg --configure try

顯示

Setting up try (0.1) ...
postinst

執行

$ dpkg -s try

顯示

Package: try
Status: install ok installed
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Description: This is a test package.

觀看「/var/lib/dpkg/status」

$ grep 'Package: try' /var/lib/dpkg/status -A 5

顯示

Package: try
Status: install ok installed
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Description: This is a test package.

觀看「/var/lib/dpkg/available」

$ grep 'Package: try' /var/lib/dpkg/available -A 5

顯示

Package: try
Maintainer: developer <developer@hell.heaven>
Architecture: all
Version: 0.1
Size: 672
Description: This is a test package.

小結

「dpkg --install」這個動作,基本上可以拆成「dpkg --unpack」和「dpkg --configure」兩階段完成

額外一提

執行「dpkg --unpack」後,也可以執行「sudo apt-get install」,一樣會完成「dpkg --configure」這個動作。 可以自行實驗看看。

更多參考