f709f2
From d5027d43ea3969426ba64423b3c0bb38491cc880 Mon Sep 17 00:00:00 2001
f709f2
From: Tao Liu <ltao@redhat.com>
f709f2
Date: Fri, 10 Jun 2022 16:39:31 +0800
f709f2
Subject: [PATCH] feat(lvm): add new module lvmthinpool-monitor
f709f2
f709f2
Previously dracut didn't support the feature of lvm thinpool autoextend.
f709f2
f709f2
The feature is useful to cases such as kdump, when vmcore to be saved to a
f709f2
lvm thin volume. The thinpool should be able to autoextend, otherwise an
f709f2
IO error will be caused and leaves an incomplete vmcore.
f709f2
f709f2
There is lvm2-monitor.service and dmeventd avaliable, however
f709f2
considering [1], it is not suitable for kdump and memory limited cases.
f709f2
f709f2
This patch achieves the same by parallel looping a shell function in the
f709f2
background, which calls lvextend periodically. If thredshold reaches,
f709f2
autoextend it, if not then nothing happens.
f709f2
f709f2
[1]: https://lists.fedoraproject.org/archives/list/kexec@lists.fedoraproject.org/message/YF254ZO3PJ3U56P4OKHV3JNYP2PJUMYX/
f709f2
f709f2
Signed-off-by: Tao Liu <ltao@redhat.com>
f709f2
f709f2
Resolves: #2098502
f709f2
---
f709f2
 dracut.spec                                        |  1 +
f709f2
 modules.d/80lvmthinpool-monitor/module-setup.sh    | 24 +++++++++++++
f709f2
 .../start-thinpool-monitor.service                 | 14 ++++++++
f709f2
 .../start-thinpool-monitor.sh                      | 41 ++++++++++++++++++++++
f709f2
 4 files changed, 80 insertions(+)
f709f2
f709f2
diff --git a/dracut.spec b/dracut.spec
f709f2
index c8783699..e1c22256 100644
f709f2
--- a/dracut.spec
f709f2
+++ b/dracut.spec
f709f2
@@ -350,6 +350,7 @@ echo 'dracut_rescue_image="yes"' > $RPM_BUILD_ROOT%{dracutlibdir}/dracut.conf.d/
f709f2
 %{dracutlibdir}/modules.d/50drm
f709f2
 %{dracutlibdir}/modules.d/50plymouth
f709f2
 %{dracutlibdir}/modules.d/80lvmmerge
f709f2
+%{dracutlibdir}/modules.d/80lvmthinpool-monitor
f709f2
 %{dracutlibdir}/modules.d/90btrfs
f709f2
 %{dracutlibdir}/modules.d/90crypt
f709f2
 %{dracutlibdir}/modules.d/90dm
f709f2
diff --git a/modules.d/80lvmthinpool-monitor/module-setup.sh b/modules.d/80lvmthinpool-monitor/module-setup.sh
f709f2
new file mode 100755
f709f2
index 00000000..ca015bdc
f709f2
--- /dev/null
f709f2
+++ b/modules.d/80lvmthinpool-monitor/module-setup.sh
f709f2
@@ -0,0 +1,24 @@
f709f2
+#!/bin/bash
f709f2
+
f709f2
+# called by dracut
f709f2
+check() {
f709f2
+    # No point trying to support lvm if the binaries are missing
f709f2
+    require_binaries lvm sort tr awk || return 1
f709f2
+
f709f2
+    return 255
f709f2
+}
f709f2
+
f709f2
+# called by dracut
f709f2
+depends() {
f709f2
+    echo lvm
f709f2
+    return 0
f709f2
+}
f709f2
+
f709f2
+# called by dracut
f709f2
+install() {
f709f2
+    inst_multiple sort tr awk
f709f2
+    inst_script "$moddir/start-thinpool-monitor.sh" "/bin/start-thinpool-monitor"
f709f2
+
f709f2
+    inst "$moddir/start-thinpool-monitor.service" "$systemdsystemunitdir/start-thinpool-monitor.service"
f709f2
+    $SYSTEMCTL -q --root "$initdir" add-wants initrd.target start-thinpool-monitor.service
f709f2
+}
f709f2
diff --git a/modules.d/80lvmthinpool-monitor/start-thinpool-monitor.service b/modules.d/80lvmthinpool-monitor/start-thinpool-monitor.service
f709f2
new file mode 100644
f709f2
index 00000000..97f5f1f4
f709f2
--- /dev/null
f709f2
+++ b/modules.d/80lvmthinpool-monitor/start-thinpool-monitor.service
f709f2
@@ -0,0 +1,14 @@
f709f2
+[Unit]
f709f2
+Description=Lvm thinpool monitor service
f709f2
+Before=initrd.target
f709f2
+After=initrd-fs.target
f709f2
+Conflicts=shutdown.target emergency.target
f709f2
+
f709f2
+[Service]
f709f2
+Type=forking
f709f2
+ExecStart=/bin/start-thinpool-monitor
f709f2
+PIDFile=/run/thinpool-moni.pid
f709f2
+StandardInput=null
f709f2
+StandardOutput=journal+console
f709f2
+StandardError=journal+console
f709f2
+KillSignal=SIGHUP
f709f2
diff --git a/modules.d/80lvmthinpool-monitor/start-thinpool-monitor.sh b/modules.d/80lvmthinpool-monitor/start-thinpool-monitor.sh
f709f2
new file mode 100755
f709f2
index 00000000..75d8eada
f709f2
--- /dev/null
f709f2
+++ b/modules.d/80lvmthinpool-monitor/start-thinpool-monitor.sh
f709f2
@@ -0,0 +1,41 @@
f709f2
+#!/bin/sh
f709f2
+
f709f2
+type getarg > /dev/null 2>&1 || . /lib/dracut-lib.sh
f709f2
+
f709f2
+LVS=$(getargs rd.lvm.lv -d rd_LVM_LV=)
f709f2
+
f709f2
+is_lvm2_thinp_device() {
f709f2
+    _device_path=$1
f709f2
+    _lvm2_thin_device=$(lvm lvs -S 'lv_layout=sparse && lv_layout=thin' \
f709f2
+        --nosuffix --noheadings -o vg_name,lv_name "$_device_path" 2> /dev/null)
f709f2
+
f709f2
+    [ -n "$_lvm2_thin_device" ] && return $?
f709f2
+}
f709f2
+
f709f2
+for LV in $LVS; do
f709f2
+    if is_lvm2_thinp_device "/dev/$LV"; then
f709f2
+        THIN_POOLS="$(lvm lvs -S 'lv_layout=sparse && lv_layout=thin' \
f709f2
+            --nosuffix --noheadings -o vg_name,pool_lv "$LV" \
f709f2
+            | awk '{printf("%s/%s",$1,$2);}') $THIN_POOLS"
f709f2
+    fi
f709f2
+done
f709f2
+
f709f2
+THIN_POOLS=$(echo "$THIN_POOLS" | tr ' ' '\n' | sort -u | tr '\n' ' ')
f709f2
+
f709f2
+if [ -n "$THIN_POOLS" ]; then
f709f2
+    if [ -e "/etc/lvm/lvm.conf" ]; then
f709f2
+        # Use 'monitoring=0' to override the value in lvm.conf, in case
f709f2
+        # dmeventd monitoring been started after the calling.
f709f2
+        CONFIG="activation {monitoring=0}"
f709f2
+    else
f709f2
+        CONFIG="activation {monitoring=0 thin_pool_autoextend_threshold=70 thin_pool_autoextend_percent=20}"
f709f2
+    fi
f709f2
+
f709f2
+    while true; do
f709f2
+        for THIN_POOL in $THIN_POOLS; do
f709f2
+            lvm lvextend --use-policies --config "$CONFIG" "$THIN_POOL"
f709f2
+        done
f709f2
+        sleep 5
f709f2
+    done &
f709f2
+    echo $! > /run/thinpool-moni.pid
f709f2
+fi
f709f2