📄 create.py
字号:
hexd = ['0x' + x for x in d] ioports.append(hexd) vals.ioports = ioports def preprocess_vtpm(vals): if not vals.vtpm: return vtpms = [] for vtpm in vals.vtpm: d = {} a = vtpm.split(',') for b in a: (k, v) = b.strip().split('=', 1) k = k.strip() v = v.strip() if k not in ['backend', 'instance']: err('Invalid vtpm specifier: ' + vtpm) d[k] = v vtpms.append(d) vals.vtpm = vtpmsdef preprocess_access_control(vals): if not vals.access_control: return access_controls = [] num = len(vals.access_control) if num == 1: access_control = (vals.access_control)[0] d = {} a = access_control.split(',') if len(a) > 2: err('Too many elements in access_control specifier: ' + access_control) for b in a: (k, v) = b.strip().split('=', 1) k = k.strip() v = v.strip() if k not in ['policy','label']: err('Invalid access_control specifier: ' + access_control) d[k] = v access_controls.append(d) vals.access_control = access_controls elif num > 1: err('Multiple access_control definitions.')def preprocess_ip(vals): if vals.ip or vals.dhcp != 'off': dummy_nfs_server = '127.0.255.255' ip = (vals.ip + ':' + (vals.nfs_server or dummy_nfs_server) + ':' + vals.gateway + ':' + vals.netmask + ':' + vals.hostname + ':' + vals.interface + ':' + vals.dhcp) else: ip = '' vals.cmdline_ip = ipdef preprocess_nfs(vals): if not vals.nfs_root: return if not vals.nfs_server: err('Must set nfs root and nfs server') nfs = 'nfsroot=' + vals.nfs_server + ':' + vals.nfs_root vals.extra = nfs + ' ' + vals.extradef get_host_addr(): host = socket.gethostname() addr = socket.gethostbyname(host) return addrVNC_BASE_PORT = 5500def choose_vnc_display(): """Try to choose a free vnc display. """ def netstat_local_ports(): """Run netstat to get a list of the local ports in use. """ l = os.popen("netstat -nat").readlines() r = [] # Skip 2 lines of header. for x in l[2:]: # Local port is field 3. y = x.split()[3] # Field is addr:port, split off the port. y = y.split(':')[-1] r.append(int(y)) return r ports = netstat_local_ports() for d in range(1, 100): port = VNC_BASE_PORT + d if port in ports: continue return d return Nonevncpid = Nonedef spawn_vnc(display): """Spawns a vncviewer that listens on the specified display. On success, returns the port that the vncviewer is listening on and sets the global vncpid. On failure, returns 0. Note that vncviewer is daemonized. """ vncargs = (["vncviewer", "-log", "*:stdout:0", "-listen", "%d" % (VNC_BASE_PORT + display) ]) global vncpid vncpid = utils.daemonize("vncviewer", vncargs) if vncpid == 0: return 0 return VNC_BASE_PORT + displaydef preprocess_vnc(vals): """If vnc was specified, spawn a vncviewer in listen mode and pass its address to the domain on the kernel command line. """ if vals.dryrun: return if vals.vncviewer: vnc_display = choose_vnc_display() if not vnc_display: warn("No free vnc display") return print 'VNC=', vnc_display vnc_port = spawn_vnc(vnc_display) if vnc_port > 0: vnc_host = get_host_addr() vnc = 'VNC_VIEWER=%s:%d' % (vnc_host, vnc_port) vals.extra = vnc + ' ' + vals.extra def preprocess(vals): preprocess_disk(vals) preprocess_pci(vals) preprocess_vscsi(vals) preprocess_ioports(vals) preprocess_ip(vals) preprocess_nfs(vals) preprocess_vnc(vals) preprocess_vtpm(vals) preprocess_access_control(vals) preprocess_cpuid(vals, 'cpuid') preprocess_cpuid(vals, 'cpuid_check')def comma_sep_kv_to_dict(c): """Convert comma-separated, equals-separated key-value pairs into a dictionary. """ d = {} c = c.strip() if len(c) > 0: a = c.split(',') for b in a: if b.find('=') == -1: err("%s should be a pair, separated by an equals sign." % b) (k, v) = b.split('=', 1) k = k.strip() v = v.strip() d[k] = v return ddef make_domain(opts, config): """Create, build and start a domain. @param opts: options @param config: configuration @return: domain id @rtype: int """ try: dominfo = server.xend.domain.create(config) except xmlrpclib.Fault, ex: import signal if vncpid: os.kill(vncpid, signal.SIGKILL) if ex.faultCode == xen.xend.XendClient.ERROR_INVALID_DOMAIN: err("the domain '%s' does not exist." % ex.faultString) else: err("%s" % ex.faultString) except Exception, ex: # main.py has good error messages that let the user know what failed. # unless the error is a create.py specific thing, it should be handled # at main. The purpose of this general-case 'Exception' handler is to # clean up create.py specific processes/data but since create.py does # not know what to do with the error, it should pass it up. import signal if vncpid: os.kill(vncpid, signal.SIGKILL) raise dom = sxp.child_value(dominfo, 'name') try: server.xend.domain.waitForDevices(dom) except xmlrpclib.Fault, ex: server.xend.domain.destroy(dom) err("%s" % ex.faultString) except: server.xend.domain.destroy(dom) err("Device creation failed for domain %s" % dom) if not opts.vals.paused: try: server.xend.domain.unpause(dom) except: server.xend.domain.destroy(dom) err("Failed to unpause domain %s" % dom) opts.info("Started domain %s" % (dom)) return int(sxp.child_value(dominfo, 'domid'))def get_xauthority(): xauth = os.getenv("XAUTHORITY") if not xauth: home = os.getenv("HOME") if not home: import posix, pwd home = pwd.getpwuid(posix.getuid())[5] xauth = home + "/.Xauthority" return xauthdef parseCommandLine(argv): gopts.reset() args = gopts.parse(argv) if gopts.vals.help or gopts.vals.help_config: if gopts.vals.help_config: print gopts.val_usage() return (None, None) if not gopts.vals.display: gopts.vals.display = os.getenv("DISPLAY") if not gopts.vals.xauthority: gopts.vals.xauthority = get_xauthority() gopts.is_xml = False # Process remaining args as config variables. for arg in args: if '=' in arg: (var, val) = arg.strip().split('=', 1) gopts.setvar(var.strip(), val.strip()) if gopts.vals.config: config = gopts.vals.config else: try: gopts.load_defconfig() preprocess(gopts.vals) if not gopts.getopt('name') and gopts.getopt('defconfig'): gopts.setopt('name', os.path.basename(gopts.getopt('defconfig'))) config = make_config(gopts.vals) except XMLFileError, ex: XMLFile = ex.getFile() gopts.is_xml = True config = ex.getFile() return (gopts, config)def help(): return str(gopts)def main(argv): is_xml = False try: (opts, config) = parseCommandLine(argv) except StandardError, ex: err(str(ex)) if not opts: return if not opts.is_xml: if type(config) == str: try: config = sxp.parse(file(config))[0] except IOError, exn: raise OptionError("Cannot read file %s: %s" % (config, exn[1])) if serverType == SERVER_XEN_API: from xen.xm.xenapi_create import sxp2xml sxp2xml_inst = sxp2xml() doc = sxp2xml_inst.convert_sxp_to_xml(config, transient=True) if opts.vals.dryrun and not opts.is_xml: SXPPrettyPrint.prettyprint(config) if opts.vals.xmldryrun and serverType == SERVER_XEN_API: from xml.dom.ext import PrettyPrint as XMLPrettyPrint XMLPrettyPrint(doc) if opts.vals.dryrun or opts.vals.xmldryrun: return if opts.vals.console_autoconnect: do_console(sxp.child_value(config, 'name', -1)) if serverType == SERVER_XEN_API: from xen.xm.xenapi_create import xenapi_create xenapi_create_inst = xenapi_create() if opts.is_xml: vm_refs = xenapi_create_inst.create(filename = config, skipdtd = opts.vals.skipdtd) else: vm_refs = xenapi_create_inst.create(document = doc, skipdtd = opts.vals.skipdtd) map(lambda vm_ref: server.xenapi.VM.start(vm_ref, 0), vm_refs) elif not opts.is_xml: dom = make_domain(opts, config) if opts.vals.vncviewer: domid = domain_name_to_domid(sxp.child_value(config, 'name', -1)) vncviewer_autopass = getattr(opts.vals,'vncviewer-autopass', False) console.runVncViewer(domid, vncviewer_autopass, True) def do_console(domain_name): cpid = os.fork() if cpid != 0: for i in range(10): # Catch failure of the create process time.sleep(1) (p, rv) = os.waitpid(cpid, os.WNOHANG) if os.WIFEXITED(rv): if os.WEXITSTATUS(rv) != 0: sys.exit(os.WEXITSTATUS(rv)) try: domid = domain_name_to_domid(domain_name) console.execConsole(domid) except: pass print("Could not start console\n"); sys.exit(0)if __name__ == '__main__': main(sys.argv)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -