[libvirt] enumerating disk devices

Serge E. Hallyn serge.hallyn at canonical.com
Tue Jul 19 16:13:46 UTC 2011


Hi,

Some people appear to have autostart VMs residing on slow storage.  If
libvirtd starts too early, it'll simply fail trying to start those VMs.
It'd be nice to know when all the storage on which autostart containers
depend becomes available, so as to safely start libvirt.

The python script appended below is one approach.  It could be called as
something like

	for f in /etc/libvirt/qemu/autostart/* /etc/libvirt/lxc/autostart/*; do
		for d in `libvirt_list_storage.py $f`; do
			if [ ! -r $d ]; then
				echo "storage not yet ready"
				exit 1
			fi
		done
	done
	echo "starting libvirtd"
	libvirtd

(Realistically I would have this running in an upstart job which is
'start on mounted' and, if all storage available, emits a
'libvirt-storage-up' event).

Are there better ways to go about this?

thanks,
-serge

#!/usr/bin/python

import sys
from xml.dom.minidom import parse

dom = parse(sys.argv[1])

def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def print_domname(dom):
    #return
    print "Domain %s\n" % getText(dom.childNodes)

def liststorages(dom):
    print_domname(dom.getElementsByTagName("name")[0])
    disks = dom.getElementsByTagName("disk")
    list_disks(disks)
    filesystems = dom.getElementsByTagName('filesystem')
    list_filesystems(filesystems)

def list_filesystems(fslist):
    for fs in fslist:
        if not fs.hasAttribute('type'):
            continue
        if fs.getAttribute('type') != 'mount':
            continue
        dirs = fs.getElementsByTagName("source")
        if len(dirs) == 0:
            continue
        if not dirs[0].getAttribute('dir'):
            continue
        print "dir: %s\n" % dirs[0].getAttribute('dir')

def list_disks(disks):
    for disk in disks:
        if not disk.hasAttribute('type'):
            continue
        if disk.getAttribute('type') == 'file':
            list_disk_type_file(disk)
        elif disk.getAttribute('type') == 'block':
            list_disk_type_block(disk)
        else:
            # not supported
            continue

def list_disk_type_file(disk):
        disksrcs = disk.getElementsByTagName("source")
        if len(disksrcs) == 0:
            return
        if not disksrcs[0].hasAttribute('file'):
            return
        print "file: %s\n" % disksrcs[0].getAttribute('file')

def list_disk_type_block(disk):
        devsrscs = disk.getElementsByTagName("source")
        if len(devsrscs) == 0:
            return
        if not devsrscs[0].hasAttribute('dev'):
            return
        print "dev: %s\n" % devsrscs[0].getAttribute('dev')

liststorages(dom)




More information about the libvir-list mailing list