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 dracut-lib.sh
  to replace a less versatile sed call.
Also, use local variables,
  use parameter expansion to assign a default value,
  test exit logic directly, where possible,
  tweak comments,
  and move the check_live_ram() function to img-lib.sh.
  • Loading branch information
FGrose committed Dec 3, 2023
1 parent 4d59421 commit 4ebe262
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
37 changes: 12 additions & 25 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 @@ -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
}
34 changes: 34 additions & 0 deletions modules.d/99img-lib/img-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 4ebe262

Please sign in to comment.