[libvirt] [re-send][PATCH 1/3] add qemu driver hook

liguang lig.fnst at cn.fujitsu.com
Tue Oct 9 01:51:25 UTC 2012


this hook aimed at migration only, it supposed
to be used as a helper of migrate --copy-storage-*
features to remove the unreasonable limitation
of pre-exist disk images at migration target.
someone can add more functions to hook files.

Signed-off-by: liguang <lig.fnst at cn.fujitsu.com>
---
 hooks/qemu |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 55 insertions(+), 0 deletions(-)
 create mode 100755 hooks/qemu

diff --git a/hooks/qemu b/hooks/qemu
new file mode 100755
index 0000000..8b93668
--- /dev/null
+++ b/hooks/qemu
@@ -0,0 +1,55 @@
+#!/usr/bin/python -u
+
+import sys
+import string
+import os
+import subprocess
+from xml.etree import ElementTree as ET
+
+id, opstr, subopstr, extra = range(4)
+
+if __name__ == "__main__":
+	args = sys.argv[1:]
+	hook_log = "/var/log/libvirt/hooks.log"
+	redir_fp = open(hook_log, 'a')
+	sys.stdout = redir_fp
+	if args:
+		if args[opstr] == "migrate":
+			file_sz = "0xffffffff"
+			c = 0
+			dom_xml = sys.stdin.read()
+			ntree = ET.fromstring(dom_xml)
+			disks = ntree.findall('.//disk/source')
+			if disks == None:
+				sys.exit(1)
+			file_fmt = ntree.findall('.//disk/driver')
+			if file_fmt == None:
+				sys.exit(1)
+			for i in disks:
+				image = i.get('file')
+				if image == None:
+					print 'No disk images'
+					sys.exit(1)
+				if os.path.isfile(image):
+					continue
+				if os.path.isdir(os.path.dirname(image)) != True:
+					try:
+						os.makedirs(os.path.dirname(image))
+					except:
+						print 'Fail to create dir of disk images'
+				ff = file_fmt[c].get('type')
+				if ff == None:
+					ff = "raw"
+				try:
+					subprocess.Popen(['qemu-img', "create", "-f", ff, image, file_sz],
+							stdout = redir_fp)
+				except:
+					print 'Fail to create disk images'
+					sys.exit(1)
+				c = c+1
+		print 'Created disk images for migration'
+	else:
+		print 'qemu <id> <ops> <subops> <extra>'
+
+	sys.stdout.close()
+
-- 
1.7.2.5




More information about the libvir-list mailing list