1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # Copyright (c) 2006 XenSource Inc. |
---|
4 | # Author: Ewan Mellor <ewan@xensource.com> |
---|
5 | |
---|
6 | import time |
---|
7 | |
---|
8 | from XmTestLib import * |
---|
9 | from acm import * |
---|
10 | |
---|
11 | import xen.util.blkif |
---|
12 | |
---|
13 | |
---|
14 | __all__ = [ "block_attach", "block_detach" ] |
---|
15 | |
---|
16 | |
---|
17 | def get_state(domain, devname): |
---|
18 | number = xen.util.blkif.blkdev_name_to_number(devname) |
---|
19 | s, o = traceCommand("xm block-list %s | awk '/^%d/ {print $4}'" % |
---|
20 | (domain.getName(), number)) |
---|
21 | if s != 0: |
---|
22 | FAIL("block-list failed") |
---|
23 | if o == "": |
---|
24 | return 0 |
---|
25 | else: |
---|
26 | return int(o) |
---|
27 | |
---|
28 | |
---|
29 | def block_attach(domain, phy, virt): |
---|
30 | ACMLabelResource(phy) |
---|
31 | status, output = traceCommand("xm block-attach %s %s %s w" % |
---|
32 | (domain.getName(), phy, virt)) |
---|
33 | if status != 0: |
---|
34 | FAIL("xm block-attach returned invalid %i != 0" % status) |
---|
35 | |
---|
36 | for i in range(10): |
---|
37 | if get_state(domain, virt) == 4: |
---|
38 | break |
---|
39 | time.sleep(1) |
---|
40 | else: |
---|
41 | FAIL("block-attach failed: device did not switch to Connected state") |
---|
42 | |
---|
43 | |
---|
44 | def block_detach(domain, virt): |
---|
45 | status, output = traceCommand("xm block-detach %s %s" % |
---|
46 | (domain.getName(), virt)) |
---|
47 | if status != 0: |
---|
48 | FAIL("xm block-detach returned invalid %i != 0" % status) |
---|
49 | |
---|
50 | for i in range(10): |
---|
51 | if get_state(domain, virt) == 0: |
---|
52 | break |
---|
53 | time.sleep(1) |
---|
54 | else: |
---|
55 | FAIL("block-detach failed: device did not disappear") |
---|