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
295
296
297
298
|
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.System;
using Microsoft.Extensions.Logging;
using OperatingSystem = MediaBrowser.Common.System.OperatingSystem;
namespace IsoMounter
{
/// <summary>
/// The ISO manager implementation for Linux.
/// </summary>
public class LinuxIsoManager : IIsoMounter
{
private const string MountCommand = "mount";
private const string UnmountCommand = "umount";
private const string SudoCommand = "sudo";
private readonly ILogger _logger;
private readonly string _mountPointRoot;
/// <summary>
/// Initializes a new instance of the <see cref="LinuxIsoManager" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
public LinuxIsoManager(ILogger logger)
{
_logger = logger;
_mountPointRoot = Path.DirectorySeparatorChar + "tmp" + Path.DirectorySeparatorChar + "Emby";
_logger.LogDebug(
"[{0}] System PATH is currently set to [{1}].",
Name,
Environment.GetEnvironmentVariable("PATH") ?? string.Empty);
_logger.LogDebug(
"[{0}] System path separator is [{1}].",
Name,
Path.PathSeparator);
_logger.LogDebug(
"[{0}] Mount point root is [{1}].",
Name,
_mountPointRoot);
}
/// <inheritdoc />
public string Name => "LinuxMount";
#pragma warning disable SA1300
#pragma warning disable SA1400
[DllImport("libc", SetLastError = true)]
static extern uint getuid();
#pragma warning restore SA1300
#pragma warning restore SA1400
/// <inheritdoc />
public bool CanMount(string path)
{
if (OperatingSystem.Id != OperatingSystemId.Linux)
{
return false;
}
_logger.LogInformation(
"[{0}] Checking we can attempt to mount [{1}], Extension = [{2}], Operating System = [{3}].",
Name,
path,
Path.GetExtension(path),
OperatingSystem.Name);
return string.Equals(Path.GetExtension(path), ".iso", StringComparison.OrdinalIgnoreCase);
}
/// <inheritdoc />
public Task<IIsoMount> Mount(string isoPath, CancellationToken cancellationToken)
{
string cmdArguments;
string cmdFilename;
string mountPoint = Path.Combine(_mountPointRoot, Guid.NewGuid().ToString());
if (string.IsNullOrEmpty(isoPath))
{
throw new ArgumentNullException(nameof(isoPath));
}
_logger.LogInformation(
"[{Name}] Attempting to mount [{Path}].",
Name,
isoPath);
_logger.LogDebug(
"[{Name}] ISO will be mounted at [{Path}].",
Name,
mountPoint);
try
{
Directory.CreateDirectory(mountPoint);
}
catch (UnauthorizedAccessException ex)
{
throw new IOException("Unable to create mount point(Permission denied) for " + isoPath, ex);
}
catch (Exception ex)
{
throw new IOException("Unable to create mount point for " + isoPath, ex);
}
if (GetUID() == 0)
{
cmdFilename = MountCommand;
cmdArguments = string.Format(
CultureInfo.InvariantCulture,
"\"{0}\" \"{1}\"",
isoPath,
mountPoint);
}
else
{
cmdFilename = SudoCommand;
cmdArguments = string.Format(
CultureInfo.InvariantCulture,
"\"{0}\" \"{1}\" \"{2}\"",
MountCommand,
isoPath,
mountPoint);
}
_logger.LogDebug(
"[{0}] Mount command [{1}], mount arguments [{2}].",
Name,
cmdFilename,
cmdArguments);
int exitcode = ExecuteCommand(cmdFilename, cmdArguments);
if (exitcode == 0)
{
_logger.LogInformation(
"[{0}] ISO mount completed successfully.",
Name);
return Task.FromResult<IIsoMount>(new LinuxMount(this, isoPath, mountPoint));
}
_logger.LogInformation(
"[{0}] ISO mount completed with errors.",
Name);
try
{
Directory.Delete(mountPoint, false);
}
catch (Exception ex)
{
_logger.LogError(ex, "[{Name}] Unhandled exception removing mount point.", Name);
throw;
}
throw new ExternalException("Mount command failed", exitcode);
}
private uint GetUID()
{
var uid = getuid();
_logger.LogDebug(
"[{0}] GetUserId() returned [{2}].",
Name,
uid);
return uid;
}
private int ExecuteCommand(string cmdFilename, string cmdArguments)
{
var startInfo = new ProcessStartInfo
{
FileName = cmdFilename,
Arguments = cmdArguments,
UseShellExecute = false,
CreateNoWindow = true,
ErrorDialog = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var process = new Process()
{
StartInfo = startInfo
};
try
{
process.Start();
_logger.LogDebug(
"[{Name}] Standard output from process is [{Error}].",
Name,
process.StandardOutput.ReadToEnd());
_logger.LogDebug(
"[{Name}] Standard error from process is [{Error}].",
Name,
process.StandardError.ReadToEnd());
return process.ExitCode;
}
catch (Exception ex)
{
_logger.LogDebug(ex, "[{Name}] Unhandled exception executing command.", Name);
throw;
}
finally
{
process?.Dispose();
}
}
/// <summary>
/// Unmounts the specified mount.
/// </summary>
/// <param name="mount">The mount.</param>
internal void OnUnmount(LinuxMount mount)
{
if (mount == null)
{
throw new ArgumentNullException(nameof(mount));
}
_logger.LogInformation(
"[{0}] Attempting to unmount ISO [{1}] mounted on [{2}].",
Name,
mount.IsoPath,
mount.MountedPath);
string cmdArguments;
string cmdFilename;
if (GetUID() == 0)
{
cmdFilename = UnmountCommand;
cmdArguments = string.Format(
CultureInfo.InvariantCulture,
"\"{0}\"",
mount.MountedPath);
}
else
{
cmdFilename = SudoCommand;
cmdArguments = string.Format(
CultureInfo.InvariantCulture,
"\"{0}\" \"{1}\"",
UnmountCommand,
mount.MountedPath);
}
_logger.LogDebug(
"[{0}] Umount command [{1}], umount arguments [{2}].",
Name,
cmdFilename,
cmdArguments);
int exitcode = ExecuteCommand(cmdFilename, cmdArguments);
if (exitcode == 0)
{
_logger.LogInformation(
"[{0}] ISO unmount completed successfully.",
Name);
}
else
{
_logger.LogInformation(
"[{0}] ISO unmount completed with errors.",
Name);
}
try
{
Directory.Delete(mount.MountedPath, false);
}
catch (Exception ex)
{
_logger.LogError(ex, "[{Name}] Unhandled exception removing mount point.", Name);
throw;
}
throw new ExternalException("Mount command failed", exitcode);
}
}
}
|