]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
feat(livenet): add memory size check depending on live image size
authorThomas Blume <Thomas.Blume@suse.com>
Mon, 17 Apr 2023 13:41:07 +0000 (15:41 +0200)
committerAntonio Álvarez Feijoo <antonio.feijoo@suse.com>
Fri, 7 Jul 2023 12:24:10 +0000 (14:24 +0200)
For writeable live images, the memory must hold the complete image.
That means it must be at least the size of the image plus some RAM
necessary to run processes.
With too small RAM, the OOM Killer steps in and makes the boot hang.
This patch lets the system go into the emergency shell instead.

The default minimum RAM after subtracting the live image size is set to 1G.
The parameter rd.minmem is added to modify this.

man/dracut.cmdline.7.asc
modules.d/90dmsquash-live/dmsquash-live-root.sh
modules.d/90livenet/livenetroot.sh
modules.d/99base/dracut-lib.sh

index 5703747bcc2bdb0695f9636758e6b156e64b8f24..2e99b7370a927bc9e6bcd1b203a099085d9222f4 100644 (file)
@@ -1126,6 +1126,13 @@ NOTE: There must be enough free RAM available to hold the complete image.
 +
 This method is very suitable for diskless boots.
 
+**rd.minmem=**__<megabyte>__::
+Specify minimum free RAM in MB after copying a live disk image into memory.
+The default is 1024.
++
+This parameter only applies together with the parameters rd.writable.fsimg
+or rd.live.ram.
+
 **root=**live:__<url>__::
 Boots a live image retrieved from __<url>__.  Requires the dracut 'livenet'
 module.  Valid handlers: __http, https, ftp, torrent, tftp__.
index e9434c9343791b944b4ca92ed2b402e69d4b72ad..81c6ed8e73170148741a2d66496cf738bebfb784 100755 (executable)
@@ -315,6 +315,8 @@ if [ -e /run/initramfs/live/${live_dir}/${squash_image} ]; then
 fi
 if [ -e "$SQUASHED" ]; then
     if [ -n "$live_ram" ]; then
+        imgsize=$(($(stat -c %s -- $SQUASHED) / (1024 * 1024)))
+        check_live_ram $imgsize
         echo 'Copying live image to RAM...' > /dev/kmsg
         echo ' (this may take a minute)' > /dev/kmsg
         dd if=$SQUASHED of=/run/initramfs/squashed.img bs=512 2> /dev/null
index 92ad805bed28297a10efba5c4ca1393acec97ecc..66dd41b39accded7ce699caea34822dea951232d 100755 (executable)
@@ -16,6 +16,13 @@ netroot="$2"
 liveurl="${netroot#livenet:}"
 info "fetching $liveurl"
 
+if getargbool 0 'rd.writable.fsimg'; then
+
+    imgsize=$(($(curl -sIL "$liveurl" | sed -n 's/Content-Length: *\([[:digit:]]*\).*/\1/p') / (1024 * 1024)))
+
+    check_live_ram $imgsize
+fi
+
 imgfile=
 #retry until the imgfile is populated with data or the max retries
 i=1
index 6eaa0fe1497d5e73cf276013737b1397dcfb2568..a8ca2c96eb747d3574dc157f7a68d7011be69ebf 100755 (executable)
@@ -1151,3 +1151,28 @@ 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
+}