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