Revision 74149d07 image_creator/bundle_volume.py
b/image_creator/bundle_volume.py | ||
---|---|---|
40 | 40 |
|
41 | 41 |
import parted |
42 | 42 |
|
43 |
from image_creator.rsync import Rsync |
|
43 | 44 |
from image_creator.util import get_command |
44 | 45 |
from image_creator.util import FatalError |
45 | 46 |
|
... | ... | |
92 | 93 |
if not os.path.isfile(f): |
93 | 94 |
raise FatalError("Unable to open: `%s'. File is missing." % f) |
94 | 95 |
|
95 |
FileSystemEntry = namedtuple('FileSystemEntry',
|
|
96 |
FileSystemTableEntry = namedtuple('FileSystemTableEntry',
|
|
96 | 97 |
'dev mpoint fs opts freq passno') |
97 | 98 |
with open(f) as table: |
98 | 99 |
for line in iter(table): |
99 | 100 |
entry = line.split('#')[0].strip().split() |
100 | 101 |
if len(entry) != 6: |
101 | 102 |
continue |
102 |
yield FileSystemEntry(*entry) |
|
103 |
yield FileSystemTableEntry(*entry)
|
|
103 | 104 |
|
104 | 105 |
def _get_root_partition(self): |
105 | 106 |
for entry in self._read_fstable('/etc/fstab'): |
... | ... | |
202 | 203 |
|
203 | 204 |
mount_options = self._get_mount_options( |
204 | 205 |
self.disk.getPartitionBySector(last.start).path) |
205 |
if mount_options is not None:
|
|
206 |
if mount_options is not None: |
|
206 | 207 |
stat = os.statvfs(mount_options.mpoint) |
207 | 208 |
# Shrink the last partition. The new size should be the size of the |
208 | 209 |
# occupied blocks |
... | ... | |
228 | 229 |
# Leave 2048 blocks at the end. |
229 | 230 |
new_end = new_size + 2048 |
230 | 231 |
|
231 |
|
|
232 | 232 |
if last.type == parted.PARTITION_LOGICAL: |
233 | 233 |
# Fix the extended partition |
234 | 234 |
extended = disk.getExtendedPartition() |
... | ... | |
273 | 273 |
for entry in self._read_fstable('/proc/mounts'): |
274 | 274 |
if entry.mpoint.startswith(os.path.abspath(target)): |
275 | 275 |
mpoints.append(entry.mpoint) |
276 |
|
|
276 |
|
|
277 | 277 |
mpoints.sort() |
278 | 278 |
for mpoint in reversed(mpoints): |
279 | 279 |
umount(mpoint) |
280 | 280 |
|
281 |
def _to_exclude(self): |
|
282 |
excluded = ['/tmp'] |
|
283 |
local_filesystems = MKFS_OPTS.keys() + ['rootfs'] |
|
284 |
for entry in self._read_fstable('/proc/mounts'): |
|
285 |
if entry.fs in local_filesystems: |
|
286 |
continue |
|
287 |
|
|
288 |
mpoint = entry.mpoint |
|
289 |
if mpoint in excluded: |
|
290 |
continue |
|
291 |
|
|
292 |
descendants = filter(lambda p: p.startswith(mpoint + '/'), |
|
293 |
excluded) |
|
294 |
if len(descendants): |
|
295 |
for d in descendants: |
|
296 |
excluded.remove(d) |
|
297 |
excluded.append(mpoint) |
|
298 |
continue |
|
299 |
|
|
300 |
dirname = mpoint |
|
301 |
basename = '' |
|
302 |
found_ancestor = False |
|
303 |
while dirname != '/': |
|
304 |
(dirname, basename) = os.path.split(dirname) |
|
305 |
if dirname in excluded: |
|
306 |
found_ancestor = True |
|
307 |
break |
|
308 |
|
|
309 |
if not found_ancestor: |
|
310 |
excluded.append(mpoint) |
|
311 |
|
|
312 |
return map(lambda d: d + "/*", excluded) |
|
313 |
|
|
281 | 314 |
def _create_filesystems(self, image): |
282 |
|
|
315 |
|
|
283 | 316 |
partitions = self._get_partitions(parted.Disk(parted.Device(image))) |
284 | 317 |
filesystems = {} |
285 | 318 |
for p in self.disk.partitions: |
... | ... | |
299 | 332 |
mapped = {} |
300 | 333 |
try: |
301 | 334 |
for p in mounted: |
302 |
i = p.num
|
|
335 |
i = p.num |
|
303 | 336 |
mapped[i] = self._map_partition(loop, i, p.start, p.end) |
304 | 337 |
|
305 | 338 |
# Create the file systems |
... | ... | |
315 | 348 |
absmpoints = self._mount(target, |
316 | 349 |
[(mapped[i], filesystems[i].mpoint) for i in mapped.keys()] |
317 | 350 |
) |
351 |
exclude = self._to_exclude() + [image] |
|
352 |
rsync = Rsync('/', target, exclude) |
|
353 |
msg = "Copying host files into the image" |
|
354 |
rsync.archive().run(self.out, msg) |
|
318 | 355 |
|
319 | 356 |
finally: |
320 | 357 |
self._umount_all(target) |
Also available in: Unified diff