๐Ÿ“How to activate memory hotplug

Memory hotplug allows you to dynamically add or remove memory from a running virtual machine (VM) or server without requiring a system reboot. This can be a valuable feature for your VPS to efficiently manage resources. In this guide, we will show you how to activate memory hotplug using a custom udev rule in the /etc/udev/rules.d/99-hotplug-cpu-mem.rules file.

Requirements

  • a VPS

  • an OnetSolutions account

Step 1: Create or Edit the udev Rule File

  1. SSH into your VPS server using your preferred terminal emulator.

  2. Navigate to the /etc/udev/rules.d/ directory, where udev rules are stored:

    cd /etc/udev/rules.d/
  3. Create or edit the 99-hotplug-cpu-mem.rules file using a text editor of your choice. You can use nano, vim, or gedit:

    sudo nano 99-hotplug-cpu-mem.rules

Step 2: Add the Memory Hotplug Rule

In the 99-hotplug-cpu-mem.rules file, add the following rule to enable memory hotplug:

SUBSYSTEM=="memory", ACTION=="add", TEST=="state", ATTR{state}=="offline", ATTR{state}="online"
SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", ATTR{online}="1"

This rule checks for CPU online events (ACTION=="add" and TEST=="online") and sets the online attribute to 1 for CPUs where online is currently 0.

Step 3: Save and Exit

Save the changes and exit your text editor:

  • For nano, press Ctrl + O, then press Enter to save, and finally press Ctrl + X to exit.

  • For vim, type :wq and press Enter to save and exit.

  • For gedit, click the "Save" option in the GUI and close the editor.

Step 4: Reload udev Rules

To apply the new udev rule without rebooting, you need to reload the udev rules:

sudo udevadm control --reload

Please run also this script:

#!/bin/bash
# Based on script by William Lam

# Bring CPUs online
for CPU_DIR in /sys/devices/system/cpu/cpu[0-9]*
do
    CPU=${CPU_DIR##*/}
    echo "Found cpu: '${CPU_DIR}' ..."
    CPU_STATE_FILE="${CPU_DIR}/online"
    if [ -f "${CPU_STATE_FILE}" ]; then
        if grep -qx 1 "${CPU_STATE_FILE}"; 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

Step 5: Test Memory Hotplug

You can now test memory hotplug by adding or removing memory from your VPS.

Conclusion

By following these steps and creating the udev rule, you have activated memory hotplug for your VPS. This will allow you to dynamically adjust memory resources on your virtual server, providing greater flexibility and resource management capabilities.

Last updated