support running specified command for mic chroot
[tools/mic.git] / plugins / imager / fs_plugin.py
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
18 import os
19 import sys
20
21 from mic import chroot, msger, rt_util
22 from mic.utils import cmdln, misc, errors, fs_related
23 from mic.imager import fs
24 from mic.conf import configmgr
25 from mic.plugin import pluginmgr
26
27 from mic.pluginbase import ImagerPlugin
28 class 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 configmgr.bootstrap['enable']:
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
64         configmgr._ksconf = ksconf
65
66         # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there.
67         if creatoropts['release'] is not None:
68             creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name'])
69
70         # try to find the pkgmgr
71         pkgmgr = None
72         backends = pluginmgr.get_plugins('backend')
73         if 'auto' == creatoropts['pkgmgr']:
74             for key in configmgr.prefer_backends:
75                 if key in backends:
76                     pkgmgr = backends[key]
77                     break
78         else:
79             for key in backends.keys():
80                 if key == creatoropts['pkgmgr']:
81                     pkgmgr = backends[key]
82                     break
83
84         if not pkgmgr:
85             raise errors.CreatorError("Can't find backend: %s, "
86                                       "available choices: %s" %
87                                       (creatoropts['pkgmgr'],
88                                        ','.join(backends.keys())))
89
90         creator = fs.FsImageCreator(creatoropts, pkgmgr)
91         creator._include_src = opts.include_src
92
93         if len(recording_pkgs) > 0:
94             creator._recording_pkgs = recording_pkgs
95
96         self.check_image_exists(creator.destdir,
97                                 creator.pack_to,
98                                 [creator.name],
99                                 creatoropts['release'])
100
101         try:
102             creator.check_depend_tools()
103             creator.mount(None, creatoropts["cachedir"])
104             creator.install()
105             #Download the source packages ###private options
106             if opts.include_src:
107                 installed_pkgs =  creator.get_installed_packages()
108                 msger.info('--------------------------------------------------')
109                 msger.info('Generating the image with source rpms included ...')
110                 if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"], creator._instroot, creatoropts["cachedir"]):
111                     msger.warning("Source packages can't be downloaded")
112
113             creator.configure(creatoropts["repomd"])
114             creator.copy_kernel()
115             creator.unmount()
116             creator.package(creatoropts["outdir"])
117             if creatoropts['release'] is not None:
118                 creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release'])
119             creator.print_outimage_info()
120         except errors.CreatorError:
121             raise
122         finally:
123             creator.cleanup()
124
125         msger.info("Finished.")
126         return 0
127
128     @classmethod
129     def do_chroot(self, target, cmd):#chroot.py parse opts&args
130             try:
131                 if len(cmd) != 0:
132                     cmdline = ' '.join(cmd)
133                 else:
134                     cmdline = "/bin/bash"
135                 envcmd = fs_related.find_binary_inchroot("env", target)
136                 if envcmd:
137                     cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
138                 chroot.chroot(target, None, cmdline)
139             finally:
140                 chroot.cleanup_after_chroot("dir", None, None, None)
141                 return 1
142