source: package_tags/sipb-xen-guest-installer/1.0/files/usr/sbin/sipb-xen-lvcopy @ 451

Last change on this file since 451 was 449, checked in by price, 17 years ago

sipb-xen-losetup: a half-sane way to use losetup

You should use this instead of plain losetup; everything that uses this
will at least not have races with other things using this.

If it's missing a feature (e.g. a losetup argument) you want,
it shouldn't be hard to add.

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