Skip to content

Commit 668c116

Browse files
authored
[fwutil]: Use overlay driver when mounting next image filesystem (#825)
* [fwutil]: Use overlay driver when mounting next image filesystem. Signed-off-by: Nazarii Hnydyn <[email protected]> * [fwutil]: Update command reference. Signed-off-by: Nazarii Hnydyn <[email protected]>
1 parent 04648f5 commit 668c116

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

doc/Command-Reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4117,7 +4117,7 @@ Supported options:
41174117
2. -f|--force - install FW regardless the current version
41184118
3. -i|--image - update FW using current/next SONiC image
41194119
4120-
Note: the default option is --image=current
4120+
Note: the default option is --image=current (current/next values are taken from `sonic_installer list`)
41214121
41224122
Go Back To [Beginning of the document](#) or [Beginning of this section](#platform-component-firmware)
41234123

fwutil/lib.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,50 +201,77 @@ class SquashFs(object):
201201
OS_PREFIX = "SONiC-OS-"
202202

203203
FS_PATH_TEMPLATE = "/host/image-{}/fs.squashfs"
204+
FS_RW_TEMPLATE = "/host/image-{}/rw"
205+
FS_WORK_TEMPLATE = "/host/image-{}/work"
204206
FS_MOUNTPOINT_TEMPLATE = "/tmp/image-{}-fs"
205207

206-
def __init__(self):
207-
current_image = self.__get_current_image()
208-
next_image = self.__get_next_image()
209-
210-
if current_image == next_image:
211-
raise RuntimeError("Next boot image is not set")
208+
OVERLAY_MOUNTPOINT_TEMPLATE = "/tmp/image-{}-overlay"
212209

213-
image_stem = next_image.lstrip(self.OS_PREFIX)
210+
def __init__(self):
211+
image_stem = self.next_image.lstrip(self.OS_PREFIX)
214212

215213
self.fs_path = self.FS_PATH_TEMPLATE.format(image_stem)
214+
self.fs_rw = self.FS_RW_TEMPLATE.format(image_stem)
215+
self.fs_work = self.FS_WORK_TEMPLATE.format(image_stem)
216216
self.fs_mountpoint = self.FS_MOUNTPOINT_TEMPLATE.format(image_stem)
217217

218-
def __get_current_image(self):
218+
self.overlay_mountpoint = self.OVERLAY_MOUNTPOINT_TEMPLATE.format(image_stem)
219+
220+
def get_current_image(self):
219221
cmd = "sonic_installer list | grep 'Current: ' | cut -f2 -d' '"
220222
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
221223

222224
return output.rstrip(NEWLINE)
223225

224-
def __get_next_image(self):
226+
def get_next_image(self):
225227
cmd = "sonic_installer list | grep 'Next: ' | cut -f2 -d' '"
226228
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
227229

228230
return output.rstrip(NEWLINE)
229231

232+
def is_next_boot_set(self):
233+
return self.current_image != self.next_image
234+
230235
def mount_next_image_fs(self):
231-
if os.path.ismount(self.fs_mountpoint):
236+
if os.path.ismount(self.fs_mountpoint) or os.path.ismount(self.overlay_mountpoint):
232237
self.umount_next_image_fs()
233238

234239
os.mkdir(self.fs_mountpoint)
235-
cmd = "mount -t squashfs {} {}".format(self.fs_path, self.fs_mountpoint)
240+
cmd = "mount -t squashfs {} {}".format(
241+
self.fs_path,
242+
self.fs_mountpoint
243+
)
236244
subprocess.check_call(cmd, shell=True)
237245

238-
return self.fs_mountpoint
246+
os.mkdir(self.overlay_mountpoint)
247+
cmd = "mount -n -r -t overlay -o lowerdir={},upperdir={},workdir={} overlay {}".format(
248+
self.fs_mountpoint,
249+
self.fs_rw,
250+
self.fs_work,
251+
self.overlay_mountpoint
252+
)
253+
subprocess.check_call(cmd, shell=True)
254+
255+
return self.overlay_mountpoint
239256

240257
def umount_next_image_fs(self):
258+
if os.path.ismount(self.overlay_mountpoint):
259+
cmd = "umount -rf {}".format(self.overlay_mountpoint)
260+
subprocess.check_call(cmd, shell=True)
261+
262+
if os.path.exists(self.overlay_mountpoint):
263+
os.rmdir(self.overlay_mountpoint)
264+
241265
if os.path.ismount(self.fs_mountpoint):
242266
cmd = "umount -rf {}".format(self.fs_mountpoint)
243267
subprocess.check_call(cmd, shell=True)
244268

245269
if os.path.exists(self.fs_mountpoint):
246270
os.rmdir(self.fs_mountpoint)
247271

272+
current_image = property(fget=get_current_image)
273+
next_image = property(fget=get_next_image)
274+
248275

249276
class PlatformComponentsParser(object):
250277
"""

fwutil/log.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,6 @@ def log_fw_install_end(self, component, firmware, status, exception=None):
124124

125125
def print_error(self, msg):
126126
click.echo("Error: {}.".format(msg))
127+
128+
def print_warning(self, msg):
129+
click.echo("Warning: {}.".format(msg))

fwutil/main.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,16 @@ def update(ctx, yes, force, image):
227227
squashfs = None
228228

229229
try:
230-
cup = None
230+
cup = ComponentUpdateProvider()
231231

232232
if image == IMAGE_NEXT:
233233
squashfs = SquashFs()
234-
fs_path = squashfs.mount_next_image_fs()
235-
cup = ComponentUpdateProvider(fs_path)
236-
else:
237-
cup = ComponentUpdateProvider()
234+
235+
if squashfs.is_next_boot_set():
236+
fs_path = squashfs.mount_next_image_fs()
237+
cup = ComponentUpdateProvider(fs_path)
238+
else:
239+
log_helper.print_warning("Next boot is set to current: fallback to defaults")
238240

239241
click.echo(cup.get_status(force))
240242

0 commit comments

Comments
 (0)