remove --preserve-order option in tar fs image
[platform/upstream/mic.git] / mic / imager / fs.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
20 from mic import msger
21 from mic.utils import runner, misc
22 from mic.utils.errors import CreatorError
23 from mic.utils.fs_related import find_binary_path
24 from mic.imager.baseimager import BaseImageCreator
25
26 class FsImageCreator(BaseImageCreator):
27     img_format = 'fs'
28
29     def __init__(self, cfgmgr = None, pkgmgr = None):
30         self.zips = {
31             "tar.bz2" : ""
32         }
33         BaseImageCreator.__init__(self, cfgmgr, pkgmgr)
34         self._fstype = None
35         self._fsopts = None
36         self._include_src = False
37
38     def package(self, destdir = "."):
39
40         ignores = ["/dev/fd",
41                    "/dev/stdin",
42                    "/dev/stdout",
43                    "/dev/stderr",
44                    "/etc/mtab"]
45
46         if not os.path.exists(destdir):
47             os.makedirs(destdir)
48
49         if self._recording_pkgs:
50             self._save_recording_pkgs(destdir)
51
52         if not self.pack_to:
53             self.image_files = {'image_files': [self.name]}
54             fsdir = os.path.join(destdir, self.name)
55
56             misc.check_space_pre_cp(self._instroot, destdir)
57             msger.info("Copying %s to %s ..." % (self._instroot, fsdir))
58             runner.show(['cp', "-af", self._instroot, fsdir])
59
60             for exclude in ignores:
61                 if os.path.exists(fsdir + exclude):
62                     os.unlink(fsdir + exclude)
63
64             self.outimage.append(fsdir)
65
66         else:
67             self.image_files = {'image_files': [self.pack_to]}
68             (tar, comp) = os.path.splitext(self.pack_to)
69             try:
70                 tarcreat = {'.tar': '-cf',
71                             '.gz': '-czf',
72                             '.bz2': '-cjf',
73                             '.tgz': '-czf',
74                             '.tbz': '-cjf'}[comp]
75             except KeyError:
76                 raise CreatorError("Unsupported comression for this image type:"
77                                    " '%s', try '.tar', '.tar.gz', etc" % comp)
78
79             dst = os.path.join(destdir, self.pack_to)
80             msger.info("Pack rootfs to %s. Please wait..." % dst)
81
82             tar = find_binary_path('tar')
83             tar_cmdline = [tar, "--numeric-owner",
84                                 "--preserve-permissions",
85                                 "--one-file-system",
86                                 "--directory",
87                                 self._instroot]
88             for ignore_entry in ignores:
89                 if ignore_entry.startswith('/'):
90                     ignore_entry = ignore_entry[1:]
91
92                 tar_cmdline.append("--exclude=%s" % (ignore_entry))
93
94             tar_cmdline.extend([tarcreat, dst, "."])
95
96             rc = runner.show(tar_cmdline)
97             if rc:
98                 raise CreatorError("Failed compress image with tar.bz2. "
99                                    "Cmdline: %s" % (" ".join(tar_cmdline)))
100
101             self.outimage.append(dst)