~ubuntu-branches/ubuntu/quantal/virtinst/quantal-proposed

« back to all changes in this revision

Viewing changes to virtinst/CloneManager.py

  • Committer: Bazaar Package Importer
  • Author(s): Laurent Léonard
  • Date: 2011-01-29 21:41:21 UTC
  • mto: (1.6.3 sid)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: james.westby@ubuntu.com-20110129214121-pjuxf2xz08l5zqew
Tags: upstream-0.500.5
Import upstream version 0.500.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
import logging
36
36
import re
 
37
import os
37
38
 
38
39
import libxml2
39
40
import urlgrabber.progress as progress
55
56
    else:
56
57
        return False, [val]
57
58
 
58
 
def generate_clone_disk_path(origpath, design):
59
 
    basename = origpath
 
59
def generate_clone_disk_path(origpath, design, newname=None):
 
60
    origname = design.original_guest
 
61
    newname = newname or design.clone_name
 
62
    path = origpath
60
63
    suffix = ""
61
64
 
62
65
    # Try to split the suffix off the existing disk name. Ex.
65
68
    # If the suffix is greater than 7 characters, assume it isn't
66
69
    # a file extension and is part of the disk name, at which point
67
70
    # just stick '-clone' on the end.
68
 
    if basename.count(".") and len(basename.rsplit(".", 1)[1]) <= 7:
69
 
        basename, suffix = basename.rsplit(".", 1)
 
71
    if origpath.count(".") and len(origpath.rsplit(".", 1)[1]) <= 7:
 
72
        path, suffix = origpath.rsplit(".", 1)
70
73
        suffix = "." + suffix
71
74
 
72
 
    return _util.generate_name(basename + "-clone",
73
 
                               lambda p: VirtualDisk.path_exists(design.original_conn, p),
74
 
                               suffix,
75
 
                               lib_collision=False)
 
75
    dirname = os.path.dirname(path)
 
76
    basename = os.path.basename(path)
 
77
 
 
78
    clonebase = basename + "-clone"
 
79
    if origname and basename == origname:
 
80
        clonebase = newname
 
81
 
 
82
    clonebase = os.path.join(dirname, clonebase)
 
83
    return _util.generate_name(
 
84
                    clonebase,
 
85
                    lambda p: VirtualDisk.path_exists(design.original_conn, p),
 
86
                    suffix,
 
87
                    lib_collision=False)
76
88
 
77
89
def generate_clone_name(design):
78
90
    # If the orig name is "foo-clone", we don't want the clone to be
119
131
        self._clone_name         = None
120
132
        self._clone_devices      = []
121
133
        self._clone_virtual_disks = []
122
 
        self._clone_bs           = 1024*1024*10
 
134
        self._clone_bs           = 1024 * 1024 * 10
123
135
        self._clone_mac          = []
124
136
        self._clone_uuid         = None
125
137
        self._clone_sparse       = True
175
187
        try:
176
188
            self._valid_guest.set_name(name)
177
189
        except ValueError, e:
178
 
            raise ValueError, _("Invalid name for new guest: %s") % (str(e),)
 
190
            raise ValueError(_("Invalid name for new guest: %s") % e)
179
191
 
180
192
        # Make sure new VM name isn't taken.
181
193
        try:
193
205
        try:
194
206
            self._valid_guest.set_uuid(uuid)
195
207
        except ValueError, e:
196
 
            raise ValueError, _("Invalid uuid for new guest: %s") % (str(e),)
 
208
            raise ValueError(_("Invalid uuid for new guest: %s") % e)
197
209
 
198
210
        if _util.vm_uuid_collision(self._hyper_conn, uuid):
199
211
            raise ValueError(_("UUID '%s' is in use by another guest.") %
406
418
 
407
419
            if status not in [libvirt.VIR_DOMAIN_SHUTOFF,
408
420
                              libvirt.VIR_DOMAIN_PAUSED]:
409
 
                raise RuntimeError, _("Domain with devices to clone must be "
410
 
                                      "paused or shutoff.")
 
421
                raise RuntimeError(_("Domain with devices to clone must be "
 
422
                                     "paused or shutoff."))
411
423
 
412
424
 
413
425
    def setup_clone(self):
437
449
 
438
450
        # changing mac
439
451
        count = ctx.xpathEval("count(/domain/devices/interface/mac)")
440
 
        for i in range(1, int(count+1)):
 
452
        for i in range(1, int(count + 1)):
441
453
            base_xpath = "/domain/devices/interface[%d]" % i
442
454
            base_node = ctx.xpathEval(base_xpath)[0]
443
455
            node = ctx.xpathEval(base_xpath + "/mac/@address")
450
462
 
451
463
            mac = None
452
464
            try:
453
 
                mac = self._clone_mac[i-1]
 
465
                mac = self._clone_mac[i - 1]
454
466
            except Exception:
455
467
                while 1:
456
468
                    mac = _util.randomMAC(typ)
572
584
        lst     = []
573
585
 
574
586
        count = _util.get_xml_path(xml, "count(/domain/devices/disk)")
575
 
        for i in range(1, int(count+1)):
 
587
        for i in range(1, int(count + 1)):
576
588
            # Check if the disk needs cloning
577
589
            (path, target) = self._do_we_clone_device(xml, i)
578
590
            if target == None:
691
703
 
692
704
        # VirtualDisk.setup handles everything
693
705
        dst_dev.setup(meter)
694