I am running Ubuntu in a virtual machine and I'd like to add CPU and RAM without rebooting the device. How can I hotplug this new virtual hardware?
3 Answers
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
-
Are you sure you actually have to do this? I would think that once plugged in, they would be automatically turned on; the ability to turn them off is so you can unplug them, or leave them off to save power.– psusiApr 28, 2016 at 23:22
-
-
1I was also expecting Hotplug to work out of the box, but seems like it doesn't ... this is what you need to do, didn't test the script, but manually took care of it, the logic should work. Super Thanx Jun 9, 2017 at 5:53
-
1It worked like a charm! Thanks. I'm just curios about how do vmware host and guest Linux OS react to this change in number of cores, at the level of NUMA (I know that vmware is quite good to communicate the right NUMA structure at the guest, but I like to know if this change si "acquired". How does this work exactly? Regards Feb 25, 2021 at 14:36
-
1@FabianoTarlao Are you wanting to know how the NUMA structure results in /sys/devices/system/* occurring? I've got no idea. Sounds like something you could find digging in the kernel code or openvmtools If you find out, let us know. Interesting question. github.com/vmware/open-vm-tools Mar 2, 2021 at 16:26
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.
Instead of creating and keeping script you can run this one line command (it can be also a script if you want). I have just typed one for memory (one can easyline change this to cpu as well):
grep line /sys/devices/system/memory/*/state |grep offline |awk -F '\/' '{print $6}' |while read xx; do echo online >/sys/devices/system/memory/$xx/state ; done