diff options
author | Ed Bartosh <ed.bartosh@linux.intel.com> | 2015-06-27 18:24:50 +0300 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-07-02 23:08:38 +0100 |
commit | 0ef9cdd1ec20d5eed06fe0148de4b996f7c7a75c (patch) | |
tree | 3a22ad684dab54322c1a60f04c9d85ed81daef1b | |
parent | a49a42270046edd460f9619368a6fbeae4d3f58d (diff) | |
download | poky-0ef9cdd1ec20d5eed06fe0148de4b996f7c7a75c.tar.gz |
wic: Refactor fstab update code
Made the code to backup and restore fstab only if it's modified.
Cleaned up the code. Made it more pythonic.
Improved code readability by moving code from several tiny
methods into one place.
(From OE-Core rev: e663b1857fd2975585003bfa4739f8f84c652708)
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r-- | scripts/lib/wic/imager/direct.py | 83 |
1 files changed, 30 insertions, 53 deletions
diff --git a/scripts/lib/wic/imager/direct.py b/scripts/lib/wic/imager/direct.py index 58a9e9d906..561c396c28 100644 --- a/scripts/lib/wic/imager/direct.py +++ b/scripts/lib/wic/imager/direct.py | |||
@@ -90,74 +90,50 @@ class DirectImageCreator(BaseImageCreator): | |||
90 | return realnum + 1 | 90 | return realnum + 1 |
91 | return realnum | 91 | return realnum |
92 | 92 | ||
93 | def __write_fstab(self, image_rootfs): | 93 | def _write_fstab(self, image_rootfs): |
94 | """overriden to generate fstab (temporarily) in rootfs. This is called | 94 | """overriden to generate fstab (temporarily) in rootfs. This is called |
95 | from _create, make sure it doesn't get called from | 95 | from _create, make sure it doesn't get called from |
96 | BaseImage.create() | 96 | BaseImage.create() |
97 | """ | 97 | """ |
98 | if image_rootfs is None: | 98 | if not image_rootfs: |
99 | return None | 99 | return |
100 | |||
101 | fstab_path = image_rootfs + "/etc/fstab" | ||
102 | if not os.path.isfile(fstab_path): | ||
103 | return | ||
100 | 104 | ||
101 | fstab = image_rootfs + "/etc/fstab" | 105 | with open(fstab_path) as fstab: |
102 | if not os.path.isfile(fstab): | 106 | fstab_lines = fstab.readlines() |
103 | return None | ||
104 | 107 | ||
105 | parts = self._get_parts() | 108 | if self._update_fstab(fstab_lines, self._get_parts()): |
109 | shutil.copyfile(fstab_path, fstab_path + ".orig") | ||
106 | 110 | ||
107 | self._save_fstab(fstab) | 111 | with open(fstab_path, "w") as fstab: |
108 | fstab_lines = self._get_fstab(fstab, parts) | 112 | fstab.writelines(fstab_lines) |
109 | self._update_fstab(fstab_lines, parts) | ||
110 | self._write_fstab(fstab, fstab_lines) | ||
111 | 113 | ||
112 | return fstab | 114 | return fstab_path |
113 | 115 | ||
114 | def _update_fstab(self, fstab_lines, parts): | 116 | def _update_fstab(self, fstab_lines, parts): |
115 | """Assume partition order same as in wks""" | 117 | """Assume partition order same as in wks""" |
116 | for num, p in enumerate(parts, 1): | 118 | updated = False |
119 | for num, part in enumerate(parts, 1): | ||
117 | pnum = self.__get_part_num(num, parts) | 120 | pnum = self.__get_part_num(num, parts) |
118 | if not p.mountpoint or p.mountpoint == "/" or p.mountpoint == "/boot" or pnum == 0: | 121 | if not pnum or not part.mountpoint \ |
122 | or part.mountpoint in ("/", "/boot"): | ||
119 | continue | 123 | continue |
120 | 124 | ||
121 | part = '' | ||
122 | # mmc device partitions are named mmcblk0p1, mmcblk0p2.. | 125 | # mmc device partitions are named mmcblk0p1, mmcblk0p2.. |
123 | if p.disk.startswith('mmcblk'): | 126 | prefix = 'p' if part.disk.startswith('mmcblk') else '' |
124 | part = 'p' | 127 | device_name = "/dev/%s%s%d" % (part.disk, prefix, pnum) |
125 | 128 | ||
126 | device_name = "/dev/" + p.disk + part + str(pnum) | 129 | opts = part.fsopts if part.fsopts else "defaults" |
127 | 130 | line = "\t".join([device_name, part.mountpoint, part.fstype, | |
128 | opts = "defaults" | 131 | opts, "0", "0"]) + "\n" |
129 | if p.fsopts: | ||
130 | opts = p.fsopts | ||
131 | |||
132 | fstab_entry = device_name + "\t" + \ | ||
133 | p.mountpoint + "\t" + \ | ||
134 | p.fstype + "\t" + \ | ||
135 | opts + "\t0\t0\n" | ||
136 | fstab_lines.append(fstab_entry) | ||
137 | |||
138 | def _write_fstab(self, fstab, fstab_lines): | ||
139 | fstab = open(fstab, "w") | ||
140 | for line in fstab_lines: | ||
141 | fstab.write(line) | ||
142 | fstab.close() | ||
143 | |||
144 | def _save_fstab(self, fstab): | ||
145 | """Save the current fstab in rootfs""" | ||
146 | shutil.copyfile(fstab, fstab + ".orig") | ||
147 | |||
148 | def _restore_fstab(self, fstab): | ||
149 | """Restore the saved fstab in rootfs""" | ||
150 | if fstab is None: | ||
151 | return | ||
152 | shutil.move(fstab + ".orig", fstab) | ||
153 | 132 | ||
154 | def _get_fstab(self, fstab, parts): | 133 | fstab_lines.append(line) |
155 | """Return the desired contents of /etc/fstab.""" | 134 | updated = True |
156 | f = open(fstab, "r") | ||
157 | fstab_contents = f.readlines() | ||
158 | f.close() | ||
159 | 135 | ||
160 | return fstab_contents | 136 | return updated |
161 | 137 | ||
162 | def set_bootimg_dir(self, bootimg_dir): | 138 | def set_bootimg_dir(self, bootimg_dir): |
163 | """ | 139 | """ |
@@ -250,7 +226,7 @@ class DirectImageCreator(BaseImageCreator): | |||
250 | if not self.ks.handler.bootloader.source and p.mountpoint == "/boot": | 226 | if not self.ks.handler.bootloader.source and p.mountpoint == "/boot": |
251 | self.ks.handler.bootloader.source = p.source | 227 | self.ks.handler.bootloader.source = p.source |
252 | 228 | ||
253 | fstab = self.__write_fstab(self.rootfs_dir.get("ROOTFS_DIR")) | 229 | fstab_path = self._write_fstab(self.rootfs_dir.get("ROOTFS_DIR")) |
254 | 230 | ||
255 | for p in parts: | 231 | for p in parts: |
256 | # need to create the filesystems in order to get their | 232 | # need to create the filesystems in order to get their |
@@ -277,7 +253,8 @@ class DirectImageCreator(BaseImageCreator): | |||
277 | part_type=p.part_type, | 253 | part_type=p.part_type, |
278 | uuid=p.uuid) | 254 | uuid=p.uuid) |
279 | 255 | ||
280 | self._restore_fstab(fstab) | 256 | if fstab_path: |
257 | shutil.move(fstab_path + ".orig", fstab_path) | ||
281 | 258 | ||
282 | self.__image.layout_partitions(self.ptable_format) | 259 | self.__image.layout_partitions(self.ptable_format) |
283 | 260 | ||