Revision 71b0ab28 image_creator/image.py

b/image_creator/image.py
53 53
        self.progress_bar = None
54 54
        self.guestfs_device = None
55 55
        self.size = 0
56
        self.mounted = False
57
        self.mounted_ro = False
58 56

  
59 57
        self.g = guestfs.GuestFS()
60 58
        self.g.add_drive_opts(self.device, readonly=0, format="raw")
......
120 118
        if not self.guestfs_enabled:
121 119
            self.enable()
122 120

  
123
        if not self.mounted:
124
            do_unmount = True
125
            self.mount(readonly=True)
126
        else:
127
            do_unmount = False
128

  
129
        try:
130
            cls = os_cls(self.distro, self.ostype)
131
            self._os = cls(self.root, self.g, self.out)
121
        cls = os_cls(self.distro, self.ostype)
122
        self._os = cls(self.root, self.g, self.out)
132 123

  
133
        finally:
134
            if do_unmount:
135
                self.umount()
124
        self._os.collect_metadata()
136 125

  
137 126
        return self._os
138 127

  
......
156 145
#
157 146
#        self.progressbar.goto((position * 100) // total)
158 147

  
159
    def mount(self, readonly=False):
160
        """Mount all disk partitions in a correct order."""
161

  
162
        msg = "Mounting the media%s ..." % (" read-only" if readonly else "")
163
        self.out.output(msg, False)
164

  
165
        #If something goes wrong when mounting rw, remount the filesystem ro
166
        remount_ro = False
167
        rw_mpoints = ('/', '/etc', '/root', '/home', '/var')
168

  
169
        # Sort the keys to mount the fs in a correct order.
170
        # / should be mounted befor /boot, etc
171
        def compare(a, b):
172
            if len(a[0]) > len(b[0]):
173
                return 1
174
            elif len(a[0]) == len(b[0]):
175
                return 0
176
            else:
177
                return -1
178
        mps = self.g.inspect_get_mountpoints(self.root)
179
        mps.sort(compare)
180

  
181
        mopts = 'ro' if readonly else 'rw'
182
        for mp, dev in mps:
183
            if self.ostype == 'freebsd':
184
                # libguestfs can't handle correct freebsd partitions on GUID
185
                # Partition Table. We have to do the translation to linux
186
                # device names ourselves
187
                m = re.match('^/dev/((?:ada)|(?:vtbd))(\d+)p(\d+)$', dev)
188
                if m:
189
                    m2 = int(m.group(2))
190
                    m3 = int(m.group(3))
191
                    dev = '/dev/sd%c%d' % (chr(ord('a') + m2), m3)
192
            try:
193
                self.g.mount_options(mopts, dev, mp)
194
            except RuntimeError as msg:
195
                if self.ostype == 'freebsd':
196
                    freebsd_mopts = "ufstype=ufs2,%s" % mopts
197
                    try:
198
                        self.g.mount_vfs(freebsd_mopts, 'ufs', dev, mp)
199
                    except RuntimeError as msg:
200
                        if readonly is False and mp in rw_mpoints:
201
                            remount_ro = True
202
                            break
203
                elif readonly is False and mp in rw_mpoints:
204
                    remount_ro = True
205
                    break
206
                else:
207
                    self.out.warn("%s (ignored)" % msg)
208
        if remount_ro:
209
            self.out.warn("Unable to mount %s read-write. "
210
                          "Remounting everything read-only..." % mp)
211
            self.umount()
212
            self.mount(True)
213
        else:
214
            self.mounted = True
215
            self.mounted_ro = readonly
216
            self.out.success("done")
217

  
218
    def umount(self):
219
        """Umount all mounted filesystems."""
220
        self.g.umount_all()
221
        self.mounted = False
222

  
223 148
    def _last_partition(self):
224 149
        """Return the last partition of the image disk"""
225 150
        if self.meta['PARTITION_TABLE'] not in 'msdos' 'gpt':

Also available in: Unified diff