Package VMBuilder :: Package plugins :: Package vmware :: Module vm
[frames] | no frames]

Source Code for Module VMBuilder.plugins.vmware.vm

  1  # 
  2  #    Uncomplicated VM Builder 
  3  #    Copyright (C) 2007-2009 Canonical Ltd. 
  4  #     
  5  #    See AUTHORS for list of contributors 
  6  # 
  7  #    This program is free software: you can redistribute it and/or modify 
  8  #    it under the terms of the GNU General Public License version 3, as 
  9  #    published by the Free Software Foundation. 
 10  # 
 11  #    This program is distributed in the hope that it will be useful, 
 12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #    GNU General Public License for more details. 
 15  # 
 16  #    You should have received a copy of the GNU General Public License 
 17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18  # 
 19  from   VMBuilder import register_hypervisor, Hypervisor 
 20  import VMBuilder 
 21  import VMBuilder.hypervisor 
 22  import os 
 23  import os.path 
 24  import stat 
 25  from shutil import move 
 26  from math import floor 
 27   
28 -class VMWare(Hypervisor):
29 filetype = 'vmdk' 30 preferred_storage = VMBuilder.hypervisor.STORAGE_DISK_IMAGE 31 needs_bootloader = True 32 vmxtemplate = 'vmware' 33
34 - def register_options(self):
35 group = self.setting_group('VM settings') 36 group.add_setting('mem', extra_args=['-m'], default='128', help='Assign MEM megabytes of memory to the guest vm. [default: %default]') 37 group.add_setting('cpus', type='int', default=1, help='Assign NUM cpus to the guest vm. [default: %default]')
38
39 - def convert(self, disks, destdir):
40 self.imgs = [] 41 for disk in self.get_disks(): 42 img_path = disk.convert(destdir, self.filetype) 43 self.imgs.append(img_path) 44 self.call_hooks('fix_ownership', img_path)
45
46 - def get_disks(self):
47 return self.disks
48
49 - def deploy(self, destdir):
50 mem = self.context.get_setting('mem') 51 cpus = self.context.get_setting('cpus') 52 hostname = self.context.distro.get_setting('hostname') 53 arch = self.context.distro.get_setting('arch') 54 mac = self.context.get_setting('mac') 55 vmdesc = VMBuilder.util.render_template('vmware', 56 self.context, 57 self.vmxtemplate, 58 { 'disks' : self.get_disks(), 59 'vmhwversion' : self.vmhwversion, 60 'mem' : mem, 61 'numvcpus' : cpus, 62 'hostname' : hostname, 63 'arch' : arch, 64 'mac' : mac, 65 'guestos' : (arch == 'amd64' and 'ubuntu-64' or 'ubuntu') }) 66 67 vmx = '%s/%s.vmx' % (destdir, hostname) 68 fp = open(vmx, 'w') 69 fp.write(vmdesc) 70 fp.close() 71 os.chmod(vmx, stat.S_IRWXU | stat.S_IRWXU | stat.S_IROTH | stat.S_IXOTH) 72 self.call_hooks('fix_ownership', vmx)
73
74 -class VMWareWorkstation6(VMWare):
75 name = 'VMWare Workstation 6' 76 arg = 'vmw6' 77 vmhwversion = 6
78
79 -class VMWareServer(VMWare):
80 name = 'VMWare Server' 81 arg = 'vmserver' 82 vmhwversion = 4
83
84 -class VMWareEsxi(VMWare):
85 name = 'VMWare ESXi' 86 arg = 'esxi' 87 vmhwversion = 4 88 adaptertype = 'lsilogic' # lsilogic | buslogic, ide is not supported by ESXi 89 vmxtemplate = 'esxi.vmx' 90 91 vmdks = [] # vmdk filenames used when deploying vmx file 92
93 - def convert(self, disks, destdir):
94 self.imgs = [] 95 for disk in disks: 96 97 # Move raw image to <imagename>-flat.vmdk 98 diskfilename = os.path.basename(disk.filename) 99 if '.' in diskfilename: 100 diskfilename = diskfilename[:diskfilename.rindex('.')] 101 102 flat = '%s/%s-flat.vmdk' % (destdir, diskfilename) 103 self.vmdks.append(diskfilename) 104 105 move(disk.filename, flat) 106 107 self.call_hooks('fix_ownership', flat) 108 109 # Create disk descriptor file 110 sectorTotal = disk.size * 2048 111 sector = int(floor(sectorTotal / 16065)) # pseudo geometry 112 113 diskdescriptor = VMBuilder.util.render_template('vmware', self.context, 'flat.vmdk', { 'adaptertype' : self.adaptertype, 'sectors' : sector, 'diskname' : os.path.basename(flat), 'disksize' : sectorTotal }) 114 vmdk = '%s/%s.vmdk' % (destdir, diskfilename) 115 116 fp = open(vmdk, 'w') 117 fp.write(diskdescriptor) 118 fp.close() 119 os.chmod(vmdk, stat.S_IRWXU | stat.S_IRWXU | stat.S_IROTH | stat.S_IXOTH) 120 121 self.call_hooks('fix_ownership', vmdk)
122
123 - def get_disks(self):
124 return self.vmdks
125 126 register_hypervisor(VMWareServer) 127 register_hypervisor(VMWareWorkstation6) 128 register_hypervisor(VMWareEsxi) 129