From 8466581e5250d65a6994488e2fe227866052b05a Mon Sep 17 00:00:00 2001 From: Eduardo Olivares Date: Thu, 28 Nov 2024 18:16:48 +0100 Subject: [PATCH] Customize downloaded Ubuntu images Instead of making tobiko customize Ubuntu images during test execution, the Tobiko ansible roles run before that will do it. Related: #TOBIKO-126 Change-Id: I5c6746c6ad488eb4b27164faea67a5c0253f15f3 --- roles/tobiko-common/defaults/main.yaml | 14 +++++++++++- roles/tobiko-configure/tasks/main.yaml | 5 +++++ roles/tobiko-download-images/tasks/main.yaml | 23 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/roles/tobiko-common/defaults/main.yaml b/roles/tobiko-common/defaults/main.yaml index 6a6010ea3..1c39449e2 100644 --- a/roles/tobiko-common/defaults/main.yaml +++ b/roles/tobiko-common/defaults/main.yaml @@ -59,6 +59,18 @@ test_collect_dir: '{{ test_src_dir | realpath }}/{{ test_report_name }}' # --- download-images options ------------------------------------------------- download_images_dir: "{{ ansible_user_dir }}/.downloaded-images" download_images: - ubuntu-minimal: + ubuntu-customized: type: ubuntu url: "https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img" + customized: false + customize_command_pattern: > + LIBGUESTFS_BACKEND=direct virt-customize -a $TMPPATH + --firstboot-command 'sh -c "hostname > /var/www/html/id"' + --install iperf3,iputils-ping,ncat,nginx,vlan + --run-command 'echo "[Unit]\nDescription=iperf3 server on port %i\nAfter=syslog.target network.target\n\n[Service]\nExecStart=/usr/bin/iperf3 -s -p %i\nRestart=always\nUser=root\n\n[Install]\nWantedBy=multi-user.target\nDefaultInstance=5201\n" + > /etc/systemd/system/iperf3-server@.service' + --run-command 'systemctl enable iperf3-server@5201' + --run-command 'echo "8021q" >> /etc/modules' + --run-command + 'echo "network:\n version: 2\n vlans:\n vlan101:\n dhcp4: true\n dhcp4-overrides:\n use-routes: false\n dhcp6: true\n dhcp6-overrides:\n use-routes: false\n id: 101\n link: ens3\n" + > /etc/netplan/75-tobiko-vlan.yaml' diff --git a/roles/tobiko-configure/tasks/main.yaml b/roles/tobiko-configure/tasks/main.yaml index 23242a89c..e67653ff4 100644 --- a/roles/tobiko-configure/tasks/main.yaml +++ b/roles/tobiko-configure/tasks/main.yaml @@ -25,6 +25,11 @@ - section: "{{ dict_value.type }}" option: image_url value: "file://{{ download_images_dir }}/{{ file_name }}" + {% if dict_value.customized | default(False) | bool or dict_value.customize_command_pattern is defined %} + - section: "{{ dict_value.type }}" + option: customized_image_provided + value: True + {% endif %} {% endfor %} vars: sections: "{{ test_default_conf | combine(test_conf, recursive=True) }}" diff --git a/roles/tobiko-download-images/tasks/main.yaml b/roles/tobiko-download-images/tasks/main.yaml index a6a8ea624..acb2bb94a 100644 --- a/roles/tobiko-download-images/tasks/main.yaml +++ b/roles/tobiko-download-images/tasks/main.yaml @@ -13,3 +13,26 @@ retries: 5 until: download is success with_dict: "{{ download_images }}" + +- name: install libguestfs-tools + become: true + package: + name: libguestfs-tools + +- name: customize images +# root needed to avoid the supermin issue: +# https://github.com/virt-manager/virt-bootstrap/issues/9 + become: yes + vars: + image_file_path: "{{ download_images_dir }}/{{ item.key }}" + shell: | + set -xe + TMPPATH=$(mktemp) + cp {{ image_file_path }} $TMPPATH + {{ item.value.customize_command_pattern }} + mv $TMPPATH {{ image_file_path }} + chmod a+r {{ image_file_path }} + when: + - not (item.value.customized | default(False) | bool) + - item.value.customize_command_pattern is defined + with_dict: "{{ download_images }}"