[vbox-dev] Script to display snapshot media tree in CLI

Miroslav Matějů melebius at gmail.com
Thu May 19 12:36:45 GMT 2016


Hi,

I’ve created a Python script to list the hierarchy of virtual disks in
command-line interface. It helps to get a quick reference of the
snapshot sizes. This can be viewed in Virtual Media Manager in GUI,
however I need to do it on a headless server.

Hope it helps someone else too.

Miroslav

=====

Sample output:

UUID:           …
Size on disk:   2126 MBytes
In use by VMs:  Ubuntu-experimental (UUID: …) [DB import (UUID: …)]
  UUID:           …
  Size on disk:   4 MBytes
  In use by VMs:  Ubuntu-experimental (UUID: …) [DB import cancelled (UUID: …)]
    UUID:           …
    Size on disk:   2 MBytes
    In use by VMs:  Ubuntu-experimental (UUID: …) [branch 0.10.x (UUID: …)]
      UUID:           …
      Size on disk:   2 MBytes
      In use by VMs:  Ubuntu-experimental (UUID: …)
  UUID:           …
  Size on disk:   4504 MBytes
  In use by VMs:  Ubuntu-experimental (UUID: …) [MySQL moved (UUID: …)]
    UUID:           …
    Size on disk:   1078 MBytes
    In use by VMs:  Ubuntu-experimental (UUID: …) [MySQL working (UUID: …)]

=====

Script:

#! /usr/bin/python3
#
# List the tree of a virtual medium, mainly the disk used in snapshots.
# Provide the filename (or UUID) of the root as the command-line parameter.
#
# Author:  Miroslav Matějů <melebius at gmail.com>
# Licence: GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.txt>

import re, subprocess, sys

def indent(level):
        result = ""
        for i in range(level):
                result += "  "
        return result

to_print = re.compile("UUID|Size on disk|In use by VM")
first_child = re.compile("Child UUIDs:")

queue = [(0, sys.argv[1])]

while queue:
        level, uuid = queue.pop(0)
        diskinfo =
subprocess.check_output(["VBoxManage","showmediuminfo","disk", uuid])
        children = False
        tmpqueue = []
        for rawline in diskinfo.splitlines():
                line = str(rawline, 'utf-8')
                if to_print.match(line):
                        print(indent(level) + line)
                if not children and first_child.match(line):
                        children = True # from now on (until EOF),
children's UUIDs are listed
                if children:
                        child = line[-36:] # UUID is 36 chars long
                        tmpqueue.append((level + 1, child))
        queue = tmpqueue + queue # match the order of VBoxManage snapshot list



More information about the vbox-dev mailing list