Last active
April 20, 2020 16:56
-
-
Save odyssey4me/e4e83b85a97dc47f5c7bb11c5a37928c to your computer and use it in GitHub Desktop.
LVM local loopback device setup (RedHat)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- hosts: localhost | |
connection: local | |
gather_facts: yes | |
vars: | |
volume_path: "/var/lib/lvm-volume1" | |
volume_size: "100M" | |
volume_name: "loopback-volumes" | |
tasks: | |
- name: Ensure LVM dependencies are installed | |
package: | |
name: lvm2 | |
state: present | |
- name: Create LVM volume file | |
command: >- | |
dd if=/dev/zero of={{ volume_path }} bs=1 count=0 seek={{ volume_size }} | |
args: | |
creates: "{{ volume_path }}" | |
- name: Check for existing LVM loopback device | |
shell: >- | |
losetup -j {{ volume_path }} -l -O NAME | tail -n-1 | |
changed_when: false | |
register: _existing_loopback_device | |
- name: Create LVM loopback device | |
command: >- | |
losetup -f {{ volume_path }} --show | |
register: _new_loopback_device | |
when: | |
- _existing_loopback_device.stdout == "" | |
- name: Set loopback device path | |
set_fact: | |
_lvm_dev_path: "{{ _new_loopback_device.stdout | default(_existing_loopback_device.stdout, True) }}" | |
- name: Create LVM volume group | |
lvg: | |
vg: "{{ volume_name }}" | |
pvs: "{{ _lvm_dev_path }}" | |
state: present | |
- name: Create service to run losetup for LVM on startup | |
copy: | |
dest: "/etc/systemd/system/lvm-{{ volume_name }}-losetup.service" | |
content: | | |
[Unit] | |
Description=LVM {{ volume_name }} loopback device setup | |
DefaultDependencies=no | |
Conflicts=umount.target | |
Requires=lvm2-lvmetad.service systemd-udev-settle.service | |
Before=local-fs.target umount.target | |
After=lvm2-lvmetad.service systemd-udev-settle.service | |
[Service] | |
Type=oneshot | |
ExecStart=/sbin/losetup {{ _lvm_dev_path }} {{ volume_path }} | |
ExecStop=/sbin/losetup -d {{ _lvm_dev_path }} | |
RemainAfterExit=yes | |
[Install] | |
WantedBy=local-fs-pre.target | |
- name: Create service to run loopback device filesystem checks | |
copy: | |
dest: "/etc/systemd/system/lvm-{{ volume_name }}-fsck.service" | |
content: | | |
[Unit] | |
Description=LVM {{ volume_name }} filesystem check | |
DefaultDependencies=no | |
Conflicts=umount.target | |
Requires=lvm-{{ volume_name }}-losetup.service | |
Before=local-fs.target umount.target | |
After=lvm-{{ volume_name }}-losetup.service | |
[Service] | |
Type=oneshot | |
ExecStart=/sbin/fsck -pfv {{ _lvm_dev_path }} | |
[Install] | |
WantedBy=local-fs-pre.target | |
- name: Enable the LVM losetup service | |
systemd: | |
name: "{{ item }}" | |
enabled: yes | |
daemon_reload: yes | |
loop: | |
- "lvm-{{ volume_name }}-losetup" | |
- "lvm-{{ volume_name }}-fsck" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- hosts: localhost | |
connection: local | |
gather_facts: yes | |
vars: | |
volume_path: "/var/lib/lvm-volume1" | |
volume_size: "100M" | |
volume_name: "loopback-volumes" | |
tasks: | |
- name: Ensure LVM dependencies are installed | |
package: | |
name: lvm2 | |
state: present | |
- name: Create LVM volume file | |
command: >- | |
dd if=/dev/zero of={{ volume_path }} bs=1 count=0 seek={{ volume_size }} | |
args: | |
creates: "{{ volume_path }}" | |
- name: Create or get LVM loopback device | |
shell: |- | |
exit_code=0 | |
existing_device=$(losetup -j {{ volume_path }} -l -O NAME | tail -n-1) | |
if [[ -z "${existing_device}" ]]; then | |
losetup -f {{ volume_path }} --show | |
exit_code=2 | |
else | |
echo ${existing_device} | |
fi | |
exit ${exit_code} | |
args: | |
executable: /bin/bash | |
register: _loopback_device | |
changed_when: _loopback_device.rc == 2 | |
failed_when: _loopback_device.rc not in [0,2] | |
- name: Create LVM volume group | |
lvg: | |
vg: "{{ volume_name }}" | |
pvs: "{{ _loopback_device.stdout }}" | |
state: present | |
- name: Create service to run losetup for LVM on startup | |
copy: | |
dest: "/etc/systemd/system/lvm-{{ volume_name }}-losetup.service" | |
content: | | |
[Unit] | |
Description=LVM {{ volume_name }} loopback device setup | |
DefaultDependencies=no | |
Conflicts=umount.target | |
Requires=lvm2-lvmetad.service systemd-udev-settle.service | |
Before=local-fs.target umount.target | |
After=lvm2-lvmetad.service systemd-udev-settle.service | |
[Service] | |
Type=oneshot | |
ExecStart=/sbin/losetup {{ _loopback_device.stdout }} {{ volume_path }} | |
ExecStop=/sbin/losetup -d {{ _loopback_device.stdout }} | |
RemainAfterExit=yes | |
[Install] | |
WantedBy=local-fs-pre.target | |
- name: Create service to run loopback device filesystem checks | |
copy: | |
dest: "/etc/systemd/system/lvm-{{ volume_name }}-fsck.service" | |
content: | | |
[Unit] | |
Description=LVM {{ volume_name }} filesystem check | |
DefaultDependencies=no | |
Conflicts=umount.target | |
Requires=lvm-{{ volume_name }}-losetup.service | |
Before=local-fs.target umount.target | |
After=lvm-{{ volume_name }}-losetup.service | |
[Service] | |
Type=oneshot | |
ExecStart=/sbin/fsck -pfv {{ _loopback_device.stdout }} | |
[Install] | |
WantedBy=local-fs-pre.target | |
- name: Enable the LVM losetup service | |
systemd: | |
name: "{{ item }}" | |
enabled: yes | |
daemon_reload: yes | |
loop: | |
- "lvm-{{ volume_name }}-losetup" | |
- "lvm-{{ volume_name }}-fsck" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment