Package VMBuilder :: Module hypervisor
[frames] | no frames]

Source Code for Module VMBuilder.hypervisor

  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  #    Hypervisor super class 
 20   
 21  import logging 
 22  import VMBuilder.distro 
 23  import VMBuilder.disk 
 24  from   VMBuilder.util    import run_cmd, tmpdir 
 25   
 26  STORAGE_DISK_IMAGE = 0 
 27  STORAGE_FS_IMAGE = 1 
 28   
29 -class Hypervisor(VMBuilder.distro.Context):
30 preferred_storage = STORAGE_DISK_IMAGE 31
32 - def __init__(self, distro):
33 self.plugin_classes = VMBuilder._hypervisor_plugins 34 super(Hypervisor, self).__init__() 35 self.plugins += [distro] 36 self.distro = distro 37 self.filesystems = [] 38 self.disks = [] 39 self.nics = []
40
41 - def add_filesystem(self, *args, **kwargs):
42 """Adds a filesystem to the virtual machine""" 43 from VMBuilder.disk import Filesystem 44 45 fs = Filesystem(self, *args, **kwargs) 46 self.filesystems.append(fs) 47 return fs
48
49 - def add_disk(self, *args, **kwargs):
50 """Adds a disk image to the virtual machine""" 51 from VMBuilder.disk import Disk 52 53 disk = Disk(self, *args, **kwargs) 54 self.disks.append(disk) 55 return disk
56
57 - def install_os(self):
58 self.nics = [self.NIC()] 59 self.call_hooks('preflight_check') 60 self.call_hooks('configure_networking', self.nics) 61 self.call_hooks('configure_mounting', self.disks, self.filesystems) 62 63 self.chroot_dir = tmpdir() 64 self.call_hooks('mount_partitions', self.chroot_dir) 65 run_cmd('rsync', '-aHA', '%s/' % self.distro.chroot_dir, self.chroot_dir) 66 self.distro.set_chroot_dir(self.chroot_dir) 67 if self.needs_bootloader: 68 self.call_hooks('install_bootloader', self.chroot_dir, self.disks) 69 self.call_hooks('install_kernel', self.chroot_dir) 70 self.call_hooks('unmount_partitions')
71
72 - def finalise(self, destdir):
73 self.call_hooks('convert', 74 self.preferred_storage == STORAGE_DISK_IMAGE and self.disks or self.filesystems, 75 destdir) 76 self.call_hooks('deploy', destdir)
77
78 - def mount_partitions(self, mntdir):
79 """Mounts all the vm's partitions and filesystems below .rootmnt""" 80 logging.info('Mounting target filesystems') 81 for fs in self.filesystems: 82 fs.create() 83 fs.mkfs() 84 for disk in self.disks: 85 disk.create() 86 disk.partition() 87 disk.map_partitions() 88 disk.mkfs() 89 fss = VMBuilder.disk.get_ordered_filesystems(self) 90 for fs in fss: 91 fs.mount(mntdir) 92 self.distro.post_mount(fs)
93
94 - def unmount_partitions(self):
95 """Unmounts all the vm's partitions and filesystems""" 96 logging.info('Unmounting target filesystem') 97 fss = VMBuilder.disk.get_ordered_filesystems(self) 98 fss.reverse() 99 for fs in fss: 100 fs.umount() 101 for disk in self.disks: 102 disk.unmap()
103
104 - def convert_disks(self, disks, destdir):
105 for disk in disks: 106 disk.convert(destdir, self.filetype)
107
108 - class NIC(object):
109 - def __init__(self, type='dhcp', ip=None, network=None, netmask=None, 110 broadcast=None, dns=None, gateway=None):
111 self.type = type 112 self.ip = ip 113 self.network = network 114 self.netmask = netmask 115 self.broadcast = broadcast 116 self.dns = dns 117 self.gateway = gateway
118