Skip to contentSkip to conversation

commands: add new command memsize

PATCH New Xinhui Yang via Grub-devel 1 participant 1 message Nov 10, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 commands: add new command memsize

Greetings all,

I rewrote the program mentioned previously in an email[1], and it is mostly complete, and it is useful for a number of scenarios, so I am sending it here for upstreaming.

- This command traverses the entire GRUB memory map, adds the size of each region together. The result is the total amount of system memory available to GRUB in bytes.

- It stores the vaule to the environment as the name `memsize' by default, but it is customizable. The unit of the value is also customizable too.

- It checks if the vaule is too large for the `test' command to process, whose limit is INT_MAX.

- This command is useful with `test' command to implement mechanisms such as booting the live media with live filesystem loaded into RAN or not, instead of waiting initrd to fail:

insmod memsize
memsize -M --set=mem_avail
if [ $mem_avail -gt 4000 ] ; then

    menuentry 'Live OS (Load into RAM)' {
      linux /boot/vmlinuz rd.live.ram=1 ...
      initrd /boot/initrd.img
    }

fi

It is also possible to implement mechanisms to boot into different mode (e.g. graphical or multi-user), depending on RAM available.

[1]: https://lists.gnu.org/archive/html/grub-devel/2023-07/msg00033.html

Regards,
Xinhui

* docs/grub.texi: add documentation for memsize

---
 docs/grub.texi               |  63 ++++++++++
 grub-core/Makefile.core.def  |   5 +
 grub-core/commands/memsize.c | 227 +++++++++++++++++++++++++++++++++++
 3 files changed, 295 insertions(+)
 create mode 100644 grub-core/commands/memsize.c
docs/grub.texi +63 -0
index 975e521d1..1dcd11b65 100644
@@ -4372,6 +4372,7 @@ you forget a command, you can run the command @command{help}
 * ls::                          List devices or files
 * lsfonts::                     List loaded fonts
 * lsmod::                       Show loaded modules
+* memsize::                     Get the amount of available RAM
 * md5sum::                      Compute or check MD5 hash
 * module::                      Load module for multiboot kernel
 * multiboot::                   Load multiboot compliant kernel
@@ -5142,6 +5143,68 @@ List loaded fonts.
 Show list of loaded modules.
 @end deffn
 
+
+@node memsize
+@subsection memsize
+
+@deffn Command memsize [@option{-b}|@option{-k}|@option{-m}|@option{-g}|@option{-K}|@option{-M}|@option{-G}] [@option{-q}] [@option{--set} VARNAME]
+Iterate through GRUB memory map to get the amount of system RAM available to
+GRUB. The final integer value of the unit specified will be exported to the
+environment as @code{VARNAME} to make good use of the @pxref{test} command,
+and optionally be printed out to the console.
+
+The final value is checked to make sure it does not exceed the limit of the
+@pxref{test} command, which is INT_MAX. Otherwise, it raises an error.
+
+The default unit used for the value is MiB (@option{-M}).
+
+The default variable name is @code{memsize}.
+
+@option{-q} disables the output.
+
+@code{VARNAME} should be a valid identifier and should not exceed 63
+characters.
+
+The units can be:
+
+@table @code
+@item @option{-b}
+Display and export the value in number of bytes.
+
+@item @option{-k}
+Display and export the vaule in kilobytes (@code{kB}).
+
+@item @option{-m}
+Display and export the vaule in megabytes (@code{MB}).
+
+@item @option{-g}
+Display and export the vaule in gigabytes (@code{GB}).
+
+@item @option{-K}
+Display and export the vaule in kibibytes (@code{KiB}).
+
+@item @option{-M}
+Display and export the vaule in mebibytes (@code{MiB}).
+
+@item @option{-G}
+Display and export the vaule in gibibytes (@code{GiB}).
+@end table
+
+One can make use of this command to implement mechanisms like e.g. booting
+into different modes depending on available RAM:
+
+@example
+memsize -M --set=mem_avail
+if [ "$mem_avail" -lt 512 ] ; then
+  set boot_target="multi-user.target"
+else
+  set boot_target="graphical.target"
+fi
+@end example
+
+@end deffn
+
+
 @node md5sum
 @subsection md5sum
 
grub-core/Makefile.core.def +5 -0
index d2cf29584..3a938ad0e 100644
@@ -1033,6 +1033,11 @@ module = {
   common = commands/memrw.c;
 };
 
+module = {
+  name = memsize;
+  common = commands/memsize.c;
+};
+
 module = {
   name = minicmd;
   common = commands/minicmd.c;
grub-core/commands/memsize.c new file +227 -0

227 lines not on this page · raw message

Signature · 7 lines
2.39.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel