~ubuntu-branches/ubuntu/maverick/vm-builder/maverick-updates

« back to all changes in this revision

Viewing changes to VMBuilder/vm.py

  • Committer: Bazaar Package Importer
  • Author(s): Soren Hansen
  • Date: 2010-02-22 13:56:18 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100222135618-la13e3mu397rg0m1
Tags: 0.12.0-0ubuntu1
* New upstream release. (FFe: LP: #525741)
  - All patches incorporated upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
92
92
 
93
93
        self.add_clean_cmd('rm', log.logfile)
94
94
 
95
 
    def get_version_info(self):
96
 
        import vcsversion
97
 
        info = vcsversion.version_info
98
 
        info['major'] = 0
99
 
        info['minor'] = 11
100
 
        info['micro'] = 3
101
 
        return info
102
 
       
103
 
    def cleanup(self):
104
 
        logging.info("Cleaning up")
105
 
        while len(self._cleanup_cbs) > 0:
106
 
            self._cleanup_cbs.pop(0)()
107
 
 
108
 
    def add_clean_cb(self, cb):
109
 
        self._cleanup_cbs.insert(0, cb)
110
 
 
111
 
    def add_clean_cmd(self, *argv, **kwargs):
112
 
        cb = lambda : util.run_cmd(*argv, **kwargs)
113
 
        self.add_clean_cb(cb)
114
 
        return cb
115
 
 
116
 
    def cancel_cleanup(self, cb):
117
 
        try:
118
 
            self._cleanup_cbs.remove(cb)
119
 
        except ValueError, e:
120
 
            # Wasn't in there. No worries.
121
 
            pass
122
 
 
123
95
    def distro_help(self):
124
96
        return 'Distro. Valid options: %s' % " ".join(VMBuilder.distros.keys())
125
97
 
149
121
        self.register_setting('-m', '--mem', type='int', default=128, help='Assign MEM megabytes of memory to the guest vm. [default: %default]')
150
122
        self.register_setting('--cpus', type='int', default=1, help='Number of virtual CPU\'s. [default: %default]')
151
123
 
152
 
        group = self.setting_group('Network related options')
153
 
        domainname = '.'.join(socket.gethostbyname_ex(socket.gethostname())[0].split('.')[1:]) or "defaultdomain"
154
 
        group.add_option('--domain', metavar='DOMAIN', default=domainname, help='Set DOMAIN as the domain name of the guest [default: The domain of the machine running this script: %default].')
155
 
        group.add_option('--ip', metavar='ADDRESS', default='dhcp', help='IP address in dotted form [default: %default].')
156
 
        group.add_option('--mac', metavar='VALUE', help='MAC address of the guest [default: one will be automatically generated on first run].')
157
 
        group.add_option('--mask', metavar='VALUE', help='IP mask in dotted form [default: based on ip setting]. Ignored if --ip is not specified.')
158
 
        group.add_option('--net', metavar='ADDRESS', help='IP net address in dotted form [default: based on ip setting]. Ignored if --ip is not specified.')
159
 
        group.add_option('--bcast', metavar='VALUE', help='IP broadcast in dotted form [default: based on ip setting]. Ignored if --ip is not specified.')
160
 
        group.add_option('--gw', metavar='ADDRESS', help='Gateway (router) address in dotted form [default: based on ip setting (first valid address in the network)]. Ignored if --ip is not specified.')
161
 
        group.add_option('--dns', metavar='ADDRESS', help='DNS address in dotted form [default: based on ip setting (first valid address in the network)] Ignored if --ip is not specified.')
162
 
        self.register_setting_group(group)
163
 
 
164
124
    def add_disk(self, *args, **kwargs):
165
125
        """Adds a disk image to the virtual machine"""
166
126
        disk = Disk(self, *args, **kwargs)
272
232
            self.distro.set_defaults()
273
233
            self.hypervisor.set_defaults()
274
234
 
275
 
 
276
 
    def ip_defaults(self):
277
 
        """
278
 
        is called to validate the ip configuration given and set defaults
279
 
        """
280
 
 
281
 
        logging.debug("ip: %s" % self.ip)
282
 
        
283
 
        if self.mac:
284
 
            valid_mac_address = re.compile("([0-9a-f]{2}:){5}([0-9a-f]{2})", re.IGNORECASE)
285
 
            if not valid_mac_address.search(self.mac):
286
 
                raise VMBuilderUserError("Malformed MAC address entered: %s" % self.mac)
287
 
            else:
288
 
                logging.debug("Valid mac given: %s" % self.mac)
289
 
 
290
 
        if self.ip != 'dhcp':
291
 
            if self.domain == '':
292
 
                raise VMBuilderUserError('Domain is undefined and host has no domain set.')
293
 
 
294
 
            try:
295
 
                numip = struct.unpack('I', socket.inet_aton(self.ip))[0] 
296
 
            except socket.error:
297
 
                raise VMBuilderUserError('%s is not a valid ip address' % self.ip)
298
 
             
299
 
            if not self.mask:
300
 
                ipclass = numip & 0xFF
301
 
                if (ipclass > 0) and (ipclass <= 127):
302
 
                    mask = 0xFF
303
 
                elif (ipclass > 128) and (ipclass < 192):
304
 
                    mask = OxFFFF
305
 
                elif (ipclass < 224):
306
 
                    mask = 0xFFFFFF
307
 
                else:
308
 
                    raise VMBuilderUserError('The class of the ip address specified (%s) does not seem right' % self.ip)
309
 
            else:
310
 
                mask = struct.unpack('I', socket.inet_aton(self.mask))[0]
311
 
 
312
 
            numnet = numip & mask
313
 
 
314
 
            if not self.net:
315
 
                self.net = socket.inet_ntoa( struct.pack('I', numnet ) )
316
 
            if not self.bcast:
317
 
                self.bcast = socket.inet_ntoa( struct.pack('I', numnet + (mask ^ 0xFFFFFFFF)))
318
 
            if not self.gw:
319
 
                self.gw = socket.inet_ntoa( struct.pack('I', numnet + 0x01000000 ) )
320
 
            if not self.dns:
321
 
                self.dns = self.gw
322
 
 
323
 
            self.mask = socket.inet_ntoa( struct.pack('I', mask ) )
324
 
 
325
 
            logging.debug("net: %s" % self.net)
326
 
            logging.debug("netmask: %s" % self.mask)
327
 
            logging.debug("broadcast: %s" % self.bcast)
328
 
            logging.debug("gateway: %s" % self.gw)
329
 
            logging.debug("dns: %s" % self.dns)
330
 
 
331
235
    def create_directory_structure(self):
332
236
        """Creates the directory structure where we'll be doing all the work
333
237
 
375
279
        """Creates the working directory for this vm and returns its path"""
376
280
        return tempfile.mkdtemp('', 'vmbuilder', self.tmp)
377
281
 
378
 
    def mount_partitions(self):
379
 
        """Mounts all the vm's partitions and filesystems below .rootmnt"""
380
 
        logging.info('Mounting target filesystems')
381
 
        fss = disk.get_ordered_filesystems(self)
382
 
        for fs in fss:
383
 
            fs.mount()
384
 
            self.distro.post_mount(fs)
385
 
 
386
 
        self.fsmounted = True
387
 
 
388
 
    def umount_partitions(self):
389
 
        """Unmounts all the vm's partitions and filesystems"""
390
 
        logging.info('Unmounting target filesystem')
391
 
        fss = VMBuilder.disk.get_ordered_filesystems(self)
392
 
        fss.reverse()
393
 
        for fs in fss:
394
 
            fs.umount()
395
 
        for disk in self.disks:
396
 
            disk.unmap()
397
 
 
398
 
        self.fsmounted = False
399
 
 
400
282
    def install(self):
401
283
        if self.in_place:
402
284
            self.installdir = self.rootmnt
423
305
            if '-' in opt:
424
306
                raise VMBuilderUserError('You specified a "%s" config option in a config file, but that is not valid. Perhaps you meant "%s"?' % (opt, opt.replace('-', '_')))
425
307
 
426
 
        self.ip_defaults()
427
308
        self.call_hooks('preflight_check')
428
309
 
429
310
        # Check repository availability
440
321
 
441
322
        testnet.close()
442
323
 
443
 
    def install_file(self, path, contents=None, source=None, mode=None):
444
 
        fullpath = '%s%s' % (self.installdir, path)
445
 
        if source and not contents:
446
 
            shutil.copy(source, fullpath) 
447
 
        else:
448
 
            fp = open(fullpath, 'w')
449
 
            fp.write(contents)
450
 
            fp.close()
451
 
        if mode:
452
 
            os.chmod(fullpath, mode)
453
 
        return fullpath
454
 
 
455
324
    def create(self):
456
325
        """
457
326
        The core vm creation method
499
368
                logging.debug("Oh, dear, an exception occurred")
500
369
            self.cleanup()
501
370
 
 
371
        if not finished:
 
372
            return(1)
 
373
        return(0)
 
374
 
502
375
class _MyOptParser(optparse.OptionParser):
503
376
    def format_arg_help(self, formatter):
504
377
        result = []