Statistics
| Branch: | Revision:

root / rbd / detach @ 35acd6c9

History | View | Annotate | Download (3.1 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
"""Unmap an rbd device from the Image it is mapped to
21

    
22
This program unmaps an rbd device from the Image it is mapped to.
23
It is idempotent if the mapping doesn't exist at all.
24

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

    
28
 - VOL_NAME: The name of the Image file to which the device points
29
 - POOL:     The RADOS pool in which the Image resides
30

    
31
Returns O upon successfull unmapping, 1 upon failure
32

    
33
"""
34

    
35
import os
36
import sys
37
import re
38

    
39
sys.path.append("/usr/share/ganeti")
40

    
41
from ganeti import utils
42

    
43

    
44
def ReadEnv():
45
  """Read the mandatory enviromental variables
46

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

    
58
  return (name, pool)
59

    
60

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

    
67
  name, pool = env
68

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

    
90
      # Unmap the rbd device
91
      cmd = ["rbd", "unmap", "%s" % rbd_dev]
92
      result = utils.RunCmd(cmd)
93
      if result.failed:
94
        sys.stderr.write("rbd unmap failed (%s): %s" %
95
                         (result.fail_reason, result.output))
96
        return 1
97
     
98
      # Succesfull unmapping
99
      return 0
100

    
101
    else:
102
      # The mapping doesn't exist. Do nothing
103
      # ! Here there is a chance that echo or grep actually failed.
104
      # TODO: Zero that chance 
105
      return 0
106

    
107

    
108
if __name__ == "__main__":
109
    sys.exit(main())