forked from vmware/photon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I8249c644c3f2c23e284672fe117f86a075fd793a Reviewed-on: http://photon-jenkins.eng.vmware.com/39 Tested-by: jenkins-photon <[email protected]> Reviewed-by: Touseef Liaqat <[email protected]>
- Loading branch information
Showing
4 changed files
with
83 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python | ||
import subprocess | ||
import sys | ||
|
||
def cleanUpChroot(chrootPath): | ||
returnVal,listmountpoints=findmountpoints(chrootPath) | ||
|
||
if not returnVal: | ||
return False | ||
|
||
sortmountpoints(listmountpoints, chrootPath) | ||
|
||
print listmountpoints | ||
|
||
if not unmountmountpoints(listmountpoints): | ||
return False | ||
|
||
if not removeAllFilesFromChroot(chrootPath): | ||
return False | ||
|
||
return True | ||
|
||
def removeAllFilesFromChroot(chrootPath): | ||
cmd="rm -rf "+chrootPath+"/*" | ||
process = subprocess.Popen("%s" %cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) | ||
retval = process.wait() | ||
if retval!=0: | ||
print "Unable to remove files from chroot "+chrootPath | ||
return False | ||
return True | ||
|
||
def unmountmountpoints(listmountpoints): | ||
if listmountpoints is None: | ||
return True | ||
result=True | ||
for mountpoint in listmountpoints: | ||
cmd="umount "+mountpoint | ||
process = subprocess.Popen("%s" %cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) | ||
retval = process.wait() | ||
if retval!=0: | ||
result = False | ||
print "Unable to unmount "+mountpoint | ||
break | ||
if not result: | ||
print "Unable to unmount all mounts. Unable to clean up the chroot" | ||
return False | ||
return True | ||
|
||
def findmountpoints(chrootPath): | ||
cmd="mount | grep "+chrootPath+" | cut -d' ' -s -f3" | ||
process = subprocess.Popen("%s" %cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) | ||
retval = process.wait() | ||
if retval!=0: | ||
print "Unable to find mountpoints in chroot" | ||
return False,None | ||
mountpoints=process.communicate()[0] | ||
mountpoints= mountpoints.replace("\n"," ").strip() | ||
if mountpoints == "": | ||
print "No mount points found" | ||
return True,None | ||
listmountpoints=mountpoints.split(" ") | ||
return True,listmountpoints | ||
|
||
def sortmountpoints(listmountpoints,chrootPath): | ||
if listmountpoints is None: | ||
return True | ||
sortedmountpoints=listmountpoints | ||
sorted(sortedmountpoints) | ||
sortedmountpoints.reverse() | ||
|
||
def main(): | ||
if len(sys.argv) < 2: | ||
print "Usage: ./clean-up-chroot.py <chrootpath>" | ||
return False | ||
return cleanUpChroot(sys.argv[1]) | ||
|
||
if __name__=="__main__": | ||
main() | ||
|