summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/plugins')
-rw-r--r--scripts/lib/wic/plugins/imager/direct.py61
-rw-r--r--scripts/lib/wic/plugins/source/rootfs.py33
2 files changed, 58 insertions, 36 deletions
diff --git a/scripts/lib/wic/plugins/imager/direct.py b/scripts/lib/wic/plugins/imager/direct.py
index 55db826e93..ea709e8c54 100644
--- a/scripts/lib/wic/plugins/imager/direct.py
+++ b/scripts/lib/wic/plugins/imager/direct.py
@@ -54,15 +54,16 @@ class DirectPlugin(ImagerPlugin):
54 self.native_sysroot = native_sysroot 54 self.native_sysroot = native_sysroot
55 self.oe_builddir = oe_builddir 55 self.oe_builddir = oe_builddir
56 56
57 self.debug = options.debug
57 self.outdir = options.outdir 58 self.outdir = options.outdir
58 self.compressor = options.compressor 59 self.compressor = options.compressor
59 self.bmap = options.bmap 60 self.bmap = options.bmap
60 self.no_fstab_update = options.no_fstab_update 61 self.no_fstab_update = options.no_fstab_update
61 self.original_fstab = None 62 self.updated_fstab_path = None
62 63
63 self.name = "%s-%s" % (os.path.splitext(os.path.basename(wks_file))[0], 64 self.name = "%s-%s" % (os.path.splitext(os.path.basename(wks_file))[0],
64 strftime("%Y%m%d%H%M")) 65 strftime("%Y%m%d%H%M"))
65 self.workdir = tempfile.mkdtemp(dir=self.outdir, prefix='tmp.wic.') 66 self.workdir = self.setup_workdir(options.workdir)
66 self._image = None 67 self._image = None
67 self.ptable_format = self.ks.bootloader.ptable 68 self.ptable_format = self.ks.bootloader.ptable
68 self.parts = self.ks.partitions 69 self.parts = self.ks.partitions
@@ -78,6 +79,16 @@ class DirectPlugin(ImagerPlugin):
78 self._image = PartitionedImage(image_path, self.ptable_format, 79 self._image = PartitionedImage(image_path, self.ptable_format,
79 self.parts, self.native_sysroot) 80 self.parts, self.native_sysroot)
80 81
82 def setup_workdir(self, workdir):
83 if workdir:
84 if os.path.exists(workdir):
85 raise WicError("Internal workdir '%s' specified in wic arguments already exists!" % (workdir))
86
87 os.makedirs(workdir)
88 return workdir
89 else:
90 return tempfile.mkdtemp(dir=self.outdir, prefix='tmp.wic.')
91
81 def do_create(self): 92 def do_create(self):
82 """ 93 """
83 Plugin entry point. 94 Plugin entry point.
@@ -90,11 +101,8 @@ class DirectPlugin(ImagerPlugin):
90 finally: 101 finally:
91 self.cleanup() 102 self.cleanup()
92 103
93 def _write_fstab(self, image_rootfs): 104 def update_fstab(self, image_rootfs):
94 """overriden to generate fstab (temporarily) in rootfs. This is called 105 """Assume partition order same as in wks"""
95 from _create, make sure it doesn't get called from
96 BaseImage.create()
97 """
98 if not image_rootfs: 106 if not image_rootfs:
99 return 107 return
100 108
@@ -104,18 +112,9 @@ class DirectPlugin(ImagerPlugin):
104 112
105 with open(fstab_path) as fstab: 113 with open(fstab_path) as fstab:
106 fstab_lines = fstab.readlines() 114 fstab_lines = fstab.readlines()
107 self.original_fstab = fstab_lines.copy()
108
109 if self._update_fstab(fstab_lines, self.parts):
110 with open(fstab_path, "w") as fstab:
111 fstab.writelines(fstab_lines)
112 else:
113 self.original_fstab = None
114 115
115 def _update_fstab(self, fstab_lines, parts):
116 """Assume partition order same as in wks"""
117 updated = False 116 updated = False
118 for part in parts: 117 for part in self.parts:
119 if not part.realnum or not part.mountpoint \ 118 if not part.realnum or not part.mountpoint \
120 or part.mountpoint == "/": 119 or part.mountpoint == "/":
121 continue 120 continue
@@ -144,7 +143,10 @@ class DirectPlugin(ImagerPlugin):
144 fstab_lines.append(line) 143 fstab_lines.append(line)
145 updated = True 144 updated = True
146 145
147 return updated 146 if updated:
147 self.updated_fstab_path = os.path.join(self.workdir, "fstab")
148 with open(self.updated_fstab_path, "w") as f:
149 f.writelines(fstab_lines)
148 150
149 def _full_path(self, path, name, extention): 151 def _full_path(self, path, name, extention):
150 """ Construct full file path to a file we generate. """ 152 """ Construct full file path to a file we generate. """
@@ -160,7 +162,7 @@ class DirectPlugin(ImagerPlugin):
160 a partitioned image. 162 a partitioned image.
161 """ 163 """
162 if not self.no_fstab_update: 164 if not self.no_fstab_update:
163 self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR")) 165 self.update_fstab(self.rootfs_dir.get("ROOTFS_DIR"))
164 166
165 for part in self.parts: 167 for part in self.parts:
166 # get rootfs size from bitbake variable if it's not set in .ks file 168 # get rootfs size from bitbake variable if it's not set in .ks file
@@ -273,14 +275,9 @@ class DirectPlugin(ImagerPlugin):
273 if os.path.isfile(path): 275 if os.path.isfile(path):
274 shutil.move(path, os.path.join(self.outdir, fname)) 276 shutil.move(path, os.path.join(self.outdir, fname))
275 277
276 #Restore original fstab 278 # remove work directory when it is not in debugging mode
277 if self.original_fstab: 279 if not self.debug:
278 fstab_path = self.rootfs_dir.get("ROOTFS_DIR") + "/etc/fstab" 280 shutil.rmtree(self.workdir, ignore_errors=True)
279 with open(fstab_path, "w") as fstab:
280 fstab.writelines(self.original_fstab)
281
282 # remove work directory
283 shutil.rmtree(self.workdir, ignore_errors=True)
284 281
285# Overhead of the MBR partitioning scheme (just one sector) 282# Overhead of the MBR partitioning scheme (just one sector)
286MBR_OVERHEAD = 1 283MBR_OVERHEAD = 1
@@ -343,6 +340,13 @@ class PartitionedImage():
343 part.fsuuid = '0x' + str(uuid.uuid4())[:8].upper() 340 part.fsuuid = '0x' + str(uuid.uuid4())[:8].upper()
344 else: 341 else:
345 part.fsuuid = str(uuid.uuid4()) 342 part.fsuuid = str(uuid.uuid4())
343 else:
344 #make sure the fsuuid for vfat/msdos align with format 0xYYYYYYYY
345 if part.fstype == 'vfat' or part.fstype == 'msdos':
346 if part.fsuuid.upper().startswith("0X"):
347 part.fsuuid = '0x' + part.fsuuid.upper()[2:].rjust(8,"0")
348 else:
349 part.fsuuid = '0x' + part.fsuuid.upper().rjust(8,"0")
346 350
347 def prepare(self, imager): 351 def prepare(self, imager):
348 """Prepare an image. Call prepare method of all image partitions.""" 352 """Prepare an image. Call prepare method of all image partitions."""
@@ -351,7 +355,8 @@ class PartitionedImage():
351 # sizes before we can add them and do the layout. 355 # sizes before we can add them and do the layout.
352 part.prepare(imager, imager.workdir, imager.oe_builddir, 356 part.prepare(imager, imager.workdir, imager.oe_builddir,
353 imager.rootfs_dir, imager.bootimg_dir, 357 imager.rootfs_dir, imager.bootimg_dir,
354 imager.kernel_dir, imager.native_sysroot) 358 imager.kernel_dir, imager.native_sysroot,
359 imager.updated_fstab_path)
355 360
356 # Converting kB to sectors for parted 361 # Converting kB to sectors for parted
357 part.size_sec = part.disk_size * 1024 // self.sector_size 362 part.size_sec = part.disk_size * 1024 // self.sector_size
diff --git a/scripts/lib/wic/plugins/source/rootfs.py b/scripts/lib/wic/plugins/source/rootfs.py
index f1db83f8a1..96d940a91d 100644
--- a/scripts/lib/wic/plugins/source/rootfs.py
+++ b/scripts/lib/wic/plugins/source/rootfs.py
@@ -94,6 +94,7 @@ class RootfsPlugin(SourcePlugin):
94 "it is not a valid path, exiting" % part.rootfs_dir) 94 "it is not a valid path, exiting" % part.rootfs_dir)
95 95
96 part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir) 96 part.rootfs_dir = cls.__get_rootfs_dir(rootfs_dir)
97 part.has_fstab = os.path.exists(os.path.join(part.rootfs_dir, "etc/fstab"))
97 pseudo_dir = os.path.join(part.rootfs_dir, "../pseudo") 98 pseudo_dir = os.path.join(part.rootfs_dir, "../pseudo")
98 if not os.path.lexists(pseudo_dir): 99 if not os.path.lexists(pseudo_dir):
99 logger.warn("%s folder does not exist. " 100 logger.warn("%s folder does not exist. "
@@ -103,9 +104,9 @@ class RootfsPlugin(SourcePlugin):
103 new_rootfs = None 104 new_rootfs = None
104 new_pseudo = None 105 new_pseudo = None
105 # Handle excluded paths. 106 # Handle excluded paths.
106 if part.exclude_path or part.include_path or part.change_directory: 107 if part.exclude_path or part.include_path or part.change_directory or part.update_fstab_in_rootfs:
107 # We need a new rootfs directory we can delete files from. Copy to 108 # We need a new rootfs directory we can safely modify without
108 # workdir. 109 # interfering with other tasks. Copy to workdir.
109 new_rootfs = os.path.realpath(os.path.join(cr_workdir, "rootfs%d" % part.lineno)) 110 new_rootfs = os.path.realpath(os.path.join(cr_workdir, "rootfs%d" % part.lineno))
110 111
111 if os.path.lexists(new_rootfs): 112 if os.path.lexists(new_rootfs):
@@ -199,17 +200,33 @@ class RootfsPlugin(SourcePlugin):
199 if not os.path.lexists(full_path): 200 if not os.path.lexists(full_path):
200 continue 201 continue
201 202
203 if new_pseudo:
204 pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo)
205 else:
206 pseudo = None
202 if path.endswith(os.sep): 207 if path.endswith(os.sep):
203 # Delete content only. 208 # Delete content only.
204 for entry in os.listdir(full_path): 209 for entry in os.listdir(full_path):
205 full_entry = os.path.join(full_path, entry) 210 full_entry = os.path.join(full_path, entry)
206 if os.path.isdir(full_entry) and not os.path.islink(full_entry): 211 rm_cmd = "rm -rf %s" % (full_entry)
207 shutil.rmtree(full_entry) 212 exec_native_cmd(rm_cmd, native_sysroot, pseudo)
208 else:
209 os.remove(full_entry)
210 else: 213 else:
211 # Delete whole directory. 214 # Delete whole directory.
212 shutil.rmtree(full_path) 215 rm_cmd = "rm -rf %s" % (full_path)
216 exec_native_cmd(rm_cmd, native_sysroot, pseudo)
217
218 # Update part.has_fstab here as fstab may have been added or
219 # removed by the above modifications.
220 part.has_fstab = os.path.exists(os.path.join(new_rootfs, "etc/fstab"))
221 if part.update_fstab_in_rootfs and part.has_fstab:
222 fstab_path = os.path.join(new_rootfs, "etc/fstab")
223 # Assume that fstab should always be owned by root with fixed permissions
224 install_cmd = "install -m 0644 %s %s" % (part.updated_fstab_path, fstab_path)
225 if new_pseudo:
226 pseudo = cls.__get_pseudo(native_sysroot, new_rootfs, new_pseudo)
227 else:
228 pseudo = None
229 exec_native_cmd(install_cmd, native_sysroot, pseudo)
213 230
214 part.prepare_rootfs(cr_workdir, oe_builddir, 231 part.prepare_rootfs(cr_workdir, oe_builddir,
215 new_rootfs or part.rootfs_dir, native_sysroot, 232 new_rootfs or part.rootfs_dir, native_sysroot,