1 #!/usr/bin/env python2.5
8 from subprocess import call, check_call, Popen, PIPE
10 def losetup(source, offset=0):
11 p = Popen(['sipb-xen-losetup', source, str(offset)], stdout=PIPE)
12 return p.communicate()[0].strip()
14 def 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')
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'])])
30 # XXX should check_call(['/sbin/lvremove', '-f', 'xenvg/'+args_volume])
32 def frob_copy(target, hostname, rootpw):
33 """target should be an LV device filename"""
35 fs = losetup(target, 32256)
36 mntdir = tempfile.mkdtemp('', 'auto-install.frob.', '/tmp')
37 call(['mount', '-t', 'ext3', fs, mntdir])
39 call(['/usr/sbin/chroot', mntdir, '/post-copy', hostname, rootpw])
41 call(['umount', mntdir])
43 call(['losetup', '-d', fs])
45 def duplicate_by_vm(source, target, rootpw, nodd=False, nofrob=False):
46 # source, target should be machine names
48 # 1. copy (by dd) source to target
49 source_device = '/dev/xenvg/' + prefix + source + '_hda'
50 target_device = '/dev/xenvg/' + prefix + target + '_hda'
52 check_call(['/bin/dd', 'bs=1M', 'conv=nocreat',
53 'if='+source_device, 'of='+target_device])
56 frob_copy(target_device, target, rootpw)
61 os.environ['PATH'] = '/usr/sbin:/usr/bin:/sbin:/bin'
62 if subcommand == 'lvcopy':
65 if args[0].startswith('--'):
66 kwargs[args[0][2:]] = True
70 print >>sys.stderr, argv[0]+': bad argument list'
73 duplicate_by_vm(*args, **kwargs)
74 elif subcommand == 'test':
77 print >>sys.stderr, argv[0]+": unknown subcommand: "+subcommand
81 if __name__ == '__main__':
82 sys.exit(main(*sys.argv))