1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2012 NetApp, Inc.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
18
"""Volume driver for using NFS as volumes storage. Nova compute part."""
23
from nova import exception
24
from nova import flags
25
from nova.openstack.common import cfg
26
from nova.openstack.common import log as logging
27
from nova import utils
28
from nova.virt.libvirt import volume
30
LOG = logging.getLogger(__name__)
33
cfg.StrOpt('nfs_mount_point_base',
34
default='$state_path/mnt',
35
help='Base dir where nfs expected to be mounted on compute'),
38
FLAGS.register_opts(volume_opts)
41
class NfsVolumeDriver(volume.LibvirtVolumeDriver):
42
""" Class implements libvirt part of volume driver for NFS
44
def __init__(self, *args, **kwargs):
45
"""Create back-end to nfs and check connection"""
46
super(NfsVolumeDriver, self).__init__(*args, **kwargs)
48
def connect_volume(self, connection_info, mount_device):
49
"""Connect the volume. Returns xml for libvirt."""
50
path = self._ensure_mounted(connection_info['data']['export'])
51
path = os.path.join(path, connection_info['data']['name'])
52
connection_info['data']['device_path'] = path
53
conf = super(NfsVolumeDriver, self).connect_volume(connection_info,
55
conf.source_type = 'file'
58
def disconnect_volume(self, connection_info, mount_device):
59
"""Disconnect the volume"""
62
def _ensure_mounted(self, nfs_export):
64
@type nfs_export: string
66
mount_path = os.path.join(FLAGS.nfs_mount_point_base,
67
self.get_hash_str(nfs_export))
68
self._mount_nfs(mount_path, nfs_export, ensure=True)
71
def _mount_nfs(self, mount_path, nfs_share, ensure=False):
72
"""Mount nfs export to mount path"""
73
if not self._path_exists(mount_path):
74
utils.execute('mkdir', '-p', mount_path)
77
utils.execute('mount', '-t', 'nfs', nfs_share, mount_path,
79
except exception.ProcessExecutionError as exc:
80
if ensure and 'already mounted' in exc.message:
81
LOG.warn(_("%s is already mounted"), nfs_share)
86
def get_hash_str(base_str):
87
"""returns string that represents hash of base_str (in a hex format)"""
88
return str(ctypes.c_uint64(hash(base_str)).value)
91
def _path_exists(path):
94
return utils.execute('stat', path, run_as_root=True)
95
except exception.ProcessExecutionError: