]>
git.siccegge.de Git - forks/vmdebootstrap.git/blob - vmdebootstrap
2 # Copyright 2011 Lars Wirzenius
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 class VmDebootstrap(cliapp
.Application
):
29 def add_settings(self
):
30 default_arch
= 'amd64'
32 self
.settings
.add_boolean_setting(['verbose'],
33 'report what is going on')
34 self
.settings
.add_string_setting(['image'],
35 'put created disk image in FILE',
37 self
.settings
.add_bytesize_setting(['size'],
38 'create a disk image of size SIZE '
42 self
.settings
.add_string_setting(['mirror'],
43 'use MIRROR as package source '
46 default
='http://cdn.debian.net/debian/')
47 self
.settings
.add_string_setting(['arch'],
48 'architecture to use '
52 self
.settings
.add_string_setting(['distribution'],
53 'release to use (%default)',
56 self
.settings
.add_string_list_setting(['package'],
57 'install PACKAGE onto system')
58 self
.settings
.add_boolean_setting(['enable-dhcp'],
59 'enable DHCP on eth0')
60 self
.settings
.add_string_setting(['root-password'],
63 self
.settings
.add_string_setting(['customize'],
64 'run SCRIPT after setting up system',
67 def process_args(self
, args
):
68 if not self
.settings
['image']:
69 raise cliapp
.AppException('You must give image filename.')
70 if not self
.settings
['size']:
71 raise cliapp
.AppException('You must give image size.')
74 self
.mount_points
= []
77 self
.create_empty_image()
78 self
.partition_image()
80 rootdev
= self
.setup_kpartx()
82 rootdir
= self
.mount(rootdev
)
83 self
.debootstrap(rootdir
)
84 self
.set_root_password(rootdir
)
85 self
.remove_udev_persistent_rules(rootdir
)
86 self
.setup_networking(rootdir
)
87 self
.install_extlinux(rootdev
, rootdir
)
88 self
.customize(rootdir
)
95 def message(self
, msg
):
96 if self
.settings
['verbose']:
99 def runcmd(self
, argv
, stdin
='', ignore_fail
=False, **kwargs
):
100 logging
.debug('runcmd: %s %s' % (argv
, kwargs
))
101 p
= subprocess
.Popen(argv
, stdin
=subprocess
.PIPE
,
102 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
,
104 out
, err
= p
.communicate(stdin
)
105 if p
.returncode
!= 0:
106 msg
= 'command failed: %s\n%s\n%s' % (argv
, out
, err
)
109 raise cliapp
.AppException(msg
)
113 dirname
= tempfile
.mkdtemp()
114 self
.remove_dirs
.append(dirname
)
115 logging
.debug('mkdir %s' % dirname
)
118 def mount(self
, device
):
119 self
.message('Mounting %s' % device
)
120 mount_point
= self
.mkdtemp()
121 self
.runcmd(['mount', device
, mount_point
])
122 self
.mount_points
.append(mount_point
)
123 logging
.debug('mounted %s on %s' % (device
, mount_point
))
126 def create_empty_image(self
):
127 self
.message('Creating disk image')
128 self
.runcmd(['qemu-img', 'create', '-f', 'raw',
129 self
.settings
['image'],
130 str(self
.settings
['size'])])
132 def partition_image(self
):
133 self
.message('Creating partitions')
134 self
.runcmd(['parted', '-s', self
.settings
['image'],
136 self
.runcmd(['parted', '-s', self
.settings
['image'],
137 'mkpart', 'primary', '0%', '100%'])
138 self
.runcmd(['parted', '-s', self
.settings
['image'],
139 'set', '1', 'boot', 'on'])
141 def install_mbr(self
):
142 self
.message('Installing MBR')
143 self
.runcmd(['install-mbr', self
.settings
['image']])
145 def setup_kpartx(self
):
146 out
= self
.runcmd(['kpartx', '-av', self
.settings
['image']])
147 devices
= [line
.split()[2]
148 for line
in out
.splitlines()
149 if line
.startswith('add map ')]
150 if len(devices
) != 1:
151 raise cliapp
.AppException('Surprising number of partitions')
152 return '/dev/mapper/%s' % devices
[0]
154 def mkfs(self
, device
):
155 self
.message('Creating filesystem')
156 self
.runcmd(['mkfs', '-t', 'ext2', device
])
158 def debootstrap(self
, rootdir
):
159 self
.message('Debootstrapping')
161 if self
.settings
['arch'] == 'i386':
164 kernel_arch
= self
.settings
['arch']
165 kernel_image
= 'linux-image-2.6-%s' % kernel_arch
167 include
= [kernel_image
] + self
.settings
['package']
169 self
.runcmd(['debootstrap',
170 '--arch=%s' % self
.settings
['arch'],
171 '--include=%s' % ','.join(include
),
172 self
.settings
['distribution'],
174 self
.settings
['mirror']])
176 def set_root_password(self
, rootdir
):
177 if self
.settings
['root-password']:
178 self
.message('Setting root password')
179 encrypted
= crypt
.crypt(self
.settings
['root-password'], '..')
180 self
.runcmd(['chroot', rootdir
, 'usermod', '-p', encrypted
,
183 self
.message('Locking root password')
184 self
.runcmd(['chroot', rootdir
, 'passwd', '-l', 'root'])
186 def remove_udev_persistent_rules(self
, rootdir
):
187 self
.message('Removing udev persistent cd and net rules')
188 for x
in ['70-persistent-cd.rules', '70-persistent-net.rules']:
189 pathname
= os
.path
.join(rootdir
, 'etc', 'udev', 'rules.d', x
)
190 if os
.path
.exists(pathname
):
191 logging
.debug('rm %s' % pathname
)
194 logging
.debug('not removing non-existent %s' % pathname
)
196 def setup_networking(self
, rootdir
):
197 self
.message('Setting up networking')
199 f
= open(os
.path
.join(rootdir
, 'etc', 'network', 'interfaces'), 'w')
201 f
.write('iface lo inet loopback\n')
203 if self
.settings
['enable-dhcp']:
205 f
.write('allow-hotplug eth0\n')
206 f
.write('iface eth0 inet dhcp\n')
210 def install_extlinux(self
, rootdev
, rootdir
):
211 self
.message('Installing extlinux')
214 dirname
= os
.path
.join(rootdir
, 'boot')
215 basenames
= os
.listdir(dirname
)
216 logging
.debug('find: %s' % basenames
)
217 for basename
in basenames
:
218 if re
.search(pattern
, basename
):
219 return os
.path
.join('boot', basename
)
220 raise cliapp
.AppException('Cannot find match: %s' % pattern
)
222 kernel_image
= find('vmlinuz-.*')
223 initrd_image
= find('initrd.img-.*')
225 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
226 '-s', 'UUID', rootdev
])
227 uuid
= out
.splitlines()[0].strip()
229 conf
= os
.path
.join(rootdir
, 'extlinux.conf')
230 logging
.debug('configure extlinux %s' % conf
)
238 append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet
240 'kernel': kernel_image
,
241 'initrd': initrd_image
,
246 self
.runcmd(['extlinux', '--install', rootdir
])
247 self
.runcmd(['sync'])
248 import time
; time
.sleep(2)
251 # Clean up after any errors.
253 self
.message('Cleaning up')
255 for mount_point
in self
.mount_points
:
256 self
.runcmd(['umount', mount_point
], ignore_fail
=True)
258 self
.runcmd(['kpartx', '-d', self
.settings
['image']], ignore_fail
=True)
260 for dirname
in self
.remove_dirs
:
261 shutil
.rmtree(dirname
)
263 def customize(self
, rootdir
):
264 script
= self
.settings
['customize']
266 self
.message('Running customize script %s' % script
)
267 self
.runcmd([script
, rootdir
])
270 if __name__
== '__main__':
271 VmDebootstrap().run()