summaryrefslogtreecommitdiffstats
path: root/scripts/lib/mic/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/mic/plugins')
-rw-r--r--scripts/lib/mic/plugins/backend/yumpkgmgr.py490
-rwxr-xr-xscripts/lib/mic/plugins/backend/zypppkgmgr.py973
-rw-r--r--scripts/lib/mic/plugins/hook/.py0
-rw-r--r--scripts/lib/mic/plugins/hook/empty_hook.py3
-rw-r--r--scripts/lib/mic/plugins/imager/fs_plugin.py143
-rw-r--r--scripts/lib/mic/plugins/imager/livecd_plugin.py255
-rw-r--r--scripts/lib/mic/plugins/imager/liveusb_plugin.py260
-rw-r--r--scripts/lib/mic/plugins/imager/loop_plugin.py255
-rw-r--r--scripts/lib/mic/plugins/imager/raw_plugin.py275
9 files changed, 2654 insertions, 0 deletions
diff --git a/scripts/lib/mic/plugins/backend/yumpkgmgr.py b/scripts/lib/mic/plugins/backend/yumpkgmgr.py
new file mode 100644
index 0000000000..955f813109
--- /dev/null
+++ b/scripts/lib/mic/plugins/backend/yumpkgmgr.py
@@ -0,0 +1,490 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2007 Red Hat Inc.
4# Copyright (c) 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
19import os, sys
20import re
21import tempfile
22import glob
23from string import Template
24
25import rpmUtils
26import yum
27
28from mic import msger
29from mic.kickstart import ksparser
30from mic.utils import misc, rpmmisc
31from mic.utils.grabber import TextProgress
32from mic.utils.proxy import get_proxy_for
33from mic.utils.errors import CreatorError
34from mic.imager.baseimager import BaseImageCreator
35
36YUMCONF_TEMP = """[main]
37installroot=$installroot
38cachedir=/var/cache/yum
39persistdir=/var/lib/yum
40plugins=0
41reposdir=
42failovermethod=priority
43http_caching=packages
44sslverify=1
45"""
46
47class MyYumRepository(yum.yumRepo.YumRepository):
48 def __del__(self):
49 pass
50
51 def dirSetup(self):
52 super(MyYumRepository, self).dirSetup()
53 # relocate package dir
54 pkgdir = os.path.join(self.basecachedir, 'packages', self.id)
55 self.setAttribute('_dir_setup_pkgdir', pkgdir)
56 self._dirSetupMkdir_p(self.pkgdir)
57
58 def _getFile(self, url=None,
59 relative=None,
60 local=None,
61 start=None,
62 end=None,
63 copy_local=None,
64 checkfunc=None,
65 text=None,
66 reget='simple',
67 cache=True,
68 size=None):
69
70 m2c_connection = None
71 if not self.sslverify:
72 try:
73 import M2Crypto
74 m2c_connection = M2Crypto.SSL.Connection.clientPostConnectionCheck
75 M2Crypto.SSL.Connection.clientPostConnectionCheck = None
76 except ImportError, err:
77 raise CreatorError("%s, please try to install python-m2crypto" % str(err))
78
79 proxy = None
80 if url:
81 proxy = get_proxy_for(url)
82 else:
83 proxy = get_proxy_for(self.urls[0])
84
85 if proxy:
86 self.proxy = str(proxy)
87
88 size = int(size) if size else None
89 rvalue = super(MyYumRepository, self)._getFile(url,
90 relative,
91 local,
92 start,
93 end,
94 copy_local,
95 checkfunc,
96 text,
97 reget,
98 cache,
99 size)
100
101 if m2c_connection and \
102 not M2Crypto.SSL.Connection.clientPostConnectionCheck:
103 M2Crypto.SSL.Connection.clientPostConnectionCheck = m2c_connection
104
105 return rvalue
106
107from mic.pluginbase import BackendPlugin
108class Yum(BackendPlugin, yum.YumBase):
109 name = 'yum'
110
111 def __init__(self, target_arch, instroot, cachedir):
112 yum.YumBase.__init__(self)
113
114 self.cachedir = cachedir
115 self.instroot = instroot
116 self.target_arch = target_arch
117
118 if self.target_arch:
119 if not rpmUtils.arch.arches.has_key(self.target_arch):
120 rpmUtils.arch.arches["armv7hl"] = "noarch"
121 rpmUtils.arch.arches["armv7tnhl"] = "armv7nhl"
122 rpmUtils.arch.arches["armv7tnhl"] = "armv7thl"
123 rpmUtils.arch.arches["armv7thl"] = "armv7hl"
124 rpmUtils.arch.arches["armv7nhl"] = "armv7hl"
125 self.arch.setup_arch(self.target_arch)
126
127 self.__pkgs_license = {}
128 self.__pkgs_content = {}
129 self.__pkgs_vcsinfo = {}
130
131 self.install_debuginfo = False
132
133 def doFileLogSetup(self, uid, logfile):
134 # don't do the file log for the livecd as it can lead to open fds
135 # being left and an inability to clean up after ourself
136 pass
137
138 def close(self):
139 try:
140 os.unlink(self.confpath)
141 os.unlink(self.conf.installroot + "/yum.conf")
142 except:
143 pass
144
145 if self.ts:
146 self.ts.close()
147 self._delRepos()
148 self._delSacks()
149 yum.YumBase.close(self)
150 self.closeRpmDB()
151
152 if not os.path.exists("/etc/fedora-release") and \
153 not os.path.exists("/etc/meego-release"):
154 for i in range(3, os.sysconf("SC_OPEN_MAX")):
155 try:
156 os.close(i)
157 except:
158 pass
159
160 def __del__(self):
161 pass
162
163 def _writeConf(self, confpath, installroot):
164 conf = Template(YUMCONF_TEMP).safe_substitute(installroot=installroot)
165
166 f = file(confpath, "w+")
167 f.write(conf)
168 f.close()
169
170 os.chmod(confpath, 0644)
171
172 def _cleanupRpmdbLocks(self, installroot):
173 # cleans up temporary files left by bdb so that differing
174 # versions of rpm don't cause problems
175 for f in glob.glob(installroot + "/var/lib/rpm/__db*"):
176 os.unlink(f)
177
178 def setup(self):
179 # create yum.conf
180 (fn, self.confpath) = tempfile.mkstemp(dir=self.cachedir,
181 prefix='yum.conf-')
182 os.close(fn)
183 self._writeConf(self.confpath, self.instroot)
184 self._cleanupRpmdbLocks(self.instroot)
185 # do setup
186 self.doConfigSetup(fn = self.confpath, root = self.instroot)
187 self.conf.cache = 0
188 self.doTsSetup()
189 self.doRpmDBSetup()
190 self.doRepoSetup()
191 self.doSackSetup()
192
193 def preInstall(self, pkg):
194 # FIXME: handle pre-install package
195 return None
196
197 def selectPackage(self, pkg):
198 """Select a given package.
199 Can be specified with name.arch or name*
200 """
201
202 try:
203 self.install(pattern = pkg)
204 return None
205 except yum.Errors.InstallError:
206 return "No package(s) available to install"
207 except yum.Errors.RepoError, e:
208 raise CreatorError("Unable to download from repo : %s" % (e,))
209 except yum.Errors.YumBaseError, e:
210 raise CreatorError("Unable to install: %s" % (e,))
211
212 def deselectPackage(self, pkg):
213 """Deselect package. Can be specified as name.arch or name*
214 """
215
216 sp = pkg.rsplit(".", 2)
217 txmbrs = []
218 if len(sp) == 2:
219 txmbrs = self.tsInfo.matchNaevr(name=sp[0], arch=sp[1])
220
221 if len(txmbrs) == 0:
222 exact, match, unmatch = yum.packages.parsePackages(
223 self.pkgSack.returnPackages(),
224 [pkg],
225 casematch=1)
226 for p in exact + match:
227 txmbrs.append(p)
228
229 if len(txmbrs) > 0:
230 for x in txmbrs:
231 self.tsInfo.remove(x.pkgtup)
232 # we also need to remove from the conditionals
233 # dict so that things don't get pulled back in as a result
234 # of them. yes, this is ugly. conditionals should die.
235 for req, pkgs in self.tsInfo.conditionals.iteritems():
236 if x in pkgs:
237 pkgs.remove(x)
238 self.tsInfo.conditionals[req] = pkgs
239 else:
240 msger.warning("No such package %s to remove" %(pkg,))
241
242 def selectGroup(self, grp, include = ksparser.GROUP_DEFAULT):
243 try:
244 yum.YumBase.selectGroup(self, grp)
245 if include == ksparser.GROUP_REQUIRED:
246 for p in grp.default_packages.keys():
247 self.deselectPackage(p)
248
249 elif include == ksparser.GROUP_ALL:
250 for p in grp.optional_packages.keys():
251 self.selectPackage(p)
252
253 return None
254 except (yum.Errors.InstallError, yum.Errors.GroupsError), e:
255 return e
256 except yum.Errors.RepoError, e:
257 raise CreatorError("Unable to download from repo : %s" % (e,))
258 except yum.Errors.YumBaseError, e:
259 raise CreatorError("Unable to install: %s" % (e,))
260
261 def addRepository(self, name, url = None, mirrorlist = None, proxy = None,
262 proxy_username = None, proxy_password = None,
263 inc = None, exc = None, ssl_verify=True, nocache=False,
264 cost = None, priority=None):
265 # TODO: Handle priority attribute for repos
266 def _varSubstitute(option):
267 # takes a variable and substitutes like yum configs do
268 option = option.replace("$basearch", rpmUtils.arch.getBaseArch())
269 option = option.replace("$arch", rpmUtils.arch.getCanonArch())
270 return option
271
272 repo = MyYumRepository(name)
273
274 # Set proxy
275 repo.proxy = proxy
276 repo.proxy_username = proxy_username
277 repo.proxy_password = proxy_password
278
279 if url:
280 repo.baseurl.append(_varSubstitute(url))
281
282 # check LICENSE files
283 if not rpmmisc.checkRepositoryEULA(name, repo):
284 msger.warning('skip repo:%s for failed EULA confirmation' % name)
285 return None
286
287 if mirrorlist:
288 repo.mirrorlist = _varSubstitute(mirrorlist)
289
290 conf = yum.config.RepoConf()
291 for k, v in conf.iteritems():
292 if v or not hasattr(repo, k):
293 repo.setAttribute(k, v)
294
295 repo.sslverify = ssl_verify
296 repo.cache = not nocache
297
298 repo.basecachedir = self.cachedir
299 repo.base_persistdir = self.conf.persistdir
300 repo.failovermethod = "priority"
301 repo.metadata_expire = 0
302 # Enable gpg check for verifying corrupt packages
303 repo.gpgcheck = 1
304 repo.enable()
305 repo.setup(0)
306 self.repos.add(repo)
307 if cost:
308 repo.cost = cost
309
310 msger.verbose('repo: %s was added' % name)
311 return repo
312
313 def installLocal(self, pkg, po=None, updateonly=False):
314 ts = rpmUtils.transaction.initReadOnlyTransaction()
315 try:
316 hdr = rpmUtils.miscutils.hdrFromPackage(ts, pkg)
317 except rpmUtils.RpmUtilsError, e:
318 raise yum.Errors.MiscError, \
319 'Could not open local rpm file: %s: %s' % (pkg, e)
320
321 self.deselectPackage(hdr['name'])
322 yum.YumBase.installLocal(self, pkg, po, updateonly)
323
324 def installHasFile(self, file):
325 provides_pkg = self.whatProvides(file, None, None)
326 dlpkgs = map(
327 lambda x: x.po,
328 filter(
329 lambda txmbr: txmbr.ts_state in ("i", "u"),
330 self.tsInfo.getMembers()))
331
332 for p in dlpkgs:
333 for q in provides_pkg:
334 if (p == q):
335 return True
336
337 return False
338
339 def runInstall(self, checksize = 0):
340 os.environ["HOME"] = "/"
341 os.environ["LD_PRELOAD"] = ""
342 try:
343 (res, resmsg) = self.buildTransaction()
344 except yum.Errors.RepoError, e:
345 raise CreatorError("Unable to download from repo : %s" %(e,))
346
347 if res != 2:
348 raise CreatorError("Failed to build transaction : %s" \
349 % str.join("\n", resmsg))
350
351 dlpkgs = map(
352 lambda x: x.po,
353 filter(
354 lambda txmbr: txmbr.ts_state in ("i", "u"),
355 self.tsInfo.getMembers()))
356
357 # record all pkg and the content
358 for pkg in dlpkgs:
359 pkg_long_name = misc.RPM_FMT % {
360 'name': pkg.name,
361 'arch': pkg.arch,
362 'version': pkg.version,
363 'release': pkg.release
364 }
365 self.__pkgs_content[pkg_long_name] = pkg.files
366 license = pkg.license
367 if license in self.__pkgs_license.keys():
368 self.__pkgs_license[license].append(pkg_long_name)
369 else:
370 self.__pkgs_license[license] = [pkg_long_name]
371
372 total_count = len(dlpkgs)
373 cached_count = 0
374 download_total_size = sum(map(lambda x: int(x.packagesize), dlpkgs))
375
376 msger.info("\nChecking packages cached ...")
377 for po in dlpkgs:
378 local = po.localPkg()
379 repo = filter(lambda r: r.id == po.repoid, self.repos.listEnabled())[0]
380 if not repo.cache and os.path.exists(local):
381 os.unlink(local)
382 if not os.path.exists(local):
383 continue
384 if not self.verifyPkg(local, po, False):
385 msger.warning("Package %s is damaged: %s" \
386 % (os.path.basename(local), local))
387 else:
388 download_total_size -= int(po.packagesize)
389 cached_count +=1
390
391 cache_avail_size = misc.get_filesystem_avail(self.cachedir)
392 if cache_avail_size < download_total_size:
393 raise CreatorError("No enough space used for downloading.")
394
395 # record the total size of installed pkgs
396 pkgs_total_size = 0L
397 for x in dlpkgs:
398 if hasattr(x, 'installedsize'):
399 pkgs_total_size += int(x.installedsize)
400 else:
401 pkgs_total_size += int(x.size)
402
403 # check needed size before actually download and install
404 if checksize and pkgs_total_size > checksize:
405 raise CreatorError("No enough space used for installing, "
406 "please resize partition size in ks file")
407
408 msger.info("Packages: %d Total, %d Cached, %d Missed" \
409 % (total_count, cached_count, total_count - cached_count))
410
411 try:
412 repos = self.repos.listEnabled()
413 for repo in repos:
414 repo.setCallback(TextProgress(total_count - cached_count))
415
416 self.downloadPkgs(dlpkgs)
417 # FIXME: sigcheck?
418
419 self.initActionTs()
420 self.populateTs(keepold=0)
421
422 deps = self.ts.check()
423 if len(deps) != 0:
424 # This isn't fatal, Ubuntu has this issue but it is ok.
425 msger.debug(deps)
426 msger.warning("Dependency check failed!")
427
428 rc = self.ts.order()
429 if rc != 0:
430 raise CreatorError("ordering packages for installation failed")
431
432 # FIXME: callback should be refactored a little in yum
433 cb = rpmmisc.RPMInstallCallback(self.ts)
434 cb.tsInfo = self.tsInfo
435 cb.filelog = False
436
437 msger.warning('\nCaution, do NOT interrupt the installation, '
438 'else mic cannot finish the cleanup.')
439
440 installlogfile = "%s/__catched_stderr.buf" % (self.instroot)
441 msger.enable_logstderr(installlogfile)
442 self.runTransaction(cb)
443 self._cleanupRpmdbLocks(self.conf.installroot)
444
445 except rpmUtils.RpmUtilsError, e:
446 raise CreatorError("mic does NOT support delta rpm: %s" % e)
447 except yum.Errors.RepoError, e:
448 raise CreatorError("Unable to download from repo : %s" % e)
449 except yum.Errors.YumBaseError, e:
450 raise CreatorError("Unable to install: %s" % e)
451 finally:
452 msger.disable_logstderr()
453
454 def getVcsInfo(self):
455 return self.__pkgs_vcsinfo
456
457 def getAllContent(self):
458 return self.__pkgs_content
459
460 def getPkgsLicense(self):
461 return self.__pkgs_license
462
463 def getFilelist(self, pkgname):
464 if not pkgname:
465 return None
466
467 pkg = filter(lambda txmbr: txmbr.po.name == pkgname, self.tsInfo.getMembers())
468 if not pkg:
469 return None
470 return pkg[0].po.filelist
471
472 def package_url(self, pkgname):
473 pkgs = self.pkgSack.searchNevra(name=pkgname)
474 if pkgs:
475 proxy = None
476 proxies = None
477 url = pkgs[0].remote_url
478 repoid = pkgs[0].repoid
479 repos = filter(lambda r: r.id == repoid, self.repos.listEnabled())
480
481 if repos:
482 proxy = repos[0].proxy
483 if not proxy:
484 proxy = get_proxy_for(url)
485 if proxy:
486 proxies = {str(url.split(':')[0]): str(proxy)}
487
488 return (url, proxies)
489
490 return (None, None)
diff --git a/scripts/lib/mic/plugins/backend/zypppkgmgr.py b/scripts/lib/mic/plugins/backend/zypppkgmgr.py
new file mode 100755
index 0000000000..c760859832
--- /dev/null
+++ b/scripts/lib/mic/plugins/backend/zypppkgmgr.py
@@ -0,0 +1,973 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2010, 2011 Intel, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; version 2 of the License
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18import os
19import shutil
20import urlparse
21import rpm
22
23import zypp
24if not hasattr(zypp, 'PoolQuery') or \
25 not hasattr(zypp.RepoManager, 'loadSolvFile'):
26 raise ImportError("python-zypp in host system cannot support PoolQuery or "
27 "loadSolvFile interface, please update it to enhanced "
28 "version which can be found in download.tizen.org/tools")
29
30from mic import msger
31from mic.kickstart import ksparser
32from mic.utils import misc, rpmmisc, runner, fs_related
33from mic.utils.grabber import myurlgrab, TextProgress
34from mic.utils.proxy import get_proxy_for
35from mic.utils.errors import CreatorError, RepoError, RpmError
36from mic.imager.baseimager import BaseImageCreator
37
38class RepositoryStub:
39 def __init__(self):
40 self.name = None
41 self.baseurl = []
42 self.mirrorlist = None
43 self.proxy = None
44 self.proxy_username = None
45 self.proxy_password = None
46 self.nocache = False
47
48 self.enabled = True
49 self.autorefresh = True
50 self.keeppackages = True
51 self.priority = None
52
53from mic.pluginbase import BackendPlugin
54class Zypp(BackendPlugin):
55 name = 'zypp'
56
57 def __init__(self, target_arch, instroot, cachedir):
58 self.cachedir = cachedir
59 self.instroot = instroot
60 self.target_arch = target_arch
61
62 self.__pkgs_license = {}
63 self.__pkgs_content = {}
64 self.__pkgs_vcsinfo = {}
65 self.repos = []
66 self.to_deselect = []
67 self.localpkgs = {}
68 self.repo_manager = None
69 self.repo_manager_options = None
70 self.Z = None
71 self.ts = None
72 self.ts_pre = None
73 self.incpkgs = {}
74 self.excpkgs = {}
75 self.pre_pkgs = []
76 self.probFilterFlags = [ rpm.RPMPROB_FILTER_OLDPACKAGE,
77 rpm.RPMPROB_FILTER_REPLACEPKG ]
78
79 self.has_prov_query = True
80 self.install_debuginfo = False
81
82 def doFileLogSetup(self, uid, logfile):
83 # don't do the file log for the livecd as it can lead to open fds
84 # being left and an inability to clean up after ourself
85 pass
86
87 def closeRpmDB(self):
88 pass
89
90 def close(self):
91 if self.ts:
92 self.ts.closeDB()
93 self.ts = None
94
95 if self.ts_pre:
96 self.ts_pre.closeDB()
97 self.ts = None
98
99 self.closeRpmDB()
100
101 if not os.path.exists("/etc/fedora-release") and \
102 not os.path.exists("/etc/meego-release"):
103 for i in range(3, os.sysconf("SC_OPEN_MAX")):
104 try:
105 os.close(i)
106 except:
107 pass
108
109 def __del__(self):
110 self.close()
111
112 def _cleanupRpmdbLocks(self, installroot):
113 # cleans up temporary files left by bdb so that differing
114 # versions of rpm don't cause problems
115 import glob
116 for f in glob.glob(installroot + "/var/lib/rpm/__db*"):
117 os.unlink(f)
118
119 def _cleanupZyppJunk(self, installroot):
120 try:
121 shutil.rmtree(os.path.join(installroot, '.zypp'))
122 except:
123 pass
124
125 def setup(self):
126 self._cleanupRpmdbLocks(self.instroot)
127
128 def whatObsolete(self, pkg):
129 query = zypp.PoolQuery()
130 query.addKind(zypp.ResKind.package)
131 query.addAttribute(zypp.SolvAttr.obsoletes, pkg)
132 query.setMatchExact()
133 for pi in query.queryResults(self.Z.pool()):
134 return pi
135 return None
136
137 def _zyppQueryPackage(self, pkg):
138 query = zypp.PoolQuery()
139 query.addKind(zypp.ResKind.package)
140 query.addAttribute(zypp.SolvAttr.name,pkg)
141 query.setMatchExact()
142 for pi in query.queryResults(self.Z.pool()):
143 return pi
144 return None
145
146 def _splitPkgString(self, pkg):
147 sp = pkg.rsplit(".",1)
148 name = sp[0]
149 arch = None
150 if len(sp) == 2:
151 arch = sp[1]
152 sysarch = zypp.Arch(self.target_arch)
153 if not zypp.Arch(arch).compatible_with (sysarch):
154 arch = None
155 name = ".".join(sp)
156 return name, arch
157
158 def selectPackage(self, pkg):
159 """Select a given package or package pattern, can be specified
160 with name.arch or name* or *name
161 """
162
163 if not self.Z:
164 self.__initialize_zypp()
165
166 def markPoolItem(obs, pi):
167 if obs == None:
168 pi.status().setToBeInstalled (zypp.ResStatus.USER)
169 else:
170 obs.status().setToBeInstalled (zypp.ResStatus.USER)
171
172 def cmpEVR(p1, p2):
173 # compare criterion: arch compatibility first, then repo
174 # priority, and version last
175 a1 = p1.arch()
176 a2 = p2.arch()
177 if str(a1) != str(a2):
178 if a1.compatible_with(a2):
179 return -1
180 else:
181 return 1
182 # Priority of a repository is an integer value between 0 (the
183 # highest priority) and 99 (the lowest priority)
184 pr1 = int(p1.repoInfo().priority())
185 pr2 = int(p2.repoInfo().priority())
186 if pr1 > pr2:
187 return -1
188 elif pr1 < pr2:
189 return 1
190
191 ed1 = p1.edition()
192 ed2 = p2.edition()
193 (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()])
194 (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()])
195 return rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
196
197 found = False
198 startx = pkg.startswith("*")
199 endx = pkg.endswith("*")
200 ispattern = startx or endx
201 name, arch = self._splitPkgString(pkg)
202
203 q = zypp.PoolQuery()
204 q.addKind(zypp.ResKind.package)
205
206 if ispattern:
207 if startx and not endx:
208 pattern = '%s$' % (pkg[1:])
209 if endx and not startx:
210 pattern = '^%s' % (pkg[0:-1])
211 if endx and startx:
212 pattern = '%s' % (pkg[1:-1])
213 q.setMatchRegex()
214 q.addAttribute(zypp.SolvAttr.name,pattern)
215
216 elif arch:
217 q.setMatchExact()
218 q.addAttribute(zypp.SolvAttr.name,name)
219
220 else:
221 q.setMatchExact()
222 q.addAttribute(zypp.SolvAttr.name,pkg)
223
224 for pitem in sorted(
225 q.queryResults(self.Z.pool()),
226 cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)),
227 reverse=True):
228 item = zypp.asKindPackage(pitem)
229 if item.name() in self.excpkgs.keys() and \
230 self.excpkgs[item.name()] == item.repoInfo().name():
231 continue
232 if item.name() in self.incpkgs.keys() and \
233 self.incpkgs[item.name()] != item.repoInfo().name():
234 continue
235
236 found = True
237 obspkg = self.whatObsolete(item.name())
238 if arch:
239 if arch == str(item.arch()):
240 item.status().setToBeInstalled (zypp.ResStatus.USER)
241 else:
242 markPoolItem(obspkg, pitem)
243 if not ispattern:
244 break
245
246 # Can't match using package name, then search from packge
247 # provides infomation
248 if found == False and not ispattern:
249 q.addAttribute(zypp.SolvAttr.provides, pkg)
250 q.addAttribute(zypp.SolvAttr.name,'')
251
252 for pitem in sorted(
253 q.queryResults(self.Z.pool()),
254 cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)),
255 reverse=True):
256 item = zypp.asKindPackage(pitem)
257 if item.name() in self.excpkgs.keys() and \
258 self.excpkgs[item.name()] == item.repoInfo().name():
259 continue
260 if item.name() in self.incpkgs.keys() and \
261 self.incpkgs[item.name()] != item.repoInfo().name():
262 continue
263
264 found = True
265 obspkg = self.whatObsolete(item.name())
266 markPoolItem(obspkg, pitem)
267 break
268
269 if found:
270 return None
271 else:
272 raise CreatorError("Unable to find package: %s" % (pkg,))
273
274 def inDeselectPackages(self, pitem):
275 """check if specified pacakges are in the list of inDeselectPackages
276 """
277 item = zypp.asKindPackage(pitem)
278 name = item.name()
279 for pkg in self.to_deselect:
280 startx = pkg.startswith("*")
281 endx = pkg.endswith("*")
282 ispattern = startx or endx
283 pkgname, pkgarch = self._splitPkgString(pkg)
284 if not ispattern:
285 if pkgarch:
286 if name == pkgname and str(item.arch()) == pkgarch:
287 return True;
288 else:
289 if name == pkgname:
290 return True;
291 else:
292 if startx and name.endswith(pkg[1:]):
293 return True;
294 if endx and name.startswith(pkg[:-1]):
295 return True;
296
297 return False;
298
299 def deselectPackage(self, pkg):
300 """collect packages should not be installed"""
301 self.to_deselect.append(pkg)
302
303 def selectGroup(self, grp, include = ksparser.GROUP_DEFAULT):
304 if not self.Z:
305 self.__initialize_zypp()
306 found = False
307 q=zypp.PoolQuery()
308 q.addKind(zypp.ResKind.pattern)
309 for pitem in q.queryResults(self.Z.pool()):
310 item = zypp.asKindPattern(pitem)
311 summary = "%s" % item.summary()
312 name = "%s" % item.name()
313 if name == grp or summary == grp:
314 found = True
315 pitem.status().setToBeInstalled (zypp.ResStatus.USER)
316 break
317
318 if found:
319 if include == ksparser.GROUP_REQUIRED:
320 map(
321 lambda p: self.deselectPackage(p),
322 grp.default_packages.keys())
323
324 return None
325 else:
326 raise CreatorError("Unable to find pattern: %s" % (grp,))
327
328 def addRepository(self, name,
329 url = None,
330 mirrorlist = None,
331 proxy = None,
332 proxy_username = None,
333 proxy_password = None,
334 inc = None,
335 exc = None,
336 ssl_verify = True,
337 nocache = False,
338 cost=None,
339 priority=None):
340 # TODO: Handle cost attribute for repos
341
342 if not self.repo_manager:
343 self.__initialize_repo_manager()
344
345 if not proxy and url:
346 proxy = get_proxy_for(url)
347
348 repo = RepositoryStub()
349 repo.name = name
350 repo.id = name
351 repo.proxy = proxy
352 repo.proxy_username = proxy_username
353 repo.proxy_password = proxy_password
354 repo.ssl_verify = ssl_verify
355 repo.nocache = nocache
356 repo.baseurl.append(url)
357 if inc:
358 for pkg in inc:
359 self.incpkgs[pkg] = name
360 if exc:
361 for pkg in exc:
362 self.excpkgs[pkg] = name
363
364 # check LICENSE files
365 if not rpmmisc.checkRepositoryEULA(name, repo):
366 msger.warning('skip repo:%s for failed EULA confirmation' % name)
367 return None
368
369 if mirrorlist:
370 repo.mirrorlist = mirrorlist
371
372 # Enable gpg check for verifying corrupt packages
373 repo.gpgcheck = 1
374 if priority is not None:
375 # priority 0 has issue in RepoInfo.setPriority
376 repo.priority = priority + 1
377
378 try:
379 repo_info = zypp.RepoInfo()
380 repo_info.setAlias(repo.name)
381 repo_info.setName(repo.name)
382 repo_info.setEnabled(repo.enabled)
383 repo_info.setAutorefresh(repo.autorefresh)
384 repo_info.setKeepPackages(repo.keeppackages)
385 baseurl = zypp.Url(repo.baseurl[0])
386 if not ssl_verify:
387 baseurl.setQueryParam("ssl_verify", "no")
388 if proxy:
389 scheme, host, path, parm, query, frag = urlparse.urlparse(proxy)
390
391 proxyinfo = host.split(":")
392 host = proxyinfo[0]
393
394 port = "80"
395 if len(proxyinfo) > 1:
396 port = proxyinfo[1]
397
398 if proxy.startswith("socks") and len(proxy.rsplit(':', 1)) == 2:
399 host = proxy.rsplit(':', 1)[0]
400 port = proxy.rsplit(':', 1)[1]
401
402 baseurl.setQueryParam ("proxy", host)
403 baseurl.setQueryParam ("proxyport", port)
404
405 repo.baseurl[0] = baseurl.asCompleteString()
406 self.repos.append(repo)
407
408 repo_info.addBaseUrl(baseurl)
409
410 if repo.priority is not None:
411 repo_info.setPriority(repo.priority)
412
413 # this hack is used to change zypp credential file location
414 # the default one is $HOME/.zypp, which cause conflicts when
415 # installing some basic packages, and the location doesn't
416 # have any interface actually, so use a tricky way anyway
417 homedir = None
418 if 'HOME' in os.environ:
419 homedir = os.environ['HOME']
420 os.environ['HOME'] = '/'
421 else:
422 os.environ['HOME'] = '/'
423
424 self.repo_manager.addRepository(repo_info)
425
426 # save back the $HOME env
427 if homedir:
428 os.environ['HOME'] = homedir
429 else:
430 del os.environ['HOME']
431
432 self.__build_repo_cache(name)
433
434 except RuntimeError, e:
435 raise CreatorError(str(e))
436
437 msger.verbose('repo: %s was added' % name)
438 return repo
439
440 def installHasFile(self, file):
441 return False
442
443 def preInstall(self, pkg):
444 self.pre_pkgs.append(pkg)
445
446 def runInstall(self, checksize = 0):
447 os.environ["HOME"] = "/"
448 os.environ["LD_PRELOAD"] = ""
449 self.buildTransaction()
450
451 todo = zypp.GetResolvablesToInsDel(self.Z.pool())
452 installed_pkgs = todo._toInstall
453 dlpkgs = []
454 for pitem in installed_pkgs:
455 if not zypp.isKindPattern(pitem) and \
456 not self.inDeselectPackages(pitem):
457 item = zypp.asKindPackage(pitem)
458 dlpkgs.append(item)
459
460 if not self.install_debuginfo or str(item.arch()) == "noarch":
461 continue
462
463 dipkg = self._zyppQueryPackage("%s-debuginfo" % item.name())
464 if dipkg:
465 ditem = zypp.asKindPackage(dipkg)
466 dlpkgs.append(ditem)
467 else:
468 msger.warning("No debuginfo rpm found for: %s" \
469 % item.name())
470
471 # record all pkg and the content
472 localpkgs = self.localpkgs.keys()
473 for pkg in dlpkgs:
474 license = ''
475 if pkg.name() in localpkgs:
476 hdr = rpmmisc.readRpmHeader(self.ts, self.localpkgs[pkg.name()])
477 pkg_long_name = misc.RPM_FMT % {
478 'name': hdr['name'],
479 'arch': hdr['arch'],
480 'version': hdr['version'],
481 'release': hdr['release']
482 }
483 license = hdr['license']
484
485 else:
486 pkg_long_name = misc.RPM_FMT % {
487 'name': pkg.name(),
488 'arch': pkg.arch(),
489 'version': pkg.edition().version(),
490 'release': pkg.edition().release()
491 }
492
493 license = pkg.license()
494
495 if license in self.__pkgs_license.keys():
496 self.__pkgs_license[license].append(pkg_long_name)
497 else:
498 self.__pkgs_license[license] = [pkg_long_name]
499
500 total_count = len(dlpkgs)
501 cached_count = 0
502 download_total_size = sum(map(lambda x: int(x.downloadSize()), dlpkgs))
503 localpkgs = self.localpkgs.keys()
504
505 msger.info("Checking packages cached ...")
506 for po in dlpkgs:
507 # Check if it is cached locally
508 if po.name() in localpkgs:
509 cached_count += 1
510 else:
511 local = self.getLocalPkgPath(po)
512 name = str(po.repoInfo().name())
513 try:
514 repo = filter(lambda r: r.name == name, self.repos)[0]
515 except IndexError:
516 repo = None
517 nocache = repo.nocache if repo else False
518
519 if os.path.exists(local):
520 if nocache or self.checkPkg(local) !=0:
521 os.unlink(local)
522 else:
523 download_total_size -= int(po.downloadSize())
524 cached_count += 1
525 cache_avail_size = misc.get_filesystem_avail(self.cachedir)
526 if cache_avail_size < download_total_size:
527 raise CreatorError("No enough space used for downloading.")
528
529 # record the total size of installed pkgs
530 install_total_size = sum(map(lambda x: int(x.installSize()), dlpkgs))
531 # check needed size before actually download and install
532
533 # FIXME: for multiple partitions for loop type, check fails
534 # skip the check temporarily
535 #if checksize and install_total_size > checksize:
536 # raise CreatorError("No enough space used for installing, "
537 # "please resize partition size in ks file")
538
539 download_count = total_count - cached_count
540 msger.info("Packages: %d Total, %d Cached, %d Missed" \
541 % (total_count, cached_count, download_count))
542
543 try:
544 if download_count > 0:
545 msger.info("Downloading packages ...")
546 self.downloadPkgs(dlpkgs, download_count)
547
548 self.installPkgs(dlpkgs)
549
550 except (RepoError, RpmError):
551 raise
552 except Exception, e:
553 raise CreatorError("Package installation failed: %s" % (e,))
554
555 def getVcsInfo(self):
556 if self.__pkgs_vcsinfo:
557 return
558
559 if not self.ts:
560 self.__initialize_transaction()
561
562 mi = self.ts.dbMatch()
563 for hdr in mi:
564 lname = misc.RPM_FMT % {
565 'name': hdr['name'],
566 'arch': hdr['arch'],
567 'version': hdr['version'],
568 'release': hdr['release']
569 }
570 self.__pkgs_vcsinfo[lname] = hdr['VCS']
571
572 return self.__pkgs_vcsinfo
573
574 def getAllContent(self):
575 if self.__pkgs_content:
576 return self.__pkgs_content
577
578 if not self.ts:
579 self.__initialize_transaction()
580
581 mi = self.ts.dbMatch()
582 for hdr in mi:
583 lname = misc.RPM_FMT % {
584 'name': hdr['name'],
585 'arch': hdr['arch'],
586 'version': hdr['version'],
587 'release': hdr['release']
588 }
589 self.__pkgs_content[lname] = hdr['FILENAMES']
590
591 return self.__pkgs_content
592
593 def getPkgsLicense(self):
594 return self.__pkgs_license
595
596 def getFilelist(self, pkgname):
597 if not pkgname:
598 return None
599
600 if not self.ts:
601 self.__initialize_transaction()
602
603 mi = self.ts.dbMatch('name', pkgname)
604 for header in mi:
605 return header['FILENAMES']
606
607 def __initialize_repo_manager(self):
608 if self.repo_manager:
609 return
610
611 # Clean up repo metadata
612 shutil.rmtree(self.cachedir + "/etc", ignore_errors = True)
613 shutil.rmtree(self.cachedir + "/solv", ignore_errors = True)
614 shutil.rmtree(self.cachedir + "/raw", ignore_errors = True)
615
616 zypp.KeyRing.setDefaultAccept( zypp.KeyRing.ACCEPT_UNSIGNED_FILE
617 | zypp.KeyRing.ACCEPT_VERIFICATION_FAILED
618 | zypp.KeyRing.ACCEPT_UNKNOWNKEY
619 | zypp.KeyRing.TRUST_KEY_TEMPORARILY
620 )
621
622 self.repo_manager_options = \
623 zypp.RepoManagerOptions(zypp.Pathname(self.instroot))
624
625 self.repo_manager_options.knownReposPath = \
626 zypp.Pathname(self.cachedir + "/etc/zypp/repos.d")
627
628 self.repo_manager_options.repoCachePath = \
629 zypp.Pathname(self.cachedir)
630
631 self.repo_manager_options.repoRawCachePath = \
632 zypp.Pathname(self.cachedir + "/raw")
633
634 self.repo_manager_options.repoSolvCachePath = \
635 zypp.Pathname(self.cachedir + "/solv")
636
637 self.repo_manager_options.repoPackagesCachePath = \
638 zypp.Pathname(self.cachedir + "/packages")
639
640 self.repo_manager = zypp.RepoManager(self.repo_manager_options)
641
642 def __build_repo_cache(self, name):
643 repo = self.repo_manager.getRepositoryInfo(name)
644 if self.repo_manager.isCached(repo) or not repo.enabled():
645 return
646
647 msger.info('Refreshing repository: %s ...' % name)
648 self.repo_manager.buildCache(repo, zypp.RepoManager.BuildIfNeeded)
649
650 def __initialize_zypp(self):
651 if self.Z:
652 return
653
654 zconfig = zypp.ZConfig_instance()
655
656 # Set system architecture
657 if self.target_arch:
658 zconfig.setSystemArchitecture(zypp.Arch(self.target_arch))
659
660 msger.info("zypp architecture is <%s>" % zconfig.systemArchitecture())
661
662 # repoPackagesCachePath is corrected by this
663 self.repo_manager = zypp.RepoManager(self.repo_manager_options)
664 repos = self.repo_manager.knownRepositories()
665 for repo in repos:
666 if not repo.enabled():
667 continue
668 self.repo_manager.loadFromCache(repo)
669
670 self.Z = zypp.ZYppFactory_instance().getZYpp()
671 self.Z.initializeTarget(zypp.Pathname(self.instroot))
672 self.Z.target().load()
673
674 def buildTransaction(self):
675 if not self.Z.resolver().resolvePool():
676 probs = self.Z.resolver().problems()
677
678 for problem in probs:
679 msger.warning("repo problem: %s, %s" \
680 % (problem.description().decode("utf-8"),
681 problem.details().decode("utf-8")))
682
683 raise RepoError("found %d resolver problem, abort!" \
684 % len(probs))
685
686 def getLocalPkgPath(self, po):
687 repoinfo = po.repoInfo()
688 cacheroot = repoinfo.packagesPath()
689 location= po.location()
690 rpmpath = str(location.filename())
691 pkgpath = "%s/%s" % (cacheroot, os.path.basename(rpmpath))
692 return pkgpath
693
694 def installLocal(self, pkg, po=None, updateonly=False):
695 if not self.ts:
696 self.__initialize_transaction()
697
698 solvfile = "%s/.solv" % (self.cachedir)
699
700 rc, out = runner.runtool([fs_related.find_binary_path("rpms2solv"),
701 pkg])
702 if rc == 0:
703 f = open(solvfile, "w+")
704 f.write(out)
705 f.close()
706
707 warnmsg = self.repo_manager.loadSolvFile(solvfile,
708 os.path.basename(pkg))
709 if warnmsg:
710 msger.warning(warnmsg)
711
712 os.unlink(solvfile)
713 else:
714 msger.warning('Can not get %s solv data.' % pkg)
715
716 hdr = rpmmisc.readRpmHeader(self.ts, pkg)
717 arch = zypp.Arch(hdr['arch'])
718 sysarch = zypp.Arch(self.target_arch)
719
720 if arch.compatible_with (sysarch):
721 pkgname = hdr['name']
722 self.localpkgs[pkgname] = pkg
723 self.selectPackage(pkgname)
724 msger.info("Marking %s to be installed" % (pkg))
725
726 else:
727 msger.warning("Cannot add package %s to transaction. "
728 "Not a compatible architecture: %s" \
729 % (pkg, hdr['arch']))
730
731 def downloadPkgs(self, package_objects, count):
732 localpkgs = self.localpkgs.keys()
733 progress_obj = TextProgress(count)
734
735 for po in package_objects:
736 if po.name() in localpkgs:
737 continue
738
739 filename = self.getLocalPkgPath(po)
740 if os.path.exists(filename):
741 if self.checkPkg(filename) == 0:
742 continue
743
744 dirn = os.path.dirname(filename)
745 if not os.path.exists(dirn):
746 os.makedirs(dirn)
747
748 url = self.get_url(po)
749 proxies = self.get_proxies(po)
750
751 try:
752 filename = myurlgrab(url, filename, proxies, progress_obj)
753 except CreatorError:
754 self.close()
755 raise
756
757 def preinstallPkgs(self):
758 if not self.ts_pre:
759 self.__initialize_transaction()
760
761 self.ts_pre.order()
762 cb = rpmmisc.RPMInstallCallback(self.ts_pre)
763 cb.headmsg = "Preinstall"
764 installlogfile = "%s/__catched_stderr.buf" % (self.instroot)
765
766 # start to catch stderr output from librpm
767 msger.enable_logstderr(installlogfile)
768
769 errors = self.ts_pre.run(cb.callback, '')
770 # stop catch
771 msger.disable_logstderr()
772 self.ts_pre.closeDB()
773 self.ts_pre = None
774
775 if errors is not None:
776 if len(errors) == 0:
777 msger.warning('scriptlet or other non-fatal errors occurred '
778 'during transaction.')
779
780 else:
781 for e in errors:
782 msger.warning(e[0])
783 raise RepoError('Could not run transaction.')
784
785 def installPkgs(self, package_objects):
786 if not self.ts:
787 self.__initialize_transaction()
788
789 # clean rpm lock
790 self._cleanupRpmdbLocks(self.instroot)
791 self._cleanupZyppJunk(self.instroot)
792 # Set filters
793 probfilter = 0
794 for flag in self.probFilterFlags:
795 probfilter |= flag
796 self.ts.setProbFilter(probfilter)
797 self.ts_pre.setProbFilter(probfilter)
798
799 localpkgs = self.localpkgs.keys()
800
801 for po in package_objects:
802 pkgname = po.name()
803 if pkgname in localpkgs:
804 rpmpath = self.localpkgs[pkgname]
805 else:
806 rpmpath = self.getLocalPkgPath(po)
807
808 if not os.path.exists(rpmpath):
809 # Maybe it is a local repo
810 rpmuri = self.get_url(po)
811 if rpmuri.startswith("file:/"):
812 rpmpath = rpmuri[5:]
813
814 if not os.path.exists(rpmpath):
815 raise RpmError("Error: %s doesn't exist" % rpmpath)
816
817 h = rpmmisc.readRpmHeader(self.ts, rpmpath)
818
819 if pkgname in self.pre_pkgs:
820 msger.verbose("pre-install package added: %s" % pkgname)
821 self.ts_pre.addInstall(h, rpmpath, 'u')
822
823 self.ts.addInstall(h, rpmpath, 'u')
824
825 unresolved_dependencies = self.ts.check()
826 if not unresolved_dependencies:
827 if self.pre_pkgs:
828 self.preinstallPkgs()
829
830 self.ts.order()
831 cb = rpmmisc.RPMInstallCallback(self.ts)
832 installlogfile = "%s/__catched_stderr.buf" % (self.instroot)
833
834 # start to catch stderr output from librpm
835 msger.enable_logstderr(installlogfile)
836
837 errors = self.ts.run(cb.callback, '')
838 # stop catch
839 msger.disable_logstderr()
840 self.ts.closeDB()
841 self.ts = None
842
843 if errors is not None:
844 if len(errors) == 0:
845 msger.warning('scriptlet or other non-fatal errors occurred '
846 'during transaction.')
847
848 else:
849 for e in errors:
850 msger.warning(e[0])
851 raise RepoError('Could not run transaction.')
852
853 else:
854 for pkg, need, needflags, sense, key in unresolved_dependencies:
855 package = '-'.join(pkg)
856
857 if needflags == rpm.RPMSENSE_LESS:
858 deppkg = ' < '.join(need)
859 elif needflags == rpm.RPMSENSE_EQUAL:
860 deppkg = ' = '.join(need)
861 elif needflags == rpm.RPMSENSE_GREATER:
862 deppkg = ' > '.join(need)
863 else:
864 deppkg = '-'.join(need)
865
866 if sense == rpm.RPMDEP_SENSE_REQUIRES:
867 msger.warning("[%s] Requires [%s], which is not provided" \
868 % (package, deppkg))
869
870 elif sense == rpm.RPMDEP_SENSE_CONFLICTS:
871 msger.warning("[%s] Conflicts with [%s]" %(package,deppkg))
872
873 raise RepoError("Unresolved dependencies, transaction failed.")
874
875 def __initialize_transaction(self):
876 if not self.ts:
877 self.ts = rpm.TransactionSet(self.instroot)
878 # Set to not verify DSA signatures.
879 self.ts.setVSFlags(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS)
880
881 if not self.ts_pre:
882 self.ts_pre = rpm.TransactionSet(self.instroot)
883 # Just unpack the files, don't run scripts
884 self.ts_pre.setFlags(rpm.RPMTRANS_FLAG_ALLFILES | rpm.RPMTRANS_FLAG_NOSCRIPTS)
885 # Set to not verify DSA signatures.
886 self.ts_pre.setVSFlags(rpm._RPMVSF_NOSIGNATURES|rpm._RPMVSF_NODIGESTS)
887
888 def checkPkg(self, pkg):
889 ret = 1
890 if not os.path.exists(pkg):
891 return ret
892 ret = rpmmisc.checkRpmIntegrity('rpm', pkg)
893 if ret != 0:
894 msger.warning("package %s is damaged: %s" \
895 % (os.path.basename(pkg), pkg))
896
897 return ret
898
899 def _add_prob_flags(self, *flags):
900 for flag in flags:
901 if flag not in self.probFilterFlags:
902 self.probFilterFlags.append(flag)
903
904 def get_proxies(self, pobj):
905 if not pobj:
906 return None
907
908 proxy = None
909 proxies = None
910 repoinfo = pobj.repoInfo()
911 reponame = "%s" % repoinfo.name()
912 repos = filter(lambda r: r.name == reponame, self.repos)
913 repourl = str(repoinfo.baseUrls()[0])
914
915 if repos:
916 proxy = repos[0].proxy
917 if not proxy:
918 proxy = get_proxy_for(repourl)
919 if proxy:
920 proxies = {str(repourl.split(':')[0]): str(proxy)}
921
922 return proxies
923
924 def get_url(self, pobj):
925 if not pobj:
926 return None
927
928 name = str(pobj.repoInfo().name())
929 try:
930 repo = filter(lambda r: r.name == name, self.repos)[0]
931 except IndexError:
932 return None
933
934 baseurl = repo.baseurl[0]
935
936 index = baseurl.find("?")
937 if index > -1:
938 baseurl = baseurl[:index]
939
940 location = pobj.location()
941 location = str(location.filename())
942 if location.startswith("./"):
943 location = location[2:]
944
945 return os.path.join(baseurl, location)
946
947 def package_url(self, pkgname):
948
949 def cmpEVR(p1, p2):
950 ed1 = p1.edition()
951 ed2 = p2.edition()
952 (e1, v1, r1) = map(str, [ed1.epoch(), ed1.version(), ed1.release()])
953 (e2, v2, r2) = map(str, [ed2.epoch(), ed2.version(), ed2.release()])
954 return rpm.labelCompare((e1, v1, r1), (e2, v2, r2))
955
956 if not self.Z:
957 self.__initialize_zypp()
958
959 q = zypp.PoolQuery()
960 q.addKind(zypp.ResKind.package)
961 q.setMatchExact()
962 q.addAttribute(zypp.SolvAttr.name, pkgname)
963 items = sorted(q.queryResults(self.Z.pool()),
964 cmp=lambda x,y: cmpEVR(zypp.asKindPackage(x), zypp.asKindPackage(y)),
965 reverse=True)
966
967 if items:
968 item = zypp.asKindPackage(items[0])
969 url = self.get_url(item)
970 proxies = self.get_proxies(item)
971 return (url, proxies)
972
973 return (None, None)
diff --git a/scripts/lib/mic/plugins/hook/.py b/scripts/lib/mic/plugins/hook/.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/scripts/lib/mic/plugins/hook/.py
diff --git a/scripts/lib/mic/plugins/hook/empty_hook.py b/scripts/lib/mic/plugins/hook/empty_hook.py
new file mode 100644
index 0000000000..397585d8c1
--- /dev/null
+++ b/scripts/lib/mic/plugins/hook/empty_hook.py
@@ -0,0 +1,3 @@
1#!/usr/bin/python
2
3# TODO: plugin base for hooks
diff --git a/scripts/lib/mic/plugins/imager/fs_plugin.py b/scripts/lib/mic/plugins/imager/fs_plugin.py
new file mode 100644
index 0000000000..8e758db544
--- /dev/null
+++ b/scripts/lib/mic/plugins/imager/fs_plugin.py
@@ -0,0 +1,143 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; version 2 of the License
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18import os
19import sys
20
21from mic import chroot, msger, rt_util
22from mic.utils import cmdln, misc, errors, fs_related
23from mic.imager import fs
24from mic.conf import configmgr
25from mic.plugin import pluginmgr
26
27from mic.pluginbase import ImagerPlugin
28class FsPlugin(ImagerPlugin):
29 name = 'fs'
30
31 @classmethod
32 @cmdln.option("--include-src",
33 dest="include_src",
34 action="store_true",
35 default=False,
36 help="Generate a image with source rpms included")
37 def do_create(self, subcmd, opts, *args):
38 """${cmd_name}: create fs image
39
40 Usage:
41 ${name} ${cmd_name} <ksfile> [OPTS]
42
43 ${cmd_option_list}
44 """
45
46 if len(args) != 1:
47 raise errors.Usage("Extra arguments given")
48
49 creatoropts = configmgr.create
50 ksconf = args[0]
51
52 if creatoropts['runtime'] == 'bootstrap':
53 configmgr._ksconf = ksconf
54 rt_util.bootstrap_mic()
55
56 recording_pkgs = []
57 if len(creatoropts['record_pkgs']) > 0:
58 recording_pkgs = creatoropts['record_pkgs']
59
60 if creatoropts['release'] is not None:
61 if 'name' not in recording_pkgs:
62 recording_pkgs.append('name')
63 if 'vcs' not in recording_pkgs:
64 recording_pkgs.append('vcs')
65
66 configmgr._ksconf = ksconf
67
68 # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
69 if creatoropts['release'] is not None:
70 creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])
71
72 # try to find the pkgmgr
73 pkgmgr = None
74 backends = pluginmgr.get_plugins('backend')
75 if 'auto' == creatoropts['pkgmgr']:
76 for key in configmgr.prefer_backends:
77 if key in backends:
78 pkgmgr = backends[key]
79 break
80 else:
81 for key in backends.keys():
82 if key == creatoropts['pkgmgr']:
83 pkgmgr = backends[key]
84 break
85
86 if not pkgmgr:
87 raise errors.CreatorError("Can't find backend: %s, "
88 "available choices: %s" %
89 (creatoropts['pkgmgr'],
90 ','.join(backends.keys())))
91
92 creator = fs.FsImageCreator(creatoropts, pkgmgr)
93 creator._include_src = opts.include_src
94
95 if len(recording_pkgs) > 0:
96 creator._recording_pkgs = recording_pkgs
97
98 self.check_image_exists(creator.destdir,
99 creator.pack_to,
100 [creator.name],
101 creatoropts['release'])
102
103 try:
104 creator.check_depend_tools()
105 creator.mount(None, creatoropts["cachedir"])
106 creator.install()
107 #Download the source packages ###private options
108 if opts.include_src:
109 installed_pkgs = creator.get_installed_packages()
110 msger.info('--------------------------------------------------')
111 msger.info('Generating the image with source rpms included ...')
112 if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]):
113 msger.warning("Source packages can't be downloaded")
114
115 creator.configure(creatoropts["repomd"])
116 creator.copy_kernel()
117 creator.unmount()
118 creator.package(creatoropts["outdir"])
119 if creatoropts['release'] is not None:
120 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
121 creator.print_outimage_info()
122 except errors.CreatorError:
123 raise
124 finally:
125 creator.cleanup()
126
127 msger.info("Finished.")
128 return 0
129
130 @classmethod
131 def do_chroot(self, target, cmd=[]):#chroot.py parse opts&args
132 try:
133 if len(cmd) != 0:
134 cmdline = ' '.join(cmd)
135 else:
136 cmdline = "/bin/bash"
137 envcmd = fs_related.find_binary_inchroot("env", target)
138 if envcmd:
139 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
140 chroot.chroot(target, None, cmdline)
141 finally:
142 chroot.cleanup_after_chroot("dir", None, None, None)
143 return 1
diff --git a/scripts/lib/mic/plugins/imager/livecd_plugin.py b/scripts/lib/mic/plugins/imager/livecd_plugin.py
new file mode 100644
index 0000000000..d24ef59264
--- /dev/null
+++ b/scripts/lib/mic/plugins/imager/livecd_plugin.py
@@ -0,0 +1,255 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; version 2 of the License
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18import os
19import shutil
20import tempfile
21
22from mic import chroot, msger, rt_util
23from mic.utils import misc, fs_related, errors
24from mic.conf import configmgr
25import mic.imager.livecd as livecd
26from mic.plugin import pluginmgr
27
28from mic.pluginbase import ImagerPlugin
29class LiveCDPlugin(ImagerPlugin):
30 name = 'livecd'
31
32 @classmethod
33 def do_create(self, subcmd, opts, *args):
34 """${cmd_name}: create livecd image
35
36 Usage:
37 ${name} ${cmd_name} <ksfile> [OPTS]
38
39 ${cmd_option_list}
40 """
41
42 if len(args) != 1:
43 raise errors.Usage("Extra arguments given")
44
45 creatoropts = configmgr.create
46 ksconf = args[0]
47
48 if creatoropts['runtime'] == 'bootstrap':
49 configmgr._ksconf = ksconf
50 rt_util.bootstrap_mic()
51
52 if creatoropts['arch'] and creatoropts['arch'].startswith('arm'):
53 msger.warning('livecd cannot support arm images, Quit')
54 return
55
56 recording_pkgs = []
57 if len(creatoropts['record_pkgs']) > 0:
58 recording_pkgs = creatoropts['record_pkgs']
59
60 if creatoropts['release'] is not None:
61 if 'name' not in recording_pkgs:
62 recording_pkgs.append('name')
63 if 'vcs' not in recording_pkgs:
64 recording_pkgs.append('vcs')
65
66 configmgr._ksconf = ksconf
67
68 # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
69 if creatoropts['release'] is not None:
70 creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])
71
72 # try to find the pkgmgr
73 pkgmgr = None
74 backends = pluginmgr.get_plugins('backend')
75 if 'auto' == creatoropts['pkgmgr']:
76 for key in configmgr.prefer_backends:
77 if key in backends:
78 pkgmgr = backends[key]
79 break
80 else:
81 for key in backends.keys():
82 if key == creatoropts['pkgmgr']:
83 pkgmgr = backends[key]
84 break
85
86 if not pkgmgr:
87 raise errors.CreatorError("Can't find backend: %s, "
88 "available choices: %s" %
89 (creatoropts['pkgmgr'],
90 ','.join(backends.keys())))
91
92 creator = livecd.LiveCDImageCreator(creatoropts, pkgmgr)
93
94 if len(recording_pkgs) > 0:
95 creator._recording_pkgs = recording_pkgs
96
97 self.check_image_exists(creator.destdir,
98 creator.pack_to,
99 [creator.name + ".iso"],
100 creatoropts['release'])
101
102 try:
103 creator.check_depend_tools()
104 creator.mount(None, creatoropts["cachedir"])
105 creator.install()
106 creator.configure(creatoropts["repomd"])
107 creator.copy_kernel()
108 creator.unmount()
109 creator.package(creatoropts["outdir"])
110 if creatoropts['release'] is not None:
111 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
112 creator.print_outimage_info()
113
114 except errors.CreatorError:
115 raise
116 finally:
117 creator.cleanup()
118
119 msger.info("Finished.")
120 return 0
121
122 @classmethod
123 def do_chroot(cls, target, cmd=[]):
124 os_image = cls.do_unpack(target)
125 os_image_dir = os.path.dirname(os_image)
126
127 # unpack image to target dir
128 imgsize = misc.get_file_size(os_image) * 1024L * 1024L
129 imgtype = misc.get_image_type(os_image)
130 if imgtype == "btrfsimg":
131 fstype = "btrfs"
132 myDiskMount = fs_related.BtrfsDiskMount
133 elif imgtype in ("ext3fsimg", "ext4fsimg"):
134 fstype = imgtype[:4]
135 myDiskMount = fs_related.ExtDiskMount
136 else:
137 raise errors.CreatorError("Unsupported filesystem type: %s" % fstype)
138
139 extmnt = misc.mkdtemp()
140 extloop = myDiskMount(fs_related.SparseLoopbackDisk(os_image, imgsize),
141 extmnt,
142 fstype,
143 4096,
144 "%s label" % fstype)
145 try:
146 extloop.mount()
147
148 except errors.MountError:
149 extloop.cleanup()
150 shutil.rmtree(extmnt, ignore_errors = True)
151 shutil.rmtree(os_image_dir, ignore_errors = True)
152 raise
153
154 try:
155 if len(cmd) != 0:
156 cmdline = ' '.join(cmd)
157 else:
158 cmdline = "/bin/bash"
159 envcmd = fs_related.find_binary_inchroot("env", extmnt)
160 if envcmd:
161 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
162 chroot.chroot(extmnt, None, cmdline)
163 except:
164 raise errors.CreatorError("Failed to chroot to %s." %target)
165 finally:
166 chroot.cleanup_after_chroot("img", extloop, os_image_dir, extmnt)
167
168 @classmethod
169 def do_pack(cls, base_on):
170 import subprocess
171
172 def __mkinitrd(instance):
173 kernelver = instance._get_kernel_versions().values()[0][0]
174 args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ]
175 try:
176 subprocess.call(args, preexec_fn = instance._chroot)
177 except OSError, (err, msg):
178 raise errors.CreatorError("Failed to execute /usr/libexec/mkliveinitrd: %s" % msg)
179
180 def __run_post_cleanups(instance):
181 kernelver = instance._get_kernel_versions().values()[0][0]
182 args = ["rm", "-f", "/boot/initrd-%s.img" % kernelver]
183
184 try:
185 subprocess.call(args, preexec_fn = instance._chroot)
186 except OSError, (err, msg):
187 raise errors.CreatorError("Failed to run post cleanups: %s" % msg)
188
189 convertoropts = configmgr.convert
190 convertoropts['name'] = os.path.splitext(os.path.basename(base_on))[0]
191 convertor = livecd.LiveCDImageCreator(convertoropts)
192 imgtype = misc.get_image_type(base_on)
193 if imgtype == "btrfsimg":
194 fstype = "btrfs"
195 elif imgtype in ("ext3fsimg", "ext4fsimg"):
196 fstype = imgtype[:4]
197 else:
198 raise errors.CreatorError("Unsupported filesystem type: %s" % fstype)
199 convertor._set_fstype(fstype)
200 try:
201 convertor.mount(base_on)
202 __mkinitrd(convertor)
203 convertor._create_bootconfig()
204 __run_post_cleanups(convertor)
205 convertor.launch_shell(convertoropts['shell'])
206 convertor.unmount()
207 convertor.package()
208 convertor.print_outimage_info()
209 finally:
210 shutil.rmtree(os.path.dirname(base_on), ignore_errors = True)
211
212 @classmethod
213 def do_unpack(cls, srcimg):
214 img = srcimg
215 imgmnt = misc.mkdtemp()
216 imgloop = fs_related.DiskMount(fs_related.LoopbackDisk(img, 0), imgmnt)
217 try:
218 imgloop.mount()
219 except errors.MountError:
220 imgloop.cleanup()
221 raise
222
223 # legacy LiveOS filesystem layout support, remove for F9 or F10
224 if os.path.exists(imgmnt + "/squashfs.img"):
225 squashimg = imgmnt + "/squashfs.img"
226 else:
227 squashimg = imgmnt + "/LiveOS/squashfs.img"
228
229 tmpoutdir = misc.mkdtemp()
230 # unsquashfs requires outdir mustn't exist
231 shutil.rmtree(tmpoutdir, ignore_errors = True)
232 misc.uncompress_squashfs(squashimg, tmpoutdir)
233
234 try:
235 # legacy LiveOS filesystem layout support, remove for F9 or F10
236 if os.path.exists(tmpoutdir + "/os.img"):
237 os_image = tmpoutdir + "/os.img"
238 else:
239 os_image = tmpoutdir + "/LiveOS/ext3fs.img"
240
241 if not os.path.exists(os_image):
242 raise errors.CreatorError("'%s' is not a valid live CD ISO : neither "
243 "LiveOS/ext3fs.img nor os.img exist" %img)
244
245 imgname = os.path.basename(srcimg)
246 imgname = os.path.splitext(imgname)[0] + ".img"
247 rtimage = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), imgname)
248 shutil.copyfile(os_image, rtimage)
249
250 finally:
251 imgloop.cleanup()
252 shutil.rmtree(tmpoutdir, ignore_errors = True)
253 shutil.rmtree(imgmnt, ignore_errors = True)
254
255 return rtimage
diff --git a/scripts/lib/mic/plugins/imager/liveusb_plugin.py b/scripts/lib/mic/plugins/imager/liveusb_plugin.py
new file mode 100644
index 0000000000..7aa8927df9
--- /dev/null
+++ b/scripts/lib/mic/plugins/imager/liveusb_plugin.py
@@ -0,0 +1,260 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; version 2 of the License
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18import os
19import shutil
20import tempfile
21
22from mic import chroot, msger, rt_util
23from mic.utils import misc, fs_related, errors
24from mic.utils.partitionedfs import PartitionedMount
25from mic.conf import configmgr
26from mic.plugin import pluginmgr
27
28import mic.imager.liveusb as liveusb
29
30from mic.pluginbase import ImagerPlugin
31class LiveUSBPlugin(ImagerPlugin):
32 name = 'liveusb'
33
34 @classmethod
35 def do_create(self, subcmd, opts, *args):
36 """${cmd_name}: create liveusb image
37
38 Usage:
39 ${name} ${cmd_name} <ksfile> [OPTS]
40
41 ${cmd_option_list}
42 """
43
44 if len(args) != 1:
45 raise errors.Usage("Extra arguments given")
46
47 creatoropts = configmgr.create
48 ksconf = args[0]
49
50 if creatoropts['runtime'] == "bootstrap":
51 configmgr._ksconf = ksconf
52 rt_util.bootstrap_mic()
53
54 if creatoropts['arch'] and creatoropts['arch'].startswith('arm'):
55 msger.warning('liveusb cannot support arm images, Quit')
56 return
57
58 recording_pkgs = []
59 if len(creatoropts['record_pkgs']) > 0:
60 recording_pkgs = creatoropts['record_pkgs']
61
62 if creatoropts['release'] is not None:
63 if 'name' not in recording_pkgs:
64 recording_pkgs.append('name')
65 if 'vcs' not in recording_pkgs:
66 recording_pkgs.append('vcs')
67
68 configmgr._ksconf = ksconf
69
70 # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
71 if creatoropts['release'] is not None:
72 creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])
73
74 # try to find the pkgmgr
75 pkgmgr = None
76 backends = pluginmgr.get_plugins('backend')
77 if 'auto' == creatoropts['pkgmgr']:
78 for key in configmgr.prefer_backends:
79 if key in backends:
80 pkgmgr = backends[key]
81 break
82 else:
83 for key in backends.keys():
84 if key == creatoropts['pkgmgr']:
85 pkgmgr = backends[key]
86 break
87
88 if not pkgmgr:
89 raise errors.CreatorError("Can't find backend: %s, "
90 "available choices: %s" %
91 (creatoropts['pkgmgr'],
92 ','.join(backends.keys())))
93
94 creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr)
95
96 if len(recording_pkgs) > 0:
97 creator._recording_pkgs = recording_pkgs
98
99 self.check_image_exists(creator.destdir,
100 creator.pack_to,
101 [creator.name + ".usbimg"],
102 creatoropts['release'])
103 try:
104 creator.check_depend_tools()
105 creator.mount(None, creatoropts["cachedir"])
106 creator.install()
107 creator.configure(creatoropts["repomd"])
108 creator.copy_kernel()
109 creator.unmount()
110 creator.package(creatoropts["outdir"])
111 if creatoropts['release'] is not None:
112 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
113 creator.print_outimage_info()
114
115 except errors.CreatorError:
116 raise
117 finally:
118 creator.cleanup()
119
120 msger.info("Finished.")
121 return 0
122
123 @classmethod
124 def do_chroot(cls, target, cmd=[]):
125 os_image = cls.do_unpack(target)
126 os_image_dir = os.path.dirname(os_image)
127
128 # unpack image to target dir
129 imgsize = misc.get_file_size(os_image) * 1024L * 1024L
130 imgtype = misc.get_image_type(os_image)
131 if imgtype == "btrfsimg":
132 fstype = "btrfs"
133 myDiskMount = fs_related.BtrfsDiskMount
134 elif imgtype in ("ext3fsimg", "ext4fsimg"):
135 fstype = imgtype[:4]
136 myDiskMount = fs_related.ExtDiskMount
137 else:
138 raise errors.CreatorError("Unsupported filesystem type: %s" % fstype)
139
140 extmnt = misc.mkdtemp()
141 extloop = myDiskMount(fs_related.SparseLoopbackDisk(os_image, imgsize),
142 extmnt,
143 fstype,
144 4096,
145 "%s label" % fstype)
146
147 try:
148 extloop.mount()
149
150 except errors.MountError:
151 extloop.cleanup()
152 shutil.rmtree(extmnt, ignore_errors = True)
153 raise
154
155 try:
156 if len(cmd) != 0:
157 cmdline = ' '.join(cmd)
158 else:
159 cmdline = "/bin/bash"
160 envcmd = fs_related.find_binary_inchroot("env", extmnt)
161 if envcmd:
162 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
163 chroot.chroot(extmnt, None, cmdline)
164 except:
165 raise errors.CreatorError("Failed to chroot to %s." %target)
166 finally:
167 chroot.cleanup_after_chroot("img", extloop, os_image_dir, extmnt)
168
169 @classmethod
170 def do_pack(cls, base_on):
171 import subprocess
172
173 def __mkinitrd(instance):
174 kernelver = instance._get_kernel_versions().values()[0][0]
175 args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ]
176 try:
177 subprocess.call(args, preexec_fn = instance._chroot)
178
179 except OSError, (err, msg):
180 raise errors.CreatorError("Failed to execute /usr/libexec/mkliveinitrd: %s" % msg)
181
182 def __run_post_cleanups(instance):
183 kernelver = instance._get_kernel_versions().values()[0][0]
184 args = ["rm", "-f", "/boot/initrd-%s.img" % kernelver]
185
186 try:
187 subprocess.call(args, preexec_fn = instance._chroot)
188 except OSError, (err, msg):
189 raise errors.CreatorError("Failed to run post cleanups: %s" % msg)
190
191 convertoropts = configmgr.convert
192 convertoropts['name'] = os.path.splitext(os.path.basename(base_on))[0]
193 convertor = liveusb.LiveUSBImageCreator(convertoropts)
194 imgtype = misc.get_image_type(base_on)
195 if imgtype == "btrfsimg":
196 fstype = "btrfs"
197 elif imgtype in ("ext3fsimg", "ext4fsimg"):
198 fstype = imgtype[:4]
199 else:
200 raise errors.CreatorError("Unsupported filesystem type: %s" % fstyp)
201 convertor._set_fstype(fstype)
202 try:
203 convertor.mount(base_on)
204 __mkinitrd(convertor)
205 convertor._create_bootconfig()
206 __run_post_cleanups(convertor)
207 convertor.launch_shell(convertoropts['shell'])
208 convertor.unmount()
209 convertor.package()
210 convertor.print_outimage_info()
211 finally:
212 shutil.rmtree(os.path.dirname(base_on), ignore_errors = True)
213
214 @classmethod
215 def do_unpack(cls, srcimg):
216 img = srcimg
217 imgsize = misc.get_file_size(img) * 1024L * 1024L
218 imgmnt = misc.mkdtemp()
219 disk = fs_related.SparseLoopbackDisk(img, imgsize)
220 imgloop = PartitionedMount(imgmnt, skipformat = True)
221 imgloop.add_disk('/dev/sdb', disk)
222 imgloop.add_partition(imgsize/1024/1024, "/dev/sdb", "/", "vfat", boot=False)
223 try:
224 imgloop.mount()
225 except errors.MountError:
226 imgloop.cleanup()
227 raise
228
229 # legacy LiveOS filesystem layout support, remove for F9 or F10
230 if os.path.exists(imgmnt + "/squashfs.img"):
231 squashimg = imgmnt + "/squashfs.img"
232 else:
233 squashimg = imgmnt + "/LiveOS/squashfs.img"
234
235 tmpoutdir = misc.mkdtemp()
236 # unsquashfs requires outdir mustn't exist
237 shutil.rmtree(tmpoutdir, ignore_errors = True)
238 misc.uncompress_squashfs(squashimg, tmpoutdir)
239
240 try:
241 # legacy LiveOS filesystem layout support, remove for F9 or F10
242 if os.path.exists(tmpoutdir + "/os.img"):
243 os_image = tmpoutdir + "/os.img"
244 else:
245 os_image = tmpoutdir + "/LiveOS/ext3fs.img"
246
247 if not os.path.exists(os_image):
248 raise errors.CreatorError("'%s' is not a valid live CD ISO : neither "
249 "LiveOS/ext3fs.img nor os.img exist" %img)
250 imgname = os.path.basename(srcimg)
251 imgname = os.path.splitext(imgname)[0] + ".img"
252 rtimage = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), imgname)
253 shutil.copyfile(os_image, rtimage)
254
255 finally:
256 imgloop.cleanup()
257 shutil.rmtree(tmpoutdir, ignore_errors = True)
258 shutil.rmtree(imgmnt, ignore_errors = True)
259
260 return rtimage
diff --git a/scripts/lib/mic/plugins/imager/loop_plugin.py b/scripts/lib/mic/plugins/imager/loop_plugin.py
new file mode 100644
index 0000000000..8f4b030f6b
--- /dev/null
+++ b/scripts/lib/mic/plugins/imager/loop_plugin.py
@@ -0,0 +1,255 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; version 2 of the License
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18import os
19import shutil
20import tempfile
21
22from mic import chroot, msger, rt_util
23from mic.utils import misc, fs_related, errors, cmdln
24from mic.conf import configmgr
25from mic.plugin import pluginmgr
26from mic.imager.loop import LoopImageCreator, load_mountpoints
27
28from mic.pluginbase import ImagerPlugin
29class LoopPlugin(ImagerPlugin):
30 name = 'loop'
31
32 @classmethod
33 @cmdln.option("--compress-disk-image", dest="compress_image",
34 type='choice', choices=("gz", "bz2"), default=None,
35 help="Same with --compress-image")
36 # alias to compress-image for compatibility
37 @cmdln.option("--compress-image", dest="compress_image",
38 type='choice', choices=("gz", "bz2"), default=None,
39 help="Compress all loop images with 'gz' or 'bz2'")
40 @cmdln.option("--shrink", action='store_true', default=False,
41 help="Whether to shrink loop images to minimal size")
42 def do_create(self, subcmd, opts, *args):
43 """${cmd_name}: create loop image
44
45 Usage:
46 ${name} ${cmd_name} <ksfile> [OPTS]
47
48 ${cmd_option_list}
49 """
50
51 if len(args) != 1:
52 raise errors.Usage("Extra arguments given")
53
54 creatoropts = configmgr.create
55 ksconf = args[0]
56
57 if creatoropts['runtime'] == "bootstrap":
58 configmgr._ksconf = ksconf
59 rt_util.bootstrap_mic()
60
61 recording_pkgs = []
62 if len(creatoropts['record_pkgs']) > 0:
63 recording_pkgs = creatoropts['record_pkgs']
64
65 if creatoropts['release'] is not None:
66 if 'name' not in recording_pkgs:
67 recording_pkgs.append('name')
68 if 'vcs' not in recording_pkgs:
69 recording_pkgs.append('vcs')
70
71 configmgr._ksconf = ksconf
72
73 # Called After setting the configmgr._ksconf
74 # as the creatoropts['name'] is reset there.
75 if creatoropts['release'] is not None:
76 creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'],
77 creatoropts['release'],
78 creatoropts['name'])
79 # try to find the pkgmgr
80 pkgmgr = None
81 backends = pluginmgr.get_plugins('backend')
82 if 'auto' == creatoropts['pkgmgr']:
83 for key in configmgr.prefer_backends:
84 if key in backends:
85 pkgmgr = backends[key]
86 break
87 else:
88 for key in backends.keys():
89 if key == creatoropts['pkgmgr']:
90 pkgmgr = backends[key]
91 break
92
93 if not pkgmgr:
94 raise errors.CreatorError("Can't find backend: %s, "
95 "available choices: %s" %
96 (creatoropts['pkgmgr'],
97 ','.join(backends.keys())))
98
99 creator = LoopImageCreator(creatoropts,
100 pkgmgr,
101 opts.compress_image,
102 opts.shrink)
103
104 if len(recording_pkgs) > 0:
105 creator._recording_pkgs = recording_pkgs
106
107 image_names = [creator.name + ".img"]
108 image_names.extend(creator.get_image_names())
109 self.check_image_exists(creator.destdir,
110 creator.pack_to,
111 image_names,
112 creatoropts['release'])
113
114 try:
115 creator.check_depend_tools()
116 creator.mount(None, creatoropts["cachedir"])
117 creator.install()
118 creator.configure(creatoropts["repomd"])
119 creator.copy_kernel()
120 creator.unmount()
121 creator.package(creatoropts["outdir"])
122
123 if creatoropts['release'] is not None:
124 creator.release_output(ksconf,
125 creatoropts['outdir'],
126 creatoropts['release'])
127 creator.print_outimage_info()
128
129 except errors.CreatorError:
130 raise
131 finally:
132 creator.cleanup()
133
134 msger.info("Finished.")
135 return 0
136
137 @classmethod
138 def _do_chroot_tar(cls, target, cmd=[]):
139 mountfp_xml = os.path.splitext(target)[0] + '.xml'
140 if not os.path.exists(mountfp_xml):
141 raise errors.CreatorError("No mount point file found for this tar "
142 "image, please check %s" % mountfp_xml)
143
144 import tarfile
145 tar = tarfile.open(target, 'r')
146 tmpdir = misc.mkdtemp()
147 tar.extractall(path=tmpdir)
148 tar.close()
149
150 mntdir = misc.mkdtemp()
151
152 loops = []
153 for (mp, label, name, size, fstype) in load_mountpoints(mountfp_xml):
154 if fstype in ("ext2", "ext3", "ext4"):
155 myDiskMount = fs_related.ExtDiskMount
156 elif fstype == "btrfs":
157 myDiskMount = fs_related.BtrfsDiskMount
158 elif fstype in ("vfat", "msdos"):
159 myDiskMount = fs_related.VfatDiskMount
160 else:
161 msger.error("Cannot support fstype: %s" % fstype)
162
163 name = os.path.join(tmpdir, name)
164 size = size * 1024L * 1024L
165 loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size),
166 os.path.join(mntdir, mp.lstrip('/')),
167 fstype, size, label)
168
169 try:
170 msger.verbose("Mount %s to %s" % (mp, mntdir + mp))
171 fs_related.makedirs(os.path.join(mntdir, mp.lstrip('/')))
172 loop.mount()
173
174 except:
175 loop.cleanup()
176 for lp in reversed(loops):
177 chroot.cleanup_after_chroot("img", lp, None, mntdir)
178
179 shutil.rmtree(tmpdir, ignore_errors=True)
180 raise
181
182 loops.append(loop)
183
184 try:
185 if len(cmd) != 0:
186 cmdline = "/usr/bin/env HOME=/root " + ' '.join(cmd)
187 else:
188 cmdline = "/usr/bin/env HOME=/root /bin/bash"
189 chroot.chroot(mntdir, None, cmdline)
190 except:
191 raise errors.CreatorError("Failed to chroot to %s." % target)
192 finally:
193 for loop in reversed(loops):
194 chroot.cleanup_after_chroot("img", loop, None, mntdir)
195
196 shutil.rmtree(tmpdir, ignore_errors=True)
197
198 @classmethod
199 def do_chroot(cls, target, cmd=[]):
200 if target.endswith('.tar'):
201 import tarfile
202 if tarfile.is_tarfile(target):
203 LoopPlugin._do_chroot_tar(target, cmd)
204 return
205 else:
206 raise errors.CreatorError("damaged tarball for loop images")
207
208 img = target
209 imgsize = misc.get_file_size(img) * 1024L * 1024L
210 imgtype = misc.get_image_type(img)
211 if imgtype == "btrfsimg":
212 fstype = "btrfs"
213 myDiskMount = fs_related.BtrfsDiskMount
214 elif imgtype in ("ext3fsimg", "ext4fsimg"):
215 fstype = imgtype[:4]
216 myDiskMount = fs_related.ExtDiskMount
217 else:
218 raise errors.CreatorError("Unsupported filesystem type: %s" \
219 % imgtype)
220
221 extmnt = misc.mkdtemp()
222 extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize),
223 extmnt,
224 fstype,
225 4096,
226 "%s label" % fstype)
227 try:
228 extloop.mount()
229
230 except errors.MountError:
231 extloop.cleanup()
232 shutil.rmtree(extmnt, ignore_errors=True)
233 raise
234
235 try:
236 if len(cmd) != 0:
237 cmdline = ' '.join(cmd)
238 else:
239 cmdline = "/bin/bash"
240 envcmd = fs_related.find_binary_inchroot("env", extmnt)
241 if envcmd:
242 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
243 chroot.chroot(extmnt, None, cmdline)
244 except:
245 raise errors.CreatorError("Failed to chroot to %s." % img)
246 finally:
247 chroot.cleanup_after_chroot("img", extloop, None, extmnt)
248
249 @classmethod
250 def do_unpack(cls, srcimg):
251 image = os.path.join(tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"),
252 "target.img")
253 msger.info("Copying file system ...")
254 shutil.copyfile(srcimg, image)
255 return image
diff --git a/scripts/lib/mic/plugins/imager/raw_plugin.py b/scripts/lib/mic/plugins/imager/raw_plugin.py
new file mode 100644
index 0000000000..1b9631dfa2
--- /dev/null
+++ b/scripts/lib/mic/plugins/imager/raw_plugin.py
@@ -0,0 +1,275 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; version 2 of the License
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12# for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc., 59
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18import os
19import shutil
20import re
21import tempfile
22
23from mic import chroot, msger, rt_util
24from mic.utils import misc, fs_related, errors, runner, cmdln
25from mic.conf import configmgr
26from mic.plugin import pluginmgr
27from mic.utils.partitionedfs import PartitionedMount
28
29import mic.imager.raw as raw
30
31from mic.pluginbase import ImagerPlugin
32class RawPlugin(ImagerPlugin):
33 name = 'raw'
34
35 @classmethod
36 @cmdln.option("--compress-disk-image", dest="compress_image", type='choice',
37 choices=("gz", "bz2"), default=None,
38 help="Same with --compress-image")
39 @cmdln.option("--compress-image", dest="compress_image", type='choice',
40 choices=("gz", "bz2"), default = None,
41 help="Compress all raw images before package")
42 @cmdln.option("--generate-bmap", action="store_true", default = None,
43 help="also generate the block map file")
44 @cmdln.option("--fstab-entry", dest="fstab_entry", type='choice',
45 choices=("name", "uuid"), default="uuid",
46 help="Set fstab entry, 'name' means using device names, "
47 "'uuid' means using filesystem uuid")
48 def do_create(self, subcmd, opts, *args):
49 """${cmd_name}: create raw image
50
51 Usage:
52 ${name} ${cmd_name} <ksfile> [OPTS]
53
54 ${cmd_option_list}
55 """
56
57 if len(args) != 1:
58 raise errors.Usage("Extra arguments given")
59
60 creatoropts = configmgr.create
61 ksconf = args[0]
62
63 if creatoropts['runtime'] == "bootstrap":
64 configmgr._ksconf = ksconf
65 rt_util.bootstrap_mic()
66
67 recording_pkgs = []
68 if len(creatoropts['record_pkgs']) > 0:
69 recording_pkgs = creatoropts['record_pkgs']
70
71 if creatoropts['release'] is not None:
72 if 'name' not in recording_pkgs:
73 recording_pkgs.append('name')
74 if 'vcs' not in recording_pkgs:
75 recording_pkgs.append('vcs')
76
77 configmgr._ksconf = ksconf
78
79 # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
80 if creatoropts['release'] is not None:
81 creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])
82
83 # try to find the pkgmgr
84 pkgmgr = None
85 backends = pluginmgr.get_plugins('backend')
86 if 'auto' == creatoropts['pkgmgr']:
87 for key in configmgr.prefer_backends:
88 if key in backends:
89 pkgmgr = backends[key]
90 break
91 else:
92 for key in backends.keys():
93 if key == creatoropts['pkgmgr']:
94 pkgmgr = backends[key]
95 break
96
97 if not pkgmgr:
98 raise errors.CreatorError("Can't find backend: %s, "
99 "available choices: %s" %
100 (creatoropts['pkgmgr'],
101 ','.join(backends.keys())))
102
103 creator = raw.RawImageCreator(creatoropts, pkgmgr, opts.compress_image,
104 opts.generate_bmap, opts.fstab_entry)
105
106 if len(recording_pkgs) > 0:
107 creator._recording_pkgs = recording_pkgs
108
109 images = ["%s-%s.raw" % (creator.name, disk_name)
110 for disk_name in creator.get_disk_names()]
111 self.check_image_exists(creator.destdir,
112 creator.pack_to,
113 images,
114 creatoropts['release'])
115
116 try:
117 creator.check_depend_tools()
118 creator.mount(None, creatoropts["cachedir"])
119 creator.install()
120 creator.configure(creatoropts["repomd"])
121 creator.copy_kernel()
122 creator.unmount()
123 creator.generate_bmap()
124 creator.package(creatoropts["outdir"])
125 if creatoropts['release'] is not None:
126 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
127 creator.print_outimage_info()
128
129 except errors.CreatorError:
130 raise
131 finally:
132 creator.cleanup()
133
134 msger.info("Finished.")
135 return 0
136
137 @classmethod
138 def do_chroot(cls, target, cmd=[]):
139 img = target
140 imgsize = misc.get_file_size(img) * 1024L * 1024L
141 partedcmd = fs_related.find_binary_path("parted")
142 disk = fs_related.SparseLoopbackDisk(img, imgsize)
143 imgmnt = misc.mkdtemp()
144 imgloop = PartitionedMount(imgmnt, skipformat = True)
145 imgloop.add_disk('/dev/sdb', disk)
146 img_fstype = "ext3"
147
148 msger.info("Partition Table:")
149 partnum = []
150 for line in runner.outs([partedcmd, "-s", img, "print"]).splitlines():
151 # no use strip to keep line output here
152 if "Number" in line:
153 msger.raw(line)
154 if line.strip() and line.strip()[0].isdigit():
155 partnum.append(line.strip()[0])
156 msger.raw(line)
157
158 rootpart = None
159 if len(partnum) > 1:
160 rootpart = msger.choice("please choose root partition", partnum)
161
162 # Check the partitions from raw disk.
163 # if choose root part, the mark it as mounted
164 if rootpart:
165 root_mounted = True
166 else:
167 root_mounted = False
168 partition_mounts = 0
169 for line in runner.outs([partedcmd,"-s",img,"unit","B","print"]).splitlines():
170 line = line.strip()
171
172 # Lines that start with number are the partitions,
173 # because parted can be translated we can't refer to any text lines.
174 if not line or not line[0].isdigit():
175 continue
176
177 # Some vars have extra , as list seperator.
178 line = line.replace(",","")
179
180 # Example of parted output lines that are handled:
181 # Number Start End Size Type File system Flags
182 # 1 512B 3400000511B 3400000000B primary
183 # 2 3400531968B 3656384511B 255852544B primary linux-swap(v1)
184 # 3 3656384512B 3720347647B 63963136B primary fat16 boot, lba
185
186 partition_info = re.split("\s+",line)
187
188 size = partition_info[3].split("B")[0]
189
190 if len(partition_info) < 6 or partition_info[5] in ["boot"]:
191 # No filesystem can be found from partition line. Assuming
192 # btrfs, because that is the only MeeGo fs that parted does
193 # not recognize properly.
194 # TODO: Can we make better assumption?
195 fstype = "btrfs"
196 elif partition_info[5] in ["ext2","ext3","ext4","btrfs"]:
197 fstype = partition_info[5]
198 elif partition_info[5] in ["fat16","fat32"]:
199 fstype = "vfat"
200 elif "swap" in partition_info[5]:
201 fstype = "swap"
202 else:
203 raise errors.CreatorError("Could not recognize partition fs type '%s'." % partition_info[5])
204
205 if rootpart and rootpart == line[0]:
206 mountpoint = '/'
207 elif not root_mounted and fstype in ["ext2","ext3","ext4","btrfs"]:
208 # TODO: Check that this is actually the valid root partition from /etc/fstab
209 mountpoint = "/"
210 root_mounted = True
211 elif fstype == "swap":
212 mountpoint = "swap"
213 else:
214 # TODO: Assing better mount points for the rest of the partitions.
215 partition_mounts += 1
216 mountpoint = "/media/partition_%d" % partition_mounts
217
218 if "boot" in partition_info:
219 boot = True
220 else:
221 boot = False
222
223 msger.verbose("Size: %s Bytes, fstype: %s, mountpoint: %s, boot: %s" % (size, fstype, mountpoint, boot))
224 # TODO: add_partition should take bytes as size parameter.
225 imgloop.add_partition((int)(size)/1024/1024, "/dev/sdb", mountpoint, fstype = fstype, boot = boot)
226
227 try:
228 imgloop.mount()
229
230 except errors.MountError:
231 imgloop.cleanup()
232 raise
233
234 try:
235 if len(cmd) != 0:
236 cmdline = ' '.join(cmd)
237 else:
238 cmdline = "/bin/bash"
239 envcmd = fs_related.find_binary_inchroot("env", imgmnt)
240 if envcmd:
241 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
242 chroot.chroot(imgmnt, None, cmdline)
243 except:
244 raise errors.CreatorError("Failed to chroot to %s." %img)
245 finally:
246 chroot.cleanup_after_chroot("img", imgloop, None, imgmnt)
247
248 @classmethod
249 def do_unpack(cls, srcimg):
250 srcimgsize = (misc.get_file_size(srcimg)) * 1024L * 1024L
251 srcmnt = misc.mkdtemp("srcmnt")
252 disk = fs_related.SparseLoopbackDisk(srcimg, srcimgsize)
253 srcloop = PartitionedMount(srcmnt, skipformat = True)
254
255 srcloop.add_disk('/dev/sdb', disk)
256 srcloop.add_partition(srcimgsize/1024/1024, "/dev/sdb", "/", "ext3", boot=False)
257 try:
258 srcloop.mount()
259
260 except errors.MountError:
261 srcloop.cleanup()
262 raise
263
264 image = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), "target.img")
265 args = ['dd', "if=%s" % srcloop.partitions[0]['device'], "of=%s" % image]
266
267 msger.info("`dd` image ...")
268 rc = runner.show(args)
269 srcloop.cleanup()
270 shutil.rmtree(os.path.dirname(srcmnt), ignore_errors = True)
271
272 if rc != 0:
273 raise errors.CreatorError("Failed to dd")
274 else:
275 return image