xref: /qemu/docs/memory-hotplug.txt (revision 9277d81f)
1a3b04217SLuiz CapitulinoQEMU memory hotplug
2a3b04217SLuiz Capitulino===================
3a3b04217SLuiz Capitulino
4a3b04217SLuiz CapitulinoThis document explains how to use the memory hotplug feature in QEMU,
5a3b04217SLuiz Capitulinowhich is present since v2.1.0.
6a3b04217SLuiz Capitulino
74fccb483SZhu GuihuaGuest support is required for memory hotplug to work.
8a3b04217SLuiz Capitulino
9a3b04217SLuiz CapitulinoBasic RAM hotplug
10a3b04217SLuiz Capitulino-----------------
11a3b04217SLuiz Capitulino
12a3b04217SLuiz CapitulinoIn order to be able to hotplug memory, QEMU has to be told how many
13a3b04217SLuiz Capitulinohotpluggable memory slots to create and what is the maximum amount of
14a3b04217SLuiz Capitulinomemory the guest can grow. This is done at startup time by means of
15a3b04217SLuiz Capitulinothe -m command-line option, which has the following format:
16a3b04217SLuiz Capitulino
17a3b04217SLuiz Capitulino -m [size=]megs[,slots=n,maxmem=size]
18a3b04217SLuiz Capitulino
19a3b04217SLuiz CapitulinoWhere,
20a3b04217SLuiz Capitulino
21a3b04217SLuiz Capitulino - "megs" is the startup RAM. It is the RAM the guest will boot with
22a3b04217SLuiz Capitulino - "slots" is the number of hotpluggable memory slots
23a3b04217SLuiz Capitulino - "maxmem" is the maximum RAM size the guest can have
24a3b04217SLuiz Capitulino
25a3b04217SLuiz CapitulinoFor example, the following command-line:
26a3b04217SLuiz Capitulino
2777fc026cSThomas Huth qemu [...] -m 1G,slots=3,maxmem=4G
28a3b04217SLuiz Capitulino
29a3b04217SLuiz CapitulinoCreates a guest with 1GB of memory and three hotpluggable memory slots.
30a3b04217SLuiz CapitulinoThe hotpluggable memory slots are empty when the guest is booted, so all
31a3b04217SLuiz Capitulinomemory the guest will see after boot is 1GB. The maximum memory the
32a3b04217SLuiz Capitulinoguest can reach is 4GB. This means that three additional gigabytes can be
33a3b04217SLuiz Capitulinohotplugged by using any combination of the available memory slots.
34a3b04217SLuiz Capitulino
35a3b04217SLuiz CapitulinoTwo monitor commands are used to hotplug memory:
36a3b04217SLuiz Capitulino
37a3b04217SLuiz Capitulino - "object_add": creates a memory backend object
38a3b04217SLuiz Capitulino - "device_add": creates a front-end pc-dimm device and inserts it
39a3b04217SLuiz Capitulino                 into the first empty slot
40a3b04217SLuiz Capitulino
41a3b04217SLuiz CapitulinoFor example, the following commands add another 1GB to the guest
42a3b04217SLuiz Capitulinodiscussed earlier:
43a3b04217SLuiz Capitulino
44a3b04217SLuiz Capitulino  (qemu) object_add memory-backend-ram,id=mem1,size=1G
45a3b04217SLuiz Capitulino  (qemu) device_add pc-dimm,id=dimm1,memdev=mem1
46a3b04217SLuiz Capitulino
47a3b04217SLuiz CapitulinoUsing the file backend
48a3b04217SLuiz Capitulino----------------------
49a3b04217SLuiz Capitulino
50a3b04217SLuiz CapitulinoBesides basic RAM hotplug, QEMU also supports using files as a memory
51a3b04217SLuiz Capitulinobackend. This is useful for using hugetlbfs in Linux, which provides
52a3b04217SLuiz Capitulinoaccess to bigger page sizes.
53a3b04217SLuiz Capitulino
54a3b04217SLuiz CapitulinoFor example, assuming that the host has 1GB hugepages available in
55a3b04217SLuiz Capitulinothe /mnt/hugepages-1GB directory, a 1GB hugepage could be hotplugged
56a3b04217SLuiz Capitulinointo the guest from the previous section with the following commands:
57a3b04217SLuiz Capitulino
58a3b04217SLuiz Capitulino  (qemu) object_add memory-backend-file,id=mem1,size=1G,mem-path=/mnt/hugepages-1GB
59a3b04217SLuiz Capitulino  (qemu) device_add pc-dimm,id=dimm1,memdev=mem1
60a3b04217SLuiz Capitulino
61a3b04217SLuiz CapitulinoIt's also possible to start a guest with memory cold-plugged into the
62a3b04217SLuiz Capitulinohotpluggable memory slots. This might seem counterintuitive at first,
63a3b04217SLuiz Capitulinobut this allows for a lot of flexibility when using the file backend.
64a3b04217SLuiz Capitulino
65*9277d81fSVille SkyttäIn the following command-line example, an 8GB guest is created where 6GB
66a3b04217SLuiz Capitulinocomes from regular RAM, 1GB is a 1GB hugepage page and 256MB is from
67a3b04217SLuiz Capitulino2MB pages. Also, the guest has additional memory slots to hotplug more
68a3b04217SLuiz Capitulino2GB if needed:
69a3b04217SLuiz Capitulino
70a3b04217SLuiz Capitulino qemu [...] -m 6GB,slots=4,maxmem=10G \
71a3b04217SLuiz Capitulino   -object memory-backend-file,id=mem1,size=1G,mem-path=/mnt/hugepages-1G \
72a3b04217SLuiz Capitulino   -device pc-dimm,id=dimm1,memdev=mem1 \
73a3b04217SLuiz Capitulino   -object memory-backend-file,id=mem2,size=256M,mem-path=/mnt/hugepages-2MB \
74a3b04217SLuiz Capitulino   -device pc-dimm,id=dimm2,memdev=mem2
754fccb483SZhu Guihua
764fccb483SZhu Guihua
774fccb483SZhu GuihuaRAM hot-unplug
784fccb483SZhu Guihua---------------
794fccb483SZhu Guihua
804fccb483SZhu GuihuaIn order to be able to hot unplug pc-dimm device, QEMU has to be told the ids
814fccb483SZhu Guihuaof pc-dimm device and memory backend object. The ids were assigned when you hot
824fccb483SZhu Guihuaplugged memory.
834fccb483SZhu Guihua
844fccb483SZhu GuihuaTwo monitor commands are used to hot unplug memory:
854fccb483SZhu Guihua
864fccb483SZhu Guihua - "device_del": deletes a front-end pc-dimm device
874fccb483SZhu Guihua - "object_del": deletes a memory backend object
884fccb483SZhu Guihua
894fccb483SZhu GuihuaFor example, assuming that the pc-dimm device with id "dimm1" exists, and its memory
904fccb483SZhu Guihuabackend is "mem1", the following commands tries to remove it.
914fccb483SZhu Guihua
924fccb483SZhu Guihua  (qemu) device_del dimm1
934fccb483SZhu Guihua  (qemu) object_del mem1
94