aboutsummaryrefslogtreecommitdiffstats
path: root/tools/xm-test/lib/XmTestLib/block_utils.py
blob: c302efeb156138a5da34cade53806269cbc81947 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python

# Copyright (c) 2006 XenSource Inc.
# Author: Ewan Mellor <ewan@xensource.com>

import time

from XmTestLib import *

import xen.util.blkif


__all__ = [ "block_attach", "block_detach" ]


def get_state(domain, devname):
    (path, number) = xen.util.blkif.blkdev_name_to_number(devname)
    s, o = traceCommand("xm block-list %s | awk '/^%d/ {print $4}'" %
                        (domain.getName(), number))
    if s != 0:
        FAIL("block-list failed")
    if o == "":
        return 0
    else:
        return int(o)


def block_attach(domain, phy, virt):
    status, output = traceCommand("xm block-attach %s %s %s w" %
                                  (domain.getName(), phy, virt))
    if status != 0:
        FAIL("xm block-attach returned invalid %i != 0" % status)

    for i in range(10):
        if get_state(domain, virt) == 4:
            break
        time.sleep(1)
    else:
        FAIL("block-attach failed: device did not switch to Connected state")


def block_detach(domain, virt):
    status, output = traceCommand("xm block-detach %s %s" %
                                  (domain.getName(), virt))
    if status != 0:
        FAIL("xm block-detach returned invalid %i != 0" % status)

    for i in range(10):
        if get_state(domain, virt) == 0:
            break
        time.sleep(1)
    else:
        FAIL("block-detach failed: device did not disappear")