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