Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mount.fuse.ceph
1 #!/usr/bin/env python
2 '''
3 Helper to mount ceph-fuse from /etc/fstab.  To use, add an entry
4 like:
5
6 DEVICE  PATH       TYPE        OPTIONS
7 none    /mnt/ceph  fuse.ceph   ceph.id=admin,_netdev,defaults  0 0
8 none    /mnt/ceph  fuse.ceph   ceph.name=client.admin,_netdev,defaults  0 0
9 none    /mnt/ceph  fuse.ceph   ceph.id=myuser,ceph.conf=/etc/ceph/foo.conf,_netdev,defaults  0 0
10
11 ceph-fuse options are specified in the fs_mntops(4) column and must begin
12 with 'ceph.' prefix. This way ceph related fs options will be passed to
13 ceph-fuse and others will be ignored by ceph-fuse.
14 First two examples above, for example, speficy that ceph-fuse will authenticate
15 as client.admin. Third example specify, that ceph-fuse will authenticate as
16 client.myuser and also sets 'conf' option to '/etc/ceph/foo.conf' via ceph-fuse
17 command line.
18 Any valid ceph-fuse options can be passed this way.
19
20 NOTE:
21 Old format is also supported
22
23 DEVICE                             PATH        TYPE        OPTIONS
24 id=admin                           /mnt/ceph   fuse.ceph   defaults   0 0
25 id=myuser,conf=/etc/ceph/foo.conf  /mnt/ceph   fuse.ceph   defaults   0 0
26 '''
27
28 import sys
29 import argparse
30 from subprocess import Popen
31
32 def ceph_options(mntops):
33     ceph_opts = [o for o in mntops if o.startswith('ceph.')]
34     return ceph_opts
35
36 def ceph_options_compat(device):
37     return [ 'ceph.' + opt for opt in device.split(',') ]
38
39 def fs_options(opts, ceph_opts):
40     # strip out noauto and _netdev options; libfuse doesn't like it
41     strip_opts = ['defaults', 'noauto', '_netdev']
42     return ','.join(list(set(opts) - set(ceph_opts) - set(strip_opts)))
43
44 def main(arguments):
45     parser = argparse.ArgumentParser(description=__doc__,
46                                      formatter_class=argparse.RawDescriptionHelpFormatter)
47     parser.add_argument('device', type=str, nargs='+',
48                         help='Device')
49     parser.add_argument('mountpoint', type=str, nargs='+',
50                         help='Mount point')
51     parser.add_argument('-o', dest='options', type=str, nargs='+',
52                         help='Filesystem options')
53     args = parser.parse_known_args(arguments)[0]
54
55     device = args.device[0]
56     mountpoint = args.mountpoint[0]
57     options = ''.join(args.options).split(',')
58
59     if '=' in device:
60         ceph_opts = ceph_options_compat(device)
61     else:
62         ceph_opts = ceph_options(options)
63
64     fs_opts = fs_options(options, ceph_opts)
65     ceph_opts = ' '.join(['--' + o.replace('ceph.', '', 1) for o in ceph_opts])
66
67     command = 'ceph-fuse %s %s' % (ceph_opts, mountpoint)
68
69     if fs_opts:
70         command += ' -o %s' % (fs_opts)
71
72     mount_cmd = Popen(command, shell=True)
73     mount_cmd.communicate()
74
75     if (mount_cmd.returncode != 0):
76         print("Mount failed with status code: {}".format(mount_cmd.returncode))
77
78 if __name__ == '__main__':
79     sys.exit(main(sys.argv[1:]))