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