Skip to content

Commit 09cbff0

Browse files
authored
[reduce_and_add_sonic_images] get image size during installing image (sonic-net#17496)
What is the motivation for this PR? Monitor image size How did you do it? Get image size during installing image How did you verify/test it? // SONiC-OS-20220531.52 "dockerfs": 353373269, "downloaded_image_version": "SONiC-OS-20220531.52", "image_size": 968080035, "squashfs": 575938560 // SONiC-OS-20230531.40 "docker_dir": 973835329, "downloaded_image_version": "SONiC-OS-20230531.40", "image_size": 664367842, "squashfs": 349605888
1 parent 4f56f4b commit 09cbff0

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

ansible/library/reduce_and_add_sonic_images.py

+72
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,73 @@ def download_new_sonic_image(module, new_image_url, save_as):
180180
exec_command(module, cmd="sudo echo {} > /tmp/downloaded-sonic-image-version".format(
181181
results["downloaded_image_version"]))
182182

183+
# get image size
184+
try:
185+
_, out, _ = exec_command(module, cmd="ls -al {}".format(save_as), msg="get new image size")
186+
lines = out.split('\n')
187+
for line in lines:
188+
if save_as in line:
189+
fields = line.split()
190+
if len(fields) < 3:
191+
return
192+
else:
193+
image_size = int(fields[4])
194+
results['image_size'] = image_size
195+
log("image size: {}".format(image_size))
196+
break
197+
except Exception as e:
198+
log("Failed to get image file size: {}".format(e))
199+
200+
201+
def get_sonic_image_size(module):
202+
global results
203+
204+
if "Unknown" == results["downloaded_image_version"]:
205+
return
206+
207+
image_path = "/host/image-{}".format(results["downloaded_image_version"].split("SONiC-OS-")[1])
208+
log("image_path: {}".format(image_path))
209+
squashfs_file_name = "fs.squashfs"
210+
docker_file_name = "dockerfs.tar.gz"
211+
212+
# get docker and squashfs file size
213+
_, out, _ = exec_command(module, cmd="ls -al {}".format(image_path), msg="ls image_dir")
214+
lines = out.split('\n')
215+
for line in lines:
216+
if docker_file_name in line:
217+
fields = line.split()
218+
if len(fields) < 3:
219+
return
220+
else:
221+
docker_file_size = int(fields[4])
222+
results['dockerfs'] = docker_file_size
223+
log("dockerfs size: {}".format(docker_file_size))
224+
elif squashfs_file_name in line:
225+
fields = line.split()
226+
if len(fields) < 3:
227+
return
228+
else:
229+
squashfs_file_size = int(fields[4])
230+
results['squashfs'] = squashfs_file_size
231+
log("squashfs size: {}".format(squashfs_file_size))
232+
233+
# get docker folder size
234+
docker_dir = image_path + "/docker"
235+
if path.exists(docker_dir):
236+
log("docker_dir: {}".format(docker_dir))
237+
_, out, _ = exec_command(module, cmd="sudo du -sb {}".format(docker_dir), msg="get docker_dir")
238+
lines = out.split('\n')
239+
for line in lines:
240+
if docker_dir in line:
241+
fields = line.split()
242+
if len(fields) < 2:
243+
return
244+
else:
245+
docker_dir_size = int(fields[0])
246+
results['docker_dir'] = docker_dir_size
247+
log("docker dir size: {}".format(docker_dir_size))
248+
break
249+
183250

184251
def install_new_sonic_image(module, new_image_url, save_as=None, required_space=1600):
185252
log("install new sonic image")
@@ -265,6 +332,11 @@ def install_new_sonic_image(module, new_image_url, save_as=None, required_space=
265332
msg="Remove config_db.json in preference of minigraph.xml"
266333
)
267334

335+
try:
336+
get_sonic_image_size(module)
337+
except Exception as e:
338+
log("Failed to get image size: {}".format(e))
339+
268340

269341
def work_around_for_slow_disks(module):
270342
# Increase hung task timeout to 600 seconds to avoid kernel panic

0 commit comments

Comments
 (0)