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