demo code to create lvm snapshots for checkpointed domains
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.
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
}
#!/usr/bin/env python
import os, syssys.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)#!/usr/bin/env python
import syssys.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)
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel