forked from turnkeylinux/ebsmount
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_udev.py
executable file
·91 lines (65 loc) · 2.43 KB
/
cmd_udev.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
# Copyright (c) 2010 Alon Swartz <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""EBS Mount - triggered by udev on EBS attach and detach
Arguments:
action action trigger (add | remove)
Environment variables (Amazon EC2):
DEVNAME (required: e.g., /dev/sdf)
PHYSDEVPATH (required: e.g., /devices/xen/vbd-2160)
Environment variables (Eucalyptus):
DEVNAME (required: e.g., /dev/vda)
DEVPATH (required: e.g., /devices/virtio-pci/virtio0/block/vda)
"""
import os
import sys
import ebsmount
from utils import log, config
def usage(e=None):
if e:
print >> sys.stderr, "error: " + str(e)
print >> sys.stderr, "Syntax: %s <action>" % sys.argv[0]
print >> sys.stderr, __doc__.strip()
sys.exit(1)
def fatal(s):
print >> sys.stderr, "error: " + str(s)
sys.exit(1)
def _expected_devpath(devpath, devpaths):
for s in devpaths:
if devpath.startswith(s):
return True
return False
def main():
if not len(sys.argv) == 2:
usage()
if not config.enabled.lower() == "true":
fatal('ebsmount is not enabled (%s)' % config.CONF_FILE)
action = sys.argv[1]
devname = os.getenv('DEVNAME', None)
devpath = os.getenv('PHYSDEVPATH', os.getenv('DEVPATH', None))
if not action in ('add', 'remove'):
usage('action must be one of: add, remove')
if not devname:
usage('DEVNAME is required')
if not devpath:
usage('PHYSDEVPATH or DEVPATH is required')
if not _expected_devpath(devpath, config.devpaths.split()):
usage('PHYSDEVPATH/DEVPATH is not of the expected structure')
# log trigger
log(devname, "received %s trigger" % action)
func = getattr(ebsmount, 'ebsmount_' + action)
func(devname, config.mountdir)
if __name__=="__main__":
main()