aboutsummaryrefslogtreecommitdiffstats
path: root/tools/python/xen/xm/create.py
blob: 12e2e8010c8488ca02cd2560ea98bbfbcc89ec4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
"""Domain creation.
"""
import string
import sys

from xen.xend import sxp
from xen.xend import PrettyPrint
from xen.xend.XendClient import server

from xen.xm.opts import *

gopts = Opts(use="""[options]

Create a domain.
""")

gopts.opt('help', short='h',
         fn=set_true, default=0,
         use="Print this help.")

gopts.opt('quiet', short='q',
         fn=set_true, default=0,
         use="Quiet.")

gopts.opt('path', val='PATH',
         fn=set_value, default='.:/etc/xen',
         use="Search path for default scripts.")

gopts.opt('defaults', short='f', val='FILE',
         fn=set_value, default='xmdefaults',
         use="Use the given default script.")

gopts.opt('config', short='F', val='FILE',
         fn=set_value, default=None,
         use='Domain configuration to use (SXP).')

gopts.opt('load', short='L', val='FILE',
          fn=set_value, default=None,
          use='Domain saved state to load.')

gopts.opt('define', short='D', val='VAR=VAL',
         fn=set_var, default=None,
         use="""Set a variable before loading defaults, e.g. '-D vmid=3'
         to set vmid. May be repeated to set more thanone variable.""")

gopts.opt('dryrun', short='n',
         fn=set_true, default=0,
         use="Dry run - print the config but don't create the domain.")

gopts.opt('name', short='N', val='NAME',
          fn=set_value, default=None,
          use="Domain name.")

gopts.opt('console', short='c',
         fn=set_true, default=0,
         use="Connect to console after domain is created.")

gopts.opt('kernel', short='k', val='FILE',
         fn=set_value, default=None,
         use="Path to kernel image.")

gopts.opt('ramdisk', short='r', val='FILE',
         fn=set_value, default='',
         use="Path to ramdisk.")

gopts.opt('builder', short='b', val='FUNCTION',
         fn=set_value, default='linux',
         use="Function to use to build the domain.")

gopts.opt('memory', short='m', val='MEMORY',
         fn=set_value, default=128,
         use="Domain memory in MB.")

gopts.opt('blkif',
          fn=set_true, default=0,
          use="Make the domain a block device backend.")

gopts.opt('netif',
          fn=set_true, default=0,
          use="Make the domain a network interface backend.")

gopts.opt('disk', short='d', val='phy:DEV,VDEV,MODE',
         fn=append_value, default=[],
         use="""Add a disk device to a domain. The physical device is DEV, which
         is exported to the domain as VDEV. The disk is read-only if MODE
         is 'r', read-write if MODE is 'w'.
         The option may be repeated to add more than one disk.
         """)

gopts.opt('pci', val='BUS,DEV,FUNC',
         fn=append_value, default=[],
         use="""Add a PCI device to a domain, using given params (in hex).
         For example '-pci c0,02,1a'.
         The option may be repeated to add more than one pci device.
         """)

gopts.opt('ipaddr', short='i', val="IPADDR",
         fn=append_value, default=[],
         use="Add an IP address to the domain.")

gopts.opt('vif', val="mac=MAC,bridge=BRIDGE",
         fn=append_value, default=[],
         use="""Add a network interface with the given MAC address and bridge.
         If mac is not specified a random MAC address is used.
         If bridge is not specified the default bridge is used.
         This option may be repeated to add more than one vif.
         Specifying vifs will increase the number of interfaces as needed.
         """)

gopts.opt('nics', val="NUM",
         fn=set_int, default=1,
         use="""Set the number of network interfaces.
         Use the vif option to define interface parameters, otherwise
         defaults are used. Specifying vifs will increase the
         number of interfaces as needed.
         """)

gopts.opt('root', short='R', val='DEVICE',
         fn=set_value, default='',
         use="""Set the root= parameter on the kernel command line.
         Use a device, e.g. /dev/sda1, or /dev/nfs for NFS root.""")

gopts.opt('extra', short='E', val="ARGS",
         fn=set_value, default='',
         use="Set extra arguments to append to the kernel command line.")

gopts.opt('ip', short='I', val='IPADDR',
         fn=set_value, default='',
         use="Set the kernel IP interface address.")

gopts.opt('gateway', val="IPADDR",
         fn=set_value, default='',
         use="Set the kernel IP gateway.")

gopts.opt('netmask', val="MASK",
         fn=set_value, default = '',
         use="Set the kernel IP netmask.")

gopts.opt('hostname', val="NAME",
         fn=set_value, default='',
         use="Set the kernel IP hostname.")

gopts.opt('interface', val="INTF",
         fn=set_value, default="eth0",
         use="Set the kernel IP interface name.")

gopts.opt('dhcp', val="off|dhcp",
         fn=set_value, default='off',
         use="Set the kernel dhcp option.")

gopts.opt('nfs_server', val="IPADDR",
         fn=set_value, default=None,
         use="Set the address of the NFS server for NFS root.")

gopts.opt('nfs_root', val="PATH",
         fn=set_value, default=None,
         use="Set the path of the root NFS directory.")

def strip(pre, s):
    """Strip prefix 'pre' if present.
    """
    if s.startswith(pre):
        return s[len(pre):]
    else:
        return s

def configure_image(config, opts):
    """Create the image config.
    """
    config_image = [ opts.builder ]
    config_image.append([ 'kernel', os.path.abspath(opts.kernel) ])
    if opts.ramdisk:
        config_image.append([ 'ramdisk', os.path.abspath(opts.ramdisk) ])
    if opts.cmdline_ip:
        cmdline_ip = strip('ip=', opts.cmdline_ip)
        config_image.append(['ip', cmdline_ip])
    if opts.root:
        cmdline_root = strip('root=', opts.root)
        config_image.append(['root', cmdline_root])
    if opts.extra:
        config_image.append(['args', opts.extra])
    config.append(['image', config_image ])
    
def configure_disks(config_devs, opts):
    """Create the config for disks (virtual block devices).
    """
    for (uname, dev, mode) in opts.disk:
        config_vbd = ['vbd',
                      ['uname', uname],
                      ['dev', dev ],
                      ['mode', mode ] ]
        config_devs.append(['device', config_vbd])

def configure_pci(config_devs, opts):
    """Create the config for pci devices.
    """
    for (bus, dev, func) in opts.pci:
        config_pci = ['pci', ['bus', bus], ['dev', dev], ['func', func]]
        config_devs.append(['device', config_pci])

def configure_vifs(config_devs, opts):
    """Create the config for virtual network interfaces.
    """
    vifs = opts.vif
    vifs_n = max(opts.nics, len(vifs))

    for idx in range(0, vifs_n):
        if idx < len(vifs):
            d = vifs[idx]
            mac = d.get('mac')
            bridge = d.get('bridge')
        else:
            mac = None
            bridge = None
        config_vif = ['vif']
        if mac:
            config_vif.append(['mac', mac])
        if bridge:
            config_vif.append(['bridge', bridge])
        config_devs.append(['device', config_vif])

def configure_vfr(config, opts):
     if not opts.ipaddr: return
     config_vfr = ['vfr']
     idx = 0 # No way of saying which IP is for which vif?
     for ip in opts.ipaddr:
         config_vfr.append(['vif', ['id', idx], ['ip', ip]])
     config.append(config_vfr)


def make_config(opts):
    """Create the domain configuration.
    """
    
    config = ['vm',
              ['name', opts.name ],
              ['memory', opts.memory ] ]
    if opts.cpu:
        config.append(['cpu', opts.cpu])
    if opts.blkif:
        config.append(['backend', ['blkif']])
    if opts.netif:
        config.append(['backend', ['netif']])
    
    configure_image(config, opts)
    config_devs = []
    configure_disks(config_devs, opts)
    configure_pci(config_devs, opts)
    configure_vifs(config_devs, opts)
    config += config_devs
    return config

def preprocess_disk(opts):
    if not opts.disk: return
    disk = []
    for v in opts.disk:
        d = v.split(',')
        if len(d) != 3:
            opts.err('Invalid disk specifier: ' + v)
        disk.append(d)
    opts.disk = disk

def preprocess_pci(opts):
    if not opts.pci: return
    pci = []
    for v in opts.pci:
        d = v.split(',')
        if len(d) != 3:
            opts.err('Invalid pci specifier: ' + v)
        # Components are in hex: add hex specifier.
        hexd = map(lambda v: '0x'+v, d)
        pci.append(hexd)
    opts.pci = pci

def preprocess_vifs(opts):
    if not opts.vif: return
    vifs = []
    for vif in opts.vif:
        d = {}
        a = vif.split(',')
        for b in a:
            (k, v) = b.strip().split('=')
            k = k.strip()
            v = v.strip()
            if k not in ['mac', 'bridge']:
                opts.err('Invalid vif specifier: ' + vif)
            d[k] = v
        vifs.append(d)
    opts.vif = vifs

def preprocess_ip(opts):
    setip = (opts.hostname or opts.netmask
             or opts.gateway or opts.dhcp or opts.interface)
    if not setip: return
    #if not opts
    ip = (opts.ip
          + ':'
          + ':' + opts.gateway
          + ':' + opts.netmask
          + ':' + opts.hostname
          + ':' + opts.interface
          + ':' + opts.dhcp)
    opts.cmdline_ip = ip

def preprocess_nfs(opts):
    if (opts.nfs_root or opts.nfs_server):
        if (not opts.nfs_root) or (not opts.nfs_server):
            opts.err('Must set nfs root and nfs server')
    else:
        return
    nfs = 'nfsroot=' + opts.nfs_server + ':' + opts.nfs_root
    opts.extra = nfs + ' ' + opts.extra
    
def preprocess(opts):
    if not opts.kernel:
        opts.err("No kernel specified")
    preprocess_disk(opts)
    preprocess_pci(opts)
    preprocess_vifs(opts)
    preprocess_ip(opts)
    preprocess_nfs(opts)
         
def make_domain(opts, config):
    """Create, build and start a domain.
    Returns: [int] the ID of the new domain.
    """
    if opts.vals.load:
        filename = os.path.abspath(opts.vals.load)
        dominfo = server.xend_domain_restore(filename, config)
    else:
        dominfo = server.xend_domain_create(config)

    dom = int(sxp.child_value(dominfo, 'id'))
    console_info = sxp.child(dominfo, 'console')
    if console_info:
        console_port = int(sxp.child_value(console_info, 'port'))
    else:
        console_port = None
    
    if server.xend_domain_unpause(dom) < 0:
        server.xend_domain_destroy(dom)
        opts.err("Failed to start domain %d" % dom)
    opts.info("Started domain %d, console on port %d"
              % (dom, console_port))
    return (dom, console_port)

def main(argv):
    opts = gopts
    args = opts.parse(argv)
    if opts.vals.help:
        opts.usage()
        return
    if opts.vals.config:
        pass
    else:
        opts.load_defaults()
    preprocess(opts.vals)
    config = make_config(opts.vals)
    if opts.vals.dryrun:
        PrettyPrint.prettyprint(config)
    else:
        make_domain(opts, config)
        
if __name__ == '__main__':
    main(sys.argv)