Debian

如何在 Debian 二進制包控製文件中註入使用者定義的欄位

  • June 25, 2021

可以使用Debian Policy Manual 中定義XBS-的方法在 debian/control 文件中添加使用者定義的欄位,這些欄位將被注入到二進制 .deb 和源包中。

在呼叫 dpkg-buildpackage 之前,類似這樣的操作可以將一個新欄位注入到 Source 和 Binary 包中。但是某些軟體包在生成它(核心等)時不包含 debian/control,因此這並不總是可靠的。

sed -i "0,/^\s*$/s//XBS-Git-Branch: ${CI_COMMIT_BRANCH}\n/" debian/control
dpkg-buildpackage

也可以在建構包時使用傳遞給 dpkg-source 的選項將使用者定義的欄位注入到生成的源包中:

dpkg-buildpackage --source-option=-DGit-Branch=${CI_COMMIT_BRANCH}

但是,這只會將該欄位注入到生成的源包中,而不是生成的二進制包中。有沒有一種方法可以可靠地將欄位注入二進制包?

--hook-buildinfo可以從呼叫後立即呼叫的鉤子中修改二進制包控製文件debian/rules builddpkg-deb用於解包/重新打包它們。

#!/bin/bash
#
# deb_insert_meta.sh
#
# Inserts CI metadata into all deb files in parent directory
# Intended for use with:
# `dpkg-buildpackage --hook-buildinfo='fakeroot deb_insert_meta.sh'`
# which is the first hook after the binary deb files are generated
# but before checksums for .changes are calculated
# Should be called with 'fakeroot' so that the repacked binaries
# have their content/control ownership/permissions preserved.

pushd .. > /dev/null || exit 1

   for deb_file in *.deb; do
       [ -e "$deb_file" ] || continue

       DEB_TMPDIR=$(mktemp -d)
       if [ -z "${DEB_TMPDIR}" ]; then
           echo "Failed to create a temporary work directory"
           exit 1
       fi

       dpkg-deb -R "${deb_file}" "${DEB_TMPDIR}"

       if [ -e "${DEB_TMPDIR}/DEBIAN/control" ]; then
           if [ -n "${CI_PROJECT_PATH}" ]; then
               echo "Git-Repo: ${CI_PROJECT_PATH}" >> "${DEB_TMPDIR}/DEBIAN/control"
           fi
           if [ -n "${CI_COMMIT_SHA}" ]; then
               echo "Git-Hash: ${CI_COMMIT_SHA}" >> "${DEB_TMPDIR}/DEBIAN/control"
           fi
           if [ -n "${CI_COMMIT_BRANCH}" ]; then
               echo "Git-Branch: ${CI_COMMIT_BRANCH}" >> "${DEB_TMPDIR}/DEBIAN/control"
           fi

           dpkg-deb -b "${DEB_TMPDIR}" "${deb_file}"

       fi

       rm -rf "${DEB_TMPDIR}"

   done

popd > /dev/null || exit 1

exit 0

似乎您的根本問題並非debian/control在所有情況下都有。解決方法總是從源包開始,因為它們必須提供一個debian/control文件。任何其他方法都將涉及直接執行部分建構,因為dpkg-buildpackagerequires debian/control; 例如,您可以執行debian/rules build,然後在執行(生成二進制包)debian/control之前修補(此時必須存在)。debian/rules binary

要控制二進制包control文件中的內容,您還可以添加選項dpkg-gencontrol例如使用dh_gencontrol

override_dh_gencontrol:
       dh_gencontrol -- -Dfoo=bar

將為您的二進制封包件添加一個Foo: bar條目control。這在您的場景中可能更有用。

您需要進行更改debian/rules才能執行此操作;你可以dh_gencontrol像上面那樣使用,或者dpkg-gencontrol如果包不使用dh.

引用自:https://unix.stackexchange.com/questions/651247