]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
fix(check_live_ram): increase /run tmpfs size, if needed
authorFrederick Grose <fgrose@sugarlabs.org>
Wed, 8 Nov 2023 18:08:19 +0000 (13:08 -0500)
committerNeal Gompa (ニール・ゴンパ) <ngompa13@gmail.com>
Sun, 31 Mar 2024 22:37:12 +0000 (18:37 -0400)
Check the size and available space in /run to enlarge it,
  if needed.
Introduce the check_meminfo() function to dracut-lib.sh
  to replace a less versatile sed call.
Also, use local variables,
  use parameter expansion to assign a default value,
  use test exit logic directly, where possible,
  tweak comments,
  and move the check_live_ram() function to img-lib.sh.

modules.d/99base/dracut-lib.sh
modules.d/99img-lib/img-lib.sh

index 39609d82abe5ae1355d37e92e3856dbe569a8923..33304dd7d60c27b26441caa6de62d44fd55dbbc5 100755 (executable)
@@ -1130,6 +1130,18 @@ show_memstats() {
     esac
 }
 
+# parameter: <memory_name:>  example: MemTotal:
+# Check /proc/meminfo
+# echo the field value, if present.
+check_meminfo() {
+    local - m sz
+    set +x
+    while read -r m sz _ || [ "$m" ]; do
+        [ "$m" = "$1" ] && echo "$sz" && return 0
+    done < /proc/meminfo
+    return 1
+}
+
 remove_hostonly_files() {
     rm -fr /etc/cmdline /etc/cmdline.d/*.conf "$hookdir/initqueue/finished"
     if [ -f /lib/dracut/hostonly-files ]; then
@@ -1151,28 +1163,3 @@ load_fstype() {
     done < /proc/filesystems
     modprobe "$1"
 }
-
-# parameter: size of live image
-# calls emergency shell if ram size is too small for the image
-check_live_ram() {
-    minmem=$(getarg rd.minmem)
-    minmem=${minmem:-1024}
-    imgsize=$1
-    memsize=$(($(sed -n 's/MemTotal: *\([[:digit:]]*\).*/\1/p' /proc/meminfo) / 1024))
-
-    if [ -z "$imgsize" ]; then
-        warn "Image size could not be determined"
-        return 0
-    fi
-
-    if [ $((memsize - imgsize)) -lt "$minmem" ]; then
-        sed -i "N;/and attach it to a bug report./s/echo$/echo\n\
-         echo \n\
-         echo 'Warning!!!'\n\
-         echo 'The memory size of your system is too small for this live image.'\n\
-         echo 'Expect killed processes due to out of memory conditions.'\n\
-         echo \n/" /usr/bin/dracut-emergency
-
-        emergency_shell
-    fi
-}
index 47008325274a9b62ba3b23d97d2f17cc39254437..efc647515bf55f11cdbc75c23e920323097605b0 100755 (executable)
@@ -106,3 +106,37 @@ unpack_img() {
         }
     fi
 }
+
+# parameter: <size of live image> in MiB
+# Call emergency shell if ram size is too small for the image.
+# Increase /run tmpfs size, if needed.
+check_live_ram() {
+    local minmem imgsize memsize runsize runavail
+    minmem=$(getarg rd.minmem)
+    imgsize=$1
+    memsize=$(($(check_meminfo MemTotal:) >> 10))
+    # shellcheck disable=SC2046
+    set -- $(findmnt -bnro SIZE,AVAIL /run)
+    # bytes to MiB
+    runsize=$(($1 >> 20))
+    runavail=$(($2 >> 20))
+
+    [ "$imgsize" ] || {
+        warn "Image size could not be determined"
+        return 0
+    }
+
+    if [ $((memsize - imgsize)) -lt "${minmem:=1024}" ]; then
+        sed -i "N;/and attach it to a bug report./s/echo$/echo\n\
+         echo \n\
+         echo 'Warning!!!'\n\
+         echo 'The memory size of your system is too small for this live image.'\n\
+         echo 'Expect killed processes due to out of memory conditions.'\n\
+         echo \n/" /usr/bin/dracut-emergency
+
+        emergency_shell
+    elif [ $((runavail - imgsize)) -lt "$minmem" ]; then
+        # Increase /run tmpfs size, if needed.
+        mount -o remount,size=$((runsize - runavail + imgsize + minmem))M /run
+    fi
+}