filesystem: add UUID change feature (#6680)

* filesystem: add UUID change feature

* Add changelog fragment for 6680

* Do not test XFS filesystem UUID reset on FreeBSD

FreeBSD error: xfs_admin: only 'rewrite' supported on V5 fs

* Apply suggestions from code review #1

Co-authored-by: Felix Fontein <felix@fontein.de>

* Set filesystem UUID on FS creation

* Fix tests - switch to ansible.builtion.to_uuid

* Fix tests - Refactor to avoid FS remove tasks

* Fail if uuid option not yet supported for fstype

* Set resizefs and uuid options mutually exclusive

* Apply suggestions from code review no 2.

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Laszlo Szomor 2023-06-19 08:59:34 +02:00 committed by GitHub
parent 24aeedbc15
commit 5d9eb8be95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 249 additions and 22 deletions

View file

@ -15,19 +15,19 @@ tested_filesystems:
# - 1.7.0 requires at least 30Mo
# - 1.10.0 requires at least 38Mo
# - resizefs asserts when initial fs is smaller than 60Mo and seems to require 1.10.0
ext4: {fssize: 10, grow: true}
ext4dev: {fssize: 10, grow: true}
ext3: {fssize: 10, grow: true}
ext2: {fssize: 10, grow: true}
xfs: {fssize: 300, grow: false} # grow requires a mounted filesystem
btrfs: {fssize: 150, grow: false} # grow requires a mounted filesystem
reiserfs: {fssize: 33, grow: false} # grow not implemented
vfat: {fssize: 20, grow: true}
ocfs2: {fssize: '{{ ocfs2_fssize }}', grow: false} # grow not implemented
f2fs: {fssize: '{{ f2fs_fssize|default(60) }}', grow: 'f2fs_version is version("1.10.0", ">=")'}
lvm: {fssize: 20, grow: true}
swap: {fssize: 10, grow: false} # grow not implemented
ufs: {fssize: 10, grow: true}
ext4: {fssize: 10, grow: true, new_uuid: 'random'}
ext4dev: {fssize: 10, grow: true, new_uuid: 'random'}
ext3: {fssize: 10, grow: true, new_uuid: 'random'}
ext2: {fssize: 10, grow: true, new_uuid: 'random'}
xfs: {fssize: 300, grow: false, new_uuid: 'generate'} # grow requires a mounted filesystem
btrfs: {fssize: 150, grow: false, new_uuid: null} # grow requires a mounted filesystem
reiserfs: {fssize: 33, grow: false, new_uuid: null} # grow not implemented
vfat: {fssize: 20, grow: true, new_uuid: null}
ocfs2: {fssize: '{{ ocfs2_fssize }}', grow: false, new_uuid: null} # grow not implemented
f2fs: {fssize: '{{ f2fs_fssize|default(60) }}', grow: 'f2fs_version is version("1.10.0", ">=")', new_uuid: null}
lvm: {fssize: 20, grow: true, new_uuid: 'something'}
swap: {fssize: 10, grow: false, new_uuid: null} # grow not implemented
ufs: {fssize: 10, grow: true, new_uuid: null}
get_uuid_any: "blkid -c /dev/null -o value -s UUID {{ dev }}"

View file

@ -29,6 +29,7 @@
fstype: '{{ item.0.key }}'
fssize: '{{ item.0.value.fssize }}'
grow: '{{ item.0.value.grow }}'
new_uuid: '{{ item.0.value.new_uuid }}'
action: '{{ item.1 }}'
when:
# FreeBSD limited support
@ -83,7 +84,7 @@
# TODO: something seems to be broken on Alpine
- 'not (ansible_distribution == "Alpine")'
loop: "{{ query('dict', tested_filesystems)|product(['create_fs', 'overwrite_another_fs', 'remove_fs'])|list }}"
loop: "{{ query('dict', tested_filesystems)|product(['create_fs', 'reset_fs_uuid', 'overwrite_another_fs', 'remove_fs', 'set_fs_uuid_on_creation', 'set_fs_uuid_on_creation_with_opts'])|list }}"
# With FreeBSD extended support (util-linux is not available before 12.2)

View file

@ -0,0 +1,59 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# Skip UUID reset tests for FreeBSD due to "xfs_admin: only 'rewrite' supported on V5 fs"
- when:
- new_uuid | default(False)
- not (ansible_system == "FreeBSD" and fstype == "xfs")
block:
- name: "Create filesystem ({{ fstype }})"
community.general.filesystem:
dev: '{{ dev }}'
fstype: '{{ fstype }}'
register: fs_result
- name: "Get UUID of created filesystem"
ansible.builtin.shell:
cmd: "{{ get_uuid_cmd }}"
changed_when: false
register: uuid
- name: "Reset filesystem ({{ fstype }}) UUID"
community.general.filesystem:
dev: '{{ dev }}'
fstype: '{{ fstype }}'
uuid: "{{ new_uuid }}"
register: fs_resetuuid_result
- name: "Get UUID of the filesystem"
ansible.builtin.shell:
cmd: "{{ get_uuid_cmd }}"
changed_when: false
register: uuid2
- name: "Assert that filesystem UUID is changed"
ansible.builtin.assert:
that:
- 'fs_resetuuid_result is changed'
- 'fs_resetuuid_result is success'
- 'uuid.stdout != uuid2.stdout'
- when:
- (grow | bool and (fstype != "vfat" or resize_vfat)) or
(fstype == "xfs" and ansible_system == "Linux" and
ansible_distribution not in ["CentOS", "Ubuntu"])
block:
- name: "Reset filesystem ({{ fstype }}) UUID and resizefs"
ignore_errors: true
community.general.filesystem:
dev: '{{ dev }}'
fstype: '{{ fstype }}'
uuid: "{{ new_uuid }}"
resizefs: true
register: fs_resetuuid_and_resizefs_result
- name: "Assert that filesystem UUID reset and resizefs failed"
ansible.builtin.assert:
that: fs_resetuuid_and_resizefs_result is failed

View file

@ -0,0 +1,44 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
- name: "Generate a random UUID"
ansible.builtin.set_fact:
random_uuid: '{{ "first_random_uuid" | ansible.builtin.to_uuid }}'
# Skip UUID set at creation tests for FreeBSD due to "xfs_admin: only 'rewrite' supported on V5 fs"
- when:
- new_uuid | default(False)
- not (ansible_system == "FreeBSD" and fstype == "xfs")
block:
- name: "Create filesystem ({{ fstype }}) with UUID"
community.general.filesystem:
dev: '{{ dev }}'
fstype: '{{ fstype }}'
uuid: '{{ random_uuid }}'
register: fs_result
- name: "Get UUID of the created filesystem"
ansible.builtin.shell:
cmd: "{{ get_uuid_cmd }}"
changed_when: false
register: uuid
- name: "Assert that filesystem UUID is the random UUID set on creation"
ansible.builtin.assert:
that: (random_uuid | replace('-','')) == ( uuid.stdout | replace('-',''))
- when: not (new_uuid | default(False))
block:
- name: "Create filesystem ({{ fstype }}) without UUID support"
ignore_errors: true
community.general.filesystem:
dev: '{{ dev }}'
fstype: '{{ fstype }}'
uuid: '{{ random_uuid }}'
register: fs_result
- name: "Assert that filesystem creation failed"
ansible.builtin.assert:
that: fs_result is failed

View file

@ -0,0 +1,33 @@
---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# UUID set at creation with opts for XFS is not supported
- when:
- new_uuid | default(False)
- fstype != "xfs"
block:
- name: "Generate random UUIDs"
ansible.builtin.set_fact:
random_uuid: '{{ "first_random_uuid" | ansible.builtin.to_uuid }}'
random_uuid2: '{{ "second_random_uuid" | ansible.builtin.to_uuid }}'
- name: "Create filesystem ({{ fstype }}) with fix UUID as opt"
community.general.filesystem:
dev: '{{ dev }}'
fstype: '{{ fstype }}'
opts: "{{ ((fstype == 'lvm') | ansible.builtin.ternary('--norestorefile --uuid ', '-U ')) + random_uuid2 }}"
uuid: '{{ random_uuid }}'
register: fs_result2
- name: "Get UUID of the created filesystem"
ansible.builtin.shell:
cmd: "{{ get_uuid_cmd }}"
changed_when: false
register: uuid2
- name: "Assert that filesystem UUID is the one set on creation with opt"
ansible.builtin.assert:
that: (random_uuid2 | replace('-','')) == ( uuid2.stdout | replace('-',''))