Use cpio gzip in mic.
[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 from mic import chroot, msger, rt_util
19 from mic.utils import misc, errors, fs_related
20 from mic.imager import fs
21 from mic.conf import configmgr
22 from mic.plugin import pluginmgr
23
24 from mic.pluginbase import ImagerPlugin
25 class FsPlugin(ImagerPlugin):
26     name = 'fs'
27
28     @classmethod
29     def do_create(self, args):
30         """${cmd_name}: create fs image
31
32         Usage:
33             ${name} ${cmd_name} <ksfile> [OPTS]
34
35         ${cmd_option_list}
36         """
37
38         if args is None:
39             raise errors.Usage("Invalid arguments.")
40
41         creatoropts = configmgr.create
42         ksconf = args.ksfile
43
44         if creatoropts['runtime'] == 'bootstrap':
45             configmgr._ksconf = ksconf
46             rt_util.bootstrap_mic()
47
48         recording_pkgs = []
49         if len(creatoropts['record_pkgs']) > 0:
50             recording_pkgs = creatoropts['record_pkgs']
51
52         if creatoropts['release'] is not None:
53             if 'name' not in recording_pkgs:
54                 recording_pkgs.append('name')
55             if 'vcs' not in recording_pkgs:
56                 recording_pkgs.append('vcs')
57
58         configmgr._ksconf = ksconf
59
60         # try to find the pkgmgr
61         pkgmgr = None
62         backends = pluginmgr.get_plugins('backend')
63         if 'auto' == creatoropts['pkgmgr']:
64             for key in configmgr.prefer_backends:
65                 if key in backends:
66                     pkgmgr = backends[key]
67                     break
68         else:
69             for key in backends.keys():
70                 if key == creatoropts['pkgmgr']:
71                     pkgmgr = backends[key]
72                     break
73
74         if not pkgmgr:
75             raise errors.CreatorError("Can't find backend: %s, "
76                                       "available choices: %s" %
77                                       (creatoropts['pkgmgr'],
78                                        ','.join(backends.keys())))
79
80         creator = fs.FsImageCreator(creatoropts, pkgmgr)
81         creator._include_src = args.include_src
82
83         if len(recording_pkgs) > 0:
84             creator._recording_pkgs = recording_pkgs
85
86         self.check_image_exists(creator.destdir,
87                                 creator.pack_to,
88                                 [creator.name],
89                                 creatoropts['release'])
90
91         try:
92             creator.check_depend_tools()
93             creator.mount(None, creatoropts["cachedir"])
94             creator.install()
95             #Download the source packages ###private options
96             if args.include_src:
97                 installed_pkgs =  creator.get_installed_packages()
98                 msger.info('--------------------------------------------------')
99                 msger.info('Generating the image with source rpms included ...')
100                 if not misc.SrcpkgsDownload(installed_pkgs, creatoropts["repomd"],
101                         creator._instroot, creatoropts["cachedir"]):
102                     msger.warning("Source packages can't be downloaded")
103
104             creator.configure(creatoropts["repomd"])
105             creator.copy_kernel()
106             if creatoropts['cpio']:
107                 creator.create_cpio_image()
108             creator.unmount()
109             creator.package(creatoropts["destdir"])
110             creator.create_manifest()
111             if creatoropts['release'] is not None:
112                 creator.release_output(ksconf, creatoropts['destdir'],
113                         creatoropts['release'])
114             creator.print_outimage_info()
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(self, target, cmd=[]):#chroot.py parse opts&args
125         try:
126             if len(cmd) != 0:
127                 cmdline = ' '.join(cmd)
128             else:
129                 cmdline = "/bin/bash"
130             envcmd = fs_related.find_binary_inchroot("env", target)
131             if envcmd:
132                 cmdline = "%s HOME=/root %s" % (envcmd, cmdline)
133             chroot.chroot(target, None, cmdline)
134         finally:
135             chroot.cleanup_after_chroot("dir", None, None, None)
136             return 1