From 4e81522571e6dc3b84939e2143db0e6a47094af9 Mon Sep 17 00:00:00 2001 From: Li Yuxuan Date: Wed, 13 Mar 2019 15:37:57 +0800 Subject: [PATCH] vc:qemu: Fix id calculation of memory hotplug QMP doesn't guarantee the order of the array that is returned by `query-memory-devices` command. So we would better search the whole array to find out the current max slot, rather than simply use the last element's slot. Fixes: #1362 Signed-off-by: Li Yuxuan --- virtcontainers/qemu.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/virtcontainers/qemu.go b/virtcontainers/qemu.go index 7ee216433f..402381ef1e 100644 --- a/virtcontainers/qemu.go +++ b/virtcontainers/qemu.go @@ -1220,7 +1220,13 @@ func (q *qemu) hotplugAddMemory(memDev *memoryDevice) (int, error) { } if len(memoryDevices) != 0 { - memDev.slot = memoryDevices[len(memoryDevices)-1].Data.Slot + 1 + maxSlot := -1 + for _, device := range memoryDevices { + if maxSlot < device.Data.Slot { + maxSlot = device.Data.Slot + } + } + memDev.slot = maxSlot + 1 } err = q.qmpMonitorCh.qmp.ExecHotplugMemory(q.qmpMonitorCh.ctx, "memory-backend-ram", "mem"+strconv.Itoa(memDev.slot), "", memDev.sizeMB) if err != nil {