[PATCH 6/8] guestfs: Use sudo for base image copy with system libvirt

Luis Chamberlain <[email protected]> Fri, 17 Oct 2025 19:31:51 -0700
Newsgroups dev.linux.lists.kdevops
Message-ID <[email protected]>
When using system libvirt (qemu:///system), the storage pool paths are
typically owned by root or the libvirt group, requiring elevated
privileges to copy base images.

Add conditional sudo support for image duplication:
- System libvirt (libvirt_uri_system=true): Use become/sudo
- Session libvirt (libvirt_uri_system=false): No privilege escalation

This fixes permission denied errors when copying base images to storage
pools like /xfs1/libvirt/kdevops/ that are not writable by the regular
user.

The --reflink=auto flag is preserved in both code paths to enable
efficient copy-on-write when the filesystem supports it (btrfs, XFS
with reflink).

Generated-by: Claude AI
Signed-off-by: Luis Chamberlain <[email protected]>
---
 playbooks/roles/guestfs/tasks/bringup/main.yml | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/playbooks/roles/guestfs/tasks/bringup/main.yml b/playbooks/roles/guestfs/tasks/bringup/main.yml
index 81bac7ce..510e1a7d 100644
--- a/playbooks/roles/guestfs/tasks/bringup/main.yml
+++ b/playbooks/roles/guestfs/tasks/bringup/main.yml
@@ -50,10 +50,21 @@
         state: directory
       delegate_to: localhost
 
-    - name: Duplicate the root disk image for each target node
+    - name: Duplicate the root disk image for each target node (as root)
+      become: true
+      become_method: ansible.builtin.sudo
+      ansible.builtin.command:
+        cmd: "cp --reflink=auto {{ base_image }} {{ root_image }}"
+      delegate_to: localhost
+      when:
+        - libvirt_uri_system|bool
+
+    - name: Duplicate the root disk image for each target node (non-root)
       ansible.builtin.command:
         cmd: "cp --reflink=auto {{ base_image }} {{ root_image }}"
       delegate_to: localhost
+      when:
+        - not libvirt_uri_system|bool
 
     - name: Get the timezone of the control host
       ansible.builtin.command:
-- 
2.51.0