shareVM- Share insights about using VM's

Simplify the use of virtualization in everyday life

Find VM’s older than N days to free up disk space

leave a comment »

I wrote a Python 2.6 script to find and list VM’s older than 90 days on my Windows workstation, so that I could compress them, move them to a 1TB drive attached to my machine, or to a file server, or delete them.

find_old_vms is a tool to find and list old VM’s (vmdk’s, vhd’s) on your hard drives that are older than a given number of days.

Usage: find_old_vms in_this_directory_tree older_than_days

Example: find_old_vms “c:\\” 90

Download for Windows XP, 2003, and Linux. The script uses atime (latest file access time), which is not supported on Windows Vista and Windows 7.


# NOTE: This script uses atime - the last access time for deciding whether a VM
# ****  file is a candidate. On Windows XP, atime is updated every hour, whereas,
#          Windows Vista and Windows 7 do not provide an atime.

import os, sys, glob, time

# dtroot is the pathname for a node in a directory tree 
# age is the number of days for which a file has not been accessed
# size in bytes is the maximum size of a file
def scan(dtroot, age, size):
	""" scan <dir> scans the <dir> on host for virtual images
	filecount = 0
	wctime = time.time(); # get current time

	for root, subdirs, files in os.walk(dtroot):
		# Build a list of filenames that have a suffix vmd* or vhd*
		vfiles = glob.glob(os.path.join(root,"*.v[mh]d*"))
		for f in vfiles:
			atime = os.path.getatime(f)
			elapsed_time = (wctime - atime)/(60*60*24)
			if elapsed_time > age:
				filecount = filecount + 1
				print f, " last accessed ", int(elapsed_time), " days ago\n",

if __name__ == "__main__":

	import sys
	# User asked for help
	if sys.argv[1] == '?':
		print "\nfind_old_vms in_this_directory_tree older_then_days\n",
		print "Find all files with the suffix .v[mh]d* that have not been accessed since older_than_days",
		print "through a recusrive descent starting from the root in_this_directory_tree",

	# Validate that the first argument in_this_directory_tree is a valid path
	if not os.path.exists(sys.argv[1]):
		print sys.argv[1], " is not a valid directory. Please provide another",

	# Arbitrarily limit search to 10 years
	invalid_age = 0
	if int(sys.argv[2]) < 1:
		invalid_age = 1
	elif int(sys.argv[2]) > 3650:
		invalid_age = 1

	if invalid_age == 1:
		print sys.argv[2], " is invalid. Please provide between 1 and 3650 days",

	scan(sys.argv[1], int(sys.argv[2]), 0)

If you remove the restriction of searching for vImh]d* files, it will help you find other older files as well. I will appreciate your feedback.

Written by paule1s

November 23, 2009 at 12:35 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: