簡體   English   中英

Yocto - 鏡像名稱中的 git 修訂

[英]Yocto - git revision in the image name

默認情況下,Yocto 將構建時間戳添加到 output 映像文件名,但我想用我的集成 Git 存儲庫的修訂版替換它(它引用了我所有的層和配置文件)。 為此,我將以下代碼放入我的圖像配方中:

def get_image_version(d):
    import subprocess
    import os.path

    try:
        parentRepo = os.path.dirname(d.getVar("COREBASE", True))
        return subprocess.check_output(["git", "describe", "--tags", "--long", "--dirty"], cwd = parentRepo, stderr = subprocess.DEVNULL).strip().decode('UTF-8')
    except:
        return d.getVar("MACHINE", True) + "-" + d.getVar("DATETIME", True)


IMAGE_VERSION = "${@get_image_version(d)}"
IMAGE_NAME = "${IMAGE_BASENAME}-${IMAGE_VERSION}"
IMAGE_NAME[vardepsexclude] = "IMAGE_VERSION"

此代碼正常工作,直到我更改 Git 修訂版(例如通過添加新提交)。 然后我收到以下錯誤:

ERROR: When reparsing /home/ubuntu/yocto/poky/../mylayer/recipes-custom/images/core-image-minimal.bb.do_image_tar, the basehash value changed from 63e1e69797d2813a4c36297517478a28 to 9788d4bf2950a23d0f758e4508b0a894. The metadata is not deterministic and this needs to be fixed.

我知道發生這種情況是因為圖像配方已經用舊的 Git 修訂版解析,但為什么構建時間戳的不斷變化不會導致相同的錯誤? 如何修復我的代碼來克服這個問題?

時間戳自從添加到 vardepsexclude 后沒有此效果: https://docs.yoctoproject.org/bitbake/bitbake-user-manual/bitbake-user-manual-metadata.html#variable-flags

[vardepsexclude]:指定一個以空格分隔的變量列表,這些變量應從變量的依賴項中排除,以便計算其簽名。

您可能需要在幾個地方添加它,例如: https://git.yoctoproject.org/poky/tree/meta/classes/image-artifact-names.bbclass#n7

IMAGE_VERSION_SUFFIX ?= "-${DATETIME}"
IMAGE_VERSION_SUFFIX[vardepsexclude] += "DATETIME SOURCE_DATE_EPOCH"
IMAGE_NAME ?= "${IMAGE_BASENAME}-${MACHINE}${IMAGE_VERSION_SUFFIX}"

經過一番研究,結果發現問題出在這一行

IMAGE_VERSION = "${@get_image_version(d)}"

因為在解析過程中調用了 function get_image_version get_image_version() 我從 aehs29 帖子中的源文件中獲得靈感,並將代碼移至匿名 Python function 中,該代碼在解析后調用。

我還必須將vardepsexclude屬性添加到IMAGE_NAME變量。 我也嘗試將vardepvalue標志添加到IMAGE_VERSION變量中,在這種特殊情況下,它的工作與vardepsexclude相同。 提到的 Bitbake class 使用這兩個標志,但我認為在我的情況下只使用其中一個就足夠了。

最終代碼如下:

IMAGE_VERSION ?= "${MACHINE}-${DATETIME}"

IMAGE_NAME = "${IMAGE_BASENAME}-${IMAGE_VERSION}"
IMAGE_NAME[vardepsexclude] += "IMAGE_VERSION"

python () {
    import subprocess
    import os.path

    try:
        parentRepo = os.path.dirname(d.getVar("COREBASE", True))
        version = subprocess.check_output(["git", "describe", "--tags", "--long", "--dirty"], cwd = parentRepo, stderr = subprocess.DEVNULL).strip().decode('UTF-8')
        d.setVar("IMAGE_VERSION", version)

    except:
        bb.warning("Could not get Git revision, image will have default name.")
}

編輯:經過一些研究,我意識到最好在layer.conf文件中定義一個全局變量,該文件包含引用該變量的配方。 該變量由 python 腳本設置,並立即展開以防止確定性構建警告:

層.conf:

require conf/image-version.py.inc
IMAGE_VERSION := "${@get_image_version(d)}"

圖像版本.py.inc:

def get_image_version(d):
    import subprocess
    import os.path

    try:
        parentRepo = os.path.dirname(d.getVar("COREBASE", True))
        return subprocess.check_output(["git", "describe", "--tags", "--long", "--dirty"], cwd = parentRepo, stderr = subprocess.DEVNULL).strip().decode('UTF-8')
    except:
        bb.warn("Could not determine image version. Default naming schema will be used.")
        return d.getVar("MACHINE", True) + "-" + d.getVar("DATETIME", True)

我認為這是更干凈的方法,更適合 BitBake 構建系統。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM