summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/plugins/imager/direct.py
diff options
context:
space:
mode:
authorEd Bartosh <ed.bartosh@linux.intel.com>2017-02-09 15:52:55 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2017-02-15 20:06:44 -0800
commitadf5e2956ca4e3136be0ee785115a7e1941a135b (patch)
treef15fb103786bbf8437fee80c89956bbe5be60e02 /scripts/lib/wic/plugins/imager/direct.py
parent47ee6c2153db204a9fda718b92616d4bf851057e (diff)
downloadpoky-adf5e2956ca4e3136be0ee785115a7e1941a135b.tar.gz
wic: move disk operations to PartitionImage class
Disk operations were spread over DirectPlugin, DiskImage and Image code making the code hard to understand. Renamed Image class to PartitionedImage. Removed DiskImage class. Moved disk operations to PartitionedImage. There was an implicit support for multiple disks: if different devices were specified in .wks file (e.g. --ondisk sda and --ondisk sdb), wic would theoretically generate multiple images. This is quite confusing option and the code supporting it was broken for a long time. The same effect (multiple output images) can be achieved in obvious and clear way - by using multiple .wks files. This functionality was removed. PartitionedImage works only with one image. This makes the code less complex and easier to maintain. (From OE-Core rev: 4dc9dbfc7fbc16d349a019e8973d50905cd28244) Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic/plugins/imager/direct.py')
-rw-r--r--scripts/lib/wic/plugins/imager/direct.py97
1 files changed, 30 insertions, 67 deletions
diff --git a/scripts/lib/wic/plugins/imager/direct.py b/scripts/lib/wic/plugins/imager/direct.py
index a9144e2f4b..fefe88e0df 100644
--- a/scripts/lib/wic/plugins/imager/direct.py
+++ b/scripts/lib/wic/plugins/imager/direct.py
@@ -36,25 +36,8 @@ from wic.plugin import pluginmgr
36from wic.pluginbase import ImagerPlugin 36from wic.pluginbase import ImagerPlugin
37from wic.utils.errors import CreatorError, ImageError 37from wic.utils.errors import CreatorError, ImageError
38from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd 38from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
39from wic.utils.partitionedfs import Image 39from wic.utils.partitionedfs import PartitionedImage
40 40
41class DiskImage():
42 """
43 A Disk backed by a file.
44 """
45 def __init__(self, device, size):
46 self.size = size
47 self.device = device
48 self.created = False
49
50 def create(self):
51 if self.created:
52 return
53 # create sparse disk image
54 with open(self.device, 'w') as sparse:
55 os.ftruncate(sparse.fileno(), self.size)
56
57 self.created = True
58 41
59class DirectPlugin(ImagerPlugin): 42class DirectPlugin(ImagerPlugin):
60 """ 43 """
@@ -189,9 +172,10 @@ class DirectPlugin(ImagerPlugin):
189 filesystems from the artifacts directly and combine them into 172 filesystems from the artifacts directly and combine them into
190 a partitioned image. 173 a partitioned image.
191 """ 174 """
192 self._image = Image(self.native_sysroot) 175 image_path = self._full_path(self.workdir, self.parts[0].disk, "direct")
176 self._image = PartitionedImage(image_path, self.ptable_format,
177 self.native_sysroot)
193 178
194 disk_ids = {}
195 for num, part in enumerate(self.parts, 1): 179 for num, part in enumerate(self.parts, 1):
196 # as a convenience, set source to the boot partition source 180 # as a convenience, set source to the boot partition source
197 # instead of forcing it to be set via bootloader --source 181 # instead of forcing it to be set via bootloader --source
@@ -203,10 +187,8 @@ class DirectPlugin(ImagerPlugin):
203 if self.ptable_format == 'gpt': 187 if self.ptable_format == 'gpt':
204 part.uuid = str(uuid.uuid4()) 188 part.uuid = str(uuid.uuid4())
205 else: # msdos partition table 189 else: # msdos partition table
206 if part.disk not in disk_ids: 190 part.uuid = '%0x-%02d' % (self._image.identifier,
207 disk_ids[part.disk] = int.from_bytes(os.urandom(4), 'little') 191 self._get_part_num(num, self.parts))
208 disk_id = disk_ids[part.disk]
209 part.uuid = '%0x-%02d' % (disk_id, self._get_part_num(num, self.parts))
210 192
211 fstab_path = self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR")) 193 fstab_path = self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR"))
212 194
@@ -225,11 +207,6 @@ class DirectPlugin(ImagerPlugin):
225 part.size = int(round(float(rsize_bb))) 207 part.size = int(round(float(rsize_bb)))
226 # need to create the filesystems in order to get their 208 # need to create the filesystems in order to get their
227 # sizes before we can add them and do the layout. 209 # sizes before we can add them and do the layout.
228 # Image.create() actually calls __format_disks() to create
229 # the disk images and carve out the partitions, then
230 # self.assemble() calls Image.assemble() which calls
231 # __write_partitition() for each partition to dd the fs
232 # into the partitions.
233 part.prepare(self, self.workdir, self.oe_builddir, self.rootfs_dir, 210 part.prepare(self, self.workdir, self.oe_builddir, self.rootfs_dir,
234 self.bootimg_dir, self.kernel_dir, self.native_sysroot) 211 self.bootimg_dir, self.kernel_dir, self.native_sysroot)
235 212
@@ -238,26 +215,14 @@ class DirectPlugin(ImagerPlugin):
238 if fstab_path: 215 if fstab_path:
239 shutil.move(fstab_path + ".orig", fstab_path) 216 shutil.move(fstab_path + ".orig", fstab_path)
240 217
241 self._image.layout_partitions(self.ptable_format) 218 self._image.layout_partitions()
242
243 for disk_name, disk in self._image.disks.items():
244 full_path = self._full_path(self.workdir, disk_name, "direct")
245 msger.debug("Adding disk %s as %s with size %s bytes" \
246 % (disk_name, full_path, disk['min_size']))
247 disk_obj = DiskImage(full_path, disk['min_size'])
248 self._image.add_disk(disk_name, disk_obj, disk_ids.get(disk_name))
249
250 self._image.create() 219 self._image.create()
251 220
252 def assemble(self): 221 def assemble(self):
253 """ 222 """
254 Assemble partitions into disk image(s) 223 Assemble partitions into disk image
255 """ 224 """
256 for disk_name, disk in self._image.disks.items(): 225 self._image.assemble()
257 full_path = self._full_path(self.workdir, disk_name, "direct")
258 msger.debug("Assembling disk %s as %s with size %s bytes" \
259 % (disk_name, full_path, disk['min_size']))
260 self._image.assemble(full_path)
261 226
262 def finalize(self): 227 def finalize(self):
263 """ 228 """
@@ -267,26 +232,25 @@ class DirectPlugin(ImagerPlugin):
267 creating and installing a bootloader configuration. 232 creating and installing a bootloader configuration.
268 """ 233 """
269 source_plugin = self.ks.bootloader.source 234 source_plugin = self.ks.bootloader.source
235 disk_name = self.parts[0].disk
270 if source_plugin: 236 if source_plugin:
271 name = "do_install_disk" 237 name = "do_install_disk"
272 methods = pluginmgr.get_source_plugin_methods(source_plugin, 238 methods = pluginmgr.get_source_plugin_methods(source_plugin,
273 {name: None}) 239 {name: None})
274 for disk_name, disk in self._image.disks.items(): 240 methods["do_install_disk"](self._image, disk_name, self, self.workdir,
275 methods["do_install_disk"](disk, disk_name, self, self.workdir, 241 self.oe_builddir, self.bootimg_dir,
276 self.oe_builddir, self.bootimg_dir, 242 self.kernel_dir, self.native_sysroot)
277 self.kernel_dir, self.native_sysroot) 243
278 244 full_path = self._image.path
279 for disk_name, disk in self._image.disks.items(): 245 # Generate .bmap
280 full_path = self._full_path(self.workdir, disk_name, "direct") 246 if self.bmap:
281 # Generate .bmap 247 msger.debug("Generating bmap file for %s" % disk_name)
282 if self.bmap: 248 exec_native_cmd("bmaptool create %s -o %s.bmap" % (full_path, full_path),
283 msger.debug("Generating bmap file for %s" % disk_name) 249 self.native_sysroot)
284 exec_native_cmd("bmaptool create %s -o %s.bmap" % (full_path, full_path), 250 # Compress the image
285 self.native_sysroot) 251 if self.compressor:
286 # Compress the image 252 msger.debug("Compressing disk %s with %s" % (disk_name, self.compressor))
287 if self.compressor: 253 exec_cmd("%s %s" % (self.compressor, full_path))
288 msger.debug("Compressing disk %s with %s" % (disk_name, self.compressor))
289 exec_cmd("%s %s" % (self.compressor, full_path))
290 254
291 def print_info(self): 255 def print_info(self):
292 """ 256 """
@@ -294,13 +258,12 @@ class DirectPlugin(ImagerPlugin):
294 """ 258 """
295 msg = "The new image(s) can be found here:\n" 259 msg = "The new image(s) can be found here:\n"
296 260
297 for disk_name in self._image.disks: 261 extension = "direct" + {"gzip": ".gz",
298 extension = "direct" + {"gzip": ".gz", 262 "bzip2": ".bz2",
299 "bzip2": ".bz2", 263 "xz": ".xz",
300 "xz": ".xz", 264 None: ""}.get(self.compressor)
301 None: ""}.get(self.compressor) 265 full_path = self._full_path(self.outdir, self.parts[0].disk, extension)
302 full_path = self._full_path(self.outdir, disk_name, extension) 266 msg += ' %s\n\n' % full_path
303 msg += ' %s\n\n' % full_path
304 267
305 msg += 'The following build artifacts were used to create the image(s):\n' 268 msg += 'The following build artifacts were used to create the image(s):\n'
306 for part in self.parts: 269 for part in self.parts: