source: trunk/packages/sipb-xen-guest-installer/files/usr/sbin/sipb-xen-lvcopy @ 452

Last change on this file since 452 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
RevLine 
[245]1#!/usr/bin/env python2.5
[231]2
3import sys
4import os
5import shutil
6import tempfile
[336]7import time
8from subprocess import call, check_call, Popen, PIPE
[231]9
10def losetup(source, offset=0):
[449]11  p = Popen(['sipb-xen-losetup', source, str(offset)], stdout=PIPE)
12  return p.communicate()[0].strip()
[231]13
[405]14def frob_copy_in_vm(target, *args):
[338]15  '''UNUSED: maybe we'll use this someday; it does isolate the frobber.'''
[336]16  # 1. prepare arguments volume
[245]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
[336]22  # 2. invoke frobber vm
[245]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
[336]30  # XXX should check_call(['/sbin/lvremove', '-f', 'xenvg/'+args_volume])
[245]31
[405]32def frob_copy(target, hostname, rootpw):
33  """target should be an LV device filename"""
[336]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
[405]39  call(['/usr/sbin/chroot', mntdir, '/post-copy', hostname, rootpw])
[336]40  # 3: clean up
41  call(['umount', mntdir])
[338]42  os.rmdir(mntdir)
[336]43  call(['losetup', '-d', fs])
44
[405]45def duplicate_by_vm(source, target, rootpw, nodd=False, nofrob=False):
[336]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'
[338]51  if not nodd:
52    check_call(['/bin/dd', 'bs=1M', 'conv=nocreat',
53                'if='+source_device, 'of='+target_device])
[336]54  # 2. frob target
[405]55  if not nofrob:
56    frob_copy(target_device, target, rootpw)
[336]57
[338]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 = {}
[405]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
[338]73    duplicate_by_vm(*args, **kwargs)
[405]74  elif subcommand == 'test':
75    pass
[338]76  else:
77    print >>sys.stderr, argv[0]+": unknown subcommand: "+subcommand
78    return 2
79  return 0
80
[231]81if __name__ == '__main__':
[338]82  sys.exit(main(*sys.argv))
Note: See TracBrowser for help on using the repository browser.