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

Bittorrent IP Blocklists

In addition to using a VPN service, as an extra precaution I've been using the blocklist feature of my bittorrent… Read More

October 26, 2019 3:31 pm

FreeNAS Error Creating Pool

command '('gpart', 'create', '-s', 'gpt', '/dev/da8')' returned non-zero exit status 1. If you get this error while trying to create… Read More

June 7, 2019 3:44 pm

Change Grub Default Boot Entry on Linux Mint

I'm dual booting Windows and Linux Mint on my laptop. The grub default is to boot into Linux Mint, however… Read More

April 23, 2019 7:45 pm

How to Reset Secure Channel On Active Directory Domain Controller

When you're a little too careless about virtualizing your domain controllers, cloning, migrating, backing up and restoring, returning from vacation… Read More

April 21, 2019 8:14 am

Run Systemd Script Before System Shutdown

I tried to retain the NGINX FastCGI cache and have it persist across system reboots instead of being ephemeral by… Read More

April 20, 2019 10:14 am

Learn Systemctl Usage to Manage Systemd Service in Linux

Systemd is new service manager for Linux. It's a replacement for all previous init systems (SysV/SysVinit & Ubuntu's Upstart) and… Read More

April 20, 2019 7:55 am