]>
git.siccegge.de Git - forks/vmdebootstrap.git/blob - vmdebootstrap
ce163b4f47a474112e4087e8b68d5f2adf18c05c
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/>.
26 class VmDebootstrap(cliapp
.Application
):
28 def add_settings(self
):
29 default_arch
= 'amd64'
31 self
.settings
.add_boolean_setting(['verbose'],
32 'report what is going on')
33 self
.settings
.add_string_setting(['image'],
34 'put created disk image in FILE',
36 self
.settings
.add_bytesize_setting(['size'],
37 'create a disk image of size SIZE '
41 self
.settings
.add_string_setting(['mirror'],
42 'use MIRROR as package source '
45 default
='http://cdn.debian.net/debian/')
46 self
.settings
.add_string_setting(['arch'],
47 'architecture to use '
51 self
.settings
.add_string_setting(['distribution'],
52 'release to use (%default)',
55 self
.settings
.add_string_list_setting(['package'],
56 'install PACKAGE onto system')
57 self
.settings
.add_boolean_setting(['enable-dhcp'],
58 'enable DHCP on eth0')
60 def process_args(self
, args
):
61 if not self
.settings
['image']:
62 raise cliapp
.AppException('You must give image filename.')
63 if not self
.settings
['size']:
64 raise cliapp
.AppException('You must give image size.')
67 self
.mount_points
= []
70 self
.create_empty_image()
71 self
.partition_image()
73 rootdev
= self
.setup_kpartx()
75 rootdir
= self
.mount(rootdev
)
76 self
.debootstrap(rootdir
)
77 self
.set_root_password(rootdir
)
78 self
.remove_udev_persistent_rules(rootdir
)
79 self
.setup_networking(rootdir
)
80 self
.install_extlinux(rootdev
, rootdir
)
87 def message(self
, msg
):
88 if self
.settings
['verbose']:
91 def runcmd(self
, argv
, stdin
='', ignore_fail
=False, **kwargs
):
92 logging
.debug('runcmd: %s %s' % (argv
, kwargs
))
93 p
= subprocess
.Popen(argv
, stdin
=subprocess
.PIPE
,
94 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
,
96 out
, err
= p
.communicate(stdin
)
98 msg
= 'command failed: %s\n%s\n%s' % (argv
, out
, err
)
101 raise cliapp
.AppException(msg
)
105 dirname
= tempfile
.mkdtemp()
106 self
.remove_dirs
.append(dirname
)
107 logging
.debug('mkdir %s' % dirname
)
110 def mount(self
, device
):
111 self
.message('Mounting %s' % device
)
112 mount_point
= self
.mkdtemp()
113 self
.runcmd(['mount', device
, mount_point
])
114 self
.mount_points
.append(mount_point
)
115 logging
.debug('mounted %s on %s' % (device
, mount_point
))
118 def create_empty_image(self
):
119 self
.message('Creating disk image')
120 self
.runcmd(['qemu-img', 'create', '-f', 'raw',
121 self
.settings
['image'],
122 str(self
.settings
['size'])])
124 def partition_image(self
):
125 self
.message('Creating partitions')
126 self
.runcmd(['parted', '-s', self
.settings
['image'],
128 self
.runcmd(['parted', '-s', self
.settings
['image'],
129 'mkpart', 'primary', '0%', '100%'])
130 self
.runcmd(['parted', '-s', self
.settings
['image'],
131 'set', '1', 'boot', 'on'])
133 def install_mbr(self
):
134 self
.message('Installing MBR')
135 self
.runcmd(['install-mbr', self
.settings
['image']])
137 def setup_kpartx(self
):
138 out
= self
.runcmd(['kpartx', '-av', self
.settings
['image']])
139 devices
= [line
.split()[2]
140 for line
in out
.splitlines()
141 if line
.startswith('add map ')]
142 if len(devices
) != 1:
143 raise cliapp
.AppException('Surprising number of partitions')
144 return '/dev/mapper/%s' % devices
[0]
146 def mkfs(self
, device
):
147 self
.message('Creating filesystem')
148 self
.runcmd(['mkfs', '-t', 'ext2', device
])
150 def debootstrap(self
, rootdir
):
151 self
.message('Debootstrapping')
153 if self
.settings
['arch'] == 'i386':
156 kernel_arch
= self
.settings
['arch']
157 kernel_image
= 'linux-image-2.6-%s' % kernel_arch
159 include
= [kernel_image
] + self
.settings
['package']
161 self
.runcmd(['debootstrap',
162 '--arch=%s' % self
.settings
['arch'],
163 '--include=%s' % ','.join(include
),
164 self
.settings
['distribution'],
166 self
.settings
['mirror']])
168 def set_root_password(self
, rootdir
):
169 self
.message('Removing root password')
170 self
.runcmd(['chroot', rootdir
, 'passwd', '-d', 'root'])
172 def remove_udev_persistent_rules(self
, rootdir
):
173 self
.message('Removing udev persistent cd and net rules')
174 for x
in ['70-persistent-cd.rules', '70-persistent-net.rules']:
175 pathname
= os
.path
.join(rootdir
, 'etc', 'udev', 'rules.d', x
)
176 if os
.path
.exists(pathname
):
177 logging
.debug('rm %s' % pathname
)
180 logging
.debug('not removing non-existent %s' % pathname
)
182 def setup_networking(self
, rootdir
):
183 self
.message('Setting up networking')
185 f
= open(os
.path
.join(rootdir
, 'etc', 'network', 'interfaces'), 'w')
187 f
.write('iface lo inet loopback\n')
189 if self
.settings
['enable-dhcp']:
191 f
.write('allow-hotplug eth0\n')
192 f
.write('iface eth0 inet dhcp\n')
196 def install_extlinux(self
, rootdev
, rootdir
):
197 self
.message('Installing extlinux')
200 dirname
= os
.path
.join(rootdir
, 'boot')
201 basenames
= os
.listdir(dirname
)
202 logging
.debug('find: %s' % basenames
)
203 for basename
in basenames
:
204 if re
.search(pattern
, basename
):
205 return os
.path
.join('boot', basename
)
206 raise cliapp
.AppException('Cannot find match: %s' % pattern
)
208 kernel_image
= find('vmlinuz-.*')
209 initrd_image
= find('initrd.img-.*')
211 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
212 '-s', 'UUID', rootdev
])
213 uuid
= out
.splitlines()[0].strip()
215 conf
= os
.path
.join(rootdir
, 'extlinux.conf')
216 logging
.debug('configure extlinux %s' % conf
)
224 append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet
226 'kernel': kernel_image
,
227 'initrd': initrd_image
,
232 self
.runcmd(['extlinux', '--install', rootdir
])
233 self
.runcmd(['sync'])
234 import time
; time
.sleep(2)
237 # Clean up after any errors.
239 self
.message('Cleaning up')
241 for mount_point
in self
.mount_points
:
242 self
.runcmd(['umount', mount_point
], ignore_fail
=True)
244 self
.runcmd(['kpartx', '-d', self
.settings
['image']], ignore_fail
=True)
246 for dirname
in self
.remove_dirs
:
247 shutil
.rmtree(dirname
)
250 if __name__
== '__main__':
251 VmDebootstrap().run()