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.distro.call_hooks('post_install') 72 self.call_hooks('unmount_partitions') 73 os.rmdir(self.chroot_dir)
74
75 - def finalise(self, destdir):
76 self.call_hooks('convert', 77 self.preferred_storage == STORAGE_DISK_IMAGE and self.disks or self.filesystems, 78 destdir) 79 self.call_hooks('deploy', destdir)
80
81 - def mount_partitions(self, mntdir):
82 """Mounts all the vm's partitions and filesystems below .rootmnt""" 83 logging.info('Mounting target filesystems') 84 for fs in self.filesystems: 85 fs.create() 86 fs.mkfs() 87 for disk in self.disks: 88 disk.create() 89 disk.partition() 90 disk.map_partitions() 91 disk.mkfs() 92 fss = VMBuilder.disk.get_ordered_filesystems(self) 93 for fs in fss: 94 fs.mount(mntdir) 95 self.distro.post_mount(fs)
96
97 - def unmount_partitions(self):
98 """Unmounts all the vm's partitions and filesystems""" 99 logging.info('Unmounting target filesystem') 100 fss = VMBuilder.disk.get_ordered_filesystems(self) 101 fss.reverse() 102 for fs in fss: 103 fs.umount() 104 for disk in self.disks: 105 disk.unmap()
106
107 - def convert_disks(self, disks, destdir):
108 for disk in disks: 109 disk.convert(destdir, self.filetype)
110
111 - class NIC(object):
112 - def __init__(self, type='dhcp', ip=None, network=None, netmask=None, 113 broadcast=None, dns=None, gateway=None):
114 self.type = type 115 self.ip = ip 116 self.network = network 117 self.netmask = netmask 118 self.broadcast = broadcast 119 self.dns = dns 120 self.gateway = gateway
121