From 835a2468dcaf0d276c982f2b3ea1da1543de4bbd Mon Sep 17 00:00:00 2001 From: Steve Baker Date: Mon, 2 Sep 2024 16:12:54 +1200 Subject: [PATCH] New elements replace-partition(-redhat) replace-partition ----------------- A standalone element which consumes a base image which was created with ``diskimage-builder`` and rebuilds it without making any packaging changes. This allows the image contents to be copied to a new block device layout. Use cases for this element include: * Rebuilding a whole-disk image with a different partition layout by setting ``DIB_BLOCK_DEVICE_CONFIG`` * Rebuilding a whole-disk image with the same partitions but with the sector size increased to 4096 bytes replace-partition-redhat ------------------------ A redhat family specific version of the ``replace-partition`` element. Change-Id: I7399c4bf6a4d6acfef43f871df0a40e2961ed44e --- bindep.txt | 2 ++ .../replace-partition-redhat/README.rst | 22 +++++++++++++ .../replace-partition-redhat/element-deps | 2 ++ .../09-replace-partition-redhat.bash | 12 +++++++ .../elements/replace-partition/README.rst | 31 +++++++++++++++++++ .../elements/replace-partition/element-deps | 1 + .../replace-partition/element-provides | 1 + .../environment.d/10-replace-partition.bash | 25 +++++++++++++++ .../root.d/10-local-image-extract | 26 ++++++++++++++++ .../replace-partition-76683c72e23b60b8.yaml | 9 ++++++ 10 files changed, 131 insertions(+) create mode 100644 diskimage_builder/elements/replace-partition-redhat/README.rst create mode 100644 diskimage_builder/elements/replace-partition-redhat/element-deps create mode 100644 diskimage_builder/elements/replace-partition-redhat/environment.d/09-replace-partition-redhat.bash create mode 100644 diskimage_builder/elements/replace-partition/README.rst create mode 100644 diskimage_builder/elements/replace-partition/element-deps create mode 100644 diskimage_builder/elements/replace-partition/element-provides create mode 100644 diskimage_builder/elements/replace-partition/environment.d/10-replace-partition.bash create mode 100755 diskimage_builder/elements/replace-partition/root.d/10-local-image-extract create mode 100644 releasenotes/notes/replace-partition-76683c72e23b60b8.yaml diff --git a/bindep.txt b/bindep.txt index fdd82e78d..8a568ec46 100644 --- a/bindep.txt +++ b/bindep.txt @@ -27,6 +27,7 @@ uuid-runtime [platform:dpkg] debootstrap [platform:dpkg] zstd [platform:dpkg] procps [platform:dpkg] +libguestfs-tools [platform:dpkg] # Version/distro specific items python-lzma [platform:ubuntu-bionic platform:debian-buster] yum-utils [platform:ubuntu-bionic platform:debian-buster] @@ -34,6 +35,7 @@ dnf [platform:dpkg !platform:debian-buster !platform:ubuntu-trusty !platform:ubu # centos/fedora qemu-img [platform:redhat] +libguestfs [platform:redhat] policycoreutils-python [platform:rhel-7 platform:centos-7] policycoreutils-python-utils [platform:fedora platform:rhel-8 platform:centos-8] procps-ng [platform:redhat] diff --git a/diskimage_builder/elements/replace-partition-redhat/README.rst b/diskimage_builder/elements/replace-partition-redhat/README.rst new file mode 100644 index 000000000..c5f125f98 --- /dev/null +++ b/diskimage_builder/elements/replace-partition-redhat/README.rst @@ -0,0 +1,22 @@ +======================== +replace-partition-redhat +======================== + +A redhat family specific version of the ``replace-partition`` element. + +A standalone element which consumes a base image which was created with +``diskimage-builder`` and rebuilds it without making any packaging changes. + +Example +======= + +The following will use ``./my-dib-image.qcow2`` as the base image and create a +new image ``my-dib-image-custom.qcow2`` with the partition layout defined in +``./block-device-custom.yaml``: + +.. code-block:: bash + + export DISTRO_NAME=rhel + export DIB_BLOCK_DEVICE_CONFIG=file://./block-device-custom.yaml + export DIB_LOCAL_IMAGE=./my-dib-image.qcow2 + disk-image-create -x -a x86_64 -o my-dib-image-custom.qcow2 --image-size 6GiB replace-partition-redhat diff --git a/diskimage_builder/elements/replace-partition-redhat/element-deps b/diskimage_builder/elements/replace-partition-redhat/element-deps new file mode 100644 index 000000000..a0d848129 --- /dev/null +++ b/diskimage_builder/elements/replace-partition-redhat/element-deps @@ -0,0 +1,2 @@ +replace-partition +selinux-fixfiles-restore \ No newline at end of file diff --git a/diskimage_builder/elements/replace-partition-redhat/environment.d/09-replace-partition-redhat.bash b/diskimage_builder/elements/replace-partition-redhat/environment.d/09-replace-partition-redhat.bash new file mode 100644 index 000000000..c07723696 --- /dev/null +++ b/diskimage_builder/elements/replace-partition-redhat/environment.d/09-replace-partition-redhat.bash @@ -0,0 +1,12 @@ +if [ -z "${DISTRO_NAME:-}" ]; then + echo "DISTRO_NAME is not set! Can not continue" + exit 1 +fi +export DIB_RELEASE=${DIB_RELEASE:-9} +if [[ $DISTRO_NAME =~ "rhel" ]]; then + export EFI_BOOT_DIR="EFI/redhat" +else + export EFI_BOOT_DIR="EFI/$DISTRO_NAME" +fi +export DIB_BLOCK_DEVICE=efi +export DIB_INIT_SYSTEM=systemd \ No newline at end of file diff --git a/diskimage_builder/elements/replace-partition/README.rst b/diskimage_builder/elements/replace-partition/README.rst new file mode 100644 index 000000000..251f2cd51 --- /dev/null +++ b/diskimage_builder/elements/replace-partition/README.rst @@ -0,0 +1,31 @@ +================= +replace-partition +================= + +A standalone element which consumes a base image which was created with +``diskimage-builder`` and rebuilds it without making any packaging changes. This +allows the image contents to be copied to a new block device layout. Use cases +for this element include: + +* Rebuilding a whole-disk image with a different partition layout by setting + ``DIB_BLOCK_DEVICE_CONFIG`` +* Rebuilding a whole-disk image with the same partitions but with the sector + size increased to 4096 bytes + +See element ``replace-partition-redhat`` for a full example of how to use this element + +Arguments +========= + +The following arguments are mandatory and should be set to the correct values as +for other distro elements: + +* ``DISTRO_NAME`` +* ``DIB_RELEASE`` +* ``EFI_BOOT_DIR`` +* ``DIB_INIT_SYSTEM`` +* ``DIB_BLOCK_DEVICE`` +* ``DIB_LOCAL_IMAGE`` + +``DIB_SOURCE_BLOCK_SIZE`` can be set to 4096 if the base image +``DIB_LOCAL_IMAGE`` was created with a 4096 byte sector size. diff --git a/diskimage_builder/elements/replace-partition/element-deps b/diskimage_builder/elements/replace-partition/element-deps new file mode 100644 index 000000000..a2991d57c --- /dev/null +++ b/diskimage_builder/elements/replace-partition/element-deps @@ -0,0 +1 @@ +bootloader \ No newline at end of file diff --git a/diskimage_builder/elements/replace-partition/element-provides b/diskimage_builder/elements/replace-partition/element-provides new file mode 100644 index 000000000..b5e019942 --- /dev/null +++ b/diskimage_builder/elements/replace-partition/element-provides @@ -0,0 +1 @@ +operating-system \ No newline at end of file diff --git a/diskimage_builder/elements/replace-partition/environment.d/10-replace-partition.bash b/diskimage_builder/elements/replace-partition/environment.d/10-replace-partition.bash new file mode 100644 index 000000000..6a5bfaddb --- /dev/null +++ b/diskimage_builder/elements/replace-partition/environment.d/10-replace-partition.bash @@ -0,0 +1,25 @@ +if [ -z "${DISTRO_NAME:-}" ]; then + echo "DISTRO_NAME is not set! Can not continue" + exit 1 +fi +if [ -z "${DIB_RELEASE:-}" ]; then + echo "DIB_RELEASE is not set! Can not continue" + exit 1 +fi +if [ -z "${EFI_BOOT_DIR:-}" ]; then + echo "EFI_BOOT_DIR is not set! Can not continue" + exit 1 +fi +if [ -z "${DIB_INIT_SYSTEM:-}" ]; then + echo "DIB_INIT_SYSTEM is not set! Can not continue" + exit 1 +fi +if [ -z "${DIB_BLOCK_DEVICE:-}" ]; then + echo "DIB_BLOCK_DEVICE is not set! Can not continue" + exit 1 +fi +export DIB_SKIP_GRUB_PACKAGE_INSTALL=True +export DIB_SKIP_BASE_PACKAGE_INSTALL=1 +export DIB_AVOID_PACKAGES_UPDATE=1 +export DIB_IMAGE_EXTRACT_GUESTFISH=True +export DIB_SOURCE_BLOCK_SIZE=512 diff --git a/diskimage_builder/elements/replace-partition/root.d/10-local-image-extract b/diskimage_builder/elements/replace-partition/root.d/10-local-image-extract new file mode 100755 index 000000000..9c1099920 --- /dev/null +++ b/diskimage_builder/elements/replace-partition/root.d/10-local-image-extract @@ -0,0 +1,26 @@ +#!/bin/bash + +if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +[ -n "$TARGET_ROOT" ] + +DIB_LOCAL_IMAGE=${DIB_LOCAL_IMAGE:-""} + +if [ -n "$DIB_LOCAL_IMAGE" ]; then + IMAGE_LOCATION=$DIB_LOCAL_IMAGE + # No need to copy a local image into the cache directory, so just specify + # the cached path as the original path. + CACHED_IMAGE=$IMAGE_LOCATION +else + echo "DIB_LOCAL_IMAGE is required" + exit 1 +fi + +guestfish -v -x -i --blocksize=$DIB_SOURCE_BLOCK_SIZE -a $CACHED_IMAGE <