From: Brendan Cully Subject: demo code to create lvm snapshots for checkpointed domains Date: Thu, 14 Dec 2006 23:46:18 -0800 Message-ID: <20061215074617.GA7243@ventoux.cs.ubc.ca> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="r5Pyd7+fXNt84Ff3" Return-path: Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Xen Developers List-Id: xen-devel@lists.xenproject.org --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Here's some terrible code that demonstrates how one might create synchronised LVM snapshots of running domains. It hooks into the external-device-migrate script to call lvcreate -s at xm save time during migrate round 2. There's also an awful wrapper around xm save -c that rewrites the resulting save record to use the snapshot as its vbd instead of the parent device. It'd be really nice if there were a way to rewrite the config at save time instead of afterwards. This code will probably set your computer on fire. I'm just posting it because it's hard to play with checkpoints without some kind of disk snapshot mechanism. The pieces: a vbd-migrate.sh script that goes into /etc/xen/scripts to be sourced by external-device-migrate, an lvm-save script called by vbd-migrate to do the actual work of creating the snapshot, and a checkpoint wrapper to create the checkpoint and rewrite the resulting image. They're not useful out of the box unless your home directory is /users/brendan. --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="vbd-migration.sh" VBDHELPER=/users/brendan/bin/lvm-save vbd_migration_step () { echo "Calling VBD migration script:" "$@" "$VBDHELPER" "$host" "$domname" "$step" 2>&1 exit $? } vbd_recover () { echo "VBD recover" exit 0 } --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=lvm-save #!/usr/bin/env python import os, sys sys.path.append('/usr/lib/python') from xen.xend import XendClient, sxp from xen.util.xmlrpclib2 import ServerProxy import xmlrpclib class LVM: def __init__(self, name): self._name = name def run(self, cmd): fd = os.popen(cmd) for line in fd: print line.rstrip() return not fd.close() def path(self): return self._name def exists(self): return self.run("lvdisplay %s > /dev/null" % (self._name)) def snapshot(self, target): return self.run("lvcreate -s -L4M -n %s %s" % (target, self._name)) def remove(self): return self.run("lvremove -f %s" % self._name) def createSnapshot(lvm, dest): snapshot = os.path.basename(dest) if not lvm.snapshot(snapshot): slvm = LVM(os.path.join(lvm.path(), snapshot)) if slvm.remove(): print "Removed old snapshot %s" % snapshot if lvm.snapshot(snapshot): return True print "Snapshot failed: %s" % slvm.path() return False return True def getBlockDevice(dominfo): vbd = None for child in sxp.children(dominfo, 'device'): for child in sxp.children(child, 'vbd'): vbd = sxp.child_value(child, 'uname') if not vbd: return None if vbd.startswith('phy:'): vbd = '/dev/' + vbd[4:] return vbd server = ServerProxy(XendClient.uri) host, domname, step = sys.argv[1:] # Only take snapshots for local saves if step != "2" or not host.startswith('/'): sys.exit(0) try: dominfo = server.xend.domain(domname, True) except xmlrpclib.Fault: try: dominfo = server.xend.domain('migrating-' + domname, True) except xmlrpclib.Fault, inst: print inst.faultString sys.exit(1) vbd = getBlockDevice(dominfo) lvm = LVM(vbd) rc = createSnapshot(lvm, host) if rc: sys.exit(0) else: sys.exit(1) --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=checkpoint #!/usr/bin/env python import sys sys.path.append('/usr/lib/python') import os, struct from time import strftime from xen.xend import sxp SIGNATURE = "LinuxGuestRecord" LENLEN = struct.calcsize('i') class VMImageException(Exception): pass class VMImage: def __init__(self, path, cfg=None): self._path = path if cfg: self._cfg = cfg else: self.parse() def parse(self): fd = file(self._path) sig = fd.read(len(SIGNATURE)) if sig != SIGNATURE: raise VMImageException('Signature not found in VM image %s' % self._path) sz = struct.unpack("!i", fd.read(LENLEN))[0] cfg = fd.read(sz) p = sxp.Parser() p.input(cfg) self._cfg = p.get_val() self._vmoff = sz + len(SIGNATURE) self._fd = fd def writehdr(self): cfgstr = sxp.to_string(self._cfg) fd = file(self._path, 'wb') fd.write(SIGNATURE) fd.write(struct.pack("!i", len(cfgstr))) fd.write(cfgstr) self._fd = fd def getvbd(self): vbd = None for child in sxp.children(self._cfg, 'device'): for child in sxp.children(child, 'vbd'): vbd = sxp.child(child, 'uname') return vbd def run(cmd): print cmd fd = os.popen(cmd) for line in fd: print line.rstrip() return not fd.close() dom = sys.argv[1] date = strftime('%m%d%H%M%S') checkpoint = '%s-%s' % (dom, date) #checkpoint = dom rc = run('sudo xm save -c %s %s' % (dom, checkpoint)) if not rc: sys.exit(1) img = VMImage(checkpoint) vbd = img.getvbd() nvbd = os.path.join(os.path.dirname(vbd[1]), checkpoint) vbd[1] = nvbd tgt = checkpoint + '.new' nimg = VMImage(tgt, img._cfg) nimg.writehdr() data = img._fd.read(65536) while data: nimg._fd.write(data) data = img._fd.read(65536) os.rename(tgt, checkpoint) --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --r5Pyd7+fXNt84Ff3--