Revision 71b0ab28 image_creator/os_type/__init__.py

b/image_creator/os_type/__init__.py
77 77
        self.root = rootdev
78 78
        self.g = ghandler
79 79
        self.out = output
80

  
81
        # Collect metadata about the OS
82 80
        self.meta = {}
81

  
82
    def collect_metadata(self):
83
        """Collect metadata about the OS"""
84

  
85
        try:
86
            if not self.mount(readonly=True):
87
                raise FatalError("Unable to mount the media read-only")
88

  
89
            self.out.output('Collecting image metadata ...', False)
90
            self._do_collect_metadata()
91
            self.out.success('done')
92
        finally:
93
            self.umount()
94

  
95
    def _do_collect_metadata(self):
96

  
83 97
        self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
84 98
        self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
85 99
        self.meta['OS'] = self.g.inspect_get_distro(self.root)
......
212 226
    def do_sysprep(self):
213 227
        """Prepere system for image creation."""
214 228

  
215
        self.out.output('Preparing system for image creation:')
229
        try:
230
            if not self.mount(readonly=False):
231
                raise FatalError("Unable to mount the media read-write")
232

  
233
            self.out.output('Preparing system for image creation:')
234

  
235
            tasks = self.list_syspreps()
236
            enabled = filter(lambda x: x.enabled, tasks)
237

  
238
            size = len(enabled)
239
            cnt = 0
240
            for task in enabled:
241
                cnt += 1
242
                self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
243
                task()
244
                setattr(task.im_func, 'executed', True)
245
            self.out.output()
246
        finally:
247
            self.umount()
248

  
249
    def _do_mount(self, readonly):
250
        try:
251
            self.g.mount_options('ro' if readonly else 'rw', self.root, '/')
252
        except RuntimeError as msg:
253
            self.out.warn("unable to mount the root partition: %s" % msg)
254
            return False
255

  
256
        return True
257

  
258
    def mount(self, readonly=False):
259
        """Mount image."""
260

  
261
        if getattr(self, "mounted", False):
262
            return True
263

  
264
        mount_type = 'read-only' if readonly else 'read-write'
265
        self.out.output("Mount the media %s ..." % mount_type, False)
266

  
267
        if not self._do_mount(readonly):
268
            return False
216 269

  
217
        tasks = self.list_syspreps()
218
        enabled = filter(lambda x: x.enabled, tasks)
270
        self.mounted = True
271
        self.out.success('done')
272
        return True
219 273

  
220
        size = len(enabled)
221
        cnt = 0
222
        for task in enabled:
223
            cnt += 1
224
            self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False)
225
            task()
226
            setattr(task.im_func, 'executed', True)
227
        self.out.output()
274
    def umount(self):
275
        """Umount all mounted filesystems."""
276
        self.g.umount_all()
277
        self.mounted = False
228 278

  
229 279
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :

Also available in: Unified diff