-
Notifications
You must be signed in to change notification settings - Fork 785
Expand file tree
/
Copy pathmount.cpp
More file actions
294 lines (244 loc) · 9.2 KB
/
mount.cpp
File metadata and controls
294 lines (244 loc) · 9.2 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* Copyright (C) Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "mount.h"
#include "animated_spinner.h"
#include "common_callbacks.h"
#include "common_cli.h"
#include <multipass/cli/argparser.h>
#include <multipass/cli/client_platform.h>
#include <multipass/exceptions/cmd_exceptions.h>
#include <multipass/format.h>
#include <multipass/logging/log.h>
#include <multipass/logging/log_location.h>
#include <QDir>
#include <QFileInfo>
#include <QRegularExpression>
namespace mp = multipass;
namespace mcp = multipass::cli::platform;
namespace mpl = multipass::logging;
namespace cmd = multipass::cmd;
namespace
{
constexpr auto category = "mount cmd";
const QString default_mount_type{"classic"};
const QString native_mount_type{"native"};
auto convert_id_for(const QString& id_string)
{
bool ok;
auto id = id_string.toUInt(&ok);
if (!ok)
throw std::runtime_error(id_string.toStdString() + " is an invalid id");
return id;
}
auto checked_mount_type(const QString& type)
{
if (type == default_mount_type)
return mp::MountRequest_MountType_CLASSIC;
if (type == native_mount_type)
return mp::MountRequest_MountType_NATIVE;
throw mp::ValidationException{
fmt::format("Bad mount type '{}' specified, please use '{}' or '{}'",
type,
default_mount_type,
native_mount_type)};
}
} // namespace
mp::ReturnCodeVariant cmd::Mount::run(mp::ArgParser* parser)
{
auto ret = parse_args(parser);
if (ret != ParseCode::Ok)
{
return parser->returnCodeFrom(ret);
}
mp::AnimatedSpinner spinner{cout, term->cout_is_live()};
auto on_success = [&spinner](mp::MountReply& reply) -> ReturnCodeVariant {
spinner.stop();
return ReturnCode::Ok;
};
auto on_failure = [this, &spinner](grpc::Status& status) -> ReturnCodeVariant {
spinner.stop();
return standard_failure_handler_for(name(), cerr, status);
};
auto streaming_callback =
make_iterative_spinner_callback<MountRequest, MountReply>(spinner, *term);
request.set_verbosity_level(parser->verbosityLevel());
return dispatch(&RpcMethod::mount, request, on_success, on_failure, streaming_callback);
}
std::string cmd::Mount::name() const
{
return "mount";
}
QString cmd::Mount::short_help() const
{
return QStringLiteral("Mount a local directory in the instance");
}
QString cmd::Mount::description() const
{
return QStringLiteral("Mount a local directory inside the instance. If the instance is\n"
"not currently running, the directory will be mounted\n"
"automatically on next boot.");
}
mp::ParseCode cmd::Mount::parse_args(mp::ArgParser* parser)
{
parser->addPositionalArgument("source", "Path of the local directory to mount", "<source>");
parser->addPositionalArgument(
"target",
QStringLiteral("Target mount points, in <name>[:<path>] format, where <name> is an "
"instance name, and optional <path> is the mount point. If omitted, the "
"mount point will be under %1/<source-dir>, where <source-dir> is the name "
"of the <source> directory.")
.arg(home_in_instance),
"<target> [<target> ...]");
QCommandLineOption gid_mappings(
{"g", "gid-map"},
"A mapping of group IDs for use in the mount. File and folder ownership will be "
"mapped from <host> to <instance> inside the instance. Can be used multiple times. "
"Mappings can only be specified as a one-to-one relationship.",
"host>:<instance");
QCommandLineOption uid_mappings(
{"u", "uid-map"},
"A mapping of user IDs for use in the mount. File and folder ownership will be "
"mapped from <host> to <instance> inside the instance. Can be used multiple times. "
"Mappings can only be specified as a one-to-one relationship.",
"host>:<instance");
QCommandLineOption mount_type_option(
{"t", "type"},
"Specify the type of mount to use.\n"
"Classic mounts use technology built into Multipass.\n"
"Native mounts use hypervisor and/or platform specific mounts.\n"
"Valid types are: \'classic\' (default) and \'native\'",
"type",
default_mount_type);
parser->addOptions({gid_mappings, uid_mappings, mount_type_option});
auto status = parser->commandParse(this);
if (status != ParseCode::Ok)
return status;
if (parser->positionalArguments().count() < 2)
{
cerr << "Not enough arguments given\n";
return ParseCode::CommandLineError;
}
QString source_path(parser->positionalArguments().at(0));
// Validate source directory of client side mounts
QFileInfo source_dir(source_path);
if (!source_dir.exists())
{
cerr << "Source path \"" << source_path.toStdString() << "\" does not exist\n";
return ParseCode::CommandLineError;
}
if (!source_dir.isDir())
{
cerr << "Source path \"" << source_path.toStdString() << "\" is not a directory\n";
return ParseCode::CommandLineError;
}
if (!source_dir.isReadable())
{
cerr << "Source path \"" << source_path.toStdString() << "\" is not readable\n";
return ParseCode::CommandLineError;
}
source_path = QDir(source_path).absolutePath();
request.set_source_path(source_path.toStdString());
request.clear_target_paths();
for (auto i = 1; i < parser->positionalArguments().count(); ++i)
{
auto argument = parser->positionalArguments().at(i);
auto instance_name = argument.section(':', 0, 0, QString::SectionSkipEmpty);
auto target_path = argument.section(':', 1, -1, QString::SectionSkipEmpty);
auto entry = request.add_target_paths();
entry->set_instance_name(instance_name.toStdString());
entry->set_target_path(target_path.toStdString());
}
QRegularExpression map_matcher("^([0-9]+[:][0-9]+)$");
request.clear_mount_maps();
auto mount_maps = request.mutable_mount_maps();
if (parser->isSet(uid_mappings))
{
auto uid_maps = parser->values(uid_mappings);
for (const auto& map : uid_maps)
{
if (!map_matcher.match(map).hasMatch())
{
cerr << "Invalid UID map given: " << map.toStdString() << "\n";
return ParseCode::CommandLineError;
}
auto parsed_map = map.split(":");
try
{
auto host_uid = convert_id_for(parsed_map.at(0));
auto instance_uid = convert_id_for(parsed_map.at(1));
auto uid_pair = mount_maps->add_uid_mappings();
uid_pair->set_host_id(host_uid);
uid_pair->set_instance_id(instance_uid);
}
catch (const std::exception& e)
{
cerr << e.what() << "\n";
return ParseCode::CommandLineError;
}
}
}
else
{
mpl::debug_location(category, "adding default uid mapping");
auto uid_pair = mount_maps->add_uid_mappings();
uid_pair->set_host_id(mcp::getuid());
uid_pair->set_instance_id(mp::default_id);
}
if (parser->isSet(gid_mappings))
{
auto gid_maps = parser->values(gid_mappings);
for (const auto& map : gid_maps)
{
if (!map_matcher.match(map).hasMatch())
{
cerr << "Invalid GID map given: " << map.toStdString() << "\n";
return ParseCode::CommandLineError;
}
auto parsed_map = map.split(":");
try
{
auto host_gid = convert_id_for(parsed_map.at(0));
auto instance_gid = convert_id_for(parsed_map.at(1));
auto gid_pair = mount_maps->add_gid_mappings();
gid_pair->set_host_id(host_gid);
gid_pair->set_instance_id(instance_gid);
}
catch (const std::exception& e)
{
cerr << e.what() << "\n";
return ParseCode::CommandLineError;
}
}
}
else
{
mpl::debug_location(category, "adding default gid mapping");
auto gid_pair = mount_maps->add_gid_mappings();
gid_pair->set_host_id(mcp::getgid());
gid_pair->set_instance_id(mp::default_id);
}
try
{
request.set_mount_type(checked_mount_type(parser->value(mount_type_option).toLower()));
}
catch (mp::ValidationException& e)
{
cerr << "error: " << e.what() << "\n";
return ParseCode::CommandLineError;
}
return ParseCode::Ok;
}