]>
git.siccegge.de Git - forks/vmdebootstrap.git/blob - vmdebootstrap
2 # Copyright 2011, 2012 Lars Wirzenius
3 # Copyright 2012 Codethink Limited
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class VmDebootstrap(cliapp
.Application
):
33 def add_settings(self
):
34 default_arch
= 'amd64'
36 self
.settings
.boolean(['verbose'], 'report what is going on')
37 self
.settings
.string(['image'], 'put created disk image in FILE',
39 self
.settings
.bytesize(['size'],
40 'create a disk image of size SIZE (%default)',
43 self
.settings
.string(['tarball'], "tar up the disk's contents in FILE",
45 self
.settings
.string(['mirror'],
46 'use MIRROR as package source (%default)',
48 default
='http://cdn.debian.net/debian/')
49 self
.settings
.string(['arch'], 'architecture to use (%default)',
52 self
.settings
.string(['distribution'],
53 'release to use (%default)',
56 self
.settings
.string_list(['package'], 'install PACKAGE onto system')
57 self
.settings
.string_list(['custom-package'],
58 'install package in DEB file onto system '
61 self
.settings
.boolean(['no-kernel'], 'do not install a linux package')
62 self
.settings
.boolean(['enable-dhcp'], 'enable DHCP on eth0')
63 self
.settings
.string(['root-password'], 'set root password',
65 self
.settings
.boolean(['lock-root-password'],
66 'lock root account so they cannot login?')
67 self
.settings
.string(['customize'],
68 'run SCRIPT after setting up system',
70 self
.settings
.string(['hostname'],
71 'set name to HOSTNAME (%default)',
74 self
.settings
.string_list(['user'],
75 'create USER with PASSWORD',
76 metavar
='USER/PASSWORD')
77 self
.settings
.boolean(['serial-console'],
78 'configure image to use a serial console')
79 self
.settings
.string(['serial-console-command'],
80 'command to manage the serial console, appended '
81 'to /etc/inittab (%default)',
83 default
='/sbin/getty -L ttyS0 115200 vt100')
84 self
.settings
.boolean(['sudo'],
85 'install sudo, and if user is created, add them '
88 def process_args(self
, args
):
89 if not self
.settings
['image'] and not self
.settings
['tarball']:
90 raise cliapp
.AppException('You must give disk image filename, '
91 'or tarball filename')
92 if self
.settings
['image'] and not self
.settings
['size']:
93 raise cliapp
.AppException('If disk image is specified, '
94 'You must give image size.')
97 self
.mount_points
= []
100 if self
.settings
['image']:
101 self
.create_empty_image()
102 self
.partition_image()
104 rootdev
= self
.setup_kpartx()
106 rootdir
= self
.mount(rootdev
)
108 rootdir
= self
.mkdtemp()
109 self
.debootstrap(rootdir
)
110 self
.set_hostname(rootdir
)
111 self
.create_fstab(rootdir
)
112 self
.install_debs(rootdir
)
113 self
.cleanup_apt_cache(rootdir
)
114 self
.set_root_password(rootdir
)
115 self
.create_users(rootdir
)
116 self
.remove_udev_persistent_rules(rootdir
)
117 self
.setup_networking(rootdir
)
118 self
.customize(rootdir
)
119 if self
.settings
['image']:
120 self
.install_extlinux(rootdev
, rootdir
)
121 self
.optimize_image(rootdir
)
122 if self
.settings
['tarball']:
123 self
.create_tarball(rootdir
)
124 except BaseException
, e
:
125 self
.message('EEEK! Something bad happened...')
126 self
.cleanup_system()
129 self
.cleanup_system()
131 def message(self
, msg
):
133 if self
.settings
['verbose']:
136 def runcmd(self
, argv
, stdin
='', ignore_fail
=False, **kwargs
):
137 logging
.debug('runcmd: %s %s' % (argv
, kwargs
))
138 p
= subprocess
.Popen(argv
, stdin
=subprocess
.PIPE
,
139 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
,
141 out
, err
= p
.communicate(stdin
)
142 if p
.returncode
!= 0:
143 msg
= 'command failed: %s\n%s\n%s' % (argv
, out
, err
)
146 raise cliapp
.AppException(msg
)
150 dirname
= tempfile
.mkdtemp()
151 self
.remove_dirs
.append(dirname
)
152 logging
.debug('mkdir %s' % dirname
)
155 def mount(self
, device
):
156 self
.message('Mounting %s' % device
)
157 mount_point
= self
.mkdtemp()
158 self
.runcmd(['mount', device
, mount_point
])
159 self
.mount_points
.append(mount_point
)
160 logging
.debug('mounted %s on %s' % (device
, mount_point
))
163 def create_empty_image(self
):
164 self
.message('Creating disk image')
165 self
.runcmd(['qemu-img', 'create', '-f', 'raw',
166 self
.settings
['image'],
167 str(self
.settings
['size'])])
169 def partition_image(self
):
170 self
.message('Creating partitions')
171 self
.runcmd(['parted', '-s', self
.settings
['image'],
173 self
.runcmd(['parted', '-s', self
.settings
['image'],
174 'mkpart', 'primary', '0%', '100%'])
175 self
.runcmd(['parted', '-s', self
.settings
['image'],
176 'set', '1', 'boot', 'on'])
178 def install_mbr(self
):
179 self
.message('Installing MBR')
180 self
.runcmd(['install-mbr', self
.settings
['image']])
182 def setup_kpartx(self
):
183 out
= self
.runcmd(['kpartx', '-av', self
.settings
['image']])
184 devices
= [line
.split()[2]
185 for line
in out
.splitlines()
186 if line
.startswith('add map ')]
187 if len(devices
) != 1:
188 raise cliapp
.AppException('Surprising number of partitions')
189 return '/dev/mapper/%s' % devices
[0]
191 def mkfs(self
, device
):
192 self
.message('Creating filesystem')
193 self
.runcmd(['mkfs', '-t', 'ext2', device
])
195 def debootstrap(self
, rootdir
):
196 self
.message('Debootstrapping')
198 necessary_packages
= ['acpid']
200 include
= self
.settings
['package']
202 if not self
.settings
['no-kernel']:
203 if self
.settings
['arch'] == 'i386':
206 kernel_arch
= self
.settings
['arch']
207 kernel_image
= 'linux-image-%s' % kernel_arch
208 include
.append(kernel_image
)
210 if self
.settings
['sudo'] and 'sudo' not in include
:
211 include
.append('sudo')
213 args
= ['debootstrap', '--arch=%s' % self
.settings
['arch']]
215 '--include=%s' % ','.join(necessary_packages
+ include
))
216 args
+= [self
.settings
['distribution'],
217 rootdir
, self
.settings
['mirror']]
220 def set_hostname(self
, rootdir
):
221 hostname
= self
.settings
['hostname']
222 with
open(os
.path
.join(rootdir
, 'etc', 'hostname'), 'w') as f
:
223 f
.write('%s\n' % hostname
)
225 etc_hosts
= os
.path
.join(rootdir
, 'etc', 'hosts')
226 with
open(etc_hosts
, 'r') as f
:
228 with
open(etc_hosts
, 'w') as f
:
229 for line
in data
.splitlines():
230 if line
.startswith('127.0.0.1'):
231 line
+= ' %s' % hostname
232 f
.write('%s\n' % line
)
234 def create_fstab(self
, rootdir
):
235 fstab
= os
.path
.join(rootdir
, 'etc', 'fstab')
236 with
open(fstab
, 'w') as f
:
237 f
.write('proc /proc proc defaults 0 0\n')
238 f
.write('/dev/sda1 / ext4 errors=remount-ro 0 1\n')
240 def install_debs(self
, rootdir
):
241 if not self
.settings
['custom-package']:
243 self
.message('Installing custom packages')
244 tmp
= os
.path
.join(rootdir
, 'tmp', 'install_debs')
246 for deb
in self
.settings
['custom-package']:
247 shutil
.copy(deb
, tmp
)
248 filenames
= [os
.path
.join('/tmp/install_debs', os
.path
.basename(deb
))
249 for deb
in self
.settings
['custom-package']]
251 self
.runcmd_unchecked(['chroot', rootdir
, 'dpkg', '-i'] + filenames
)
252 logging
.debug('stdout:\n%s' % out
)
253 logging
.debug('stderr:\n%s' % err
)
254 out
= self
.runcmd(['chroot', rootdir
,
255 'apt-get', '-f', '--no-remove', 'install'])
256 logging
.debug('stdout:\n%s' % out
)
259 def cleanup_apt_cache(self
, rootdir
):
260 out
= self
.runcmd(['chroot', rootdir
, 'apt-get', 'clean'])
261 logging
.debug('stdout:\n%s' % out
)
263 def set_root_password(self
, rootdir
):
264 if self
.settings
['root-password']:
265 self
.message('Setting root password')
266 self
.set_password(rootdir
, 'root', self
.settings
['root-password'])
267 elif self
.settings
['lock-root-password']:
268 self
.message('Locking root password')
269 self
.runcmd(['chroot', rootdir
, 'passwd', '-l', 'root'])
271 self
.message('Give root an empty password')
272 self
.delete_password(rootdir
, 'root')
274 def create_users(self
, rootdir
):
275 def create_user(user
):
276 self
.runcmd(['chroot', rootdir
, 'adduser', '--gecos', user
,
277 '--disabled-password', user
])
278 if self
.settings
['sudo']:
279 self
.runcmd(['chroot', rootdir
, 'adduser', user
, 'sudo'])
281 for userpass
in self
.settings
['user']:
283 user
, password
= userpass
.split('/', 1)
285 self
.set_password(rootdir
, user
, password
)
287 create_user(userpass
)
288 self
.delete_password(rootdir
, userpass
)
290 def set_password(self
, rootdir
, user
, password
):
291 encrypted
= crypt
.crypt(password
, '..')
292 self
.runcmd(['chroot', rootdir
, 'usermod', '-p', encrypted
, user
])
294 def delete_password(self
, rootdir
, user
):
295 self
.runcmd(['chroot', rootdir
, 'passwd', '-d', user
])
297 def remove_udev_persistent_rules(self
, rootdir
):
298 self
.message('Removing udev persistent cd and net rules')
299 for x
in ['70-persistent-cd.rules', '70-persistent-net.rules']:
300 pathname
= os
.path
.join(rootdir
, 'etc', 'udev', 'rules.d', x
)
301 if os
.path
.exists(pathname
):
302 logging
.debug('rm %s' % pathname
)
305 logging
.debug('not removing non-existent %s' % pathname
)
307 def setup_networking(self
, rootdir
):
308 self
.message('Setting up networking')
310 f
= open(os
.path
.join(rootdir
, 'etc', 'network', 'interfaces'), 'w')
312 f
.write('iface lo inet loopback\n')
314 if self
.settings
['enable-dhcp']:
316 f
.write('auto eth0\n')
317 f
.write('iface eth0 inet dhcp\n')
321 def install_extlinux(self
, rootdev
, rootdir
):
322 self
.message('Installing extlinux')
325 dirname
= os
.path
.join(rootdir
, 'boot')
326 basenames
= os
.listdir(dirname
)
327 logging
.debug('find: %s' % basenames
)
328 for basename
in basenames
:
329 if re
.search(pattern
, basename
):
330 return os
.path
.join('boot', basename
)
331 raise cliapp
.AppException('Cannot find match: %s' % pattern
)
333 kernel_image
= find('vmlinuz-.*')
334 initrd_image
= find('initrd.img-.*')
336 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
337 '-s', 'UUID', rootdev
])
338 uuid
= out
.splitlines()[0].strip()
340 conf
= os
.path
.join(rootdir
, 'extlinux.conf')
341 logging
.debug('configure extlinux %s' % conf
)
349 append initrd=%(initrd)s root=UUID=%(uuid)s ro %(kserial)s
352 'kernel': kernel_image
,
353 'initrd': initrd_image
,
356 'console=ttyS0,115200' if self
.settings
['serial-console'] else '',
357 'extserial': 'serial 0 115200' if self
.settings
['serial-console'] else '',
361 if self
.settings
['serial-console']:
362 serial_command
= self
.settings
['serial-console-command']
363 logging
.debug('adding getty to serial console')
364 inittab
= os
.path
.join(rootdir
, 'etc/inittab')
365 with
open(inittab
, 'a') as f
:
366 f
.write('\nS0:23:respawn:%s\n' % serial_command
)
368 self
.runcmd(['extlinux', '--install', rootdir
])
369 self
.runcmd(['sync'])
370 import time
; time
.sleep(2)
372 def optimize_image(self
, rootdir
):
374 Filing up the image with zeros will increase its compression rate
376 zeros
= os
.path
.join(rootdir
, 'ZEROS')
377 self
.runcmd_unchecked(['dd', 'if=/dev/zero', 'of=' + zeros
, 'bs=1M'])
378 self
.runcmd(['rm', '-f', zeros
])
381 def cleanup_system(self
):
382 # Clean up after any errors.
384 self
.message('Cleaning up')
386 if self
.settings
['image']:
387 for mount_point
in self
.mount_points
:
388 self
.runcmd(['umount', mount_point
], ignore_fail
=True)
390 self
.runcmd(['kpartx', '-d', self
.settings
['image']], ignore_fail
=True)
392 for dirname
in self
.remove_dirs
:
393 shutil
.rmtree(dirname
)
395 def customize(self
, rootdir
):
396 script
= self
.settings
['customize']
398 self
.message('Running customize script %s' % script
)
399 with
open('/dev/tty', 'w') as tty
:
400 cliapp
.runcmd([script
, rootdir
], stdout
=tty
, stderr
=tty
)
402 def create_tarball(self
, rootdir
):
403 # Create a tarball of the disk's contents
404 # shell out to runcmd since it more easily handles rootdir
405 self
.message('Creating tarball of disk contents')
406 self
.runcmd(['tar', '-cf', self
.settings
['tarball'], '-C', rootdir
, '.'])
409 if __name__
== '__main__':
410 VmDebootstrap(version
=__version__
).run()