Skip to content

Commit

Permalink
Create LVM on loop device to deploy Openshift Storage
Browse files Browse the repository at this point in the history
The Openshift Storage that uses topolvm was raising an error, because
the Microshift host does not have any LVM device. In previous role
stage, we just remove the openshift-storage namespace and storageclass,
because the CI tests mostly were using local storage for PV.
  • Loading branch information
danpawlik committed Mar 20, 2023
1 parent bc839a2 commit 05d56bc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
6 changes: 5 additions & 1 deletion defaults/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ standard_user:
namespace: dev-admin
role: admin

delete_openshift_storage: true
delete_openshift_storage: false
disk_file_path: /mnt/microshift-storage-file
disk_file_size: 20G
vg_name: rhel

overwrite_container_policy: false
3 changes: 1 addition & 2 deletions tasks/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@
ansible.builtin.include_tasks: standard-user.yaml
when: standard_user

- name: Delete openshift storage
- name: Create lvm or delete openshift storage
ansible.builtin.include_tasks: openshift-storage.yaml
when: delete_openshift_storage
82 changes: 68 additions & 14 deletions tasks/openshift-storage.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,69 @@
---
# NOTE: The Openshift storage uses topolvm, that requires additional volume,
# which is not necessary for us, due we mostly use that playbook for CI.
- name: Check if openshift-storage namespace exists
ansible.builtin.command: oc get namespace openshift-storage
register: _openshift_storage_ns
failed_when: _openshift_storage_ns.rc not in [0, 1]

- name: Delete openshift storage namespace
ansible.builtin.command: oc delete namespace openshift-storage
when: _openshift_storage_ns.rc == 0

- name: Delete topolvm-provisioner storageclass
ansible.builtin.command: oc delete storageclass topolvm-provisioner
when: _openshift_storage_ns.rc == 0
- name: Delete openshift-storage if needed
when: delete_openshift_storage
block:
# NOTE: The Openshift storage uses topolvm, that requires additional volume,
# which is not necessary for us, due we mostly use that playbook for CI.
- name: Check if openshift-storage namespace exists
ansible.builtin.command: oc get namespace openshift-storage
register: _openshift_storage_ns
failed_when: _openshift_storage_ns.rc not in [0, 1]

- name: Delete openshift storage namespace
ansible.builtin.command: oc delete namespace openshift-storage
when: _openshift_storage_ns.rc == 0

- name: Delete topolvm-provisioner storageclass
ansible.builtin.command: oc delete storageclass topolvm-provisioner
when: _openshift_storage_ns.rc == 0

- name: Create LVM on loop device to deploy openshift storage with topolvm
when: not delete_openshift_storage
block:
- name: Check if file already exists
become: true
stat:
path: "{{ disk_file_path }}"
register: _disk_file

- name: Create loop disk
when: not _disk_file.stat.exists
block:
- name: Create empty file
become: true
community.general.filesize:
path: "{{ disk_file_path }}"
size: "{{ disk_file_size }}"

- name: Make partition on empty file
become: true
community.general.parted:
device: "{{ disk_file_path }}"
number: 1
state: present
flags: ["lvm"]

- name: Get first available loop device
become: true
ansible.builtin.command: losetup -f
register: loop_device

- name: Create loop device service
become: true
ansible.builtin.template:
src: microshift-loop-device.service.j2
dest: /usr/lib/systemd/system/microshift-loop-device.service

- name: Enable loop device service
become: true
ansible.builtin.systemd:
name: microshift-loop-device
state: started
enabled: true
daemon_reload: true

- name: Create volume group
become: true
community.general.lvg:
vg: "{{ vg_name }}"
pvs: "{{ loop_device.stdout }}"
1 change: 1 addition & 0 deletions tasks/pre.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- git
- golang
- python3-cryptography
- lvm2
state: present

- name: Add host to the hosts file
Expand Down
16 changes: 16 additions & 0 deletions templates/microshift-loop-device.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=Setup loopback device
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target microshift.service

[Service]
ExecStart=/sbin/losetup {{ loop_device.stdout }} {{ disk_file_path }}
ExecStop=/sbin/losetup -d {{ loop_device.stdout }}
Type=oneshot
TimeoutSec=60
RemainAfterExit=yes

[Install]
WantedBy=local-fs-pre.target
Also=systemd-udevd.service

0 comments on commit 05d56bc

Please sign in to comment.