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