Revision 3b2f6619
b/image_creator/disk.py | ||
---|---|---|
19 | 19 |
|
20 | 20 |
|
21 | 21 |
class Disk(object): |
22 |
"""This class represents a hard disk hosting an Operating System |
|
23 |
|
|
24 |
A Disk instance never alters the source media it is created from. |
|
25 |
Any change is done on a snapshot created by the device-mapper of |
|
26 |
the Linux kernel. |
|
27 |
""" |
|
22 | 28 |
|
23 | 29 |
def __init__(self, source): |
30 |
"""Create a new Disk instance out of a source media. The source |
|
31 |
media can be an image file, a block device or a directory.""" |
|
24 | 32 |
self._cleanup_jobs = [] |
25 | 33 |
self._devices = [] |
26 | 34 |
self.source = source |
... | ... | |
38 | 46 |
raise NotImplementedError |
39 | 47 |
|
40 | 48 |
def cleanup(self): |
49 |
"""Cleanup internal data. This needs to be called before the |
|
50 |
program ends. |
|
51 |
""" |
|
41 | 52 |
while len(self._devices): |
42 | 53 |
device = self._devices.pop() |
43 | 54 |
device.destroy() |
... | ... | |
47 | 58 |
job(*args) |
48 | 59 |
|
49 | 60 |
def get_device(self): |
61 |
"""Returns a newly created DiskDevice instance. |
|
62 |
|
|
63 |
This instance is a snapshot of the original source media of |
|
64 |
the Disk instance. |
|
65 |
""" |
|
50 | 66 |
sourcedev = self.source |
51 | 67 |
mode = os.stat(self.source).st_mode |
52 | 68 |
if stat.S_ISDIR(mode): |
... | ... | |
79 | 95 |
return new_device |
80 | 96 |
|
81 | 97 |
def destroy_device(self, device): |
98 |
"""Destroys a DiskDevice instance previously created by |
|
99 |
get_device method. |
|
100 |
""" |
|
82 | 101 |
self._devices.remove(device) |
83 | 102 |
device.destroy() |
84 | 103 |
|
85 | 104 |
|
86 | 105 |
class DiskDevice(object): |
106 |
"""This class represents a block device hosting an Operating System |
|
107 |
as created by the device-mapper. |
|
108 |
""" |
|
87 | 109 |
|
88 | 110 |
def __init__(self, device, bootable=True): |
111 |
"""Create a new DiskDevice.""" |
|
89 | 112 |
self.device = device |
90 | 113 |
self.bootable = bootable |
91 | 114 |
|
... | ... | |
106 | 129 |
self.distro = self.g.inspect_get_distro(self.root) |
107 | 130 |
|
108 | 131 |
def destroy(self): |
132 |
"""Destroy this DiskDevice instance.""" |
|
109 | 133 |
self.g.umount_all() |
110 | 134 |
self.g.sync() |
111 | 135 |
# Close the guestfs handler |
112 | 136 |
self.g.close() |
113 |
del self.g |
|
114 | 137 |
|
115 | 138 |
def mount(self): |
139 |
"""Mount all disk partitions in a correct order.""" |
|
116 | 140 |
mps = self.g.inspect_get_mountpoints(self.root) |
117 | 141 |
|
118 | 142 |
# Sort the keys to mount the fs in a correct order. |
... | ... | |
132 | 156 |
print "%s (ignored)" % msg |
133 | 157 |
|
134 | 158 |
def umount(self): |
159 |
"""Umount all mounted filesystems.""" |
|
135 | 160 |
self.g.umount_all() |
136 | 161 |
|
137 | 162 |
def shrink(self): |
163 |
"""Shrink the disk. |
|
164 |
|
|
165 |
This is accomplished by shrinking the last filesystem in the |
|
166 |
disk and then updating the partition table. The new disk size |
|
167 |
(in bytes) is returned. |
|
168 |
""" |
|
138 | 169 |
dev = self.g.part_to_dev(self.root) |
139 | 170 |
parttype = self.g.part_get_parttype(dev) |
140 | 171 |
if parttype != 'msdos': |
... | ... | |
167 | 198 |
return (end + 1) * sector_size |
168 | 199 |
|
169 | 200 |
|
170 |
|
|
171 | 201 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/main.py | ||
---|---|---|
51 | 51 |
osclass = get_os_class(dev.distro, dev.ostype) |
52 | 52 |
image_os = osclass(dev.root, dev.g) |
53 | 53 |
metadata = image_os.get_metadata() |
54 |
for key in metadata.keys(): |
|
55 |
print "%s=%s" % (key, metadata[key]) |
|
54 | 56 |
image_os.data_cleanup() |
55 | 57 |
dev.umount() |
56 |
dev.shrink() |
|
58 |
#dev.shrink()
|
|
57 | 59 |
|
58 | 60 |
finally: |
59 | 61 |
disk.cleanup() |
b/image_creator/os_type/__init__.py | ||
---|---|---|
11 | 11 |
|
12 | 12 |
|
13 | 13 |
class OSBase(object): |
14 |
"""Basic operating system class""" |
|
14 | 15 |
def __init__(self, rootdev, ghandler): |
15 | 16 |
self.root = rootdev |
16 | 17 |
self.g = ghandler |
17 | 18 |
|
18 | 19 |
@add_prefix |
19 | 20 |
def ls(self, directory): |
21 |
"""List the name of all files under a directory""" |
|
20 | 22 |
return self.g.ls(directory) |
21 | 23 |
|
22 | 24 |
@add_prefix |
23 | 25 |
def find(self, directory): |
26 |
"""List the name of all files recursively under a directory""" |
|
24 | 27 |
return self.g.find(directory) |
25 | 28 |
|
26 | 29 |
def foreach_file(self, directory, action, **kargs): |
30 |
"""Perform an action recursively on all files under a directory. |
|
27 | 31 |
|
32 |
The following options are allowed: |
|
33 |
|
|
34 |
* maxdepth: If defined the action will not be performed on |
|
35 |
files that are below this level of directories under the |
|
36 |
directory parameter. |
|
37 |
|
|
38 |
* ftype: The action will only be performed on files of this |
|
39 |
type. For a list of all allowed filetypes, see here: |
|
40 |
http://libguestfs.org/guestfs.3.html#guestfs_readdir |
|
41 |
|
|
42 |
* exclude: Exclude all files that follow this pattern. |
|
43 |
""" |
|
28 | 44 |
maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth'] |
29 | 45 |
if maxdepth == 0: |
30 | 46 |
return |
... | ... | |
53 | 69 |
action(full_path) |
54 | 70 |
|
55 | 71 |
def get_metadata(self): |
72 |
"""Returnes some descriptive metadata of the OS.""" |
|
56 | 73 |
meta = {} |
57 | 74 |
meta["OSFAMILY"] = self.g.inspect_get_type(self.root) |
58 | 75 |
meta["OS"] = self.g.inspect_get_distro(self.root) |
... | ... | |
61 | 78 |
return meta |
62 | 79 |
|
63 | 80 |
def data_cleanup(self): |
81 |
"""Cleanup sesitive data out of the OS image.""" |
|
64 | 82 |
raise NotImplementedError |
65 | 83 |
|
66 | 84 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/os_type/slackware.py | ||
---|---|---|
3 | 3 |
|
4 | 4 |
class Slackware(Linux): |
5 | 5 |
def cleanup_log(self): |
6 |
# In slackware the the installed packages info are stored in
|
|
7 |
# /var/log/packages. Clearing all /var/log files will destroy
|
|
8 |
# the package management
|
|
6 |
# In slackware the metadata about installed packages are
|
|
7 |
# stored in /var/log/packages. Clearing all /var/log files
|
|
8 |
# will destroy the package management system.
|
|
9 | 9 |
self.foreach_file('/var/log', self.g.truncate, ftype='r', \ |
10 | 10 |
exclude='/var/log/packages') |
11 | 11 |
|
Also available in: Unified diff