A.13. VDSM Hook 实例
红帽不会对这里所提供的 hook 脚本进行支持,您需要在系统使用任何 hook 脚本前,对它进行全面的测试。
例 A.5. NUMA 节点优化
目的:
这个 hook 脚本会根据 numaset 这个自定义属性的值在一个 NUMA 主机上优化内存分配。如果这个自定义属性没有被设置,则不会进行任何操作。
配置字符串:
numaset=^(interleave|strict|preferred):[\^]?\d+(-\d+)?(,[\^]?\d+(-\d+)?)*$
这里的正则表达式允许通过虚拟机上的
numaset 自定义属性指定使用的分配模式(interleave、strict、preferred)和节点,这两个值以冒号(:)分隔。这个正则表达式还限定了 nodeset 的有效值为:
- 一个特定的节点(
numaset=strict:1指定只使用节点 1),或 - 一个范围之内的节点(
numaset=strict:1-4指定使用节点 1 到节点 4),或 - 一个特定的节点不能被使用(
numaset=strict:^3指定节点 3 不被使用),或 - 以逗号分隔的以上值的组合。(
numaset=strict:1-4,6指定使用节点 1 到节点 4,以及节点 6)。
脚本:
/usr/libexec/vdsm/hooks/before_vm_start/50_numa
#!/usr/bin/python
import os
import sys
import hooking
import traceback
'''
numa hook
=========
add numa support for domain xml:
<numatune>
<memory mode="strict" nodeset="1-4,^3" />
</numatune>
memory=interleave|strict|preferred
numaset="1" (use one NUMA node)
numaset="1-4" (use 1-4 NUMA nodes)
numaset="^3" (don't use NUMA node 3)
numaset="1-4,^3,6" (or combinations)
syntax:
numa=strict:1-4
'''
if os.environ.has_key('numa'):
try:
mode, nodeset = os.environ['numa'].split(':')
domxml = hooking.read_domxml()
domain = domxml.getElementsByTagName('domain')[0]
numas = domxml.getElementsByTagName('numatune')
if not len(numas) > 0:
numatune = domxml.createElement('numatune')
domain.appendChild(numatune)
memory = domxml.createElement('memory')
memory.setAttribute('mode', mode)
memory.setAttribute('nodeset', nodeset)
numatune.appendChild(memory)
hooking.write_domxml(domxml)
else:
sys.stderr.write('numa: numa already exists in domain xml')
sys.exit(2)
except:
sys.stderr.write('numa: [unexpected error]: %s\n' % traceback.format_exc())
sys.exit(2)