]>
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 '
44 default
='http://cdn.debian.net/debian/')
45 self
.settings
.add_string_setting(['arch'],
46 'architecture to use '
50 self
.settings
.add_string_setting(['distribution'],
51 'release to use (%default)',
55 def process_args(self
, args
):
56 if not self
.settings
['image']:
57 raise cliapp
.AppException('You must give image filename.')
58 if not self
.settings
['size']:
59 raise cliapp
.AppException('You must give image size.')
62 self
.mount_points
= []
65 self
.create_empty_image()
66 self
.partition_image()
68 rootdev
= self
.setup_kpartx()
70 rootdir
= self
.mount(rootdev
)
71 self
.debootstrap(rootdir
)
72 self
.set_root_password(rootdir
)
73 self
.install_extlinux(rootdev
, rootdir
)
80 def message(self
, msg
):
81 if self
.settings
['verbose']:
84 def runcmd(self
, argv
, stdin
='', ignore_fail
=False, **kwargs
):
85 logging
.debug('runcmd: %s %s' % (argv
, kwargs
))
86 p
= subprocess
.Popen(argv
, stdin
=subprocess
.PIPE
,
87 stdout
=subprocess
.PIPE
, stderr
=subprocess
.PIPE
,
89 out
, err
= p
.communicate(stdin
)
91 msg
= 'command failed: %s\n%s' % (argv
, err
)
94 raise cliapp
.AppException(msg
)
98 dirname
= tempfile
.mkdtemp()
99 self
.remove_dirs
.append(dirname
)
100 logging
.debug('mkdir %s' % dirname
)
103 def mount(self
, device
):
104 self
.message('Mounting %s' % device
)
105 mount_point
= self
.mkdtemp()
106 self
.runcmd(['mount', device
, mount_point
])
107 self
.mount_points
.append(mount_point
)
108 logging
.debug('mounted %s on %s' % (device
, mount_point
))
111 def create_empty_image(self
):
112 self
.message('Creating disk image')
113 self
.runcmd(['qemu-img', 'create', '-f', 'raw',
114 self
.settings
['image'],
115 str(self
.settings
['size'])])
117 def partition_image(self
):
118 self
.message('Creating partitions')
119 self
.runcmd(['parted', '-s', self
.settings
['image'],
121 self
.runcmd(['parted', '-s', self
.settings
['image'],
122 'mkpart', 'primary', '0%', '100%'])
123 self
.runcmd(['parted', '-s', self
.settings
['image'],
124 'set', '1', 'boot', 'on'])
126 def install_mbr(self
):
127 self
.message('Installing MBR')
128 self
.runcmd(['install-mbr', self
.settings
['image']])
130 def setup_kpartx(self
):
131 out
= self
.runcmd(['kpartx', '-av', self
.settings
['image']])
132 devices
= [line
.split()[2]
133 for line
in out
.splitlines()
134 if line
.startswith('add map ')]
135 if len(devices
) != 1:
136 raise cliapp
.AppException('Surprising number of partitions')
137 return '/dev/mapper/%s' % devices
[0]
139 def mkfs(self
, device
):
140 self
.message('Creating filesystem')
141 self
.runcmd(['mkfs', '-t', 'ext2', device
])
143 def debootstrap(self
, rootdir
):
144 self
.message('Debootstrapping')
146 if self
.settings
['arch'] == 'i386':
149 kernel_arch
= self
.settings
['arch']
150 kernel_image
= 'linux-image-2.6-%s' % kernel_arch
152 include
= [kernel_image
]
154 self
.runcmd(['debootstrap',
155 '--arch=%s' % self
.settings
['arch'],
156 '--include=%s' % ','.join(include
),
157 self
.settings
['distribution'],
159 self
.settings
['mirror']])
161 def set_root_password(self
, rootdir
):
162 self
.message('Removing root password')
163 self
.runcmd(['chroot', rootdir
, 'passwd', '-d', 'root'])
165 def install_extlinux(self
, rootdev
, rootdir
):
166 self
.message('Installing extlinux')
169 dirname
= os
.path
.join(rootdir
, 'boot')
170 basenames
= os
.listdir(dirname
)
171 logging
.debug('find: %s' % basenames
)
172 for basename
in basenames
:
173 if re
.search(pattern
, basename
):
174 return os
.path
.join('boot', basename
)
175 raise cliapp
.AppException('Cannot find match: %s' % pattern
)
177 kernel_image
= find('vmlinuz-.*')
178 initrd_image
= find('initrd.img-.*')
180 out
= self
.runcmd(['blkid', '-c', '/dev/null', '-o', 'value',
181 '-s', 'UUID', rootdev
])
182 uuid
= out
.splitlines()[0].strip()
184 conf
= os
.path
.join(rootdir
, 'extlinux.conf')
185 logging
.debug('configure extlinux %s' % conf
)
193 append initrd=%(initrd)s root=UUID=%(uuid)s ro
195 'kernel': kernel_image
,
196 'initrd': initrd_image
,
201 self
.runcmd(['extlinux', '--install', rootdir
])
202 self
.runcmd(['sync'])
203 import time
; time
.sleep(2)
206 # Clean up after any errors.
208 self
.message('Cleaning up')
210 for mount_point
in self
.mount_points
:
211 self
.runcmd(['umount', mount_point
], ignore_fail
=True)
213 self
.runcmd(['kpartx', '-d', self
.settings
['image']], ignore_fail
=True)
215 for dirname
in self
.remove_dirs
:
216 shutil
.rmtree(dirname
)
219 if __name__
== '__main__':
220 VmDebootstrap().run()