Package VMBuilder :: Package plugins :: Package cli
[frames] | no frames]

Source Code for Package VMBuilder.plugins.cli

  1  #    Uncomplicated VM Builder 
  2  #    Copyright (C) 2007-2008 Canonical Ltd. 
  3  #     
  4  #    See AUTHORS for list of contributors 
  5  # 
  6  #    This program is free software: you can redistribute it and/or modify 
  7  #    it under the terms of the GNU General Public License as published by 
  8  #    the Free Software Foundation, either version 3 of the License, or 
  9  #    (at your option) any later version. 
 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  #    CLI plugin 
 20  from gettext import gettext 
 21  import logging 
 22  import optparse 
 23  import sys 
 24  import textwrap 
 25  import VMBuilder 
 26  import VMBuilder.hypervisor 
 27  _ = gettext 
 28   
 29   
30 -class CLI(VMBuilder.Frontend):
31 arg = 'cli' 32
33 - def run(self):
34 try: 35 next = False 36 conf = None 37 for val in sys.argv: 38 if (val == '-c') | (val == '--config'): 39 next = True 40 elif next: 41 conf = val 42 break 43 44 vm = VMBuilder.VM(conf) 45 vm.register_setting('--rootsize', metavar='SIZE', type='int', default=4096, help='Size (in MB) of the root filesystem [default: %default]') 46 vm.register_setting('--optsize', metavar='SIZE', type='int', default=0, help='Size (in MB) of the /opt filesystem. If not set, no /opt filesystem will be added.') 47 vm.register_setting('--swapsize', metavar='SIZE', type='int', default=1024, help='Size (in MB) of the swap partition [default: %default]') 48 vm.register_setting('--raw', metavar='PATH', type='string', help="Specify a file (or block device) to as first disk image.") 49 vm.register_setting('--part', metavar='PATH', type='string', help="Allows to specify a partition table in PATH each line of partfile should specify (root first): \n mountpoint size \none per line, separated by space, where size is in megabytes. You can have up to 4 virtual disks, a new disk starts on a line containing only '---'. ie: \n root 2000 \n /boot 512 \n swap 1000 \n --- \n /var 8000 \n /var/log 2000") 50 self.set_usage(vm) 51 52 vm.optparser.disable_interspersed_args() 53 (foo, args) = vm.optparser.parse_args() 54 self.handle_args(vm, args) 55 vm.optparser.enable_interspersed_args() 56 57 for opt in vm.optparser.option_list + sum([grp.option_list for grp in vm.optparser.option_groups], []): 58 if len(opt._long_opts) > 1 or (opt.action == 'store' and opt._long_opts[0][2:] != opt.dest): 59 opt.help += " Config option: %s" % opt.dest 60 61 (settings, args) = vm.optparser.parse_args(values=optparse.Values()) 62 for (k,v) in settings.__dict__.iteritems(): 63 setattr(vm, k, v) 64 65 self.set_disk_layout(vm) 66 67 vm.create() 68 except VMBuilder.VMBuilderUserError, e: 69 print >> sys.stderr, e
70
71 - def set_usage(self, vm):
72 vm.optparser.set_usage('%prog hypervisor distro [options]') 73 vm.optparser.arg_help = (('hypervisor', vm.hypervisor_help), ('distro', vm.distro_help))
74
75 - def handle_args(self, vm, args):
76 if len(args) < 2: 77 vm.optparser.error("You need to specify at least the hypervisor type and the distro") 78 vm.set_hypervisor(args[0]) 79 vm.set_distro(args[1])
80
81 - def set_disk_layout(self, vm):
82 if not vm.part: 83 if vm.hypervisor.preferred_storage == VMBuilder.hypervisor.STORAGE_FS_IMAGE: 84 vm.add_filesystem(size='%dM' % vm.rootsize, type='ext3', mntpnt='/') 85 vm.add_filesystem(size='%dM' % vm.swapsize, type='swap', mntpnt=None) 86 if vm.optsize > 0: 87 vm.add_filesystem(size='%dM' % optsize, type='ext3', mntpnt='/opt') 88 else: 89 if vm.raw: 90 disk = vm.add_disk(filename=vm.raw, preallocated=True) 91 else: 92 size = vm.rootsize + vm.swapsize + vm.optsize 93 disk = vm.add_disk(size='%dM' % size) 94 offset = 0 95 disk.add_part(offset, vm.rootsize, 'ext3', '/') 96 offset += vm.rootsize 97 disk.add_part(offset, vm.swapsize, 'swap', 'swap') 98 offset += vm.swapsize 99 if vm.optsize > 0: 100 disk.add_part(offset, vm.optsize, 'ext3', '/opt') 101 else: 102 # We need to parse the file specified 103 if vm.hypervisor.preferred_storage == VMBuilder.hypervisor.STORAGE_FS_IMAGE: 104 try: 105 for line in file(vm.part): 106 elements = line.strip().split(' ') 107 if elements[0] == 'root': 108 vm.add_filesystem(elements[1], type='ext3', mntpnt='/') 109 elif elements[0] == 'swap': 110 vm.add_filesystem(elements[1], type='swap', mntpnt=None) 111 elif elements[0] == '---': 112 # We just ignore the user's attempt to specify multiple disks 113 pass 114 elif len(elements) == 3: 115 vm.add_filesystem(elements[1], type='ext3', mntpnt=elements[0], devletter='', device=elements[2], dummy=(int(elements[1]) == 0)) 116 else: 117 vm.add_filesystem(elements[1], type='ext3', mntpnt=elements[0]) 118 119 except IOError, (errno, strerror): 120 vm.optparser.error("%s parsing --part option: %s" % (errno, strerror)) 121 else: 122 try: 123 curdisk = list() 124 size = 0 125 for line in file(vm.part): 126 pair = line.strip().split(' ',1) 127 if pair[0] == '---': 128 self.do_disk(vm, curdisk, size) 129 curdisk = list() 130 size = 0 131 elif pair[0] != '': 132 logging.debug("part: %s, size: %d" % (pair[0], int(pair[1]))) 133 curdisk.append((pair[0], pair[1])) 134 size += int(pair[1]) 135 136 self.do_disk(vm, curdisk, size) 137 138 except IOError, (errno, strerror): 139 vm.optparser.error("%s parsing --part option: %s" % (errno, strerror))
140
141 - def do_disk(self, vm, curdisk, size):
142 disk = vm.add_disk(size+1) 143 logging.debug("do_disk - size: %d" % size) 144 offset = 0 145 for pair in curdisk: 146 logging.debug("do_disk - part: %s, size: %s, offset: %d" % (pair[0], pair[1], offset)) 147 if pair[0] == 'root': 148 disk.add_part(offset, int(pair[1]), 'ext3', '/') 149 elif pair[0] == 'swap': 150 disk.add_part(offset, int(pair[1]), pair[0], pair[0]) 151 else: 152 disk.add_part(offset, int(pair[1]), 'ext3', pair[0]) 153 offset += int(pair[1])
154
155 -class UVB(CLI):
156 arg = 'ubuntu-vm-builder' 157
158 - def set_usage(self, vm):
159 vm.optparser.set_usage('%prog hypervisor suite [options]') 160 vm.optparser.arg_help = (('hypervisor', vm.hypervisor_help), ('suite', self.suite_help))
161
162 - def suite_help(self):
163 return 'Suite. Valid options: %s' % " ".join(VMBuilder.plugins.ubuntu.distro.Ubuntu.suites)
164
165 - def handle_args(self, vm, args):
166 if len(args) < 2: 167 vm.optparser.error("You need to specify at least the hypervisor type and the suite") 168 vm.set_hypervisor(args[0]) 169 vm.set_distro('ubuntu') 170 vm.suite = args[1]
171 172 VMBuilder.register_frontend(CLI) 173 VMBuilder.register_frontend(UVB) 174