~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/virt/libvirt/volume_nfs.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2012 NetApp, Inc.
 
4
# All Rights Reserved.
 
5
#
 
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
 
9
#
 
10
#         http://www.apache.org/licenses/LICENSE-2.0
 
11
#
 
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
 
16
#    under the License.
 
17
 
 
18
"""Volume driver for using NFS as volumes storage. Nova compute part."""
 
19
 
 
20
import ctypes
 
21
import os
 
22
 
 
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
 
29
 
 
30
LOG = logging.getLogger(__name__)
 
31
 
 
32
volume_opts = [
 
33
    cfg.StrOpt('nfs_mount_point_base',
 
34
               default='$state_path/mnt',
 
35
               help='Base dir where nfs expected to be mounted on compute'),
 
36
]
 
37
FLAGS = flags.FLAGS
 
38
FLAGS.register_opts(volume_opts)
 
39
 
 
40
 
 
41
class NfsVolumeDriver(volume.LibvirtVolumeDriver):
 
42
    """ Class implements libvirt part of volume driver for NFS
 
43
    """
 
44
    def __init__(self, *args, **kwargs):
 
45
        """Create back-end to nfs and check connection"""
 
46
        super(NfsVolumeDriver, self).__init__(*args, **kwargs)
 
47
 
 
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,
 
54
                                                           mount_device)
 
55
        conf.source_type = 'file'
 
56
        return conf
 
57
 
 
58
    def disconnect_volume(self, connection_info, mount_device):
 
59
        """Disconnect the volume"""
 
60
        pass
 
61
 
 
62
    def _ensure_mounted(self, nfs_export):
 
63
        """
 
64
        @type nfs_export: string
 
65
        """
 
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)
 
69
        return mount_path
 
70
 
 
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)
 
75
 
 
76
        try:
 
77
            utils.execute('mount', '-t', 'nfs', nfs_share, mount_path,
 
78
                          run_as_root=True)
 
79
        except exception.ProcessExecutionError as exc:
 
80
            if ensure and 'already mounted' in exc.message:
 
81
                LOG.warn(_("%s is already mounted"), nfs_share)
 
82
            else:
 
83
                raise
 
84
 
 
85
    @staticmethod
 
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)
 
89
 
 
90
    @staticmethod
 
91
    def _path_exists(path):
 
92
        """Check path """
 
93
        try:
 
94
            return utils.execute('stat', path, run_as_root=True)
 
95
        except exception.ProcessExecutionError:
 
96
            return False