linux核心已經實作了記憶體熱插的支援,當一個linux系統不管運作在 實體環境 或者 虛拟環境 時隻要宿主能提供記憶體熱插拔機制,linux核心就能相應的增加或者減少記憶體。
目前已知的VMware已經可以做到記憶體熱插。
改變記憶體容量有兩種方式:
40 1.1 purpose of memory hotplug
41 ------------
42 Memory Hotplug allows users to increase/decrease the amount of memory.
43 Generally, there are two purposes.
44
45 (A) For changing the amount of memory.
46 This is to allow a feature like capacity on demand.
47 (B) For installing/removing DIMMs or NUMA-nodes physically.
48 This is to exchange DIMMs/NUMA-nodes, reduce power consumption, etc.
前些天檢查maillist時發現已經有人實作了DIMMs 方式更改記憶體大小。這種方式還沒有并入mainline tree。
40 1.1 purpose of memory hotplug
41 ------------
42 Memory Hotplug allows users to increase/decrease the amount of memory.
43 Generally, there are two purposes.
44
45 (A) For changing the amount of memory.
46 This is to allow a feature like capacity on demand.
47 (B) For installing/removing DIMMs or NUMA-nodes physically.
48 This is to exchange DIMMs/NUMA-nodes, reduce power consumption, etc.
可以測試下:
1)下載下傳帶有 dimm hotplug 功能的qemu-kvm:
git clone -b memhp-v3-alt https://github.com/vliaskov/qemu-kvm/commits/memhp-v3-alt
配置編譯:
$./configure --target-list="i386-softmmu x86_64-softmmu i386-linux-user x86_64-linux-user" --enable-kvm
$make
2)下載下傳linux核心 linux-3.0.1.tar.bz2 做測試:(32bit)
linux memory hotplug 需要核心編譯進如下選項:
- For all memory hotplug
Memory model -> Sparse Memory (CONFIG_SPARSEMEM)
location:
Processor type and features
Memory model (Sparse Memory) --->
Allow for memory hot-add (CONFIG_MEMORY_HOTPLUG)
location:
Power management and ACPI options
ACPI (Advanced Configuration and Power Interface) Support
Memory Hotplug
- To enable memory removal, the followings are also necessary
Allow for memory hot remove (CONFIG_MEMORY_HOTREMOVE)
Page Migration (CONFIG_MIGRATION)
- For ACPI memory hotplug, the followings are also necessary
Memory hotplug (under ACPI Support menu) (CONFIG_ACPI_HOTPLUG_MEMORY)
This option can be kernel module.
- As a related configuration, if your box has a feature of NUMA-node hotplug
via ACPI, then this option is necessary too.
ACPI0004,PNP0A05 and PNP0A06 Container Driver (under ACPI Support menu)
(CONFIG_ACPI_CONTAINER).
This option can be kernel module too.
配置好之後就可以用make 了。
編譯完成後将得到的bzImage檔案拷貝出來。
3)制作啟動所需固件:
當記憶體hotplugged之後,核心需要知道新增加上的記憶體并且制作記憶體管理表,生成sysfs檔案來操作記憶體。
由于這需要固件的支援,而現有的qemu mainline 中包含的bios并不支援相應的功能,是以需要下載下傳bios來重新編譯:
git clone -b memhp-v3-alt https://github.com/vliaskov/seabios/commits/memhp-v3-alt
建立一個放固件的目錄:bios
裡面放的固件如下所示:(這些都是編譯後qemu目錄下的)
.
├── bios.bin
├── keymaps
│ ├── common
│ ├── en-us
│ └── modifiers
├── kvmvapic.bin
├── linuxboot.bin
├── multiboot.bin
├── optionrom
│ ├── kvmvapic.bin
│ ├── linuxboot.bin
│ └── multiboot.bin
├── ppc_rom.bin
├── pxe-e1000.rom
├── sgabios.bin
├── slof.bin
├── spapr-rtas.bin
├── vgabios.bin
├── vgabios-cirrus.bin
├── vgabios-qxl.bin
├── vgabios-stdvga.bin
└── vgabios-vmware.bin
将編譯好的固件 ./out/bios.bin 替換上面的 bios.bin。
4)制作檔案系統busybox.img
可參考如下連接配接:
http://blog.csdn.net/gudujianjsk/article/details/7655855
現在測試用的核心檔案都得到了,用如下腳本開啟測試:
./../qemu-kvm/i386-softmmu/qemu-system-i386 -enable-kvm -m 512 -kernel bzImage -L ./../bios -append "root=/dev/sda" -dimm id=dimm0,size=512M,node=0,populated=off -boot c -smp 1 -hda busybox.img -monitor stdio -k en-us -vnc :2
啟動後系統顯示如下:

由于編譯時配置将 CONFIG_ACPI_HOTPLUG_MEMORY 配置為核心子產品了,是以這裡需要首先加載一個核心子產品:acpi_memhotplug.ko.
所有的sections都在sysfs下有它們的裝置資訊。每一個section都是在 /sys/devices/system/memory/目錄下的記憶體塊的一部分。
/sys/devices/system/memory/memoryXXX
在這裡XXX就是記憶體ID。
在monitor中動态 增加dimm:
[email protected]:/var/dbg/test-dimm/busybox$ sudo ./test-box.sh
QEMU 1.2.50 monitor - type 'help' for more information
(qemu) device_add dimm,id=dimm0
此時在虛拟機中可見多了個memory1,其狀态是offline:
此時新增加的記憶體還沒有做好使用的準備。為了使用新增加的記憶體你需要把新增加的記憶體設定為online 狀态
此時用free檢視總計的記憶體數量:
測試完畢。
http://www.mjmwired.net/kernel/Documentation/memory-hotplug.txt