Statistics
| Branch: | Revision:

root / rbd / attach @ master

History | View | Annotate | Download (4 kB)

1
#!/usr/bin/env python
2
#
3
# Copyright (C) 2011 Greek Research and Technology Network
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
# General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18
# 02110-1301, USA.
19

    
20
"""Map an existing rbd Image to a block device
21

    
22
This program maps an existing rbd Image to a block device
23
e.g. /dev/rbd{X} and returns the device path. If the mapping
24
already exists, it returns the corresponding device path.
25

    
26
It takes it's input from environment variables. Specifically the
27
following variables should be present:
28

    
29
 - VOL_NAME: The name of the Image to map
30
 - POOL:     The RADOS pool in which the Image resides
31

    
32
Returns the block device path (which maps the Image) as a string
33
upon success, or 1 upon failure
34

    
35
"""
36

    
37
import os
38
import sys
39
import re
40

    
41
sys.path.insert(0, "/etc/ganeti/share")
42

    
43
from ganeti import utils
44

    
45

    
46
def ReadEnv():
47
  """Read the mandatory enviromental variables
48

    
49
  """
50
  name = os.getenv("VOL_NAME")
51
  if name is None:
52
    sys.stderr.write('The environment variable VOL_NAME is missing.\n')
53
    return None
54
#  pool = os.getenv("POOL")
55
  pool = "rbd"
56
  if pool is None:
57
    sys.stderr.write('The environment variable POOL is missing.\n')
58
    return None
59

    
60
  return (name, pool)
61

    
62

    
63
def main():
64
  env = ReadEnv()
65
  if env is None:
66
    sys.stderr.write('Wrong environment. Aborting...\n')
67
    return 1
68

    
69
  name, pool = env
70

    
71
  # Check if the mapping already exists
72
  cmd1 = ["rbd", "showmapped"]
73
  result = utils.RunCmd(cmd1)
74
  if result.failed:
75
    sys.stderr.write("rbd showmapped failed (%s): %s" %
76
                     (result.fail_reason, result.output))
77
    return 1
78
  else:
79
    cmd2 = "echo '%s' | grep %s | grep %s" % (result.output, pool, name)
80
    result = utils.RunCmd(cmd2)
81
    if not result.failed:
82
      # The mapping already exists.
83
      # Parse the result and return the rbd device
84
      try:
85
        rbd_dev = re.search("(/dev/rbd\d+)", result.output).group(1)
86
      except:
87
        sys.stderr.write("The rbd mapping exists but couldn't parse "
88
                         "the result of rbd showmapped to find the "
89
                         "corresponding rbd block device (/dev/rbd*)")
90
        return 1
91

    
92
      #return str(rbd_dev)
93
      sys.stdout.write("%s" % str(rbd_dev))
94
      sys.exit(0)
95

    
96
    else:
97
      # The mapping doesn't exist. Create it
98
      cmd = ["rbd", "map", "-p", "%s" % pool, "%s" % name]
99
      result = utils.RunCmd(cmd)
100
      if result.failed:
101
        sys.stderr.write("rbd map failed (%s): %s" % 
102
                         (result.fail_reason, result.output))
103
        return 1
104
      # Use rbd showmapped again to find the rbd device
105
      # the image was mapped to
106
      cmd3 = "rbd showmapped | grep %s | grep %s" % (pool, name)
107
      result = utils.RunCmd(cmd3)
108
      if result.failed:
109
        sys.stderr.write("Can't find mapped device. "
110
                         "rbd showmapped failed (%s): %s" %
111
                         (result.fail_reason, result.output))
112
        return 1
113
      try:
114
        rbd_dev = re.search("(/dev/rbd\d+)", result.output).group(1)
115
      except:
116
        sys.stderr.write("The rbd mapping was created succesfully, but "
117
                         "couldn't parse the result of rbd showmapped to "
118
                         "find the corresponding rbd block device "
119
                         "(/dev/rbd*)")
120
        return 1
121

    
122
      # The device was successfully mapped. Return it
123
      #return str(rbd_dev)
124
      sys.stdout.write("%s" % str(rbd_dev))
125
      sys.exit(0)
126

    
127
if __name__ == "__main__":
128
    sys.exit(main())