All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Q. Gylstorff" <Quirin.Gylstorff@siemens.com>
To: cip-dev@lists.cip-project.org, jan.kiszka@siemens.com
Subject: [cip-dev][isar-cip-core][RFC 2/8] Add classes for dm-verity based rootfs
Date: Fri, 12 Nov 2021 12:50:11 +0100	[thread overview]
Message-ID: <20211112115017.401779-4-Quirin.Gylstorff@siemens.com> (raw)
In-Reply-To: <20211112115017.401779-1-Quirin.Gylstorff@siemens.com>

From: Quirin Gylstorff <quirin.gylstorff@siemens.com>

Add a bbclass to add dm-verity to a existing root file system
partition. As we need the output of `veritysetup` to generate
the initrd. Therefore do_verity_image must be called before wic
generates the final disk image.

Signed-off-by: Quirin Gylstorff <quirin.gylstorff@siemens.com>
---
 classes/verity-img.bbclass     | 73 ++++++++++++++++++++++++++++++++++
 classes/wic-verity-img.bbclass | 17 ++++++++
 2 files changed, 90 insertions(+)
 create mode 100644 classes/verity-img.bbclass
 create mode 100644 classes/wic-verity-img.bbclass

diff --git a/classes/verity-img.bbclass b/classes/verity-img.bbclass
new file mode 100644
index 0000000..82159b3
--- /dev/null
+++ b/classes/verity-img.bbclass
@@ -0,0 +1,73 @@
+#
+# CIP Core, generic profile
+#
+# Copyright (c) Siemens AG, 2021
+#
+# Authors:
+#  Quirin Gylstorff <quirin.gylstorff@siemens.com>
+#
+# SPDX-License-Identifier: MIT
+#
+IMAGER_INSTALL += "cryptsetup"
+
+VERITY_IMAGE_TYPE ?= "squashfs"
+VERITY_IMAGE ?= "${IMAGE_FULLNAME}.${VERITY_IMAGE_TYPE}.img"
+VERITY_OUTPUT_IMAGE ?= "${IMAGE_FULLNAME}.${VERITY_IMAGE_TYPE}.verity.img"
+VERITY_IMAGE_METADATA = "${VERITY_OUTPUT_IMAGE}.metadata"
+VERITY_HASH_BLOCK_SIZE ?= "1024"
+VERITY_DATA_BLOCK_SIZE ?= "1024"
+
+create_verity_env_file() {
+
+    local ENV="${DEPLOY_DIR_IMAGE}/${IMAGE_FULLNAME}.verity.${VERITY_IMAGE_TYPE}.env"
+    rm -f $ENV
+
+    local input="${WORKDIR}/${VERITY_IMAGE_METADATA}"
+    # remove header from verity meta data
+    sed -i '/VERITY header information for/d' $input
+    IFS=":"
+    while read KEY VAL; do
+        printf '%s=%s\n' \
+            "$(echo "$KEY" | tr '[:lower:]' '[:upper:]' | sed 's/ /_/g')" \
+            "$(echo "$VAL" | tr -d ' \t')" >> $ENV
+    done < $input
+}
+
+verity_setup() {
+    rm -f ${DEPLOY_DIR_IMAGE}/${VERITY_OUTPUT_IMAGE}
+    rm -f ${WORKDIR}/${VERITY_IMAGE_METADATA}
+
+    cp -a ${DEPLOY_DIR_IMAGE}/${VERITY_IMAGE} ${DEPLOY_DIR_IMAGE}/${VERITY_OUTPUT_IMAGE}
+
+    image_do_mounts
+    sudo chroot "${BUILDCHROOT_DIR}" /sbin/veritysetup format \
+        --hash-block-size "${VERITY_HASH_BLOCK_SIZE}"  \
+        --data-block-size "${VERITY_DATA_BLOCK_SIZE}"  \
+        --data-blocks "${VERITY_DATA_BLOCKS}" \
+        --hash-offset "${VERITY_INPUT_IMAGE_SIZE}" \
+        "${PP_DEPLOY}/${VERITY_OUTPUT_IMAGE}" \
+        "${PP_DEPLOY}/${VERITY_OUTPUT_IMAGE}" \
+        >"${WORKDIR}/${VERITY_IMAGE_METADATA}"
+
+    echo "Hash offset:    	${VERITY_INPUT_IMAGE_SIZE}" \
+        >>"${WORKDIR}/${VERITY_IMAGE_METADATA}"
+}
+
+do_verity_image[cleandirs] = "${WORKDIR}/verity"
+python do_verity_image() {
+    import os
+
+    image_file = os.path.join(
+        d.getVar("DEPLOY_DIR_IMAGE"),
+        d.getVar("VERITY_IMAGE")
+    )
+    data_block_size = int(d.getVar("VERITY_DATA_BLOCK_SIZE"))
+    size = os.stat(image_file).st_size
+    assert size % data_block_size == 0, f"image is not well-sized!"
+    d.setVar("VERITY_INPUT_IMAGE_SIZE", str(size))
+    d.setVar("VERITY_DATA_BLOCKS", str(size // data_block_size))
+
+    bb.build.exec_func('verity_setup', d)
+    bb.build.exec_func('create_verity_env_file', d)
+}
+addtask verity_image before do_image after do_image_tools
diff --git a/classes/wic-verity-img.bbclass b/classes/wic-verity-img.bbclass
new file mode 100644
index 0000000..e185cf8
--- /dev/null
+++ b/classes/wic-verity-img.bbclass
@@ -0,0 +1,17 @@
+#
+# CIP Core, generic profile
+#
+# Copyright (c) Siemens AG, 2021
+#
+# Authors:
+#  Quirin Gylstorff <quirin.gylstorff@siemens.com>
+#
+# SPDX-License-Identifier: MIT
+#
+
+inherit squashfs-img
+inherit verity-img
+inherit wic-img
+
+addtask verity_image after do_squashfs_image
+addtask do_wic_image after do_verity_image
-- 
2.30.2



  parent reply	other threads:[~2021-11-12 11:50 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-12 11:50 [cip-dev][isar-cip-core][RFC 0/8] Read-only root file system with dm-verity Q. Gylstorff
2021-11-12 11:50 ` [cip-dev][isar-cip-core][RFC 1/8] Add new class to create a squashfs based root file system Q. Gylstorff
2021-11-12 12:41   ` Jan Kiszka
2021-11-12 11:50 ` [PATCH] recipes-core/swupdate: Update the SRC_URI and SWUPDATE_BUILD_PROFILES append for buster Q. Gylstorff
2021-11-12 11:58   ` [cip-dev] " Gylstorff Quirin
2021-11-12 11:50 ` Q. Gylstorff [this message]
2021-11-12 11:50 ` [cip-dev][isar-cip-core][RFC 3/8] linux-cip-common: Add options necessary for dm-verity Q. Gylstorff
2021-11-12 12:46   ` Jan Kiszka
2021-11-16 10:43     ` Gylstorff Quirin
2021-11-12 11:50 ` [cip-dev][isar-cip-core][RFC 4/8] Create a initrd with support " Q. Gylstorff
2021-11-12 12:47   ` Jan Kiszka
     [not found]   ` <39f049c6-510a-5ac4-6e73-8a865e95405e@siemens.com>
2021-11-15 18:43     ` Gylstorff Quirin
2021-11-12 11:50 ` [cip-dev][isar-cip-core][RFC 5/8] Create an read-only rootfs with dm-verity Q. Gylstorff
2021-11-12 12:53   ` Jan Kiszka
2021-11-12 11:50 ` [cip-dev][isar-cip-core][RFC 6/8] Create systemd mount units for a etc overlay Q. Gylstorff
2021-11-12 11:50 ` [cip-dev][isar-cip-core][RFC 7/8] Mount writable home partition Q. Gylstorff
2021-11-12 11:50 ` [cip-dev][isar-cip-core][RFC 8/8] swupdate: Backport patches from SWUpdate Master Q. Gylstorff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211112115017.401779-4-Quirin.Gylstorff@siemens.com \
    --to=quirin.gylstorff@siemens.com \
    --cc=cip-dev@lists.cip-project.org \
    --cc=jan.kiszka@siemens.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.