summaryrefslogtreecommitdiffstats
path: root/commands/storage/lmi/scripts/storage/mount_cmd.py
blob: fee96d0251f1b6e19d6ac3aea4269611935441ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Storage Management Providers
#
# Copyright (c) 2013, Red Hat, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the FreeBSD Project.
#
# Authors: Jan Synacek <jsynacek@redhat.com>
#
"""
Mount management.

Usage:
    %(cmd)s list [ --all ] [ <device> ... ]
    %(cmd)s create <device> <mountpoint> [ (-t <fs_type>) (-o <options>) (-p <other_options>) ]
    %(cmd)s delete <target>
    %(cmd)s show [ --all ] [ <device> ... ]

Commands:
    list     List mounted filesystems with a device attached to them.
             Optionally, show all mounted filesystems.

    create   Mount a specified device on the path given by mountpoint.
             Optionally, filesystem type, common options (filesystem
             independent) and filesystem specific options can be provided. If no
             filesystem type is specified, it is automatically detected.

             Common options can be provided as a comma-separated string of
             'option_name:value' items.  Possible option names are:

             AllowExecution AllowMandatoryLock AllowSUID AllowUserMount
             AllowWrite Auto Dump FileSystemCheckOrder InterpretDevices
             OtherOptions Silent SynchronousDirectoryUpdates SynchronousIO
             UpdateAccessTimes UpdateDirectoryAccessTimes UpdateFullAccessTimes
             UpdateRelativeAccessTimes

             Possible option values for all of the options except for
             FileSystemCheckOrder are 't', 'true', 'f', 'false'. All of them are
             case insensitive.
             The FileSystemCheckOrder option's value is a number.

             Other options can be specified as a string.

             Examples:

             create /dev/vda1 /mnt -t ext4 -o 'AllowWrite:F,InterpretDevices:false'

             create /dev/vda2 /mnt -o 'FileSystemCheckOrder:2'

             create /dev/vda3 /mnt -p 'user_xattr,barrier=0'

             create /dev/vda4 /mnt -o 'Dump:t, AllowMandatoryLock:t' -p 'acl'

    delete   Unmount a mounted filesystem. Can be specified either as a device
             path or a mountpoint.

    show     Show detailed information about mounted filesystems with a device
             attached to them. Optionally, show all mounted filesystems.
"""

from lmi.scripts.common import command, get_logger
from lmi.scripts.common.errors import LmiFailed
from lmi.scripts.common.formatter import command as fcmd
from lmi.scripts.storage import mount
from lmi.scripts.storage.common import str2device

def get_mounts_for_devices(ns, devices):
    """
    Return list of LMI_MountedFilesystem instances for given devices.
    """
    mounts = []
    for device in devices:
        device = str2device(ns, device)
        filesystems = device.associators(AssocClass="LMI_ResidesOnExtent",
                Role="Antecedent")
        for fs in filesystems:
            mounts += fs.associators(ResultClass="LMI_MountedFileSystem")
    return mounts

class Lister(command.LmiLister):
    COLUMNS = ('FileSystemSpec', 'FileSystemType', 'MountPointPath', 'Options', 'OtherOptions')
    OPT_NO_UNDERSCORES = True

    def transform_options(self, options):
        """
        Rename 'device' option to 'devices' parameter name for better
        readability.
        """
        options['<devices>'] = options.pop('<device>')

    def execute(self, ns, all=None, devices=None):
        """
        Implementation of 'mount list' command.
        """
        if devices:
            mounts = get_mounts_for_devices(ns, devices)
        else:
            mounts = mount.get_mounts(ns)

        if all is False:
            transients = [mnt.Name for mnt in ns.LMI_TransientFileSystem.instances()]

        for mnt in mounts:
            # treat root specially (can be mounted twice - as a rootfs and with
            # a device)
            if mnt.FileSystemSpec == 'rootfs':
                continue

            if all is False and mnt.MountPointPath != '/':
                # do not list nodevice filesystems
                name = 'PATH=' + mnt.MountPointPath
                if name in transients:
                    continue

            opts_str = mount.build_opts_str(mnt)

            yield(mnt.FileSystemSpec,
                  mnt.FileSystemType,
                  mnt.MountPointPath,
                  opts_str[0],
                  opts_str[1])

class Show(command.LmiLister):
    COLUMNS = ('Name', 'Value')
    OPT_NO_UNDERSCORES = True

    def transform_options(self, options):
        """
        Rename 'device' option to 'devices' parameter name for better
        readability.
        """
        options['<devices>'] = options.pop('<device>')


    def execute(self, ns, all=None, devices=None):
        """
        Implementation of 'mount show' command.
        """
        if devices:
            mounts = get_mounts_for_devices(ns, devices)
        else:
            mounts = mount.get_mounts(ns)

        if all is False:
            transients = [mnt.Name for mnt in ns.LMI_TransientFileSystem.instances()]

        yield fcmd.NewTableCommand('Mounted filesystems')
        for mnt in mounts:
            # treat root specially (can be mounted twice - as a rootfs and with
            # a device)
            if mnt.FileSystemSpec == 'rootfs':
                continue

            if all is False and mnt.MountPointPath != '/':
                # do not list nodevice filesystems
                name = 'PATH=' + mnt.MountPointPath
                if name in transients:
                    continue

            opts_str = mount.build_opts_str(mnt)

            yield('Filesystem', '%s (%s)' % (mnt.FileSystemSpec, mnt.FileSystemType))
            yield('Mountpoint', mnt.MountPointPath)
            yield('Options', opts_str[0])
            yield('OtherOptions', opts_str[1])
            yield ''

class Create(command.LmiCheckResult):
    EXPECT = None

    def transform_options(self, options):
        """
        There is only one <device> option, but docopt passes it as array
        (because in other commands it is used with '...'). So let's
        transform it to scalar.
        """
        options['<device>'] = options.pop('<device>')[0]

    def execute(self, ns, device, mountpoint, fs_type=None, options=None, other_options=None):
        """
        Implementation of 'mount create' command.
        """
        return mount.mount_create(ns, device, mountpoint, fs_type, options, other_options)

class Delete(command.LmiCheckResult):
    EXPECT = None

    def transform_options(self, options):
        """
        There is only one <device> option, but docopt passes it as array
        (because in other commands it is used with '...'). So let's
        transform it to scalar.
        """
        options['<device>'] = options.pop('<device>')[0]

    def execute(self, ns, target):
        """
        Implementation of 'mount delete' command.
        """
        return mount.mount_delete(ns, target)

Mount = command.register_subcommands(
        'Mount', __doc__,
        { 'list'    : Lister,
          'create'  : Create,
          'delete'  : Delete,
          'show'    : Show,
        },
    )