1 | #!/usr/bin/env python2.5 |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import shutil |
---|
6 | import tempfile |
---|
7 | import time |
---|
8 | from subprocess import call, check_call, Popen, PIPE |
---|
9 | |
---|
10 | def losetup(source, offset=0): |
---|
11 | p = Popen(['sipb-xen-losetup', source, str(offset)], stdout=PIPE) |
---|
12 | return p.communicate()[0].strip() |
---|
13 | |
---|
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') |
---|
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 | |
---|
32 | def 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 | |
---|
45 | def 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 | |
---|
58 | def 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 | |
---|
81 | if __name__ == '__main__': |
---|
82 | sys.exit(main(*sys.argv)) |
---|