summaryrefslogtreecommitdiffstats
path: root/scripts/lib/mic/plugins/imager/loop_plugin.py
diff options
context:
space:
mode:
authorTom Zanussi <tom.zanussi@linux.intel.com>2013-08-24 15:31:34 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2013-10-01 22:56:03 +0100
commit9fc88f96d40b17c90bac53b90045a87b2d2cff84 (patch)
tree63010e5aabf895697655baf89bd668d6752b3f97 /scripts/lib/mic/plugins/imager/loop_plugin.py
parent53a1d9a788fd9f970af980da2ab975cca60685c4 (diff)
downloadpoky-9fc88f96d40b17c90bac53b90045a87b2d2cff84.tar.gz
wic: Add mic w/pykickstart
This is the starting point for the implemention described in [YOCTO 3847] which came to the conclusion that it would make sense to use kickstart syntax to implement image creation in OpenEmbedded. I subsequently realized that there was an existing tool that already implemented image creation using kickstart syntax, the Tizen/Meego mic tool. As such, it made sense to use that as a starting point - this commit essentially just copies the relevant Python code from the MIC tool to the scripts/lib dir, where it can be accessed by the previously created wic tool. Most of this will be removed or renamed by later commits, since we're initially focusing on partitioning only. Care should be taken so that we can easily add back any additional functionality should we decide later to expand the tool, though (we may also want to contribute our local changes to the mic tool to the Tizen project if it makes sense, and therefore should avoid gratuitous changes to the original code if possible). Added the /mic subdir from Tizen mic repo as a starting point: git clone git://review.tizen.org/tools/mic.git For reference, the top commit: commit 20164175ddc234a17b8a12c33d04b012347b1530 Author: Gui Chen <gui.chen@intel.com> Date: Sun Jun 30 22:32:16 2013 -0400 bump up to 0.19.2 Also added the /plugins subdir, moved to under the /mic subdir (to match the default plugin_dir location in mic.conf.in, which was renamed to yocto-image.conf (moved and renamed by later patches) and put into /scripts. (From OE-Core rev: 31f0360f1fd4ebc9dfcaed42d1c50d2448b4632e) Signed-off-by: Tom Zanussi <tom.zanussi@linux.intel.com> Signed-off-by: Saul Wold <sgw@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/mic/plugins/imager/loop_plugin.py')
-rw-r--r--scripts/lib/mic/plugins/imager/loop_plugin.py255
1 files changed, 255 insertions, 0 deletions
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