]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
fix(dracut-functions): implement a cache for get_maj_min
authorRenaud Métrich <rmetrich@redhat.com>
Wed, 5 May 2021 13:46:08 +0000 (15:46 +0200)
committerHarald Hoyer <harald@hoyer.xyz>
Thu, 6 May 2021 07:14:10 +0000 (09:14 +0200)
On systems with a large number of devices, usually multipath devices,
dracut can spend a lot of time stat'ing the devices to collect the
major/minor numbers, leading to huge slowness rebuilding the initramfs
when stat'ing devices is slow (seen with oracleasm file systems in
particular).
This commit implements a basic cache stored in a file under
DRACUT_TMPDIR storing the major:minor corresponding to the specified
device.

Reproducer: create N loopback devices used as a LVM extension to volume
group hosting the root file system

  # LVMVG="rhel"
  # NDEVICES=200
  # mkdir devices; for i in $(seq 1 $NDEVICES); do
    truncate -s 10m devices/$i; losetup loop$i devices/$i
  done
  # vgextend $LVMVG $(/bin/ls -1 /dev/loop[0-9]*)

With standard code (tested with RHEL8.3 dracut):

  # dracut -f --debug /tmp/initramfs.img $(uname -r) >/tmp/debug 2>&1
  # grep -c "stat -L -c" /tmp/debug
  2440

With this code:

  # dracut -f --debug /tmp/initramfs.img $(uname -r) >/tmp/debug_optim 2>&1
  # grep -c "stat -L -c" /tmp/debug_optim
  205

Signed-off-by: Renaud Métrich <rmetrich@redhat.com>
dracut-functions.sh
dracut.sh

index 5af4e458622a44a91dc3e272ca8b3789d465e1b1..1991c62e0ec4a9da5d48d5789dd6201806ce4bd9 100755 (executable)
@@ -234,8 +234,13 @@ get_fs_env() {
 # 8:2
 get_maj_min() {
     local _majmin
-    _majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
-    printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))"
+    out="$(grep -m1 -oP "^$1 \K\S+$" "${get_maj_min_cache_file:?}")"
+    if [ -z "$out" ]; then
+        _majmin="$(stat -L -c '%t:%T' "$1" 2> /dev/null)"
+        out="$(printf "%s" "$((0x${_majmin%:*})):$((0x${_majmin#*:}))")"
+        echo "$1 $out" >> "${get_maj_min_cache_file:?}"
+    fi
+    echo -n "$out"
 }
 
 # get_devpath_block <device>
index ec00097e2deb29603cbb887d6ada25db3bc52f0f..c19606ae5ec1d7a567a0424be76eec39d1b6c1fe 100755 (executable)
--- a/dracut.sh
+++ b/dracut.sh
@@ -1131,6 +1131,10 @@ readonly DRACUT_TMPDIR="$(mktemp -p "$TMPDIR/" -d -t dracut.XXXXXX)"
     exit 1
 }
 
+# Cache file used to optimize get_maj_min()
+declare -x -r get_maj_min_cache_file="${DRACUT_TMPDIR}/majmin_cache"
+: > "$get_maj_min_cache_file"
+
 # clean up after ourselves no matter how we die.
 trap '
     ret=$?;