Steve Baker c3f417bddf Revert "Revert "Use tripleo-mount-image for modify-image""
This reverts commit a7a05c4371bf894a58b63622a7995e6e4ee31098.

This was failing on train, ussuri, victoria because the fallback path
assumed the overcloud-full partition image is in raw format, but is
qcow2 on these stable branches.

This revert now includes the same image format detection that was
introduced to tripleo-mount-image in change
I628abc4cb8c46c3b2e5bfd3cd16bb6d5cc95fabe.

Change-Id: I0354a7f8804949916cfccfc22c59f26b54c4c134
2021-11-01 11:44:25 +13:00

139 lines
4.7 KiB
YAML

---
- name: Set abs path for image
command: echo "{{ image_to_modify }}"
register: image_to_modify_abs_path
changed_when: false
- name: Create a temp dir for extracting images
command: mktemp -d
register: mktemp_command
changed_when: true
- name: Set names for images and temp dir
set_fact:
mount_tempdir: "{{ mktemp_command.stdout }}"
- name: Ensure qemu-img is installed
package:
name: qemu-img
state: present
become: true
- when: not initramfs_image|bool
block:
- name: Set names for images and temp dir
set_fact:
image_to_modify_qcow: "{{ image_to_modify_abs_path.stdout }}"
image_to_modify_raw: "{{ image_to_modify_abs_path.stdout|replace('qcow2', 'raw') }}"
- name: Convert image
shell: >
set -eo pipefail;
qemu-img convert -f qcow2 -O raw {{ image_to_modify_qcow }} {{ image_to_modify_raw }};
rm -rf "{{ image_to_modify_qcow }}";
- name: Install required packages used in modify-image role
become: true
package:
state: present
name:
- lvm2
- name: Mount image
shell: |
set -ex
if type tripleo-mount-image >/dev/null; then
tripleo-mount-image -a {{ image_to_modify_raw }} -m {{ mount_tempdir }}
else
# stable branches do not have tripleo-mount-image, and only use
# partition images
modprobe nbd
if qemu-img info --output json {{ image_to_modify_raw }} |grep '"format": "raw"' ; then
image_format='--format raw'
elif qemu-img info --output json {{ image_to_modify_raw }} |grep '"format": "qcow2"' ; then
image_format='--format qcow2'
else
image_format=''
fi
qemu-nbd $image_format --connect /dev/nbd0 {{ image_to_modify_raw }}
mount /dev/nbd0 {{ mount_tempdir }}
fi
become: true
register: mount_result
ignore_errors: true
- name: Debug image mount
debug:
msg:
stdout: "{{ mount_result.stdout }}"
stderr: "{{ mount_result.stderr }}"
failed_when: mount_result.rc != 0
- name: Extract initramfs image
shell: set -o pipefail && gunzip -c {{ image_to_modify_abs_path.stdout }} | cpio -i
become: true
args:
chdir: "{{ mount_tempdir }}"
when: initramfs_image|bool
- become: true
block:
- name: Upload files to image
shell: >
cp {{ item.src }} {{ mount_tempdir }}/{{ item.dest }};
with_items: "{{ modify_image_upload_files }}"
- name: Check if /etc/resolv.conf exists
stat:
path: "{{ mount_tempdir }}/etc/resolv.conf"
register: resolv_stat_result
- name: Create {{ mount_tempdir }}/etc/resolv.conf if it does not exist
file:
path: "{{ mount_tempdir }}/etc/resolv.conf"
state: touch
when: not resolv_stat_result.stat.exists
- name: Run script on image
shell: >
chmod 775 {{ mount_tempdir }};
mount -o bind /dev {{ mount_tempdir }}/dev/;
mv {{ mount_tempdir }}/etc/resolv.conf{,_};
echo -e "nameserver 8.8.8.8\nnameserver 8.8.4.4" > {{ mount_tempdir }}/etc/resolv.conf;
cp {{ modify_script }} {{ mount_tempdir }}/tmp/{{ modify_script|basename }};
{% if initramfs_image|bool %}sed -i "s/sudo //g" {{ mount_tempdir }}/tmp/{{ modify_script|basename }};{% endif %}
set -o pipefail && chroot {{ mount_tempdir }} /bin/bash /tmp/{{ modify_script|basename }} 2>&1
{{ timestamper_cmd }} > {{ working_dir }}/{{ modify_script|basename }}.$(date +%s).log;
mv -f {{ mount_tempdir }}/etc/resolv.conf{_,};
umount {{ mount_tempdir }}/dev/;
- name: Extract files from image
shell: cp {{ mount_tempdir }}/{{ item }} {{ modify_image_working_dir }}/;
with_items: "{{ modify_image_extract_list }}"
ignore_errors: true
- name: Close initramfs image
shell: >
pushd {{ mount_tempdir }};
find . -print | cpio -o -H newc | gzip > {{ image_to_modify_abs_path.stdout }};
popd;
rm -rf "{{ mount_tempdir }}";
when: initramfs_image|bool
- name: Close qcow2 image
shell: |
chroot {{ mount_tempdir }} setfiles /etc/selinux/targeted/contexts/files/file_contexts /
if type tripleo-unmount-image >/dev/null; then
tripleo-unmount-image -m {{ mount_tempdir }}
else
umount {{ mount_tempdir }}
qemu-nbd --disconnect /dev/nbd0
fi
qemu-img convert -c -f raw -O qcow2 {{ image_to_modify_raw }} {{ image_to_modify_qcow }}
rm -rf "{{ image_to_modify_raw }}"
rm -rf "{{ mount_tempdir }}"
chown -R {{ undercloud_user }}: /home/{{ undercloud_user }}/
when: not initramfs_image|bool