1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import VMBuilder
21 from VMBuilder import register_distro, Distro
22 from VMBuilder.util import run_cmd
23 from VMBuilder.exception import VMBuilderUserError
24 import socket
25 import logging
26 import types
27 import os
28
30 name = 'Ubuntu'
31 arg = 'ubuntu'
32 suites = ['dapper', 'gutsy', 'hardy', 'intrepid', 'jaunty']
33
34
35 valid_archs = { 'amd64' : ['amd64', 'i386', 'lpia' ],
36 'i386' : [ 'i386', 'lpia' ],
37 'lpia' : [ 'i386', 'lpia' ] }
38
39 xen_kernel = ''
40
42 group = self.vm.setting_group('Package options')
43 group.add_option('--addpkg', action='append', metavar='PKG', help='Install PKG into the guest (can be specfied multiple times).')
44 group.add_option('--removepkg', action='append', metavar='PKG', help='Remove PKG from the guest (can be specfied multiple times)')
45 self.vm.register_setting_group(group)
46
47 group = self.vm.setting_group('General OS options')
48 self.host_arch = run_cmd('dpkg', '--print-architecture').rstrip()
49 group.add_option('-a', '--arch', default=self.host_arch, help='Specify the target architecture. Valid options: amd64 i386 lpia (defaults to host arch)')
50 group.add_option('--hostname', default='ubuntu', help='Set NAME as the hostname of the guest. Default: ubuntu. Also uses this name as the VM name.')
51 self.vm.register_setting_group(group)
52
53 group = self.vm.setting_group('Installation options')
54 group.add_option('--suite', default='jaunty', help='Suite to install. Valid options: %s [default: %%default]' % ' '.join(self.suites))
55 group.add_option('--flavour', '--kernel-flavour', help='Kernel flavour to use. Default and valid options depend on architecture and suite')
56 group.add_option('--iso', metavar='PATH', help='Use an iso image as the source for installation of file. Full path to the iso must be provided. If --mirror is also provided, it will be used in the final sources.list of the vm. This requires suite and kernel parameter to match what is available on the iso, obviously.')
57 group.add_option('--mirror', metavar='URL', help='Use Ubuntu mirror at URL instead of the default, which is http://archive.ubuntu.com/ubuntu for official arches and http://ports.ubuntu.com/ubuntu-ports otherwise')
58 group.add_option('--install-mirror', metavar='URL', help='Use Ubuntu mirror at URL for the installation only. Apt\'s sources.list will still use default or URL set by --mirror')
59 group.add_option('--security-mirror', metavar='URL', help='Use Ubuntu security mirror at URL instead of the default, which is http://security.ubuntu.com/ubuntu for official arches and http://ports.ubuntu.com/ubuntu-ports otherwise.')
60 group.add_option('--install-security-mirror', metavar='URL', help='Use the security mirror at URL for installation only. Apt\'s sources.list will still use default or URL set by --security-mirror')
61 group.add_option('--components', metavar='COMPS', help='A comma seperated list of distro components to include (e.g. main,universe).')
62 group.add_option('--ppa', metavar='PPA', action='append', help='Add ppa belonging to PPA to the vm\'s sources.list.')
63 group.add_option('--lang', metavar='LANG', default=self.get_locale(), help='Set the locale to LANG [default: %default]')
64 self.vm.register_setting_group(group)
65
66 group = self.vm.setting_group('Settings for the initial user')
67 group.add_option('--user', default='ubuntu', help='Username of initial user [default: %default]')
68 group.add_option('--name', default='Ubuntu', help='Full name of initial user [default: %default]')
69 group.add_option('--pass', default='ubuntu', help='Password of initial user [default: %default]')
70 self.vm.register_setting_group(group)
71
72 group = self.vm.setting_group('Other options')
73 group.add_option('--ssh-key', metavar='PATH', help='Add PATH to root\'s ~/.ssh/authorized_keys (WARNING: this has strong security implications).')
74 group.add_option('--ssh-user-key', help='Add PATH to the user\'s ~/.ssh/authorized_keys.')
75 self.vm.register_setting_group(group)
76
78 if not self.vm.mirror:
79 if self.vm.arch == 'lpia':
80 self.vm.mirror = 'http://ports.ubuntu.com/ubuntu-ports'
81 else:
82 self.vm.mirror = 'http://archive.ubuntu.com/ubuntu'
83
84 if not self.vm.security_mirror:
85 if self.vm.arch == 'lpia':
86 self.vm.security_mirror = 'http://ports.ubuntu.com/ubuntu-ports'
87 else:
88 self.vm.security_mirror = 'http://security.ubuntu.com/ubuntu'
89
90 if not self.vm.components:
91 self.vm.components = ['main', 'restricted', 'universe']
92 else:
93 self.vm.components = self.vm.components.split(',')
94
96 try:
97 return os.environ['LANG']
98 except:
99 return None
100
102 """While not all of these are strictly checks, their failure would inevitably
103 lead to failure, and since we can check them before we start setting up disk
104 and whatnot, we might as well go ahead an do this now."""
105
106 if not self.vm.suite in self.suites:
107 raise VMBuilderUserError('Invalid suite. Valid suites are: %s' % ' '.join(self.suites))
108
109 modname = 'VMBuilder.plugins.ubuntu.%s' % (self.vm.suite, )
110 mod = __import__(modname, fromlist=[self.vm.suite])
111 self.suite = getattr(mod, self.vm.suite.capitalize())(self.vm)
112
113 if self.vm.arch not in self.valid_archs[self.host_arch] or \
114 not self.suite.check_arch_validity(self.vm.arch):
115 raise VMBuilderUserError('%s is not a valid architecture. Valid architectures are: %s' % (self.vm.arch,
116 ' '.join(self.valid_archs[self.host_arch])))
117
118 if not self.vm.components:
119 self.vm.components = ['main', 'restricted', 'universe']
120 else:
121 if type(self.vm.components) is str:
122 self.vm.components = self.vm.components.split(',')
123
124 if self.vm.hypervisor.name == 'Xen':
125 logging.info('Xen kernel default: linux-image-%s %s', self.suite.xen_kernel_flavour, self.xen_kernel_version())
126
127 self.vm.virtio_net = self.use_virtio_net()
128
132
133 - def post_mount(self, fs):
135
138
140 devmapfile = '%s/device.map' % self.vm.workdir
141 devmap = open(devmapfile, 'w')
142 for (disk, id) in zip(self.vm.disks, range(len(self.vm.disks))):
143 devmap.write("(hd%d) %s\n" % (id, disk.filename))
144 devmap.close()
145 run_cmd('grub', '--device-map=%s' % devmapfile, '--batch', stdin='''root (hd0,0)
146 setup (hd0)
147 EOT''')
148
150 if self.suite.xen_kernel_flavour:
151 if not self.xen_kernel:
152 rmad = run_cmd('rmadison', 'linux-image-%s' % self.suite.xen_kernel_flavour)
153 version = ['0', '0','0', '0']
154
155 for line in rmad.splitlines():
156 sline = line.split('|')
157
158 if sline[2].strip().startswith(self.vm.suite):
159 vt = sline[1].strip().split('.')
160 for i in range(4):
161 if int(vt[i]) > int(version[i]):
162 version = vt
163 break
164
165 if version[0] == '0':
166 raise VMBuilderException('Something is wrong, no valid xen kernel for the suite %s found by rmadison' % self.vm.suite)
167
168 self.xen_kernel = '%s.%s.%s-%s' % (version[0],version[1],version[2],version[3])
169 return self.xen_kernel
170 else:
171 raise VMBuilderUserError('There is no valid xen kernel for the suite selected.')
172
176
180
181
182 register_distro(Ubuntu)
183