Hot-adding memory to a Linux VM

Problem: When I hot-add memory to a linux VM, it doesn’t show up in the system when I type¬†free

Solution:
This has likely been bashed to death and can be readily found on the internet if you search for it, but here’s one more for completeness.

In it’s simplest people just need to refer to the kernel documentation either in the kernel source Documentation/memory-hotadd.txt or online via YAGS (yet another google search)

Here’s a script to “online” any offline memory after you have added it.

#!/bin/bash
 
if [ "$UID" -ne "0" ]; then
 echo -e "You must be root to run this script"
 exit 1
fi
 
for MEMORY in /sys/devices/system/memory/memory*
 do
  if grep -q online "${MEMORY}/state"; then
   echo -e "${MEMORY} is online"
  else
   echo -en "${MEMORY} is offline, bringing online ..."
   echo online > "${MEMORY}/state"
   echo "OK"
  fi
 done