Skip to content

Instantly share code, notes, and snippets.

@syci
Forked from miebach/xenmigrate.py
Last active September 29, 2016 06:54
Show Gist options
  • Save syci/d18d2693cc98d5535a1e to your computer and use it in GitHub Desktop.
Save syci/d18d2693cc98d5535a1e to your computer and use it in GitHub Desktop.
xenmigrate - convert a Citrix *.xva file to a XEN *.img file.
#!/usr/bin/env python
"""
xenmigrate.py
Xen Migrate
Migrate XenServer to Open Source Xen
(c)2011 Jolokia Networks and Mark Pace -- jolokianetworks.com
pace@jolokianetworks.com
AGPL License
source=open(refdir+filename,'rb')
while True:
data=source.read(blocksize)
if len(data)==0:
source.close()
#sys.stdout.write(str('\nProcessing '+refdir+filename+'...'))
break # EOF
dest.write(data)
else:
#print '\n'+refdir+filename+' not found, skipping...'
if gz:
dest.write(blankblock)
else:
dest.seek(blocksize,1)
if (filenum+1)%notification==0:
sys.stdout.write('w ')
sys.stdout.flush()
filenum+=1
print '\nSuccessful convert'
finally:
try:
dest.close()
source.close()
finally:
print
else:
print 'ERROR: rawfile '+rawfile+' exists'
else:
print 'ERROR: refdir '+refdir+' does not exist'
def vmdktoraw(vmdkfile,rawfile,gz):
"""
take the ref directory of an xva file and create a raw importable file
"""
if debug:
print 'vmdk :',vmdkfile
print 'to raw :',rawfile
print 'gzip :',gz
if (not gz and not os.path.exists(rawfile)) or ((gz and not os.path.exists(rawfile+'.gz')) and (gz and not os.path.exists(rawfile))):
try:
cmd='qemu-img convert '+vmdkfile+' -O raw '+rawfile
print 'Converting...'
response=docmd(cmd)
print response
if gz:
cmd='gzip -v '+rawfile
print 'Gzipping...'
response=docmd(cmd)
print 'Sucessful convert'
except:
print 'ERROR: problem converting file (do you have qemu-img installed?)'
else:
if gz:
print 'ERROR: rawfile '+rawfile+' or '+rawfile+'.gz exists'
else:
print 'ERROR: rawfile '+rawfile+' exists'
##
## Main Program
##
if __name__=='__main__':
# globals
global debug
debug=False
# Hello world
print 'xenmigrate 0.7.4 -- 2011.09.13\n(c)2011 Jolokia Networks and Mark Pace -- jolokianetworks.com\n'
# process arguments
from optparse import OptionParser
parser=OptionParser(usage='%prog [-cdhiltvxz] [vmname]|[exportLVdev]|[importVolGroup]|[importdiskuuid]|[converttofile]')
parser.add_option('-c','--convert',action='store',type='string',dest='convert',metavar='DIR',help='convert DIR or vmdk to importable rawfile')
parser.add_option('-d','--disk',action='store_true',dest='disk',help='display vm disk uuids',default=False)
parser.add_option('--debug',action='store_true',dest='debug',help='display debug info',default=False)
parser.add_option('-i','--import',action='store',type='string',dest='doimport',metavar='FILE',help='import from FILE to [type=xen:importVolGroup]|\n[type=xenserver:importdiskuuid]')
parser.add_option('-l','--lvdev',action='store_true',dest='lvdev',help='display vm logical volume devices',default=False)
parser.add_option('-t','--type',action='store',type='string',dest='type',metavar='TYPE',help='import to [xen]|[xenserver]',default='xen')
parser.add_option('-x','--export',action='store',type='string',dest='export',metavar='FILE',help='export from Xen Server or from Logical Volume dev to FILE')
parser.add_option('-z','--gzip',action='store_true',dest='gz',help='use compression for import, export, or convert (SLOW!)',default=False)
(opts,args)=parser.parse_args()
if len(args)<1:
parser.print_help()
sys.exit(1)
if opts.debug:
debug=True
if opts.disk or opts.lvdev or opts.export:
vmname=args[0]
if '/dev' in vmname and opts.export:
#print 'export dev :',vmname
pass
else:
vmuuid=getvmuuid(vmname)
print 'vm name-label :',vmname
print 'vm uuid :',vmuuid
vmdiskuuids=getvmdiskuuid(vmuuid)
for vmdiskuuid in vmdiskuuids:
print 'vm disk uuid :',vmdiskuuid[0]
print 'vm disk partid :',vmdiskuuid[1]
if opts.lvdev:
lvdev,lvsize=getlvdevxen(vmdiskuuid[0])
if lvdev is not None:
print 'vm disk dev name :',lvdev
print 'vm disk size :',lvsize+'GB'
else:
print 'vm disk dev name : not found in mounted storage repositories'
if opts.export and opts.doimport:
print 'ERROR: export and import cannot be run at the same time'
elif opts.export and opts.convert:
print 'ERROR: export and convert cannot be run at the same time'
elif opts.doimport and opts.convert:
print 'ERROR: import and convert cannot be run at the same time'
elif opts.export and opts.doimport and opts.convert:
print 'ERROR: you have got to be kidding me -- need some more options to run at the same time?'
elif opts.export:
if '/dev' in vmname:
lvdest=lvdesttmp.split('_')[0]
if index==0:
lvdest=lvdesttmp.split('_')[0]
else:
lvdest=lvdest+'_'+lvdesttmp.split('_')[index]
print 'import raw file :',opts.doimport
print 'to lv :',lvdest
print 'in vg :',args[0]
print 'lv size :',lvsize+'GB'
print 'xen config partid :',lvpartid
importvm(lvdest,opts.doimport,args[0],lvsize,opts.gz)
elif opts.type=='xenserver':
print 'import raw file :',opts.doimport
print 'to disk uuid :',args[0]
vmuuid=getdiskuuidvm(args[0])
print 'vm uuid :',vmuuid
importxenserverdisk(opts.doimport,args[0],vmuuid,opts.gz)
else:
print 'ERROR: unknown Xen type for import'
elif opts.convert:
if os.path.isdir(opts.convert):
print 'convert ref dir :',opts.convert
print 'to raw file :',args[0]
reftoraw(opts.convert,args[0],opts.gz)
elif os.path.isfile(opts.convert):
if opts.convert[-5:]=='.vmdk':
filename=args[0]
if filename[-3:]=='.gz':
opts.gz=True
filename=filename[:-3]
print 'convert vmdk file :',opts.convert
print 'to raw file :',filename
vmdktoraw(opts.convert,filename,opts.gz)
else:
print 'ERROR: unknown file convert format'
else:
print 'ERROR: convert source directory or file does not exist'
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment