source: branches/wsgi/packages/sipb-xen-guest-installer/files/usr/sbin/sipb-xen-lvcopy @ 2071

Last change on this file since 2071 was 542, checked in by broder, 16 years ago

Change sipb-xen-lvcopy so that it fails if you try to clone anything but ice3

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1#!/usr/bin/env python2.5
2
3import sys
4import os
5import shutil
6import tempfile
7import time
8from subprocess import call, check_call, Popen, PIPE
9
10# Make sure to use up fd 0 to avoid a bug in subprocess in Python <= 2.5.1
11# NB we need to do this on every Python remctl script.
12# See r53624 in Python svn.
13sys.stdin = open('/dev/null')
14
15def losetup(source, offset=0):
16  p = Popen(['sipb-xen-losetup', source, str(offset)], stdout=PIPE)
17  return p.communicate()[0].strip()
18
19def frob_copy_in_vm(target, *args):
20  '''UNUSED: maybe we'll use this someday; it does isolate the frobber.'''
21  # 1. prepare arguments volume
22  args_volume = prefix+target+'_args'
23  args_device = '/dev/xenvg/' + args_volume
24  check_call(['/sbin/lvcreate', 'xenvg', '--name', args_volume, '--size', '4K'])
25  file(args_device, 'w').write('\n'.join(args) + '\n')
26
27  # 2. invoke frobber vm
28  copier_device = '/dev/xenvg/d_wert_hda'
29  check_call(['/usr/sbin/xm', 'create', 'sipb-database',
30              'machine_name='+target,
31              'disks=' + ' '.join(['phy:'+copier_device+',hda,w',
32                                   'phy:'+target_device+',hdc,w',
33                                   'phy:'+args_device+',hdd,w'])])
34
35  # XXX should check_call(['/sbin/lvremove', '-f', 'xenvg/'+args_volume])
36
37def frob_copy(target, hostname, rootpw):
38  """target should be an LV device filename"""
39  # 1: mount filesystem
40  fs = losetup(target, 32256)
41  mntdir = tempfile.mkdtemp('', 'auto-install.frob.', '/tmp')
42  call(['mount', '-t', 'ext3', fs, mntdir])
43  # 2: do frobbing
44  call(['/usr/sbin/chroot', mntdir, '/post-copy', hostname, rootpw])
45  # 3: clean up
46  call(['umount', mntdir])
47  os.rmdir(mntdir)
48  call(['losetup', '-d', fs])
49
50def duplicate_by_vm(source, target, rootpw, nodd=False, nofrob=False):
51  if source != "ice3":
52     print "Error: can't clone that VM"
53     sys.exit(1)
54  # source, target should be machine names
55  prefix = 'd_'
56  # 1. copy (by dd) source to target
57  source_device = '/dev/xenvg/' + prefix + source + '_hda'
58  target_device = '/dev/xenvg/' + prefix + target + '_hda'
59  if not nodd:
60    check_call(['/bin/dd', 'bs=1M', 'conv=nocreat',
61                'if='+source_device, 'of='+target_device])
62  # 2. frob target
63  if not nofrob:
64    frob_copy(target_device, target, rootpw)
65
66def main(*argv):
67  subcommand = argv[1]
68  args = argv[2:]
69  os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
70  if subcommand == 'lvcopy':
71    kwargs = {}
72    while True:
73      if args[0].startswith('--'):
74        kwargs[args[0][2:]] = True
75        args = args[1:]
76        continue
77      if len(args) != 3:
78        print >>sys.stderr, argv[0]+': bad argument list'
79        return 2
80      break
81    duplicate_by_vm(*args, **kwargs)
82  elif subcommand == 'test':
83    pass
84  else:
85    print >>sys.stderr, argv[0]+": unknown subcommand: "+subcommand
86    return 2
87  return 0
88
89if __name__ == '__main__':
90  sys.exit(main(*sys.argv))
Note: See TracBrowser for help on using the repository browser.