Example #1
0
        /// <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);
        }
Example #2
0
        private void UnmountISO(LinuxMount mount)
        {
            string cmdArguments;
            string cmdFilename;

            if (mount != null)
            {
                _logger.LogInformation(
                    "[{0}] Attempting to unmount ISO [{1}] mounted on [{2}].",
                    Name,
                    mount.IsoPath,
                    mount.MountedPath
                    );
            }
            else
            {
                throw new ArgumentNullException(nameof(mount));
            }

            if (GetUID() == 0)
            {
                cmdFilename  = UmountCommand;
                cmdArguments = string.Format("\"{0}\"", mount.MountedPath);
            }
            else
            {
                cmdFilename  = SudoCommand;
                cmdArguments = string.Format("\"{0}\" \"{1}\"", UmountCommand, mount.MountedPath);
            }

            _logger.LogDebug(
                "[{0}] Umount command [{1}], umount arguments [{2}].",
                Name,
                cmdFilename,
                cmdArguments
                );

            if (ExecuteCommand(cmdFilename, cmdArguments))
            {
                _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.LogInformation(ex, "[{Name}] Unhandled exception removing mount point.", Name);
            }
        }
Example #3
0
 internal void OnUnmount(LinuxMount mount)
 {
     UnmountISO(mount);
 }
Example #4
0
        private bool MountISO(string isoPath, out LinuxMount mountedISO)
        {
            string cmdArguments;
            string cmdFilename;
            string mountPoint = Path.Combine(MountPointRoot, Guid.NewGuid().ToString());

            if (!string.IsNullOrEmpty(isoPath))
            {
                _logger.LogInformation(
                    "[{Name}] Attempting to mount [{Path}].",
                    Name,
                    isoPath
                    );

                _logger.LogDebug(
                    "[{Name}] ISO will be mounted at [{Path}].",
                    Name,
                    mountPoint
                    );
            }
            else
            {
                throw new ArgumentNullException(nameof(isoPath));
            }

            try
            {
                Directory.CreateDirectory(mountPoint);
            }
            catch (UnauthorizedAccessException)
            {
                throw new IOException("Unable to create mount point(Permission denied) for " + isoPath);
            }
            catch (Exception)
            {
                throw new IOException("Unable to create mount point for " + isoPath);
            }

            if (GetUID() == 0)
            {
                cmdFilename  = MountCommand;
                cmdArguments = string.Format("\"{0}\" \"{1}\"", isoPath, mountPoint);
            }
            else
            {
                cmdFilename  = SudoCommand;
                cmdArguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", MountCommand, isoPath, mountPoint);
            }

            _logger.LogDebug(
                "[{0}] Mount command [{1}], mount arguments [{2}].",
                Name,
                cmdFilename,
                cmdArguments
                );

            if (ExecuteCommand(cmdFilename, cmdArguments))
            {
                _logger.LogInformation(
                    "[{0}] ISO mount completed successfully.",
                    Name
                    );

                mountedISO = new LinuxMount(this, isoPath, mountPoint);
            }
            else
            {
                _logger.LogInformation(
                    "[{0}] ISO mount completed with errors.",
                    Name
                    );

                try
                {
                    Directory.Delete(mountPoint, false);
                }
                catch (Exception ex)
                {
                    _logger.LogInformation(ex, "[{Name}] Unhandled exception removing mount point.", Name);
                }

                mountedISO = null;
            }

            return(mountedISO != null);
        }