Hot-add Memory to Linux Guest in VMware

Symptoms

Memory you have hot added in Linux guest virtual machines is not registered using the free or top commands.

Cause

This issue occurs when Linux requires manual intervention to expose the newly added memory in the guest. The process is called setting the memory to online.

Resolution

To resolve this issue, set the memory online in RHEL/CentOS, and Ubuntu/Debian.

Verify if the operating system actually sees the newly added memory:

grep -i MemTotal /proc/meminfo

Look for memory that appears offline (to tools like free and top):

grep line /sys/devices/system/memory/*/state
Shell Tip: In order to run multi-line shell scripts which are copy/pasted into the terminal, they must be enclosed in parenthesis. Type ( and then paste the contents of your clipboard, and then type ) and press enter.

Use the following shell script to set all memory files to online:

for RAM in $(grep line /sys/devices/system/memory/*/state)
do
        echo "Found ram: ${RAM} ..."
        if [[ "${RAM}" == *":offline" ]]; then
                echo "Bringing online"
                echo $RAM | sed "s/:offline$//"|sed "s/^/echo online > /"|source /dev/stdin
        else
                echo "Already online"
        fi
done

You can run the script twice and you should see similar output:

Found ram: /sys/devices/system/memory/memory0/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory10/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory11/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory12/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory13/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory14/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory15/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory1/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory2/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory3/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory4/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory5/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory6/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory7/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory8/state:online ...
Already online
Found ram: /sys/devices/system/memory/memory9/state:online ...
Already online

Or you can manually update each file when memory appears offline:

echo online >/sys/devices/system/memory/memory[number]/state

Verify you can see the extra memory by running this command:

free -m

Turn On all Available Guest vCPU and Memory

The script below will turn on all available vCPU and memory on your VM:

#!/bin/bash
# Based on script by William Lam - http://engineering.ucsb.edu/~duonglt/vmware/

# Bring CPUs online
for CPU in $(ls /sys/devices/system/cpu/ |grep -E '(cpu[0-9])')
do
        CPU_DIR="/sys/devices/system/cpu/${CPU}"
        echo "Found cpu: \"${CPU_DIR}\" ..."
        CPU_STATE_FILE="${CPU_DIR}/online"
        if [ -f "${CPU_STATE_FILE}" ]; then
                STATE=$(cat "${CPU_STATE_FILE}" | grep 1)
                if [ "${STATE}" == "1" ]; then
                        echo -e "\t${CPU} already online"
                else
                         echo -e "\t${CPU} is new cpu, onlining cpu ..."
                         echo 1 > "${CPU_STATE_FILE}"
                fi
        else 
                echo -e "\t${CPU} already configured prior to hot-add"
        fi
done

# Bring all new Memory online
for RAM in $(grep line /sys/devices/system/memory/*/state)
do
        echo "Found ram: ${RAM} ..."
        if [[ "${RAM}" == *":offline" ]]; then
                echo "Bringing online"
                echo $RAM | sed "s/:offline$//"|sed "s/^/echo online > /"|source /dev/stdin
        else
                echo "Already online"
        fi
done

 

Source & credit: http://www.supermaru.com/2016/10/hot-add-cpu-memory-ubuntu-guest-vmware/

Share
Disqus Comments Loading...

Recent Posts

VMWare vSphere 6.7 ESXTOP Output Garbled

If your VMWare vSphere 6.x environment command output of esxtop looks like a bunch of garbled gibberish (it's actually CSV… Read More

February 28, 2019 7:39 pm 19:39

How To Run RoboCopy Backup in Parallel

From time to time Windows Admins will surprise you with band-aid and bubble gum scripts, that's entirely expected I think.… Read More

February 28, 2019 12:20 pm 12:20

Windows 10 GodMode - The Ultimate Administrator Shortcut

Have you ever wondered what life might be like if the Windows 10 OS somehow had a single folder that… Read More

February 28, 2019 7:58 am 07:58

Samsung Galaxy S9 G960/G965 Stock ROM Firmware Download

There could be any number of reasons for needing to flash manufacturer stock OEM firmware on a Samsung Galaxy S9… Read More

January 24, 2019 7:42 am 07:42

Output IP Address with ipconfig Findstr Ethernet Adapter

How many times have you entered  ipconfig /all at the command line to return a single IP address, then have to strain… Read More

January 20, 2019 12:39 am 00:39

AWS SES Assistance in Enterprise Market - A Must Read

Designing e-mail solutions on a large scale can be a complex and costly challenge for a business: you need to… Read More

December 25, 2018 4:01 pm 16:01