Domian 0 上運作配置相關的配置檔案,運作下面指令: xm create example。進入Domain U 的建立過程;
代碼級别分析開始:
首先對xm指令進行分析,找到xen/tools/python/xen/xm/main.py函數:
def main(argv=sys.argv): 首先導入所有指令對應的處理函數:
for c in IMPORTED_COMMANDS:
commands[c] = eval_r('lambda args: xm_importcommand("%s", args)' % c)
def xm_importcommand(command, args):
cmd = __import__(command, globals(), locals(), 'xen.xm')
cmd.main([command] + args)
通過對上面的操作分析,最後根據cmd = create的指令,分析xm_importcommand,可以看到他首先import create.py,然後調用create.main(create + args);
找到相同檔案下的create.py檔案
def main(argv):
……
dom = make_domain(opts, config)
根據相應的調用過程,找到make_domain()函數;
def make_domain(opts, config):
dominfo = server.xend.domain.create(config)
....
server.xend.domain.waitForDevices(dom)
server.xend.domain.unpause(dom)
對該含函數進行分析,找到最為主要的建立函數過程。根據所調用的函數查找相應的檔案。
在XMLRPCServer.py中注冊:self.server.register_function(domain_create, 'xend.domain.create')
對應的處理函數為XendDomain中的domain_create函數:
而其中又跳到XendDomainInfo的建立函數
dominfo = XendDomainInfo.create(config)
所對應的檔案是/xen/tools/xen/python/xen/xend/XendDomainInfo.py其中的create函數;
def create(config):
vm = XendDomainInfo(domconfig)
try:
vm.start() #XendDomainInfo.start()
except:
log.exception('Domain construction failed')
vm.destroy()
raise
對該函數分析得到其主要的過程,其中有vm的建立資訊的獲得,同時啟動該vm。vm是一個XendDomainInfo類:
class XendDomainInfo:
{
def __init__(self, info, domid = None, dompath = None, augment = False,
priv = False, resume = False, vmpath = None):
def start(self, is_managed = False):
}
其中_init_的函數主要是讀取DomU建立過程的配置檔案,同時設定相應的值,其中主要包括maxmem、memory、建立對應的VMMetrics等。
配置完成以後,該vm類直接調用start()函數:
def start(self, is_managed = False):
from xen.xend import XendDomain
if self._stateGet() in (XEN_API_VM_POWER_STATE_HALTED, XEN_API_VM_POWER_STATE_SUSPENDED, XEN_API_VM_POWER_STATE_CRASHED):
try:
XendTask.log_progress(0, 30, self._constructDomain)
XendTask.log_progress(31, 60, self._initDomain)
XendTask.log_progress(61, 70, self._storeVmDetails)
XendTask.log_progress(71, 80, self._storeDomDetails)
XendTask.log_progress(81, 90, self._registerWatches)
XendTask.log_progress(91, 100, self.refreshShutdown)
xendomains = XendDomain.instance()
# save running configuration if XendDomains believe domain is
# persistent
if is_managed:
xendomains.managed_config_save(self)
except:
log.exception('VM start failed')
self.destroy()
raise
else:
raise XendError('VM already running')
通過類的初始化過程_init_設定self._statGet()過程 self._stateSet(DOM_STATE_HALTED),然後執行start()函數時,根據判斷條件進入上述四個XendTask.log_progeress()函數中;現在挨個分析該三個函數的操作過程,以及所得結果。
-----------------------------------------------------------------------------------------------
通過分析XendTask.log_progress()函數,發現該函數隻是一個包裝函數,它通過線程的方式把參數傳遞給想要調用的函數接口,同時執行該調用函數,現在挨個分析:
def _constructDomain(self):
self.domid = xc.domain_create(
domid = 0,
ssidref = ssidref,
handle = uuid.fromString(self.info['uuid']),
flags = flags,
target = self.info.target())
……
XendDomain.instance().add_domain(self) #添加到XendDomain裡面去
該函數主要還完成了hvm的判斷、設定domain為TSC模式、設定時間配置、設定接口、最大CPU數目以及PCI等。其中該過程調用了xen.lowlevle.xc.xc()函數建立domain;然後找到相應的xc.c檔案,查找相應的函數接口;
static PyObject *pyxc_domain_create(XcObject *self,
PyObject *args,
PyObject *kwds)
if ( (ret = xc_domain_create(self->xc_handle, ssidref,
handle, flags, &dom)) < 0 )
return pyxc_error_to_exception();
if ( target )
if ( (ret = xc_domain_set_target(self->xc_handle, dom, target)) < 0 )
return pyxc_error_to_exception();
其中它又調用相關的 libxc庫檔案;
int xc_domain_create(int xc_handle,
uint32_t ssidref,
xen_domain_handle_t handle,
uint32_t flags,
uint32_t *pdomid)
int err;
DECLARE_DOMCTL;
domctl.cmd = XEN_DOMCTL_createdomain;
domctl.domain = (domid_t)*pdomid;
domctl.u.createdomain.ssidref = ssidref;
domctl.u.createdomain.flags = flags;
memcpy(domctl.u.createdomain.handle, handle, sizeof(xen_domain_handle_t));
if ( (err = do_domctl(xc_handle, &domctl)) != 0 )
return err;
*pdomid = (uint16_t)domctl.domain;
return 0;
然後調用了該do_domctl()控制接口來建立DomU;
static inline int do_domctl(int xc_handle, struct xen_domctl *domctl)
hypercall.op = __HYPERVISOR_domctl;
hypercall.arg[0] = (unsigned long)domctl;
if ( (ret = do_xen_hypercall(xc_handle, &hypercall)) < 0 )
{
if ( errno == EACCES )
DPRINTF("domctl operation failed -- need to"
" rebuild the user-space tool set?\n");
}
-------------------------------------------------------------------------
第二個處理函數分析:
def _initDomain(self):
self.image = image.create(self, self.info)
# repin domain vcpus if a restricted cpus list is provided
# this is done prior to memory allocation to aide in memory
# distribution for NUMA systems.
node = self._setCPUAffinity()
# Set scheduling parameters.
self._setSchedParams()
balloon.free(memory + shadow + vtd_mem, self)
# machine address size
if self.info.has_key('machine_address_size'):
log.debug("_initDomain: setting maximum machine address size %d" % self.info['machine_address_size'])
xc.domain_set_machine_address_size(self.domid, self.info['machine_address_size'])
if self.info.has_key('suppress_spurious_page_faults') and self.info['suppress_spurious_page_faults']:
log.debug("_initDomain: suppressing spurious page faults")
xc.domain_suppress_spurious_page_faults(self.domid)
該函數具體還有建立事件通道self._createChannels()、 self._introduceDomain()、self._freeDMAmemory(node)、 self._createDevices();
----------------------------------------------------------------------------------------------------------
第三個處理過程分析:
def _storeVmDetails(self):
……
self._writeVm(to_store)
self._setVmPermissions()
該函數主要完成存儲Vm細節資訊;
-----------------------------------------------------------------------------------------------------------
其中第三個和第三個函數調用過程,是完成DomU的系統注冊和資料重新整理工作,這裡就不再詳細分析,其中主要根據第一個和第二個的domu的建立和初始化過程,找到在系統核心中的操作位置,同時找到記憶體配置設定過程的相關内容。
---------------------------------------------------------------------------------------------------------------
所有的xm的最後接口都到達了lowlevle.xc.xc()函數的處理過程,那麼重點分析該過程到核心檔案的互動過程,同時關注參數的傳遞過程即可。
其中lowlevel下面的xc主要是通過函數調用,主要過程排程到外層的libxc庫中進行處理。在這裡lowlevel.xc.xc()主要作為一個 函數排程轉換接口,其中在python層xm進行資料處理和分析過程,然後進入該層重組參數序列,然後進入到外層的libxc進行具體的細節操作過程,現 在問題就是通過在libxc層,找到突破口到系統核心的處理過程分析。
最後通過分析所有的xm指令到會到domctl這個過程,進而引發hypercall的操作;
通過分析得到所有的domctl指令都到libxc/Xc_private.h中,從這個接口來調用對hypercall的操作;
現在先寫到這裡,後面核心分析會在下篇中介紹。
本文轉自feisky部落格園部落格,原文連結:http://www.cnblogs.com/feisky/archive/2012/01/17/2325018.html,如需轉載請自行聯系原作者