]>
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
.install_extlinux(rootdev
, rootdir
)
83 def message(self
, msg
):
84 if self
.settings
['verbose']:
87 def runcmd(self
, argv
, stdin
='', ignore_fail
=False, **kwargs
):
88 logging
.debug('runcmd: %s %s' % (argv
, kwargs
))
89 p
= subprocess
.Popen(argv
, stdin
=subprocess
.PIPE
,
90 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
,
92 out
, err
= p
.communicate(stdin
)
94 msg
= 'command failed: %s\n%s\n%s' % (argv
, out
, err
)
97 raise cliapp
.AppException(msg
)
101 dirname
= tempfile
.mkdtemp()
102 self
.remove_dirs
.append(dirname
)
103 logging
.debug('mkdir %s' % dirname
)
106 def mount(self
, device
):
107 self
.message('Mounting %s' % device
)
108 mount_point
= self
.mkdtemp()
109 self
.runcmd(['mount', device
, mount_point
])
110 self
.mount_points
.append(mount_point
)
111 logging
.debug('mounted %s on %s' % (device
, mount_point
))
114 def create_empty_image(self
):
115 self
.message('Creating disk image')
116 self
.runcmd(['qemu-img', 'create', '-f', 'raw',
117 self
.settings
['image'],
118 str(self
.settings
['size'])])
120 def partition_image(self
):
121 self
.message('Creating partitions')
122 self
.runcmd(['parted', '-s', self
.settings
['image'],
124 self
.runcmd(['parted', '-s', self
.settings
['image'],
125 'mkpart', 'primary', '0%', '100%'])
126 self
.runcmd(['parted', '-s', self
.settings
['image'],
127 'set', '1', 'boot', 'on'])
129 def install_mbr(self
):
130 self
.message('Installing MBR')
131 self
.runcmd(['install-mbr', self
.settings
['image']])
133 def setup_kpartx(self
):
134 out
= self
.runcmd(['kpartx', '-av', self
.settings
['image']])
135 devices
= [line
.split()[2]
136 for line
in out
.splitlines()
137 if line
.startswith('add map ')]
138 if len(devices
) != 1:
139 raise cliapp
.AppException('Surprising number of partitions')
140 return '/dev/mapper/%s' % devices
[0]
142 def mkfs(self
, device
):
143 self
.message('Creating filesystem')
144 self
.runcmd(['mkfs', '-t', 'ext2', device
])
146 def debootstrap(self
, rootdir
):
147 self
.message('Debootstrapping')
149 if self
.settings
['arch'] == 'i386':
152 kernel_arch
= self
.settings
['arch']
153 kernel_image
= 'linux-image-2.6-%s' % kernel_arch
155 include
= [kernel_image
] + self
.settings
['package']
157 self
.runcmd(['debootstrap',
158 '--arch=%s' % self
.settings
['arch'],
159 '--include=%s' % ','.join(include
),
160 self
.settings
['distribution'],
162 self
.settings
['mirror']])
164 def set_root_password(self
, rootdir
):
165 self
.message('Removing root password')
166 self
.runcmd(['chroot', rootdir
, 'passwd', '-d', 'root'])
168 def install_extlinux(self
, rootdev
, rootdir
):
169 self
.message('Installing extlinux')
172 dirname
= os
.path
.join(rootdir
, 'boot')
173 basenames
= os
.listdir(dirname
)
174 logging
.debug('find: %s' % basenames
)
175 for basename
in basenames
:
176 if re
.search(pattern
, basename
):
177 return os
.path
.join('boot', basename
)
178 raise cliapp
.AppException('Cannot find match: %s' % pattern
)
180 kernel_image
= find('vmlinuz-.*')
181 initrd_image
= find('initrd.img-.*')
183 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
184 '-s', 'UUID', rootdev
])
185 uuid
= out
.splitlines()[0].strip()
187 conf
= os
.path
.join(rootdir
, 'extlinux.conf')
188 logging
.debug('configure extlinux %s' % conf
)
196 append initrd=%(initrd)s root=UUID=%(uuid)s ro quiet
198 'kernel': kernel_image
,
199 'initrd': initrd_image
,
204 self
.runcmd(['extlinux', '--install', rootdir
])
205 self
.runcmd(['sync'])
206 import time
; time
.sleep(2)
209 # Clean up after any errors.
211 self
.message('Cleaning up')
213 for mount_point
in self
.mount_points
:
214 self
.runcmd(['umount', mount_point
], ignore_fail
=True)
216 self
.runcmd(['kpartx', '-d', self
.settings
['image']], ignore_fail
=True)
218 for dirname
in self
.remove_dirs
:
219 shutil
.rmtree(dirname
)
222 if __name__
== '__main__':
223 VmDebootstrap().run()