How to change your kernel space memory in Ubuntu and embedded

While writing a driver for Ubuntu or porting linux to a new embedded board one may need to either limit or increase the RAM (Random Access Memory) used by kernel. This can be achieved by tweaking the kernel command line arguments sent from the second stage bootloader to the kernel.

This magical argument is the “mem=” argument. One needs to write that amount of memory after the “=” which one wants the kernel to use. The rest will be used by the user space and for the other OS purposes. For example, if you have 4GB RAM and currently your kernel space/user space split is 1 G/3 G (this you can know by the command “free –m”) and you need to change this to 2 G/2 G then you specify “mem=2048m” in your command line.
Although this argument may not be present in your command line already that doesn’t necessarily mean your kernel is free to use how much ever memory it likes.

Ubuntu Desktop

An Ubuntu desktop system may use LILO or GRUB (these are the ones I am aware of) to load the kernel. From these I know how to change the command line arguments in GRUB, so I will show those, but I am sure this may be achieved in LILO in a similar fashion.

To do this:
1.    Open the default grub file. This will need root permissions. If you don’t have these use sudo and then type the command. On being asked for passwoprd then write your current user password.
root@ubuntu:~# vi /etc/default/grub
2.    Save this file and exit. To do that first press “Esc” then type “:wq” and press “Enter”.
3.    Now to update GRUB with this latest configuration (this will change the /boot/grub/grub.cfg file automatically. Editing this file directly may be dangerous. You can diff to find the change.) use the following command
root@ubuntu:~# update-grub
4.    Reboot the machine. To do this from command line type
root@ubuntu:~# reboot -f
5.    This will make the kernel boot with new configuration. To verify this issue:
root@ubuntu:~# cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-3.2.59-custom root=UUID=e0541e6d-949a-493a-ac72-11440265d7e0 ro mem=2048m quiet splash vt.handoff=7

6.    You can also check free pages available to kernel using:
root@ubuntu:~# free -m
             total       used       free     shared    buffers     cached
Mem:          1998        448       1550          0         41        264
-/+ buffers/cache:        142       1856
Swap:         3994          0       3994

Embedded Linux (UBOOT)

When you have uboot as the second stage bootloader then you can append the mem=<memory> into the “bootargs” environment variable.
1.    To get the current environment variables type “print” command at the uboot prompt.
uboot> print
2.    Now append “mem=2048m” to the bootargs variable using the following command.
uboot> setenv bootargs ‘$(bootargs) mem=2048m’
3.    Save the environment varibales using the following command:
uboot> saveenv
4.    Now boot the kernel and verify using cat /proc/cmdline.

Happy kernelling!!

No comments:

Post a Comment