cinder-volume-setup revision 2949
2605N/A#!/usr/bin/python2.6
2605N/A
2605N/A# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2605N/A#
2605N/A# Licensed under the Apache License, Version 2.0 (the "License"); you may
2605N/A# not use this file except in compliance with the License. You may obtain
2605N/A# a copy of the License at
2605N/A#
2605N/A# http://www.apache.org/licenses/LICENSE-2.0
2605N/A#
2605N/A# Unless required by applicable law or agreed to in writing, software
2605N/A# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
2605N/A# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
2605N/A# License for the specific language governing permissions and limitations
2605N/A# under the License.
2605N/A
2605N/Aimport ConfigParser
2605N/Aimport os
2605N/Aimport sys
2605N/A
2605N/Aimport smf_include
2605N/A
2605N/Afrom subprocess import CalledProcessError, Popen, PIPE, check_call
2605N/A
2605N/A
2605N/Adef start():
2605N/A """ retrieves the setting for 'zfs_volume_base' from Cinder's conf file in
2605N/A order to set it up properly for Cinder to use.
2605N/A
2605N/A """
2900N/A cinder_conf = "/etc/cinder/cinder.conf"
2900N/A if not os.path.exists(cinder_conf):
2900N/A print "%s doesn't exist" % cinder_conf
2900N/A return smf_include.SMF_EXIT_ERR_CONFIG
2605N/A
2900N/A parser = ConfigParser.ConfigParser()
2900N/A parser.read(cinder_conf)
2900N/A
2900N/A # retrieve the top-level dataset or just get the default (rpool/cinder)
2900N/A try:
2900N/A top_ds = parser.get("DEFAULT", "zfs_volume_base")
2900N/A except ConfigParser.NoOptionError:
2900N/A top_ds = "rpool/cinder"
2605N/A
2605N/A # look to see if the dataset exists
2605N/A cmd = ["/usr/sbin/zfs", "list", top_ds]
2605N/A try:
2605N/A check_call(cmd, stdout=PIPE, stderr=PIPE)
2605N/A except CalledProcessError as err:
2605N/A # the dataset doesn't exist, so go create it
2605N/A try:
2605N/A check_call(["/usr/sbin/zfs", "create", "-p", top_ds])
2605N/A except CalledProcessError as err:
2605N/A print "unable to create %s: %s" % (top_ds, err)
2605N/A return smf_include.SMF_EXIT_ERR_CONFIG
2605N/A
2605N/A # get the mountpoint
2605N/A cmd = ["/usr/sbin/zfs", "get", "-H", "-o", "value", "mountpoint", top_ds]
2605N/A p = Popen(cmd, stdout=PIPE, stderr=PIPE)
2605N/A mountpoint, error = p.communicate()
2605N/A if p.returncode != 0:
2605N/A print "unable to determine mountpoint of %s: %s" % (top_ds, error)
2605N/A return smf_include.SMF_EXIT_ERR_CONFIG
2605N/A
2605N/A p = Popen(["/usr/bin/ls", "-dv", mountpoint], stdout=PIPE, stderr=PIPE)
2605N/A output, error = p.communicate()
2605N/A
2605N/A if "user:cinder:add_subdirectory/append_data:allow" not in output:
2605N/A # set an ACL to all mountpoint access
2605N/A try:
2605N/A check_call(["/usr/bin/chmod",
2605N/A "A+user:cinder:add_subdirectory:allow",
2605N/A mountpoint.strip()])
2605N/A except CalledProcessError as err:
2605N/A print "ACL creation for mountpoint access of "
2605N/A print "%s to 'cinder' failed: %s" % (top_ds, err)
2605N/A return smf_include.SMF_EXIT_ERR_CONFIG
2605N/A
2605N/A # set delegation
2605N/A cmd = ["/usr/sbin/zfs", "allow", "cinder",
2605N/A "clone,create,destroy,mount,snapshot", top_ds]
2605N/A try:
2605N/A check_call(cmd)
2605N/A except CalledProcessError as err:
2605N/A print "delegation of %s to 'cinder' failed: %s" % (top_ds, err)
2605N/A return smf_include.SMF_EXIT_ERR_CONFIG
2605N/A
2949N/A return smf_include.SMF_EXIT_OK
2605N/A
2605N/Aif __name__ == "__main__":
2605N/A os.putenv("LC_ALL", "C")
2605N/A smf_include.smf_main()