Skip to content

Commit

Permalink
Add utils task files to get freebsd partition info
Browse files Browse the repository at this point in the history
Signed-off-by: Qi Zhang <[email protected]>
  • Loading branch information
keirazhang committed Aug 18, 2023
1 parent 5f36466 commit 3db3a3b
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 26 deletions.
41 changes: 41 additions & 0 deletions linux/utils/freebsd_get_geom_conf_xml.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2023 VMware, Inc.
# SPDX-License-Identifier: BSD-2-Clause
---
# Description:
# Get GEOM config info in XML on FreeBSD
#
- name: "Set fact of local file to save FreeBSD GEOM config info"
ansible.builtin.set_fact:
freebsd_geom_xml_file: "{{ current_test_log_folder }}/geom_conf_{{ lookup('pipe', 'date +%Y%m%d%H%M%S') }}.xml"

- name: "Check exitence of current test case log folder"
ansible.builtin.stat:
path: "{{ current_test_log_folder }}"
register: test_log_folder_stat

- name: "Create log folder for current test case"
include_tasks: ../../common/create_directory.yml
vars:
dir_path: "{{ current_test_log_folder }}"
dir_mode: "0777"
when: not (test_log_folder_stat.exists | default(False))

- name: "Create local file to save FreeBSD GEOM config info"
ansible.builtin.file:
path: "{{ freebsd_geom_xml_file }}"
state: touch
mode: "0666"

- name: "Get FreeBSD GEOM config info"
ansible.builtin.shell: "sysctl kern.geom.confxml"
delegate_to: "{{ vm_guest_ip }}"
register: geom_in_xml_result

- name: "Write FreeBSD GEOM info to XML file"
ansible.builtin.copy:
content: "{{ geom_in_xml_result.stdout | replace('kern.geom.confxml: ', '') }}"
dest: "{{ freebsd_geom_xml_file }}"

- name: "Print the file path of FreeBSD GEOM config info"
ansible.builtin.debug:
msg: "FreeBSD GEOM config in XML format are saved into file {{ freebsd_geom_xml_file }}"
89 changes: 89 additions & 0 deletions linux/utils/freebsd_get_partition_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright 2023 VMware, Inc.
# SPDX-License-Identifier: BSD-2-Clause
---
# Description:
# Get disk partition info by partition name or UUID on FreeBSD
# Paramters:
# disk_partition_name: The disk partition name on FreeBSD
# disk_partition_uuid: The disk partition UUID on FreeBSD
# Return:
# disk_partition_info: The disk partition info on FreeBSD
#
- name: "Check paramters to get FreeBSD disk partition info"
ansible.builtin.assert:
that:
- disk_partition_name | default('') or disk_partition_uuid | default('')
fail_msg: >-
Parameter 'disk_partition_name' or 'disk_partition_uuid' must be set
to get disk partition info.
- name: "Initialize facts of FreeBSD disk partition info"
ansible.builtin.set_fact:
disk_partition_info: {}
disk_partition_config_info: {}

- name: "Get FreeBSD GEOM config file in XML"
include_tasks: freebsd_get_geom_conf_xml.yml

- name: "Set facts of XPATHs for geting disk partition info"
ansible.builtin.set_fact:
xpath_of_disk_part: "/mesh/class[name='PART']/geom/provider[name='{{ disk_partition_name }}']/*"
xpath_of_disk_part_config: "/mesh/class[name='PART']/geom/provider[name='{{ disk_partition_name }}']/config/*"
when:
- disk_partition_name is defined
- disk_partition_name

- name: "Set facts of XPATHs for geting disk partition info"
ansible.builtin.set_fact:
xpath_of_disk_part: "/mesh/class[name='PART']/geom/provider/*[../config[rawuuid='{{ disk_partition_uuid }}']]"
xpath_of_disk_part_config: "/mesh/class[name='PART']/geom/provider/config[rawuuid='{{ disk_partition_uuid }}']/*"
when:
- disk_partition_uuid is defined
- disk_partition_uuid

- name: "Get disk partition"
community.general.xml:
path: "{{ freebsd_geom_xml_file }}"
xpath: "{{ xpath_of_disk_part }}"
content: text
register: geom_part_xml

- name: "Set fact of disk partition info"
ansible.builtin.set_fact:
disk_partition_info: >-
{{
disk_partition_info | combine({'config': ''}) if geom_item['config'] is defined
else disk_partition_info | combine(geom_item)
}}
with_items: "{{ geom_part_xml.matches }}"
loop_control:
loop_var: geom_item
when:
- geom_part_xml.matches is defined
- geom_part_xml.matches | length > 0

- debug: var=disk_partition_info
- name: "Get disk partition config"
community.general.xml:
path: "{{ freebsd_geom_xml_file }}"
xpath: "{{ xpath_of_disk_part_config }}"
content: text
register: geom_conf_xml

- name: "Set fact of disk partition info"
ansible.builtin.set_fact:
disk_partition_config_info: "{{ disk_partition_config_info | combine(geom_item) }}"
with_items: "{{ geom_conf_xml.matches }}"
loop_control:
loop_var: geom_item
when:
- geom_conf_xml.matches is defined
- geom_conf_xml.matches | length > 0

- debug: var=disk_partition_config_info
- name: "Update fact of disk partition with config info"
ansible.builtin.set_fact:
disk_partition_info: "{{ disk_partition_info | combine({'config': disk_partition_config_info}) }}"

- name: "Print disk partition info on FreeBSD"
ansible.builtin.debug: var=disk_partition_info
17 changes: 7 additions & 10 deletions linux/vhba_hot_add_remove/freebsd_create_disk_partition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ansible.builtin.set_fact:
partition_name: ""
partition_device_path: ""
partition_uuid: ""

- name: "Set default partition filesystem to ext4"
ansible.builtin.set_fact:
Expand Down Expand Up @@ -61,19 +62,15 @@
register: partition_result
when: partition_fstype == "ufs"

- name: "Get the UUID of partition {{ partition_device_path }}"
ansible.builtin.shell: "gpart list {{ disk_name }} | grep rawuuid"
changed_when: false
delegate_to: "{{ vm_guest_ip }}"
register: blkid_part_result
- name: "Get disk partition info"
include_tasks: ../utils/freebsd_get_partition_info.yml
vars:
disk_partition_name: "{{ partition_name }}"

- name: "Set the fact of partition {{ partition_device_path }} UUID"
ansible.builtin.set_fact:
partition_uuid: "{{ blkid_part_result.stdout.split(':')[-1] }}"
when:
- blkid_part_result is defined
- blkid_part_result.stdout is defined
- blkid_part_result.stdout
partition_uuid: "{{ disk_partition_info.config.rawuuid }}"
when: disk_partition_info.config.rawuuid | default('')

- name: "Check the UUID of partition {{ partition_device_path }}"
ansible.builtin.assert:
Expand Down
57 changes: 41 additions & 16 deletions linux/vhba_hot_add_remove/test_file_read_write.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,49 @@
- name: "Reboot guest OS to check file content again"
include_tasks: ../utils/reboot.yml

- name: "Get new partition path with UUID {{ new_partition_uuid }} after reboot"
ansible.builtin.shell: "blkid -U {{ new_partition_uuid }}"
changed_when: false
delegate_to: "{{ vm_guest_ip }}"
register: blkid_uuid_result
- name: "Get new partition device path and mount path after reboot on {{ guest_os_ansible_distribution }}"
when: guest_os_family != 'FreeBSD'
block:
- name: "Get new partition path with UUID {{ new_partition_uuid }} after reboot"
ansible.builtin.shell: "blkid -U {{ new_partition_uuid }}"
changed_when: false
delegate_to: "{{ vm_guest_ip }}"
register: blkid_uuid_result

- name: "Check guest device with UUID {{ new_partition_uuid }} exists"
ansible.builtin.assert:
that:
- blkid_uuid_result is defined
- blkid_uuid_result.stdout is defined
- blkid_uuid_result.stdout
fail_msg: "Failed to find guest device with UUID {{ new_partition_uuid }}"
- name: "Check partition with UUID {{ new_partition_uuid }} existing on {{ guest_os_ansible_distribution }}"
ansible.builtin.assert:
that:
- blkid_uuid_result is defined
- blkid_uuid_result.stdout is defined
- blkid_uuid_result.stdout
fail_msg: "Failed to find partition with UUID {{ new_partition_uuid }} on {{ guest_os_ansible_distribution }}"

- name: "Set facts of test partition path and mount path after reboot"
ansible.builtin.set_fact:
new_partition_device_path: "{{ blkid_uuid_result.stdout }}"
new_partition_mount_path: "/mnt/{{ blkid_uuid_result.stdout.split('/')[-1] }}"
- name: "Set facts of test partition path and mount path after reboot"
ansible.builtin.set_fact:
new_partition_device_path: "{{ blkid_uuid_result.stdout }}"
new_partition_mount_path: "/mnt/{{ blkid_uuid_result.stdout.split('/')[-1] }}"

- name: "Get new partition device path and mount path after reboot on FreeBSD"
when: guest_os_family == 'FreeBSD'
block:
- name: "Get disk partition info by UUID on FreeBSD"
include_tasks: ../utils/freebsd_get_partition_info.yml
vars:
disk_partition_uuid: "{{ new_partition_uuid }}"

- name: "Check partition with UUID {{ new_partition_uuid }} existing on FreeBSD"
ansible.builtin.assert:
that:
- disk_partition_info.name is defined
- disk_partition_info.name
- disk_partition_info.config.rawuuid is defined
- disk_partition_info.config.rawuuid == new_partition_uuid
fail_msg: "Failed to get partition by UUID {{ new_partition_uuid }} on FreeBSD"

- name: "Set facts of test partition path and mount path after reboot"
ansible.builtin.set_fact:
new_partition_device_path: "/dev/{{ disk_partition_info.name }}"
new_partition_mount_path: "/mnt/{{ disk_partition_info.name }}"

- name: "Set fact of test file path after reboot"
ansible.builtin.set_fact:
Expand Down

0 comments on commit 3db3a3b

Please sign in to comment.