服务器 - 如何虚拟机中热插拔CPU和内存

1,400 阅读1分钟
原文链接: askubuntu.com
up vote 6 down vote accepted

These can be enabled through the use of the /sys filesystem using root credentials.

For the CPU, you change the 0 to a 1 in to the appropriate file of the pattern: /sys/devices/system/cpu/cpu*/online.

For the RAM, you find the state in the files /sys/devices/system/memory/memory*/state and change the contents from offline to online.

The script below will turn all CPU and RAM online for you.

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

# 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
share|improve this answer edited Jul 28 '17 at 10:54 answered Apr 28 '16 at 23:02 up vote 3 down vote

Instead of operating the kernel parameters, you can automatically enable hotplugged CPU or memory by using udev rules:

/etc/udev/rules.d/94-hotplug-cpu-mem.rules

ACTION=="add", SUBSYSTEM=="cpu", ATTR{online}=="0", ATTR{online}="1"

ACTION=="add", SUBSYSTEM=="memory", ATTR{state}=="offline", ATTR{state}="online"

Tested on CentOS 6/7, Ubuntu 14.

Debian 7 crashed for unknown reason. Further testing would be required.

share|improve this answer edited Jan 4 '17 at 17:14 answered Jan 4 '17 at 8:01

Not the answer you're looking for? Browse other questions tagged server ram cpu virtual hotplug or ask your own question.