Skip to content

Commit

Permalink
fix(check_live_ram): increase /run tmpfs size, if needed
Browse files Browse the repository at this point in the history
Check the size and available space in /run to enlarge it,
  if needed.
Introduce the check_meminfo() function to replace a less
  versatile sed call.
Also, use local variables.
  • Loading branch information
FGrose committed Dec 3, 2023
1 parent 4d59421 commit 0f0f9e7
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions modules.d/99base/dracut-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
}

0 comments on commit 0f0f9e7

Please sign in to comment.