Skip to content

Commit d12f660

Browse files
michburkopenshift-merge-bot[bot]
authored andcommitted
[libvirt_manager] Add opt-in KVM discard/TRIM support
Adds fstrim_enabled per-VM-type param. When true, sets discard='unmap' on the primary disk and schedules daily fstrim.timer in the guest via a systemd drop-in over SSH, for all VM types including OCP/RHCOS. Generated-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Michael Burke <michburk@redhat.com>
1 parent eadb4bc commit d12f660

5 files changed

Lines changed: 92 additions & 1 deletion

File tree

roles/libvirt_manager/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ cifmw_libvirt_manager_configuration:
9191
extra_disks_num: (integer, optional. Number of extra disks to be configured.)
9292
extra_disks_size: (string, optional. Storage capacity to be allocated. Example 1G, 512M)
9393
extra_disks_bus: (string, optional. Bus type for extra disks. It can be virtio or scsi. Defaults to `virtio`)
94+
fstrim_enabled: (boolean, optional. When true, sets discard='unmap' on the primary disk libvirt driver and enables fstrim.timer on a daily schedule in the guest. Defaults to false.)
9495
user: (string, optional. Username to create on the vm which can becomes root. Defaults to `zuul`)
9596
password: (string, optional, defaults to fooBar. Root password for console access)
9697
target: (Hypervisor hostname you want to deploy the family on. Optional)

roles/libvirt_manager/molecule/deploy_layout/converge.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
disksize: 20
6060
memory: 1
6161
cpus: 1
62+
fstrim_enabled: true
6263
nets:
6364
- public
6465
- osp_trunk
@@ -267,6 +268,31 @@
267268
- expected_count == found_count
268269
loop: "{{ volume_count.results }}"
269270

271+
- name: Test discard='unmap' on primary disk for fstrim_enabled VMs
272+
block:
273+
- name: Get compute-0 XML
274+
register: _compute_xml
275+
community.libvirt.virt:
276+
command: get_xml
277+
name: cifmw-compute-0
278+
uri: qemu:///system
279+
280+
- name: Assert discard=unmap on primary disk driver
281+
community.general.xml:
282+
count: true
283+
xmlstring: "{{ _compute_xml.get_xml }}"
284+
xpath: "/domain/devices/disk/driver[@discard='unmap']"
285+
register: _discard_count
286+
287+
- name: Verify discard attribute present
288+
ansible.builtin.assert:
289+
that:
290+
- _discard_count.count == 1
291+
msg: >-
292+
Primary disk driver for cifmw-compute-0 does not have
293+
discard='unmap' -- fstrim_enabled may not be wired into
294+
domain.xml.j2 correctly
295+
270296
# Redeploying the exact same layout will ensure we
271297
# are able to run the role multiple time over the
272298
# same data, without facing any issue.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
# Copyright Red Hat, Inc.
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
- name: Configure fstrim timer in guest
18+
become: true
19+
delegate_to: "{{ _fstrim_target }}"
20+
remote_user: "{{ _fstrim_user }}"
21+
block:
22+
- name: Create fstrim.timer drop-in directory
23+
ansible.builtin.file:
24+
path: /etc/systemd/system/fstrim.timer.d
25+
state: directory
26+
mode: "0755"
27+
28+
- name: Write daily fstrim override
29+
ansible.builtin.copy:
30+
dest: /etc/systemd/system/fstrim.timer.d/override.conf
31+
content: |
32+
[Timer]
33+
OnCalendar=
34+
OnCalendar=daily
35+
mode: "0644"
36+
37+
- name: Enable fstrim.timer
38+
ansible.builtin.systemd:
39+
name: fstrim.timer
40+
daemon_reload: true
41+
enabled: true
42+
state: started

roles/libvirt_manager/tasks/deploy_layout.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,28 @@
266266
loop_control:
267267
loop_var: _vm
268268

269+
- name: Configure fstrim on VMs where enabled
270+
ansible.builtin.include_tasks: configure_fstrim_vm.yml
271+
vars:
272+
_fstrim_enabled_types: >-
273+
{{
274+
_cifmw_libvirt_manager_layout.vms | dict2items |
275+
selectattr('value.fstrim_enabled', 'defined') |
276+
selectattr('value.fstrim_enabled', 'equalto', true) |
277+
map(attribute='key') | list
278+
}}
279+
_fstrim_enabled_vms: >-
280+
{{
281+
cifmw_libvirt_manager_all_vms | dict2items |
282+
selectattr('value', 'in', _fstrim_enabled_types) | list
283+
}}
284+
_fstrim_target: "{{ (_vm.key | replace('ocp-', '')) }}.{{ inventory_hostname }}"
285+
_fstrim_user: "{{ 'core' if _vm.key is match('^(crc|ocp).*') else 'zuul' }}"
286+
loop: "{{ _fstrim_enabled_vms }}"
287+
loop_control:
288+
loop_var: _vm
289+
label: "{{ _vm.key }}"
290+
269291
- name: Create VBMC entity
270292
when:
271293
- _vbmc_available is defined

roles/libvirt_manager/templates/domain.xml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<devices>
4343
<emulator>/usr/libexec/qemu-kvm</emulator>
4444
<disk type='file' device='disk'>
45-
<driver name='qemu' type='qcow2'/>
45+
<driver name='qemu' type='qcow2'{% if vm_data.fstrim_enabled | default(false) | bool %} discard='unmap'{% endif %}/>
4646
<source file='{{ _chdir }}/{{ vm }}.qcow2'/>
4747
{% set disk_bus = vm_data.disk_bus | default('scsi') %}
4848
{% if disk_bus == 'scsi' %}

0 commit comments

Comments
 (0)