diff --git a/modules.d/99base/dracut-lib.sh b/modules.d/99base/dracut-lib.sh index 39609d82ab..655469af30 100755 --- a/modules.d/99base/dracut-lib.sh +++ b/modules.d/99base/dracut-lib.sh @@ -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 u + set +x + while read -r m sz u || [ "$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 @@ -1152,13 +1164,20 @@ load_fstype() { modprobe "$1" } -# parameter: size of live image +# parameter: size of live image in MiB # calls 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) minmem=${minmem:-1024} imgsize=$1 - memsize=$(($(sed -n 's/MemTotal: *\([[:digit:]]*\).*/\1/p' /proc/meminfo) / 1024)) + memsize=$(($(check_meminfo MemTotal:) >> 10)) + # shellcheck disable=SC2046 + set -- $(findmnt -bnro SIZE,AVAIL /run) + # bytes to MiB + runsize=$(($1 >> 20)) + runavail=$(($2 >> 20)) if [ -z "$imgsize" ]; then warn "Image size could not be determined" @@ -1174,5 +1193,8 @@ check_live_ram() { 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 }