]>
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/>.
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')
58 def process_args(self
, args
):
59 if not self
.settings
['image']:
60 raise cliapp
.AppException('You must give image filename.')
61 if not self
.settings
['size']:
62 raise cliapp
.AppException('You must give image size.')
65 self
.mount_points
= []
68 self
.create_empty_image()
69 self
.partition_image()
71 rootdev
= self
.setup_kpartx()
73 rootdir
= self
.mount(rootdev
)
74 self
.debootstrap(rootdir
)
75 self
.set_root_password(rootdir
)
76 self
.remove_udev_persistent_rules(rootdir
)
77 self
.install_extlinux(rootdev
, rootdir
)
84 def message(self
, msg
):
85 if self
.settings
['verbose']:
88 def runcmd(self
, argv
, stdin
='', ignore_fail
=False, **kwargs
):
89 logging
.debug('runcmd: %s %s' % (argv
, kwargs
))
90 p
= subprocess
.Popen(argv
, stdin
=subprocess
.PIPE
,
91 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
,
93 out
, err
= p
.communicate(stdin
)
95 msg
= 'command failed: %s\n%s\n%s' % (argv
, out
, err
)
98 raise cliapp
.AppException(msg
)
102 dirname
= tempfile
.mkdtemp()
103 self
.remove_dirs
.append(dirname
)
104 logging
.debug('mkdir %s' % dirname
)
107 def mount(self
, device
):
108 self
.message('Mounting %s' % device
)
109 mount_point
= self
.mkdtemp()
110 self
.runcmd(['mount', device
, mount_point
])
111 self
.mount_points
.append(mount_point
)
112 logging
.debug('mounted %s on %s' % (device
, mount_point
))
115 def create_empty_image(self
):
116 self
.message('Creating disk image')
117 self
.runcmd(['qemu-img', 'create', '-f', 'raw',
118 self
.settings
['image'],
119 str(self
.settings
['size'])])
121 def partition_image(self
):
122 self
.message('Creating partitions')
123 self
.runcmd(['parted', '-s', self
.settings
['image'],
125 self
.runcmd(['parted', '-s', self
.settings
['image'],
126 'mkpart', 'primary', '0%', '100%'])
127 self
.runcmd(['parted', '-s', self
.settings
['image'],
128 'set', '1', 'boot', 'on'])
130 def install_mbr(self
):
131 self
.message('Installing MBR')
132 self
.runcmd(['install-mbr', self
.settings
['image']])
134 def setup_kpartx(self
):
135 out
= self
.runcmd(['kpartx', '-av', self
.settings
['image']])
136 devices
= [line
.split()[2]
137 for line
in out
.splitlines()
138 if line
.startswith('add map ')]
139 if len(devices
) != 1:
140 raise cliapp
.AppException('Surprising number of partitions')
141 return '/dev/mapper/%s' % devices
[0]
143 def mkfs(self
, device
):
144 self
.message('Creating filesystem')
145 self
.runcmd(['mkfs', '-t', 'ext2', device
])
147 def debootstrap(self
, rootdir
):
148 self
.message('Debootstrapping')
150 if self
.settings
['arch'] == 'i386':
153 kernel_arch
= self
.settings
['arch']
154 kernel_image
= 'linux-image-2.6-%s' % kernel_arch
156 include
= [kernel_image
] + self
.settings
['package']
158 self
.runcmd(['debootstrap',
159 '--arch=%s' % self
.settings
['arch'],
160 '--include=%s' % ','.join(include
),
161 self
.settings
['distribution'],
163 self
.settings
['mirror']])
165 def set_root_password(self
, rootdir
):
166 self
.message('Removing root password')
167 self
.runcmd(['chroot', rootdir
, 'passwd', '-d', 'root'])
169 def remove_udev_persistent_rules(self
, rootdir
):
170 self
.message('Removing udev persistent cd and net rules')
171 for x
in ['70-persistent-cd.rules', '70-persistent-net.rules']:
172 pathname
= os
.path
.join(rootdir
, 'etc', 'udev', 'rules.d', x
)
173 if os
.path
.exists(pathname
):
174 logging
.debug('rm %s' % pathname
)
177 logging
.debug('not removing non-existent %s' % pathname
)
179 def install_extlinux(self
, rootdev
, rootdir
):
180 self
.message('Installing extlinux')
183 dirname
= os
.path
.join(rootdir
, 'boot')
184 basenames
= os
.listdir(dirname
)
185 logging
.debug('find: %s' % basenames
)
186 for basename
in basenames
:
187 if re
.search(pattern
, basename
):
188 return os
.path
.join('boot', basename
)
189 raise cliapp
.AppException('Cannot find match: %s' % pattern
)
191 kernel_image
= find('vmlinuz-.*')
192 initrd_image
= find('initrd.img-.*')
194 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
195 '-s', 'UUID', rootdev
])
196 uuid
= out
.splitlines()[0].strip()
198 conf
= os
.path
.join(rootdir
, 'extlinux.conf')
199 logging
.debug('configure extlinux %s' % conf
)
207 append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet
209 'kernel': kernel_image
,
210 'initrd': initrd_image
,
215 self
.runcmd(['extlinux', '--install', rootdir
])
216 self
.runcmd(['sync'])
217 import time
; time
.sleep(2)
220 # Clean up after any errors.
222 self
.message('Cleaning up')
224 for mount_point
in self
.mount_points
:
225 self
.runcmd(['umount', mount_point
], ignore_fail
=True)
227 self
.runcmd(['kpartx', '-d', self
.settings
['image']], ignore_fail
=True)
229 for dirname
in self
.remove_dirs
:
230 shutil
.rmtree(dirname
)
233 if __name__
== '__main__':
234 VmDebootstrap().run()