From 5c10b708f0277ea2d75df7ba02746ebf36be2c46 Mon Sep 17 00:00:00 2001 From: Simon Westphahl Date: Thu, 27 Jun 2024 09:32:17 +0200 Subject: [PATCH] Add a role to convert diskimages between formats This adds a role convert-diskimage which uses the qemu-img tool to convert diskimages from one format to another. Currently supported image formats are raw and qcow2. Change-Id: I4770af04c37f39e0cce23d5dd59ead744bed7d74 --- doc/source/dib-roles.rst | 1 + roles/convert-diskimage/README.rst | 19 ++++++ roles/convert-diskimage/tasks/main.yaml | 32 +++++++++ roles/convert-diskimage/vars/main.yaml | 3 + .../diskimage-builder/convert-diskimage.yaml | 21 ++++++ zuul-tests.d/diskimage-builder-roles.yaml | 66 +++++++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 roles/convert-diskimage/README.rst create mode 100644 roles/convert-diskimage/tasks/main.yaml create mode 100644 roles/convert-diskimage/vars/main.yaml create mode 100644 test-playbooks/diskimage-builder/convert-diskimage.yaml diff --git a/doc/source/dib-roles.rst b/doc/source/dib-roles.rst index ff0876931..691b8f4b5 100644 --- a/doc/source/dib-roles.rst +++ b/doc/source/dib-roles.rst @@ -3,3 +3,4 @@ Diskimage-Builder Roles .. zuul:autorole:: ensure-dib .. zuul:autorole:: build-diskimage +.. zuul:autorole:: convert-diskimage diff --git a/roles/convert-diskimage/README.rst b/roles/convert-diskimage/README.rst new file mode 100644 index 000000000..21223eea7 --- /dev/null +++ b/roles/convert-diskimage/README.rst @@ -0,0 +1,19 @@ +This role converts diskimages from one format to other using the qemu-img tool. + +Currently, only Ubuntu and Debian distributions are supported. + +**Role variables** + +.. zuul:rolevar:: convert_diskimage_source_image + + The path to the source image file. + +.. zuul:rolevar:: convert_diskimage_target_image + + The path of the desired target image file. + +.. zuul:rolevar:: convert_diskimage_target_image_formats + :type: list + + The desired formats of the target image. + Supported values are "raw" and "qcow2". diff --git a/roles/convert-diskimage/tasks/main.yaml b/roles/convert-diskimage/tasks/main.yaml new file mode 100644 index 000000000..90278289b --- /dev/null +++ b/roles/convert-diskimage/tasks/main.yaml @@ -0,0 +1,32 @@ +- name: Make sure the role is run on Debian or Ubuntu + fail: + msg: "This role supports Debian and Ubuntu distributions only" + when: ansible_distribution not in ["Debian", "Ubuntu"] + +- name: Make sure required variables are set + assert: + that: + - convert_diskimage_source_image is defined + - convert_diskimage_target_image is defined + - convert_diskimage_target_formats is defined + +- name: Make sure target formats are supported + assert: + that: zj_format in convert_diskimage_supported_formats + loop: "{{ convert_diskimage_target_formats }}" + loop_control: + loop_var: zj_format + +- name: Install qemu tools + become: true + package: + state: present + name: qemu-utils + +- name: Convert image to the given formats + command: > + qemu-img convert {{ convert_diskimage_source_image }} + -O {{ zj_format }} {{ convert_diskimage_target_image }}.{{ zj_format }} + loop: "{{ convert_diskimage_target_formats }}" + loop_control: + loop_var: zj_format diff --git a/roles/convert-diskimage/vars/main.yaml b/roles/convert-diskimage/vars/main.yaml new file mode 100644 index 000000000..59735240e --- /dev/null +++ b/roles/convert-diskimage/vars/main.yaml @@ -0,0 +1,3 @@ +convert_diskimage_supported_formats: + - raw + - qcow2 diff --git a/test-playbooks/diskimage-builder/convert-diskimage.yaml b/test-playbooks/diskimage-builder/convert-diskimage.yaml new file mode 100644 index 000000000..7ff307be9 --- /dev/null +++ b/test-playbooks/diskimage-builder/convert-diskimage.yaml @@ -0,0 +1,21 @@ +- hosts: all + roles: + - ensure-dib + - role: build-diskimage + build_diskimage_image_name: foo-ubuntu-image + build_diskimage_formats: ["raw"] + build_diskimage_elements: ["vm", "ubuntu"] + environment: + DIB_RELEASE: "noble" + - role: convert-diskimage + convert_diskimage_source_image: "/home/zuul/dib-images/foo-ubuntu-image.raw" + convert_diskimage_target_image: "/home/zuul/dib-images/foo-ubuntu-image" + convert_diskimage_target_formats: ["qcow2"] + tasks: + - name: Gather image file info + stat: + path: "/home/zuul/dib-images/foo-ubuntu-image.qcow2" + register: image_file + - name: Assert image file exists + assert: + that: image_file.stat.exists diff --git a/zuul-tests.d/diskimage-builder-roles.yaml b/zuul-tests.d/diskimage-builder-roles.yaml index b29b9bff9..2140f26a2 100644 --- a/zuul-tests.d/diskimage-builder-roles.yaml +++ b/zuul-tests.d/diskimage-builder-roles.yaml @@ -116,6 +116,67 @@ - name: ubuntu-noble label: ubuntu-noble +- job: + name: zuul-jobs-test-convert-diskimage + description: Test the convert-diskimage role + files: + - roles/convert-diskimage/.* + - test-playbooks/diskimage-builder/.* + run: test-playbooks/diskimage-builder/convert-diskimage.yaml + required-projects: + - openstack/diskimage-builder + tags: debuntu-platforms + +- job: + name: zuul-jobs-test-convert-diskimage-debian-bookworm + description: Test the convert-diskimage role on debian-bookworm + parent: zuul-jobs-test-convert-diskimage + tags: auto-generated + nodeset: + nodes: + - name: debian-bookworm + label: debian-bookworm + +- job: + name: zuul-jobs-test-convert-diskimage-debian-bullseye + description: Test the convert-diskimage role on debian-bullseye + parent: zuul-jobs-test-convert-diskimage + tags: auto-generated + nodeset: + nodes: + - name: debian-bullseye + label: debian-bullseye + +- job: + name: zuul-jobs-test-convert-diskimage-ubuntu-focal + description: Test the convert-diskimage role on ubuntu-focal + parent: zuul-jobs-test-convert-diskimage + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-focal + label: ubuntu-focal + +- job: + name: zuul-jobs-test-convert-diskimage-ubuntu-jammy + description: Test the convert-diskimage role on ubuntu-jammy + parent: zuul-jobs-test-convert-diskimage + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-jammy + label: ubuntu-jammy + +- job: + name: zuul-jobs-test-convert-diskimage-ubuntu-noble + description: Test the convert-diskimage role on ubuntu-noble + parent: zuul-jobs-test-convert-diskimage + tags: auto-generated + nodeset: + nodes: + - name: ubuntu-noble + label: ubuntu-noble + - project: check: jobs: &id001 @@ -129,6 +190,11 @@ - zuul-jobs-test-build-diskimage-ubuntu-focal - zuul-jobs-test-build-diskimage-ubuntu-jammy - zuul-jobs-test-build-diskimage-ubuntu-noble + - zuul-jobs-test-convert-diskimage-debian-bookworm + - zuul-jobs-test-convert-diskimage-debian-bullseye + - zuul-jobs-test-convert-diskimage-ubuntu-focal + - zuul-jobs-test-convert-diskimage-ubuntu-jammy + - zuul-jobs-test-convert-diskimage-ubuntu-noble gate: jobs: *id001 periodic-weekly: