@@ -114,6 +114,40 @@ def get_next_image():
114
114
next_image = images [next_image_index ]
115
115
return next_image
116
116
117
+ def remove_image (image ):
118
+ if get_image_type () == IMAGE_TYPE_ABOOT :
119
+ nextimage = get_next_image ()
120
+ current = get_current_image ()
121
+ if image == nextimage :
122
+ image_dir = current .replace (IMAGE_PREFIX , IMAGE_DIR_PREFIX )
123
+ command = "echo \" SWI=flash:%s/.sonic-boot.swi\" > %s/%s" % (image_dir , HOST_PATH , ABOOT_BOOT_CONFIG )
124
+ run_command (command )
125
+ click .echo ("Set next boot to current image %s" % current )
126
+
127
+ image_dir = image .replace (IMAGE_PREFIX , IMAGE_DIR_PREFIX )
128
+ click .echo ('Removing image root filesystem...' )
129
+ shutil .rmtree (HOST_PATH + '/' + image_dir )
130
+ click .echo ('Image removed' )
131
+ else :
132
+ click .echo ('Updating GRUB...' )
133
+ config = open (HOST_PATH + '/grub/grub.cfg' , 'r' )
134
+ old_config = config .read ()
135
+ menuentry = re .search ("menuentry '" + image + "[^}]*}" , old_config ).group ()
136
+ config .close ()
137
+ config = open (HOST_PATH + '/grub/grub.cfg' , 'w' )
138
+ # remove menuentry of the image in grub.cfg
139
+ config .write (old_config .replace (menuentry , "" ))
140
+ config .close ()
141
+ click .echo ('Done' )
142
+
143
+ image_dir = image .replace (IMAGE_PREFIX , IMAGE_DIR_PREFIX )
144
+ click .echo ('Removing image root filesystem...' )
145
+ shutil .rmtree (HOST_PATH + '/' + image_dir )
146
+ click .echo ('Done' )
147
+
148
+ run_command ('grub-set-default --boot-directory=' + HOST_PATH + ' 0' )
149
+ click .echo ('Image removed' )
150
+
117
151
# Callback for confirmation prompt. Aborts if user enters "n"
118
152
def abort_if_false (ctx , param , value ):
119
153
if not value :
@@ -216,44 +250,14 @@ def remove(image):
216
250
""" Uninstall image """
217
251
images = get_installed_images ()
218
252
current = get_current_image ()
219
- nextimage = get_next_image ()
220
253
if image not in images :
221
254
click .echo ('Image does not exist' )
222
255
sys .exit (1 )
223
256
if image == current :
224
257
click .echo ('Cannot remove current image' )
225
258
sys .exit (1 )
226
259
227
- if get_image_type () == IMAGE_TYPE_ABOOT :
228
- if image == nextimage :
229
- image_dir = current .replace (IMAGE_PREFIX , IMAGE_DIR_PREFIX )
230
- command = "echo \" SWI=flash:%s/.sonic-boot.swi\" > %s/%s" % (image_dir , HOST_PATH , ABOOT_BOOT_CONFIG )
231
- run_command (command )
232
- click .echo ("Set next boot to current image %s" % current )
233
-
234
- image_dir = image .replace (IMAGE_PREFIX , IMAGE_DIR_PREFIX )
235
- click .echo ('Removing image root filesystem...' )
236
- shutil .rmtree (HOST_PATH + '/' + image_dir )
237
- click .echo ('Image removed' )
238
- else :
239
- click .echo ('Updating GRUB...' )
240
- config = open (HOST_PATH + '/grub/grub.cfg' , 'r' )
241
- old_config = config .read ()
242
- menuentry = re .search ("menuentry '" + image + "[^}]*}" , old_config ).group ()
243
- config .close ()
244
- config = open (HOST_PATH + '/grub/grub.cfg' , 'w' )
245
- # remove menuentry of the image in grub.cfg
246
- config .write (old_config .replace (menuentry , "" ))
247
- config .close ()
248
- click .echo ('Done' )
249
-
250
- image_dir = image .replace (IMAGE_PREFIX , IMAGE_DIR_PREFIX )
251
- click .echo ('Removing image root filesystem...' )
252
- shutil .rmtree (HOST_PATH + '/' + image_dir )
253
- click .echo ('Done' )
254
-
255
- run_command ('grub-set-default --boot-directory=' + HOST_PATH + ' 0' )
256
- click .echo ('Image removed' )
260
+ remove_image (image )
257
261
258
262
# Retrieve version from binary image file and print to screen
259
263
@cli .command ()
@@ -291,5 +295,24 @@ def binary_version(binary_image_path):
291
295
292
296
click .echo (IMAGE_PREFIX + version_num )
293
297
298
+ # Remove installed images which are not current and next
299
+ @cli .command ()
300
+ @click .option ('-y' , '--yes' , is_flag = True , callback = abort_if_false ,
301
+ expose_value = False , prompt = 'Remove images which are not current and next, continue?' )
302
+ def cleanup ():
303
+ """ Remove installed images which are not current and next """
304
+ images = get_installed_images ()
305
+ curimage = get_current_image ()
306
+ nextimage = get_next_image ()
307
+ image_removed = 0
308
+ for image in get_installed_images ():
309
+ if image != curimage and image != nextimage :
310
+ click .echo ("Removing image %s" % image )
311
+ remove_image (image )
312
+ image_removed += 1
313
+
314
+ if image_removed == 0 :
315
+ click .echo ("No image(s) to remove" )
316
+
294
317
if __name__ == '__main__' :
295
318
cli ()
0 commit comments