components/openstack/cinder/files/cinder-volume-setup
changeset 1760 353323c7bdc1
child 1944 56ac2df1785b
child 3050 b32b8b41ab85
equal deleted inserted replaced
1759:b412ae0aa701 1760:353323c7bdc1
       
     1 #!/usr/bin/python2.6
       
     2 
       
     3 # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     4 #
       
     5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
       
     6 #    not use this file except in compliance with the License. You may obtain
       
     7 #    a copy of the License at
       
     8 #
       
     9 #         http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 #    Unless required by applicable law or agreed to in writing, software
       
    12 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
       
    13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
       
    14 #    License for the specific language governing permissions and limitations
       
    15 #    under the License.
       
    16 
       
    17 import ConfigParser
       
    18 import os
       
    19 import sys
       
    20 
       
    21 import smf_include
       
    22 
       
    23 from subprocess import CalledProcessError, Popen, PIPE, check_call
       
    24 
       
    25 
       
    26 def start():
       
    27     """ retrieves the setting for 'zfs_volume_base' from Cinder's conf file in
       
    28     order to set it up properly for Cinder to use.
       
    29 
       
    30     """
       
    31     parser = ConfigParser.ConfigParser()
       
    32     parser.read("/etc/cinder/cinder.conf")
       
    33 
       
    34     # set up the top-level dataset with the proper permissions for cinder
       
    35     top_ds = parser.get("DEFAULT", "zfs_volume_base")
       
    36 
       
    37     # look to see if the dataset exists
       
    38     cmd = ["/usr/sbin/zfs", "list", top_ds]
       
    39     try:
       
    40         check_call(cmd, stdout=PIPE, stderr=PIPE)
       
    41     except CalledProcessError as err:
       
    42         # the dataset doesn't exist, so go create it
       
    43         try:
       
    44             check_call(["/usr/sbin/zfs", "create", "-p", top_ds])
       
    45         except CalledProcessError as err:
       
    46             print "unable to create %s:  %s" % (top_ds, err)
       
    47             return smf_include.SMF_EXIT_ERR_CONFIG
       
    48 
       
    49     # get the mountpoint
       
    50     cmd = ["/usr/sbin/zfs", "get", "-H", "-o", "value", "mountpoint", top_ds]
       
    51     p = Popen(cmd, stdout=PIPE, stderr=PIPE)
       
    52     mountpoint, error = p.communicate()
       
    53     if p.returncode != 0:
       
    54         print "unable to determine mountpoint of %s:  %s" % (top_ds, error)
       
    55         return smf_include.SMF_EXIT_ERR_CONFIG
       
    56 
       
    57     p = Popen(["/usr/bin/ls", "-dv", mountpoint], stdout=PIPE, stderr=PIPE)
       
    58     output, error = p.communicate()
       
    59 
       
    60     if "user:cinder:add_subdirectory/append_data:allow" not in output:
       
    61         # set an ACL to all mountpoint access
       
    62         try:
       
    63             check_call(["/usr/bin/chmod",
       
    64                         "A+user:cinder:add_subdirectory:allow",
       
    65                         mountpoint.strip()])
       
    66         except CalledProcessError as err:
       
    67             print "ACL creation for mountpoint access of "
       
    68             print "%s to 'cinder' failed:  %s" % (top_ds, err)
       
    69             return smf_include.SMF_EXIT_ERR_CONFIG
       
    70 
       
    71     # set delegation
       
    72     cmd = ["/usr/sbin/zfs", "allow", "cinder",
       
    73            "clone,create,destroy,mount,snapshot", top_ds]
       
    74     try:
       
    75         check_call(cmd)
       
    76     except CalledProcessError as err:
       
    77         print "delegation of %s to 'cinder' failed:  %s" % (top_ds, err)
       
    78         return smf_include.SMF_EXIT_ERR_CONFIG
       
    79 
       
    80     return smf_include.SMF_EXIT_OK
       
    81 
       
    82 if __name__ == "__main__":
       
    83     os.putenv("LC_ALL", "C")
       
    84     smf_include.smf_main()