ia64/xen-unstable

changeset 6077:356b14d5ffc8

merge
author kaf24@firebug.cl.cam.ac.uk
date Tue Aug 09 13:04:54 2005 +0000 (2005-08-09)
parents 2379e795ca69 4125b9fea242
children 95d2bbf6a273
files linux-2.6-xen-sparse/arch/xen/i386/kernel/Makefile tools/python/xen/sv/CreateDomain.py tools/python/xen/sv/DomInfo.py tools/python/xen/sv/GenTabbed.py tools/python/xen/sv/HTMLBase.py tools/python/xen/sv/Main.py tools/python/xen/sv/NodeInfo.py tools/python/xen/sv/RestoreDomain.py tools/python/xen/sv/Wizard.py tools/python/xen/sv/util.py tools/python/xen/xm/main.py tools/sv/inc/style.css tools/sv/index.psp xen/arch/x86/mm.c xen/include/asm-x86/page.h
line diff
    11.1 --- a/tools/python/xen/xm/main.py	Tue Aug 09 13:04:31 2005 +0000
    11.2 +++ b/tools/python/xen/xm/main.py	Tue Aug 09 13:04:54 2005 +0000
    11.3 @@ -1,28 +1,113 @@
    11.4 -# Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
    11.5 +# (C) Copyright IBM Corp. 2005
    11.6 +# Copyright (C) 2004 Mike Wray
    11.7 +#
    11.8 +# Authors:
    11.9 +#     Sean Dague <sean at dague dot net>
   11.10 +#     Mike Wray <mike dot wray at hp dot com>
   11.11 +#
   11.12 +# This software may be used and distributed according to the terms
   11.13 +# of the GNU General Public License v2.  Full details on license
   11.14 +# terms and conditions are included with this distribution
   11.15 +
   11.16  """Grand unified management application for Xen.
   11.17  """
   11.18  import os
   11.19  import os.path
   11.20  import sys
   11.21  import commands
   11.22 +import re
   11.23  from getopt import getopt
   11.24  import socket
   11.25  import warnings
   11.26  warnings.filterwarnings('ignore', category=FutureWarning)
   11.27 -
   11.28  from xen.xend import PrettyPrint
   11.29  from xen.xend import sxp
   11.30 -# this is a nasty place to stick this in, but required because
   11.31 -# log file access is set up via a 5 deep import chain.  This
   11.32 -# ensures the user sees a useful message instead of a stack trace
   11.33 -if os.getuid() != 0:
   11.34 -    print "xm requires root access to execute, please try again as root"
   11.35 -    sys.exit(1)
   11.36 +from xen.xm.opts import *
   11.37 +shorthelp = """Usage: xm <subcommand> [args]
   11.38 +    Control, list, and manipulate Xen guest instances
   11.39 +
   11.40 +xm common subcommands:
   11.41 +    console <DomId>         attach to console of DomId
   11.42 +    create <CfgFile>        create a domain based on Config File
   11.43 +    destroy <DomId>         terminate a domain immediately
   11.44 +    help                    display this message
   11.45 +    list [DomId, ...]       list information about domains
   11.46 +    mem-max <DomId> <Mem>   set the maximum memory reservation for a domain
   11.47 +    mem-set <DomId> <Mem>   adjust the current memory usage for a domain
   11.48 +    migrate <DomId> <Host>  migrate a domain to another machine
   11.49 +    pause <DomId>           pause execution of a domain
   11.50 +    reboot <DomId>          reboot a domain
   11.51 +    restore <File>          create a domain from a saved state file
   11.52 +    save <DomId> <File>     save domain state (and config) to file
   11.53 +    shutdown <DomId>        shutdown a domain
   11.54 +    unpause <DomId>         unpause a paused domain
   11.55 +
   11.56 +For a complete list of subcommands run 'xm help --long'
   11.57 +For more help on xm see the xm(1) man page
   11.58 +For more help on xm create, see the xmdomain.cfg(5) man page"""
   11.59 +
   11.60 +longhelp = """Usage: xm <subcommand> [args]
   11.61 +    Control, list, and manipulate Xen guest instances
   11.62 +
   11.63 +xm full list of subcommands:
   11.64  
   11.65 -from xen.xend.XendClient import XendError, server
   11.66 -from xen.xend.XendClient import main as xend_client_main
   11.67 -from xen.xm import create, destroy, migrate, shutdown, sysrq
   11.68 -from xen.xm.opts import *
   11.69 +  Domain Commands:
   11.70 +    console <DomId>         attach to console of DomId
   11.71 +    cpus-list <DomId> <VCpu>          get the list of cpus for a VCPU
   11.72 +    cpus-set <DomId> <VCpu> <CPUS>    set which cpus a VCPU can use. 
   11.73 +    create  <ConfigFile>      create a domain
   11.74 +    destroy <DomId>           terminate a domain immediately
   11.75 +    domid   <DomName>         convert a domain name to a domain id
   11.76 +    domname <DomId>           convert a domain id to a domain name
   11.77 +    list                      list information about domains
   11.78 +    mem-max <DomId> <Mem>     set domain maximum memory limit
   11.79 +    mem-set <DomId> <Mem>     set the domain's memory dynamically
   11.80 +    migrate <DomId> <Host>    migrate a domain to another machine
   11.81 +    pause   <DomId>           pause execution of a domain
   11.82 +    reboot   [-w|-a] <DomId>  reboot a domain
   11.83 +    restore <File>            create a domain from a saved state file
   11.84 +    save    <DomId> <File>    save domain state (and config) to file
   11.85 +    shutdown [-w|-a] <DomId>  shutdown a domain
   11.86 +    sysrq   <DomId> <letter>  send a sysrq to a domain
   11.87 +    unpause <DomId>           unpause a paused domain
   11.88 +    vcpu-enable <DomId> <VCPU>        disable VCPU in a domain
   11.89 +    vcpu-disable <DomId> <VCPU>       enable VCPU in a domain
   11.90 +    vcpu-list <DomId>                 get the list of VCPUs for a domain
   11.91 +
   11.92 +  Xen Host Commands:
   11.93 +    dmesg   [--clear]         read or clear Xen's message buffer
   11.94 +    info                      get information about the xen host
   11.95 +    log                       print the xend log
   11.96 +
   11.97 +  Scheduler Commands:
   11.98 +    bvt <options>             set BVT scheduler parameters
   11.99 +    bvt_ctxallow <Allow>      set the BVT scheduler context switch allowance
  11.100 +    sedf <options>            set simple EDF parameters
  11.101 +
  11.102 +  Virtual Device Commands:
  11.103 +    block-create <DomId> <BackDev> <FrontDev> <Mode> [BackDomId]
  11.104 +        Create a new virtual block device 
  11.105 +    block-destroy <DomId> <DevId>  Destroy a domain's virtual block device
  11.106 +    block-list    <DomId>          List virtual block devices for a domain
  11.107 +    block-refresh <DomId> <DevId>  Refresh a virtual block device for a domain
  11.108 +    network-limit   <DomId> <Vif> <Credit> <Period>
  11.109 +        Limit the transmission rate of a virtual network interface
  11.110 +    network-list    <DomId>        List virtual network interfaces for a domain
  11.111 +
  11.112 +For a short list of subcommands run 'xm help'
  11.113 +For more help on xm see the xm(1) man page
  11.114 +For more help on xm create, see the xmdomain.cfg(5) man page"""
  11.115 +
  11.116 +####################################################################
  11.117 +#
  11.118 +#  Utility functions
  11.119 +#
  11.120 +####################################################################
  11.121 +
  11.122 +def arg_check(args,num,name):
  11.123 +    if len(args) < num:
  11.124 +        err("'xm %s' requires %s argument(s)!\n" % (name, num))
  11.125 +        usage(name)
  11.126  
  11.127  def unit(c):
  11.128      if not c.isalpha():
  11.129 @@ -49,692 +134,318 @@ def int_unit(str, dest):
  11.130      else:
  11.131          return value * (base / dst_base)
  11.132  
  11.133 -class Group:
  11.134 -
  11.135 -    name = ""
  11.136 -    info = ""
  11.137 -    
  11.138 -    def __init__(self, xm):
  11.139 -        self.xm = xm
  11.140 -        self.progs = {}
  11.141 -
  11.142 -    def addprog(self, prog):
  11.143 -        self.progs[prog.name] = prog
  11.144 -
  11.145 -    def getprog(self, name):
  11.146 -        return self.progs.get(name)
  11.147 +def err(msg):
  11.148 +    print >>sys.stderr, "Error:", msg
  11.149  
  11.150 -    def proglist(self):
  11.151 -        kl = self.progs.keys()
  11.152 -        kl.sort()
  11.153 -        return [ self.getprog(k) for k in kl ]
  11.154 -
  11.155 -    def help(self, args):
  11.156 -        if self.info:
  11.157 -            print 
  11.158 -            print self.info
  11.159 -            print
  11.160 -        else:
  11.161 -            print
  11.162 -        
  11.163 -    def shortHelp(self, args):
  11.164 -        self.help(args)
  11.165 -        for p in self.proglist():
  11.166 -            p.shortHelp(args)
  11.167 -
  11.168 -class Prog:
  11.169 -    """Base class for sub-programs.
  11.170 -    """
  11.171 +def handle_xend_error(cmd, dom, ex):
  11.172 +    error = str(ex)
  11.173 +    if error == "Not found" and dom != None:
  11.174 +        err("Domain '%s' not found when running 'xm %s'" % (dom, cmd))
  11.175 +        sys.exit(1)
  11.176 +    else:
  11.177 +        raise ex
  11.178 +    
  11.179  
  11.180 -    """Program group it belongs to"""
  11.181 -    group = 'all'
  11.182 -    """Program name."""
  11.183 -    name = '??'
  11.184 -    """Short program info."""
  11.185 -    info = ''
  11.186 -
  11.187 -    def __init__(self, xm):
  11.188 -        self.xm = xm
  11.189 -
  11.190 -    def err(self, msg):
  11.191 -        self.xm.err(msg)
  11.192 -
  11.193 -    def help(self, args):
  11.194 -        self.shortHelp(args)
  11.195 -
  11.196 -    def shortHelp(self, args):
  11.197 -        print "%-14s %s" % (self.name, self.info)
  11.198 +#########################################################################
  11.199 +#
  11.200 +#  Main xm functions
  11.201 +#
  11.202 +#########################################################################
  11.203  
  11.204 -    def main(self, args):
  11.205 -        """Program main entry point.
  11.206 -        """
  11.207 -        pass
  11.208 -
  11.209 -
  11.210 -class ProgUnknown(Prog):
  11.211 +def xm_create(args):
  11.212 +    from xen.xm import create
  11.213 +    # ugly hack because the opt parser apparently wants
  11.214 +    # the subcommand name just to throw it away!
  11.215 +    args.insert(0,"bogus")
  11.216 +    create.main(args)
  11.217  
  11.218 -    name = 'unknown'
  11.219 -    info = ''
  11.220 -    
  11.221 -    def help(self, args):
  11.222 -        self.xm.err("Unknown command: %s\nTry '%s help' for more information."
  11.223 -                    % (args[0], self.xm.name))
  11.224 -
  11.225 -    main = help
  11.226 -
  11.227 -class Xm:
  11.228 -    """Main application.
  11.229 -    """
  11.230 -
  11.231 -    def __init__(self):
  11.232 -        self.name = 'xm'
  11.233 -        self.unknown = ProgUnknown(self)
  11.234 -        self.progs = {}
  11.235 -        self.groups = {}
  11.236 +def xm_save(args):
  11.237 +    arg_check(args,2,"save")
  11.238  
  11.239 -    def err(self, msg):
  11.240 -        print >>sys.stderr, "Error:", msg
  11.241 -        sys.exit(1)
  11.242 -
  11.243 -    def main(self, args):
  11.244 -        try:
  11.245 -            self.main_call(args)
  11.246 -        except socket.error, ex:
  11.247 -            print >>sys.stderr, ex
  11.248 -            self.err("Error connecting to xend, is xend running?")
  11.249 -        except XendError, ex:
  11.250 -            self.err(str(ex))
  11.251 +    dom = args[0] # TODO: should check if this exists
  11.252 +    savefile = os.path.abspath(args[1])
  11.253 +    
  11.254 +    from xen.xend.XendClient import server
  11.255 +    server.xend_domain_save(dom, savefile)
  11.256 +    
  11.257 +def xm_restore(args):
  11.258 +    arg_check(args,1,"restore")
  11.259  
  11.260 -    def main_call(self, args):
  11.261 -        """Main entry point. Dispatches to the progs.
  11.262 -        """
  11.263 -        self.name = args[0]
  11.264 -        if len(args) < 2:
  11.265 -        	args.append('help')
  11.266 -	help = self.helparg(args)
  11.267 -        p = self.getprog(args[1], self.unknown)
  11.268 -        if help or len(args) < 2: 
  11.269 -            p.help(args[1:])
  11.270 -        else:
  11.271 -            p.main(args[1:])
  11.272 +    savefile = os.path.abspath(args[0])
  11.273  
  11.274 -    def helparg(self, args):
  11.275 -        for a in args:
  11.276 -            if a in ['-h', '--help']:
  11.277 -                return 1
  11.278 -        return 0
  11.279 -
  11.280 -    def prog(self, pklass):
  11.281 -        """Add a sub-program.
  11.282 -
  11.283 -        pklass  program class (Prog subclass)
  11.284 -        """
  11.285 -        p = pklass(self)
  11.286 -        self.progs[p.name] = p
  11.287 -        self.getgroup(p.group).addprog(p)
  11.288 -        return p
  11.289 +    from xen.xend.XendClient import server
  11.290 +    info = server.xend_domain_restore(savefile)
  11.291 +    PrettyPrint.prettyprint(info)
  11.292 +    id = sxp.child_value(info, 'id')
  11.293 +    if id is not None:
  11.294 +        server.xend_domain_unpause(id)
  11.295  
  11.296 -    def getprog(self, name, val=None):
  11.297 -        """Get a sub-program.
  11.298 -        name  Name of the sub-program (or optionally, an unambiguous
  11.299 -              prefix of its name)
  11.300 -        val   Default return value if no (unique) match is found
  11.301 -        """
  11.302 -
  11.303 -        match = None
  11.304 -        for progname in self.progs.keys():
  11.305 -            if progname == name:
  11.306 -                match = progname
  11.307 -                break
  11.308 -            if progname.startswith(name):
  11.309 -                if not match:
  11.310 -                    match = progname
  11.311 -                else:
  11.312 -                    return val # name is ambiguous - bail out
  11.313 -
  11.314 -        return self.progs.get(match, val)
  11.315 +def xm_migrate(args):
  11.316 +    # TODO: arg_check
  11.317 +    from xen.xm import migrate
  11.318 +    # ugly hack because the opt parser apparently wants
  11.319 +    # the subcommand name just to throw it away!
  11.320 +    args.insert(0,"bogus")
  11.321 +    migrate.main(args)
  11.322  
  11.323 -    def group(self, klass):
  11.324 -        g = klass(self)
  11.325 -        self.groups[g.name] = g
  11.326 -        return g
  11.327 -
  11.328 -    def getgroup(self, name):
  11.329 -        return self.groups[name]
  11.330 -
  11.331 -    def grouplist(self):
  11.332 -        kl = self.groups.keys()
  11.333 -        kl.sort()
  11.334 -        return [ self.getgroup(k) for k in kl ]
  11.335 -        
  11.336 -# Create the application object, then add the sub-program classes.
  11.337 -xm = Xm()
  11.338 -
  11.339 -class GroupAll(Group):
  11.340 -
  11.341 -    name = "all"
  11.342 -    info = ""
  11.343 -
  11.344 -xm.group(GroupAll)
  11.345 -
  11.346 -class GroupDomain(Group):
  11.347 +def xm_list(args):
  11.348 +    use_long = 0
  11.349 +    show_vcpus = 0
  11.350 +    (options, params) = getopt(args, 'lv', ['long','vcpus'])
  11.351 +    
  11.352 +    n = len(params)
  11.353 +    for (k, v) in options:
  11.354 +        if k in ['-l', '--long']:
  11.355 +            use_long = 1
  11.356 +        if k in ['-v', '--vcpus']:
  11.357 +            show_vcpus = 1
  11.358  
  11.359 -    name = "domain"
  11.360 -    info = "Commands on domains:"
  11.361 -    
  11.362 -xm.group(GroupDomain)
  11.363 -
  11.364 -class GroupScheduler(Group):
  11.365 -
  11.366 -    name = "scheduler"
  11.367 -    info = "Comands controlling scheduling:"
  11.368 -
  11.369 -xm.group(GroupScheduler)
  11.370 -
  11.371 -class GroupHost(Group):
  11.372 -
  11.373 -    name = "host"
  11.374 -    info = "Commands related to the xen host (node):"
  11.375 -
  11.376 -xm.group(GroupHost)
  11.377 -
  11.378 -class GroupConsole(Group):
  11.379 -
  11.380 -    name = "console"
  11.381 -    info = "Commands related to consoles:"
  11.382 -
  11.383 -xm.group(GroupConsole)
  11.384 -
  11.385 -class GroupVbd(Group):
  11.386 -
  11.387 -    name = "vbd"
  11.388 -    info = "Commands related to virtual block devices:"
  11.389 -
  11.390 -xm.group(GroupVbd)
  11.391 -
  11.392 -class GroupVif(Group):
  11.393 -
  11.394 -    name = "vif"
  11.395 -    info = "Commands related to virtual network interfaces:"
  11.396 -
  11.397 -xm.group(GroupVif)
  11.398 -
  11.399 -class ProgHelp(Prog):
  11.400 +    domsinfo = []
  11.401 +    from xen.xend.XendClient import server
  11.402 +    if n == 0:
  11.403 +        doms = server.xend_domains()
  11.404 +        doms.sort()
  11.405 +    else:
  11.406 +        doms = params
  11.407 +    for dom in doms:
  11.408 +        info = server.xend_domain(dom)
  11.409 +        domsinfo.append(parse_doms_info(info))
  11.410 +               
  11.411 +    if use_long:
  11.412 +        # this actually seems like a bad idea, as it just dumps sexp out
  11.413 +        PrettyPrint.prettyprint(info)
  11.414 +    elif show_vcpus:
  11.415 +        xm_show_vcpus(domsinfo)
  11.416 +    else:
  11.417 +        xm_brief_list(domsinfo)
  11.418  
  11.419 -    name = "help"
  11.420 -    info = "Print help."
  11.421 -    
  11.422 -    def help(self, args):
  11.423 -        if len(args) == 2:
  11.424 -            name = args[1]
  11.425 -            p = self.xm.getprog(name)
  11.426 -            if p:
  11.427 -                p.help(args[1:])
  11.428 -            else:
  11.429 -                print '%s: Unknown command: %s' % (self.name, name)
  11.430 -        else:
  11.431 -            for g in self.xm.grouplist():
  11.432 -                g.shortHelp(args)
  11.433 -            print "\nTry '%s help CMD' for help on CMD" % self.xm.name
  11.434 -
  11.435 -    main = help
  11.436 -
  11.437 -xm.prog(ProgHelp)
  11.438 -
  11.439 -class ProgCreate(Prog):
  11.440 -
  11.441 -    group = 'domain'
  11.442 -    name = "create"
  11.443 -    info = """Create a domain."""
  11.444 -
  11.445 -    def help(self, args):
  11.446 -        create.main([args[0], '-h'])
  11.447 -
  11.448 -    def main(self, args):
  11.449 -        create.main(args)
  11.450 -
  11.451 -xm.prog(ProgCreate)
  11.452 -
  11.453 -class ProgSave(Prog):
  11.454 -    group = 'domain'
  11.455 -    name = "save"
  11.456 -    info = """Save domain state (and config) to file."""
  11.457 -
  11.458 -    def help(self, args):
  11.459 -        print args[0], "DOM FILE"
  11.460 -        print """\nSave domain with id DOM to FILE."""
  11.461 +def parse_doms_info(info):
  11.462 +    dominfo = {}
  11.463 +    dominfo['dom'] = int(sxp.child_value(info, 'id', '-1'))
  11.464 +    dominfo['name'] = sxp.child_value(info, 'name', '??')
  11.465 +    dominfo['mem'] = int(sxp.child_value(info, 'memory', '0'))
  11.466 +    dominfo['cpu'] = str(sxp.child_value(info, 'cpu', '0'))
  11.467 +    dominfo['vcpus'] = int(sxp.child_value(info, 'vcpus', '0'))
  11.468 +    # if there is more than 1 cpu, the value doesn't mean much
  11.469 +    if dominfo['vcpus'] > 1:
  11.470 +        dominfo['cpu'] = '-'
  11.471 +    dominfo['state'] = sxp.child_value(info, 'state', '??')
  11.472 +    dominfo['cpu_time'] = float(sxp.child_value(info, 'cpu_time', '0'))
  11.473 +    # security identifiers
  11.474 +    if ((int(sxp.child_value(info, 'ssidref', '0'))) != 0):
  11.475 +        dominfo['ssidref1'] =  int(sxp.child_value(info, 'ssidref', '0')) & 0xffff
  11.476 +        dominfo['ssidref2'] = (int(sxp.child_value(info, 'ssidref', '0')) >> 16) & 0xffff
  11.477 +    # get out the vcpu information
  11.478 +    dominfo['vcpulist'] = []
  11.479 +    vcpu_to_cpu = sxp.child_value(info, 'vcpu_to_cpu', '-1').split('|')
  11.480 +    cpumap = sxp.child_value(info, 'cpumap', [])
  11.481 +    mask = ((int(sxp.child_value(info, 'vcpus', '0')))**2) - 1
  11.482 +    count = 0
  11.483 +    for cpu in vcpu_to_cpu:
  11.484 +        vcpuinfo = {}
  11.485 +        vcpuinfo['name']   = sxp.child_value(info, 'name', '??')
  11.486 +        vcpuinfo['dom']    = int(sxp.child_value(info, 'id', '-1'))
  11.487 +        vcpuinfo['vcpu']   = int(count)
  11.488 +        vcpuinfo['cpu']    = int(cpu)
  11.489 +        vcpuinfo['cpumap'] = int(cpumap[count])&mask
  11.490 +        count = count + 1
  11.491 +        dominfo['vcpulist'].append(vcpuinfo)
  11.492 +    return dominfo
  11.493          
  11.494 -    def main(self, args):
  11.495 -        if len(args) < 3: self.err("%s: Missing arguments" % args[0])
  11.496 -        dom = args[1]
  11.497 -        savefile = os.path.abspath(args[2])
  11.498 -        server.xend_domain_save(dom, savefile)
  11.499 -
  11.500 -xm.prog(ProgSave)
  11.501 -
  11.502 -class ProgRestore(Prog):
  11.503 -    group = 'domain'
  11.504 -    name = "restore"
  11.505 -    info = """Create a domain from a saved state."""
  11.506 +def xm_brief_list(domsinfo):
  11.507 +    print 'Name              Id  Mem(MB)  CPU VCPU(s)  State  Time(s)'
  11.508 +    for dominfo in domsinfo:
  11.509 +        if dominfo.has_key("ssidref1"):
  11.510 +            print ("%(name)-16s %(dom)3d  %(mem)7d  %(cpu)3s  %(vcpus)5d   %(state)5s  %(cpu_time)7.1f     %s:%(ssidref2)02x/p:%(ssidref1)02x" % dominfo)
  11.511 +        else:
  11.512 +            print ("%(name)-16s %(dom)3d  %(mem)7d  %(cpu)3s  %(vcpus)5d   %(state)5s  %(cpu_time)7.1f" % dominfo)
  11.513  
  11.514 -    def help(self, args):
  11.515 -        print args[0], "FILE"
  11.516 -        print "\nRestore a domain from FILE."
  11.517 -    
  11.518 -    def main(self, args):
  11.519 -        if len(args) < 2: self.err("%s: Missing arguments" % args[0])
  11.520 -        savefile = os.path.abspath(args[1])
  11.521 -        info = server.xend_domain_restore(savefile)
  11.522 -        PrettyPrint.prettyprint(info)
  11.523 -        id = sxp.child_value(info, 'id')
  11.524 -        if id is not None:
  11.525 -            server.xend_domain_unpause(id)
  11.526 +def xm_show_vcpus(domsinfo):
  11.527 +    print 'Name              Id  VCPU  CPU  CPUMAP'
  11.528 +    for dominfo in domsinfo:
  11.529 +        for vcpuinfo in dominfo['vcpulist']:
  11.530 +            print ("%(name)-16s %(dom)3d  %(vcpu)4d  %(cpu)3d  0x%(cpumap)x" %
  11.531 +                   vcpuinfo)
  11.532  
  11.533 -xm.prog(ProgRestore)
  11.534 -
  11.535 -class ProgMigrate(Prog):
  11.536 -    group = 'domain'
  11.537 -    name = "migrate"
  11.538 -    info = """Migrate a domain to another machine."""
  11.539 -
  11.540 -    def help(self, args):
  11.541 -        migrate.help([self.name] + args)
  11.542 -    
  11.543 -    def main(self, args):
  11.544 -        migrate.main(args)
  11.545 -
  11.546 -xm.prog(ProgMigrate)
  11.547 +def xm_vcpu_list(args):
  11.548 +    args.insert(0,"-v")
  11.549 +    xm_list(args)
  11.550  
  11.551 -class ProgList(Prog):
  11.552 -    group = 'domain'
  11.553 -    name = "list"
  11.554 -    info = """List information about domains."""
  11.555 -
  11.556 -    short_options = 'lv'
  11.557 -    long_options = ['long','vcpus']
  11.558 -
  11.559 -    def help(self, args):
  11.560 -        if help:
  11.561 -            print args[0], '[options] [DOM...]'
  11.562 -            print """\nGet information about domains.
  11.563 -            Either all domains or the domains given.
  11.564 +def xm_destroy(args):
  11.565 +    arg_check(args,1,"destroy")
  11.566  
  11.567 -            -l, --long   Get more detailed information.
  11.568 -            -v, --vcpus  Show VCPU to CPU mapping.
  11.569 -            """
  11.570 -            return
  11.571 -        
  11.572 -    def main(self, args):
  11.573 -        use_long = 0
  11.574 -        show_vcpus = 0
  11.575 -        (options, params) = getopt(args[1:],
  11.576 -                                   self.short_options,
  11.577 -                                   self.long_options)
  11.578 -        n = len(params)
  11.579 -        for (k, v) in options:
  11.580 -            if k in ['-l', '--long']:
  11.581 -                use_long = 1
  11.582 -            if k in ['-v', '--vcpus']:
  11.583 -                show_vcpus = 1
  11.584 -                
  11.585 -        if n == 0:
  11.586 -            doms = server.xend_domains()
  11.587 -            doms.sort()
  11.588 -        else:
  11.589 -            doms = params
  11.590 +    from xen.xm import destroy
  11.591 +    # ugly hack because the opt parser apparently wants
  11.592 +    # the subcommand name just to throw it away!
  11.593 +    args.insert(0,"bogus")
  11.594 +    destroy.main(args)
  11.595              
  11.596 -        if use_long:
  11.597 -            self.long_list(doms)
  11.598 -        elif show_vcpus:
  11.599 -            self.show_vcpus(doms)
  11.600 -        else:
  11.601 -            self.brief_list(doms)
  11.602 +# TODO: make reboot do the right thing, right now
  11.603 +# reboot and shutdown are exactly the same
  11.604 +def xm_reboot(args):
  11.605 +    arg_check(args,1,"reboot")
  11.606 +    # ugly hack because the opt parser apparently wants
  11.607 +    # the subcommand name just to throw it away!
  11.608 +    args.insert(0,"bogus")
  11.609 +    from xen.xm import shutdown
  11.610 +    shutdown.main(args)
  11.611  
  11.612 -    def brief_list(self, doms):
  11.613 -        print 'Name              Id  Mem(MB)  CPU VCPU(s)  State  Time(s)'
  11.614 -        for dom in doms:
  11.615 -            info = server.xend_domain(dom)
  11.616 -            d = {}
  11.617 -            d['dom'] = int(sxp.child_value(info, 'id', '-1'))
  11.618 -            d['name'] = sxp.child_value(info, 'name', '??')
  11.619 -            d['mem'] = int(sxp.child_value(info, 'memory', '0'))
  11.620 -            d['cpu'] = str(sxp.child_value(info, 'cpu', '0'))
  11.621 -            d['vcpus'] = int(sxp.child_value(info, 'vcpus', '0'))
  11.622 -            d['state'] = sxp.child_value(info, 'state', '??')
  11.623 -            d['cpu_time'] = float(sxp.child_value(info, 'cpu_time', '0'))
  11.624 -            if d['vcpus'] > 1:
  11.625 -                d['cpu'] = '-'
  11.626 -            if ((int(sxp.child_value(info, 'ssidref', '0'))) != 0):
  11.627 -                d['ssidref1'] =  int(sxp.child_value(info, 'ssidref', '0')) & 0xffff
  11.628 -                d['ssidref2'] = (int(sxp.child_value(info, 'ssidref', '0')) >> 16) & 0xffff
  11.629 -                print ("%(name)-16s %(dom)3d  %(mem)7d  %(cpu)3s  %(vcpus)5d   %(state)5s  %(cpu_time)7.1f     %s:%(ssidref2)02x/p:%(ssidref1)02x" % d)
  11.630 -            else:
  11.631 -                print ("%(name)-16s %(dom)3d  %(mem)7d  %(cpu)3s  %(vcpus)5d   %(state)5s  %(cpu_time)7.1f" % d)
  11.632 +def xm_shutdown(args):
  11.633 +    arg_check(args,1,"shutdown")
  11.634  
  11.635 -    def show_vcpus(self, doms):
  11.636 -        print 'Name              Id  VCPU  CPU  CPUMAP'
  11.637 -        for dom in doms:
  11.638 -            info = server.xend_domain(dom)
  11.639 -            vcpu_to_cpu = sxp.child_value(info, 'vcpu_to_cpu', '-1').split('|')
  11.640 -            cpumap = sxp.child_value(info, 'cpumap', [])
  11.641 -            mask = ((int(sxp.child_value(info, 'vcpus', '0')))**2) - 1
  11.642 -            count = 0
  11.643 -            for cpu in vcpu_to_cpu:
  11.644 -                d = {}
  11.645 -                d['name']   = sxp.child_value(info, 'name', '??')
  11.646 -                d['dom']    = int(sxp.child_value(info, 'id', '-1'))
  11.647 -                d['vcpu']   = int(count)
  11.648 -                d['cpu']    = int(cpu)
  11.649 -                d['cpumap'] = int(cpumap[count])&mask
  11.650 -                count = count + 1
  11.651 -                print ("%(name)-16s %(dom)3d  %(vcpu)4d  %(cpu)3d  0x%(cpumap)x" % d)
  11.652 +    # ugly hack because the opt parser apparently wants
  11.653 +    # the subcommand name just to throw it away!
  11.654 +    args.insert(0,"bogus")
  11.655 +    from xen.xm import shutdown
  11.656 +    shutdown.main(args)
  11.657 +
  11.658 +def xm_sysrq(args):
  11.659 +    from xen.xm import sysrq
  11.660 +    # ugly hack because the opt parser apparently wants
  11.661 +    # the subcommand name just to throw it away!
  11.662 +    args.insert(0,"bogus")
  11.663 +    sysrq.main(args)
  11.664  
  11.665 -    def long_list(self, doms):
  11.666 -        for dom in doms:
  11.667 -            info = server.xend_domain(dom)
  11.668 -            PrettyPrint.prettyprint(info)
  11.669 -
  11.670 -xm.prog(ProgList)
  11.671 +def xm_pause(args):
  11.672 +    arg_check(args, 1, "pause")
  11.673 +    dom = args[0]
  11.674  
  11.675 -class ProgDestroy(Prog):
  11.676 -    group = 'domain'
  11.677 -    name = "destroy"
  11.678 -    info = """Terminate a domain immediately."""
  11.679 -
  11.680 -    def help(self, args):
  11.681 -        destroy.main([args[0], '-h'])
  11.682 -
  11.683 -    def main(self, args):
  11.684 -        destroy.main(args)
  11.685 -
  11.686 -xm.prog(ProgDestroy)
  11.687 +    from xen.xend.XendClient import server
  11.688 +    server.xend_domain_pause(dom)
  11.689  
  11.690 -class ProgShutdown(Prog):
  11.691 -    group = 'domain'
  11.692 -    name = "shutdown"
  11.693 -    info = """Shutdown a domain."""
  11.694 -
  11.695 -    def help(self, args):
  11.696 -        shutdown.main([args[0], '-h'])
  11.697 -    
  11.698 -    def main(self, args):
  11.699 -        shutdown.main(args)
  11.700 +def xm_unpause(args):
  11.701 +    arg_check(args, 1, "unpause")
  11.702 +    dom = args[0]
  11.703  
  11.704 -xm.prog(ProgShutdown)
  11.705 +    from xen.xend.XendClient import server
  11.706 +    server.xend_domain_unpause(dom)
  11.707  
  11.708 -class ProgSysrq(Prog):
  11.709 -    group = 'domain'
  11.710 -    name = "sysrq"
  11.711 -    info = """Send a sysrq to a domain."""
  11.712 -
  11.713 -    def help(self, args):
  11.714 -        sysrq.main([args[0], '-h'])
  11.715 -    
  11.716 -    def main(self, args):
  11.717 -        sysrq.main(args)
  11.718 +#############################################################
  11.719  
  11.720 -xm.prog(ProgSysrq)
  11.721 -
  11.722 -class ProgPause(Prog):
  11.723 -    group = 'domain'
  11.724 -    name = "pause"
  11.725 -    info = """Pause execution of a domain."""
  11.726 -
  11.727 -    def help(self, args):
  11.728 -        print args[0], 'DOM'
  11.729 -        print '\nPause execution of domain DOM.'
  11.730 -
  11.731 -    def main(self, args):
  11.732 -        if len(args) < 2: self.err("%s: Missing domain" % args[0])
  11.733 -        dom = args[1]
  11.734 -        server.xend_domain_pause(dom)
  11.735 -
  11.736 -xm.prog(ProgPause)
  11.737 +def cpu_make_map(cpulist):
  11.738 +    cpus = []
  11.739 +    cpumap = 0
  11.740 +    for c in cpulist.split(','):
  11.741 +        if c.find('-') != -1:
  11.742 +            (x,y) = c.split('-')
  11.743 +            for i in range(int(x),int(y)+1):
  11.744 +                cpus.append(int(i))
  11.745 +        else:
  11.746 +            cpus.append(int(c))
  11.747 +    cpus.sort()
  11.748 +    for c in cpus:
  11.749 +        cpumap = cpumap | 1<<c
  11.750  
  11.751 -class ProgUnpause(Prog):
  11.752 -    group = 'domain'
  11.753 -    name = "unpause"
  11.754 -    info = """Unpause a paused domain."""
  11.755 -
  11.756 -    def help(self, args):
  11.757 -        print args[0], 'DOM'
  11.758 -        print '\nUnpause execution of domain DOM.'
  11.759 -
  11.760 -    def main(self, args):
  11.761 -        if len(args) < 2: self.err("%s: Missing domain" % args[0])
  11.762 -        dom = args[1]
  11.763 -        server.xend_domain_unpause(dom)
  11.764 -
  11.765 -xm.prog(ProgUnpause)
  11.766 -
  11.767 -class ProgPincpu(Prog):
  11.768 -    group = 'domain'
  11.769 -    name = "pincpu"
  11.770 -    info = """Set which cpus a VCPU can use. """
  11.771 -
  11.772 -    def help(self, args):
  11.773 -        print args[0],'DOM VCPU CPUS'
  11.774 -        print '\nSet which cpus VCPU in domain DOM can use.'
  11.775 +    return cpumap
  11.776  
  11.777 -    # convert list of cpus to bitmap integer value
  11.778 -    def make_map(self, cpulist):
  11.779 -        cpus = []
  11.780 -        cpumap = 0
  11.781 -        for c in cpulist.split(','):
  11.782 -            if c.find('-') != -1:
  11.783 -                (x,y) = c.split('-')
  11.784 -                for i in range(int(x),int(y)+1):
  11.785 -                    cpus.append(int(i))
  11.786 -            else:
  11.787 -                cpus.append(int(c))
  11.788 -        cpus.sort()
  11.789 -        for c in cpus:
  11.790 -            cpumap = cpumap | 1<<c
  11.791 -
  11.792 -        return cpumap
  11.793 +def xm_cpus_set(args):
  11.794 +    arg_check(args, 3, "cpus-set")
  11.795 +    
  11.796 +    dom  = args[0]
  11.797 +    vcpu = int(args[1])
  11.798 +    cpumap = cpu_make_map(args[2])
  11.799 +    
  11.800 +    from xen.xend.XendClient import server
  11.801 +    server.xend_domain_pincpu(dom, vcpu, cpumap)
  11.802  
  11.803 -    def main(self, args):
  11.804 -        if len(args) != 4: self.err("%s: Invalid argument(s)" % args[0])
  11.805 -        dom  = args[1]
  11.806 -        vcpu = int(args[2])
  11.807 -        cpumap  = self.make_map(args[3]);
  11.808 -        server.xend_domain_pincpu(dom, vcpu, cpumap)
  11.809 -
  11.810 -xm.prog(ProgPincpu)
  11.811 +def xm_mem_max(args):
  11.812 +    arg_check(args, 2, "mem-max")
  11.813 +    
  11.814 +    dom = args[0]
  11.815 +    mem = int_unit(args[1], 'm')
  11.816  
  11.817 -class ProgMaxmem(Prog):
  11.818 -    group = 'domain'
  11.819 -    name = 'maxmem'
  11.820 -    info = """Set domain memory limit."""
  11.821 -
  11.822 -    def help(self, args):
  11.823 -        print args[0], "DOM MEMORY"
  11.824 -        print "\nSet the memory limit for domain DOM to MEMORY megabytes."
  11.825 -
  11.826 -    def main(self, args):
  11.827 -        if len(args) != 3: self.err("%s: Invalid argument(s)" % args[0])
  11.828 -        dom = args[1]
  11.829 -        mem = int_unit(args[2], 'm')
  11.830 -        server.xend_domain_maxmem_set(dom, mem)
  11.831 -
  11.832 -xm.prog(ProgMaxmem)
  11.833 +    from xen.xend.XendClient import server
  11.834 +    server.xend_domain_maxmem_set(dom, mem)
  11.835 +    
  11.836 +def xm_mem_set(args):
  11.837 +    arg_check(args, 2, "mem-set")
  11.838 +    
  11.839 +    dom = args[0]
  11.840 +    mem_target = int_unit(args[1], 'm')
  11.841  
  11.842 -class ProgSetMem(Prog):
  11.843 -    group = 'domain'
  11.844 -    name  = 'set-mem'
  11.845 -    info  = """Set the domain's memory footprint using the balloon driver."""
  11.846 -
  11.847 -    def help(self, args):
  11.848 -        print args[0], "DOM MEMORY_TARGET"
  11.849 -        print """\nRequest domain DOM to adjust its memory footprint to
  11.850 -MEMORY_TARGET megabytes"""
  11.851 -
  11.852 -    def main(self, args):
  11.853 -        if len(args) != 3: self.err("%s: Invalid argument(s)" % args[0])
  11.854 -        dom = args[1]
  11.855 -        mem_target = int_unit(args[2], 'm')
  11.856 -        server.xend_domain_mem_target_set(dom, mem_target)
  11.857 -
  11.858 -xm.prog(ProgSetMem)
  11.859 -
  11.860 -class ProgVcpuhotplug(Prog):
  11.861 -    group = 'domain'
  11.862 -    name  = 'vcpu-hotplug'
  11.863 -    info  = """Enable or disable a VCPU in a domain."""
  11.864 -
  11.865 -    def help(self, args):
  11.866 -        print args[0], "DOM VCPU [0|1]"
  11.867 -        print """\nRequest virtual processor VCPU to be disabled or enabled in
  11.868 -domain DOM"""
  11.869 -
  11.870 -    def main(self, args):
  11.871 -        if len(args) != 4: self.err("%s: Invalid arguments(s)" % args[0])
  11.872 -        name = args[1]
  11.873 -        vcpu = int(args[2])
  11.874 -        state = int(args[3])
  11.875 -        dom = server.xend_domain(name)
  11.876 -        id = sxp.child_value(dom, 'id')
  11.877 -        server.xend_domain_vcpu_hotplug(id, vcpu, state)
  11.878 -
  11.879 -xm.prog(ProgVcpuhotplug)
  11.880 -
  11.881 -class ProgDomid(Prog):
  11.882 -    group = 'domain'
  11.883 -    name = 'domid'
  11.884 -    info = 'Convert a domain name to a domain id.'
  11.885 +    from xen.xend.XendClient import server
  11.886 +    server.xend_domain_mem_target_set(dom, mem_target)
  11.887 +    
  11.888 +# TODO: why does this lookup by name?  and what if that fails!?
  11.889 +def xm_vcpu_enable(args):
  11.890 +    arg_check(args, 2, "vcpu-enable")
  11.891 +    
  11.892 +    name = args[0]
  11.893 +    vcpu = int(args[1])
  11.894 +    
  11.895 +    from xen.xend.XendClient import server
  11.896 +    dom = server.xend_domain(name)
  11.897 +    id = sxp.child_value(dom, 'id')
  11.898 +    server.xend_domain_vcpu_hotplug(id, vcpu, 1)
  11.899  
  11.900 -    def help(self, args):
  11.901 -        print args[0], "DOM"
  11.902 -        print '\nGet the domain id for the domain with name DOM.'
  11.903 -        
  11.904 -    def main (self, args):
  11.905 -        if len(args) != 2: self.err("%s: Invalid argument(s)" % args[0])
  11.906 -        name = args[1]
  11.907 -        dom = server.xend_domain(name)
  11.908 -        print sxp.child_value(dom, 'id')
  11.909 -
  11.910 -xm.prog(ProgDomid)
  11.911 -
  11.912 -class ProgDomname(Prog):
  11.913 -    group = 'domain'
  11.914 -    name = 'domname'
  11.915 -    info = 'Convert a domain id to a domain name.'
  11.916 +def xm_vcpu_disable(args):
  11.917 +    arg_check(args, 2, "vcpu-disable")
  11.918 +    
  11.919 +    name = args[0]
  11.920 +    vcpu = int(args[1])
  11.921 +    
  11.922 +    from xen.xend.XendClient import server
  11.923 +    dom = server.xend_domain(name)
  11.924 +    id = sxp.child_value(dom, 'id')
  11.925 +    server.xend_domain_vcpu_hotplug(id, vcpu, 0)
  11.926  
  11.927 -    def help(self, args):
  11.928 -        print args[0], "DOM"
  11.929 -        print '\nGet the name for the domain with id DOM.'
  11.930 -        
  11.931 -    def main (self, args):
  11.932 -        if len(args) != 2: self.err("%s: Invalid argument(s)" % args[0])
  11.933 -        name = args[1]
  11.934 -        dom = server.xend_domain(name)
  11.935 -        print sxp.child_value(dom, 'name')
  11.936 -
  11.937 -xm.prog(ProgDomname)
  11.938 +def xm_domid(args):
  11.939 +    name = args[0]
  11.940  
  11.941 -class ProgBvt(Prog):
  11.942 -    group = 'scheduler'
  11.943 -    name = "bvt"
  11.944 -    info = """Set BVT scheduler parameters."""
  11.945 +    from xen.xend.XendClient import server
  11.946 +    dom = server.xend_domain(name)
  11.947 +    print sxp.child_value(dom, 'id')
  11.948      
  11.949 -    def help(self, args):
  11.950 -        print args[0], "DOM MCUADV WARPBACK WARPVALUE WARPL WARPU"
  11.951 -        print '\nSet Borrowed Virtual Time scheduler parameters.'
  11.952 +def xm_domname(args):
  11.953 +    name = args[0]
  11.954  
  11.955 -    def main(self, args):
  11.956 -        if len(args) != 7: self.err("%s: Invalid argument(s)" % args[0])
  11.957 -        dom = args[1]
  11.958 -        v = map(long, args[2:7])
  11.959 -        server.xend_domain_cpu_bvt_set(dom, *v)
  11.960 +    from xen.xend.XendClient import server
  11.961 +    dom = server.xend_domain(name)
  11.962 +    print sxp.child_value(dom, 'name')
  11.963  
  11.964 -xm.prog(ProgBvt)
  11.965 -
  11.966 -class ProgBvtslice(Prog):
  11.967 -    group = 'scheduler'
  11.968 -    name = "bvt_ctxallow"
  11.969 -    info = """Set the BVT scheduler context switch allowance."""
  11.970 +def xm_bvt(args):
  11.971 +    arg_check(args, 6, "bvt")
  11.972 +    dom = args[0]
  11.973 +    v = map(long, args[1:6])
  11.974 +    from xen.xend.XendClient import server
  11.975 +    server.xend_domain_cpu_bvt_set(dom, *v)
  11.976  
  11.977 -    def help(self, args):
  11.978 -        print args[0], 'CTX_ALLOW'
  11.979 -        print '\nSet Borrowed Virtual Time scheduler context switch allowance.'
  11.980 -
  11.981 -    def main(self, args):
  11.982 -        if len(args) < 2: self.err('%s: Missing context switch allowance'
  11.983 -                                                            % args[0])
  11.984 -        slice = int(args[1])
  11.985 -        server.xend_node_cpu_bvt_slice_set(slice)
  11.986 -
  11.987 -xm.prog(ProgBvtslice)
  11.988 +def xm_bvt_ctxallow(args):
  11.989 +    arg_check(args, 1, "bvt_ctxallow")
  11.990  
  11.991 -class ProgSedf(Prog):
  11.992 -    group = 'scheduler'
  11.993 -    name= "sedf"
  11.994 -    info = """Set simple EDF parameters."""
  11.995 -
  11.996 -    def help(self, args):
  11.997 -        print args[0], "DOM PERIOD SLICE LATENCY EXTRATIME WEIGHT"
  11.998 -        print "\nSet simple EDF parameters."
  11.999 +    slice = int(args[0])
 11.1000 +    from xen.xend.XendClient import server
 11.1001 +    server.xend_node_cpu_bvt_slice_set(slice)
 11.1002  
 11.1003 -    def main(self, args):
 11.1004 -	if len(args) != 7: self.err("%s: Invalid argument(s)" % args[0])
 11.1005 -	dom = args[1]
 11.1006 -	v = map(int, args[2:7])
 11.1007 -	server.xend_domain_cpu_sedf_set(dom, *v)
 11.1008 -
 11.1009 -xm.prog(ProgSedf)
 11.1010 -
 11.1011 -class ProgInfo(Prog):
 11.1012 -    group = 'host'
 11.1013 -    name = "info"
 11.1014 -    info = """Get information about the xen host."""
 11.1015 +def xm_sedf(args):
 11.1016 +    arg_check(args, 6, "sedf")
 11.1017 +    
 11.1018 +    dom = args[0]
 11.1019 +    v = map(int, args[1:5])
 11.1020 +    from xen.xend.XendClient import server
 11.1021 +    server.xend_domain_cpu_sedf_set(dom, *v)
 11.1022  
 11.1023 -    def main(self, args):
 11.1024 -        info = server.xend_node()
 11.1025 -        for x in info[1:]:
 11.1026 -            print "%-23s:" % x[0], x[1]
 11.1027 -
 11.1028 -xm.prog(ProgInfo)
 11.1029 -
 11.1030 -class ProgConsole(Prog):
 11.1031 -    group = 'console'
 11.1032 -    name = "console"
 11.1033 -    info = """Open a console to a domain."""
 11.1034 +def xm_info(args):
 11.1035 +    from xen.xend.XendClient import server
 11.1036 +    info = server.xend_node()
 11.1037      
 11.1038 -    def help(self, args):
 11.1039 -        print args[0], "DOM"
 11.1040 -        print "\nOpen a console to domain DOM."
 11.1041 +    for x in info[1:]:
 11.1042 +        print "%-23s:" % x[0], x[1]
 11.1043  
 11.1044 -    def main(self, args):
 11.1045 -        if len(args) < 2: self.err("%s: Missing domain" % args[0])
 11.1046 -        dom = args[1]
 11.1047 -        info = server.xend_domain(dom)
 11.1048 -        domid = int(sxp.child_value(info, 'id', '-1'))
 11.1049 -        cmd = "/usr/libexec/xen/xenconsole %d" % domid
 11.1050 -        os.execvp('/usr/libexec/xen/xenconsole', cmd.split())
 11.1051 +# TODO: remove as soon as console server shows up
 11.1052 +def xm_console(args):
 11.1053 +    arg_check(args,1,"console")
 11.1054  
 11.1055 -xm.prog(ProgConsole)
 11.1056 -
 11.1057 -class ProgCall(Prog):
 11.1058 -    name = "call"
 11.1059 -    info = "Call xend api functions."
 11.1060 +    dom = args[0]
 11.1061 +    from xen.xend.XendClient import server
 11.1062 +    info = server.xend_domain(dom)
 11.1063 +    domid = int(sxp.child_value(info, 'id', '-1'))
 11.1064 +    cmd = "/usr/libexec/xen/xenconsole %d" % domid
 11.1065 +    os.execvp('/usr/libexec/xen/xenconsole', cmd.split())
 11.1066 +    console = sxp.child(info, "console")
 11.1067  
 11.1068 -    def help (self, args):
 11.1069 -        print args[0], "function args..."
 11.1070 -        print """
 11.1071 -        Call a xend HTTP API function. The leading 'xend_' on the function
 11.1072 -can be omitted. See xen.xend.XendClient for the API functions.
 11.1073 -"""
 11.1074 -
 11.1075 -    def main(self, args):
 11.1076 -        xend_client_main(args)
 11.1077 -
 11.1078 -xm.prog(ProgCall)
 11.1079 -
 11.1080 -class ProgDmesg(Prog):
 11.1081 -    group = 'host'
 11.1082 -    name  =  "dmesg"
 11.1083 -    info  = """Read or clear Xen's message buffer."""
 11.1084 -
 11.1085 +def xm_dmesg(args):
 11.1086 +    
 11.1087      gopts = Opts(use="""[-c|--clear]
 11.1088  
 11.1089  Read Xen's message buffer (boot output, warning and error messages) or clear
 11.1090 @@ -744,161 +455,212 @@ its contents if the [-c|--clear] flag is
 11.1091      gopts.opt('clear', short='c',
 11.1092                fn=set_true, default=0,
 11.1093                use="Clear the contents of the Xen message buffer.")
 11.1094 -
 11.1095 -    short_options = ['-c']
 11.1096 -    long_options = ['--clear']
 11.1097 +    # Work around for gopts
 11.1098 +    args.insert(0,"bogus")
 11.1099 +    gopts.parse(args)
 11.1100 +    if not (1 <= len(args) <= 2):
 11.1101 +        err('Invalid arguments: ' + str(args))
 11.1102  
 11.1103 -    def help(self, args):
 11.1104 -        self.gopts.argv = args
 11.1105 -        self.gopts.usage()
 11.1106 -
 11.1107 -    def main(self, args):
 11.1108 -        self.gopts.parse(args)
 11.1109 -        if not (1 <= len(args) <=2):
 11.1110 -            self.gopts.err('Invalid arguments: ' + str(args))
 11.1111 +    from xen.xend.XendClient import server
 11.1112 +    if not gopts.vals.clear:
 11.1113 +        print server.xend_node_get_dmesg()
 11.1114 +    else:
 11.1115 +        server.xend_node_clear_dmesg()
 11.1116  
 11.1117 -        if not self.gopts.vals.clear:
 11.1118 -            print server.xend_node_get_dmesg()
 11.1119 -        else:
 11.1120 -            server.xend_node_clear_dmesg()
 11.1121 -
 11.1122 -xm.prog(ProgDmesg)
 11.1123 +def xm_log(args):
 11.1124 +    from xen.xend.XendClient import server
 11.1125 +    print server.xend_node_log()
 11.1126  
 11.1127 -class ProgLog(Prog):
 11.1128 -    group = 'host'
 11.1129 -    name  =  "log"
 11.1130 -    info  = """Print the xend log."""
 11.1131 +def xm_network_limit(args):
 11.1132 +    arg_check(args,4,"network-limit")
 11.1133 +    dom = args[0]
 11.1134 +    v = map(int, args[1:4])
 11.1135 +    from xen.xend.XendClient import server
 11.1136 +    server.xend_domain_vif_limit(dom, *v)
 11.1137  
 11.1138 -    def main(self, args):
 11.1139 -        print server.xend_node_log()
 11.1140 -
 11.1141 -xm.prog(ProgLog)
 11.1142 +def xm_network_list(args):
 11.1143 +    arg_check(args,1,"network-list")
 11.1144 +    dom = args[0]
 11.1145 +    from xen.xend.XendClient import server
 11.1146 +    for x in server.xend_domain_devices(dom, 'vif'):
 11.1147 +        sxp.show(x)
 11.1148 +        print
 11.1149  
 11.1150 -class ProgVifCreditLimit(Prog):
 11.1151 -    group = 'vif'
 11.1152 -    name= "vif-limit"
 11.1153 -    info = """Limit the transmission rate of a virtual network interface."""
 11.1154 +def xm_block_list(args):
 11.1155 +    arg_check(args,1,"block-list")
 11.1156 +    dom = args[0]
 11.1157 +    from xen.xend.XendClient import server
 11.1158 +    for x in server.xend_domain_devices(dom, 'vbd'):
 11.1159 +        sxp.show(x)
 11.1160 +        print
 11.1161  
 11.1162 -    def help(self, args):
 11.1163 -        print args[0], "DOMAIN VIF CREDIT_IN_BYTES PERIOD_IN_USECS"
 11.1164 -        print "\nSet the credit limit of a virtual network interface."
 11.1165 -
 11.1166 -    def main(self, args):
 11.1167 -        if len(args) != 5: self.err("%s: Invalid argument(s)" % args[0])
 11.1168 -        dom = args[1]
 11.1169 -        v = map(int, args[2:5])
 11.1170 -        server.xend_domain_vif_limit(dom, *v)
 11.1171 -
 11.1172 -xm.prog(ProgVifCreditLimit)
 11.1173 +def xm_block_create(args):
 11.1174 +    n = len(args)
 11.1175 +    if n < 4 or n > 5:
 11.1176 +        err("%s: Invalid argument(s)" % args[0])
 11.1177 +        usage("block-create")
 11.1178  
 11.1179 -class ProgVifList(Prog):
 11.1180 -    group = 'vif'
 11.1181 -    name  = 'vif-list'
 11.1182 -    info  = """List virtual network interfaces for a domain."""
 11.1183 +    dom = args[0]
 11.1184 +    vbd = ['vbd',
 11.1185 +           ['uname', args[1]],
 11.1186 +           ['dev',   args[2]],
 11.1187 +           ['mode',  args[3]]]
 11.1188 +    if n == 5:
 11.1189 +        vbd.append(['backend', args[4]])
 11.1190  
 11.1191 -    def help(self, args):
 11.1192 -        print args[0], "DOM"
 11.1193 -        print "\nList virtual network interfaces for domain DOM"
 11.1194 +    from xen.xend.XendClient import server
 11.1195 +    server.xend_domain_device_create(dom, vbd)
 11.1196 +
 11.1197 +def xm_block_refresh(args):
 11.1198 +    arg_check(args,2,"block-refresh")
 11.1199  
 11.1200 -    def main(self, args):
 11.1201 -        if len(args) != 2: self.err("%s: Invalid argument(s)" % args[0])
 11.1202 -        dom = args[1]
 11.1203 -        for x in server.xend_domain_devices(dom, 'vif'):
 11.1204 -            sxp.show(x)
 11.1205 -            print
 11.1206 +    dom = args[0]
 11.1207 +    dev = args[1]
 11.1208 +
 11.1209 +    from xen.xend.XendClient import server
 11.1210 +    server.xend_domain_device_refresh(dom, 'vbd', dev)
 11.1211  
 11.1212 -xm.prog(ProgVifList)
 11.1213 +def xm_block_destroy(args):
 11.1214 +    arg_check(args,2,"block-destroy")
 11.1215 +
 11.1216 +    dom = args[0]
 11.1217 +    dev = args[1]
 11.1218 +
 11.1219 +    from xen.xend.XendClient import server
 11.1220 +    server.xend_domain_device_destroy(dom, 'vbd', dev)
 11.1221  
 11.1222 -class ProgVbdList(Prog):
 11.1223 -    group = 'vbd'
 11.1224 -    name  = 'vbd-list'
 11.1225 -    info  = """List virtual block devices for a domain."""
 11.1226 -
 11.1227 -    def help(self, args):
 11.1228 -        print args[0], "DOM"
 11.1229 -        print "\nList virtual block devices for domain DOM"
 11.1230 -
 11.1231 -    def main(self, args):
 11.1232 -        if len(args) != 2: self.err("%s: Invalid argument(s)" % args[0])
 11.1233 -        dom = args[1]
 11.1234 -        for x in server.xend_domain_devices(dom, 'vbd'):
 11.1235 -            sxp.show(x)
 11.1236 -            print
 11.1237 +commands = {
 11.1238 +    # console commands
 11.1239 +    "console": xm_console,
 11.1240 +    # domain commands
 11.1241 +    "domid": xm_domid,
 11.1242 +    "domname": xm_domname,
 11.1243 +    "create": xm_create,
 11.1244 +    "destroy": xm_destroy,
 11.1245 +    "restore": xm_restore,
 11.1246 +    "save": xm_save,
 11.1247 +    "shutdown": xm_shutdown,
 11.1248 +    "reboot": xm_reboot,
 11.1249 +    "list": xm_list,
 11.1250 +    # memory commands
 11.1251 +    "mem-max": xm_mem_max,
 11.1252 +    "mem-set": xm_mem_set,
 11.1253 +    # cpu commands
 11.1254 +    "cpus-set": xm_cpus_set,
 11.1255 +#    "cpus-list": xm_cpus_list,
 11.1256 +    "vcpu-enable": xm_vcpu_enable,
 11.1257 +    "vcpu-disable": xm_vcpu_disable,
 11.1258 +    "vcpu-list": xm_vcpu_list,
 11.1259 +    # migration
 11.1260 +    "migrate": xm_migrate,
 11.1261 +    # special
 11.1262 +    "sysrq": xm_sysrq,
 11.1263 +    "pause": xm_pause,
 11.1264 +    "unpause": xm_unpause,
 11.1265 +    # host commands
 11.1266 +    "dmesg": xm_dmesg,
 11.1267 +    "info": xm_info,
 11.1268 +    "log": xm_log,
 11.1269 +    # scheduler
 11.1270 +    "bvt": xm_bvt,
 11.1271 +    "bvt_ctxallow": xm_bvt_ctxallow,
 11.1272 +    "sedf": xm_sedf,
 11.1273 +    # block
 11.1274 +    "block-create": xm_block_create,
 11.1275 +    "block-destroy": xm_block_destroy,
 11.1276 +    "block-list": xm_block_list,
 11.1277 +    "block-refresh": xm_block_refresh,
 11.1278 +    # network
 11.1279 +    "network-limit": xm_network_limit,
 11.1280 +    "network-list": xm_network_list
 11.1281 +    }
 11.1282  
 11.1283 -xm.prog(ProgVbdList)
 11.1284 -
 11.1285 -class ProgVbdCreate(Prog):
 11.1286 -    group = 'vbd'
 11.1287 -    name  = 'vbd-create'
 11.1288 -    info = """Create a new virtual block device for a domain"""
 11.1289 +aliases = {
 11.1290 +    "balloon": "mem-set",
 11.1291 +    "vif-list": "network-list",
 11.1292 +    "vif-limit": "network-limit",
 11.1293 +    "vbd-create": "block-create",
 11.1294 +    "vbd-destroy": "block-destroy",
 11.1295 +    "vbd-list": "block-list",
 11.1296 +    "vbd-refresh": "block-refresh",
 11.1297 +    }
 11.1298  
 11.1299 -    def help(self, args):
 11.1300 -        print args[0], "DOM UNAME DEV MODE [BACKEND]"
 11.1301 -        print """
 11.1302 -Create a virtual block device for a domain.
 11.1303 -
 11.1304 -  UNAME   - device to export, e.g. phy:hda2
 11.1305 -  DEV     - device name in the domain, e.g. sda1
 11.1306 -  MODE    - access mode: r for read, w for read-write
 11.1307 -  BACKEND - backend driver domain
 11.1308 -"""
 11.1309 +help = {
 11.1310 +    "--long": longhelp
 11.1311 +   }
 11.1312  
 11.1313 -    def main(self, args):
 11.1314 -        n = len(args)
 11.1315 -        if n < 5 or n > 6: self.err("%s: Invalid argument(s)" % args[0])
 11.1316 -        dom = args[1]
 11.1317 -        vbd = ['vbd',
 11.1318 -               ['uname', args[2]],
 11.1319 -               ['dev',   args[3]],
 11.1320 -               ['mode',  args[4]]]
 11.1321 -        if n == 6:
 11.1322 -            vbd.append(['backend', args[5]])
 11.1323 -        server.xend_domain_device_create(dom, vbd)
 11.1324 +def xm_lookup_cmd(cmd):
 11.1325 +    if commands.has_key(cmd):
 11.1326 +        return commands[cmd]
 11.1327 +    elif aliases.has_key(cmd):
 11.1328 +        deprecated(cmd,aliases[cmd])
 11.1329 +        return commands[aliases[cmd]]
 11.1330 +    else:
 11.1331 +        err('Sub Command %s not found!' % cmd)
 11.1332 +        usage()
 11.1333 +
 11.1334 +def deprecated(old,new):
 11.1335 +    err('Option %s is deprecated, and will be removed in future!!!' % old)
 11.1336 +    err('Option %s is the new replacement, see "xm help %s" for more info' % (new, new))
 11.1337  
 11.1338 -xm.prog(ProgVbdCreate)
 11.1339 +def usage(cmd=None):
 11.1340 +    if cmd == "full":
 11.1341 +        print fullhelp
 11.1342 +    elif help.has_key(cmd):
 11.1343 +        print help[cmd]
 11.1344 +    else:
 11.1345 +        print shorthelp
 11.1346 +    sys.exit(1)
 11.1347 +
 11.1348 +def main(argv=sys.argv):
 11.1349 +    if len(argv) < 2:
 11.1350 +        usage()
 11.1351 +    
 11.1352 +    if re.compile('-*help').match(argv[1]):
 11.1353 +	if len(argv) > 2 and help.has_key(argv[2]):
 11.1354 +	    usage(argv[2])
 11.1355 +	else:
 11.1356 +	    usage()
 11.1357 +	sys.exit(0)
 11.1358 +
 11.1359 +    cmd = xm_lookup_cmd(argv[1])
 11.1360  
 11.1361 -class ProgVbdRefresh(Prog):
 11.1362 -    group = 'vbd'
 11.1363 -    name  = 'vbd-refresh'
 11.1364 -    info = """Refresh a virtual block device for a domain"""
 11.1365 -
 11.1366 -    def help(self, args):
 11.1367 -        print args[0], "DOM DEV"
 11.1368 -        print """
 11.1369 -Refresh a virtual block device for a domain.
 11.1370 +    # strip off prog name and subcmd
 11.1371 +    args = argv[2:]
 11.1372 +    if cmd:
 11.1373 +        try:
 11.1374 +            from xen.xend.XendClient import XendError
 11.1375 +            rc = cmd(args)
 11.1376 +            if rc:
 11.1377 +                usage()
 11.1378 +        except socket.error, ex:
 11.1379 +            print >>sys.stderr, ex
 11.1380 +            err("Error connecting to xend, is xend running?")
 11.1381 +            sys.exit(1)
 11.1382 +        except IOError:
 11.1383 +            err("Most commands need root access.  Please try again as root")
 11.1384 +            sys.exit(1)
 11.1385 +        except XendError, ex:
 11.1386 +            if len(args) > 0:
 11.1387 +                handle_xend_error(argv[1], args[0], ex)
 11.1388 +            else:
 11.1389 +                print "Unexpected error:", sys.exc_info()[0]
 11.1390 +                print
 11.1391 +                print "Please report to xen-devel@lists.xensource.com"
 11.1392 +                raise
 11.1393 +        except SystemExit:
 11.1394 +            sys.exit(1)
 11.1395 +        except:
 11.1396 +            print "Unexpected error:", sys.exc_info()[0]
 11.1397 +            print
 11.1398 +            print "Please report to xen-devel@lists.xensource.com"
 11.1399 +            raise
 11.1400 +                
 11.1401 +    else:
 11.1402 +        usage()
 11.1403  
 11.1404 -  DEV     - idx field in the device information
 11.1405 -"""
 11.1406 -
 11.1407 -    def main(self, args):
 11.1408 -        if len(args) != 3: self.err("%s: Invalid argument(s)" % args[0])
 11.1409 -        dom = args[1]
 11.1410 -        dev = args[2]
 11.1411 -        server.xend_domain_device_refresh(dom, 'vbd', dev)
 11.1412 -
 11.1413 -xm.prog(ProgVbdRefresh)
 11.1414 +if __name__ == "__main__":
 11.1415 +    main()
 11.1416  
 11.1417  
 11.1418 -class ProgVbdDestroy(Prog):
 11.1419 -    group = 'vbd'
 11.1420 -    name = 'vbd-destroy'
 11.1421 -    info = """Destroy a domain's virtual block device"""
 11.1422  
 11.1423 -    def help(self, args):
 11.1424 -        print args[0], "DOM DEV"
 11.1425 -        print """
 11.1426 -Destroy vbd DEV attached to domain DOM. Detaches the device
 11.1427 -from the domain, but does not destroy the device contents.
 11.1428 -The device indentifier DEV is the idx field in the device
 11.1429 -information. This is visible in 'xm vbd-list'."""
 11.1430 -
 11.1431 -    def main(self, args):
 11.1432 -        if len(args) != 3: self.err("%s: Invalid argument(s)" % args[0])
 11.1433 -        dom = args[1]
 11.1434 -        dev = args[2]
 11.1435 -        server.xend_domain_device_destroy(dom, 'vbd', dev)
 11.1436 -
 11.1437 -xm.prog(ProgVbdDestroy)
 11.1438 -
 11.1439 -def main(args):
 11.1440 -    xm.main(args)