1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
#!/usr/bin/python -tt
#
# Copyright (c) 2007 Red Hat Inc.
# Copyright (c) 2009, 2010, 2011 Intel, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; version 2 of the License
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from __future__ import with_statement
import os, sys
import tempfile
import shutil
from mic import kickstart
from mic import msger
from mic.utils.errors import CreatorError
from mic.utils import misc, runner, fs_related as fs
class BaseImageCreator(object):
"""Base class for image creation.
BaseImageCreator is the simplest creator class available; it will
create a system image according to the supplied kickstart file.
e.g.
import mic.imgcreate as imgcreate
ks = imgcreate.read_kickstart("foo.ks")
imgcreate.ImageCreator(ks, "foo").create()
"""
def __del__(self):
self.cleanup()
def __init__(self, createopts = None):
"""Initialize an ImageCreator instance.
ks -- a pykickstart.KickstartParser instance; this instance will be
used to drive the install by e.g. providing the list of packages
to be installed, the system configuration and %post scripts
name -- a name for the image; used for e.g. image filenames or
filesystem labels
"""
self.__builddir = None
self.ks = None
self.name = "target"
self.tmpdir = "/var/tmp/wic"
self.workdir = "/var/tmp/wic/build"
# setup tmpfs tmpdir when enabletmpfs is True
self.enabletmpfs = False
if createopts:
# Mapping table for variables that have different names.
optmap = {"outdir" : "destdir",
}
# update setting from createopts
for key in createopts.keys():
if key in optmap:
option = optmap[key]
else:
option = key
setattr(self, option, createopts[key])
self.destdir = os.path.abspath(os.path.expanduser(self.destdir))
self._dep_checks = ["ls", "bash", "cp", "echo"]
# Output image file names
self.outimage = []
# No ks provided when called by convertor, so skip the dependency check
if self.ks:
# If we have btrfs partition we need to check necessary tools
for part in self.ks.handler.partition.partitions:
if part.fstype and part.fstype == "btrfs":
self._dep_checks.append("mkfs.btrfs")
break
# make sure the specified tmpdir and cachedir exist
if not os.path.exists(self.tmpdir):
os.makedirs(self.tmpdir)
#
# Hooks for subclasses
#
def _create(self):
"""Create partitions for the disk image(s)
This is the hook where subclasses may create the partitions
that will be assembled into disk image(s).
There is no default implementation.
"""
pass
def _cleanup(self):
"""Undo anything performed in _create().
This is the hook where subclasses must undo anything which was
done in _create().
There is no default implementation.
"""
pass
#
# Actual implementation
#
def __ensure_builddir(self):
if not self.__builddir is None:
return
try:
self.workdir = os.path.join(self.tmpdir, "build")
if not os.path.exists(self.workdir):
os.makedirs(self.workdir)
self.__builddir = tempfile.mkdtemp(dir = self.workdir,
prefix = "imgcreate-")
except OSError, (err, msg):
raise CreatorError("Failed create build directory in %s: %s" %
(self.tmpdir, msg))
def __setup_tmpdir(self):
if not self.enabletmpfs:
return
runner.show('mount -t tmpfs -o size=4G tmpfs %s' % self.workdir)
def __clean_tmpdir(self):
if not self.enabletmpfs:
return
runner.show('umount -l %s' % self.workdir)
def create(self):
"""Create partitions for the disk image(s)
Create the partitions that will be assembled into disk
image(s).
"""
self.__setup_tmpdir()
self.__ensure_builddir()
self._create()
def cleanup(self):
"""Undo anything performed in create().
Note, make sure to call this method once finished with the creator
instance in order to ensure no stale files are left on the host e.g.:
creator = ImageCreator(ks, name)
try:
creator.create()
finally:
creator.cleanup()
"""
if not self.__builddir:
return
self._cleanup()
shutil.rmtree(self.__builddir, ignore_errors = True)
self.__builddir = None
self.__clean_tmpdir()
def print_outimage_info(self):
msg = "The new image can be found here:\n"
self.outimage.sort()
for file in self.outimage:
msg += ' %s\n' % os.path.abspath(file)
msger.info(msg)
|