2024-02-28 15:45:30 +01:00
// Copyright (C) 2024, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2017-01-09 11:35:38 +01:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text.RegularExpressions ;
2018-11-02 22:13:25 +01:00
using Duplicati.Library.Common ;
2017-01-09 11:35:38 +01:00
using Duplicati.Library.Snapshots ;
namespace Duplicati.Library.Modules.Builtin
{
public class HyperVOptions : Interface . IGenericSourceModule
{
2018-03-12 14:07:11 +01:00
/// <summary>
/// The tag used for logging
/// </summary>
private static readonly string LOGTAG = Logging . Log . LogTagFromType < HyperVOptions >();
2017-01-09 11:35:38 +01:00
private const string m_HyperVPathGuidRegExp = @"\%HYPERV\%\\([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})" ;
private const string m_HyperVPathAllRegExp = @"%HYPERV%" ;
#region IGenericModule Members
public string Key
{
get { return "hyperv-options" ; }
}
public string DisplayName
{
get { return Strings . HyperVOptions . DisplayName ; }
}
public string Description
{
get { return Strings . HyperVOptions . Description ; }
}
public bool LoadAsDefault
{
2018-11-02 22:13:25 +01:00
get { return Platform . IsClientWindows ; }
2017-01-09 11:35:38 +01:00
}
public IList < Interface . ICommandLineArgument > SupportedCommands
{
get { return null ; }
}
public void Configure ( IDictionary < string , string > commandlineOptions )
{
2018-10-10 21:28:00 -07:00
// Do nothing. Implementation needed for IGenericModule interface.
2017-01-09 11:35:38 +01:00
}
#endregion
#region Implementation of IGenericSourceModule
public Dictionary < string , string > ParseSourcePaths ( ref string [] paths , ref string filter , Dictionary < string , string > commandlineOptions )
{
// Early exit in case we are non-windows to prevent attempting to load Windows-only components
2018-11-02 22:13:25 +01:00
if (! Platform . IsClientWindows )
2017-01-09 11:35:38 +01:00
{
2018-03-12 14:07:11 +01:00
Logging . Log . WriteWarningMessage ( LOGTAG , "HyperVWindowsOnly" , null , "Hyper-V backup works only on Windows OS" );
2017-01-09 11:35:38 +01:00
if ( paths != null )
paths = paths . Where ( x => ! x . Equals ( m_HyperVPathAllRegExp , StringComparison . OrdinalIgnoreCase ) && ! Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant )). ToArray ();
if (! string . IsNullOrEmpty ( filter ))
{
var filters = filter . Split ( new string [] { System . IO . Path . PathSeparator . ToString () }, StringSplitOptions . RemoveEmptyEntries );
var remainingfilters = filters . Where ( x => ! Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant )). ToArray ();
filter = string . Join ( System . IO . Path . PathSeparator . ToString (), remainingfilters );
}
return new Dictionary < string , string >();
}
// Windows, do the real stuff!
return RealParseSourcePaths ( ref paths , ref filter , commandlineOptions );
}
// Make sure the JIT does not attempt to inline this call and thus load
// referenced types from System.Management here
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
private Dictionary < string , string > RealParseSourcePaths ( ref string [] paths , ref string filter , Dictionary < string , string > commandlineOptions )
{
var changedOptions = new Dictionary < string , string >();
var filtersInclude = new List < string >();
var filtersExclude = new List < string >();
if (! string . IsNullOrEmpty ( filter ))
{
var filters = filter . Split ( new string [] { System . IO . Path . PathSeparator . ToString () }, StringSplitOptions . RemoveEmptyEntries );
2017-11-26 10:53:14 -08:00
filtersInclude = filters . Where ( x => x . StartsWith ( "+" , StringComparison . Ordinal ) && Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ))
2017-01-09 11:35:38 +01:00
. Select ( x => Regex . Match ( x . Substring ( 1 ), m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ). Groups [ 1 ]. Value ). ToList ();
2017-11-26 10:53:14 -08:00
filtersExclude = filters . Where ( x => x . StartsWith ( "-" , StringComparison . Ordinal ) && Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ))
2017-01-09 11:35:38 +01:00
. Select ( x => Regex . Match ( x . Substring ( 1 ), m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ). Groups [ 1 ]. Value ). ToList ();
var remainingfilters = filters . Where ( x => ! Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant )). ToArray ();
filter = string . Join ( System . IO . Path . PathSeparator . ToString (), remainingfilters );
}
var hypervUtility = new HyperVUtility ();
if ( paths == null || ! ContainFilesForBackup ( paths ) || ! hypervUtility . IsHyperVInstalled )
return changedOptions ;
if ( commandlineOptions . Keys . Contains ( "vss-exclude-writers" ))
{
var excludedWriters = commandlineOptions [ "vss-exclude-writers" ]. Split ( ';' ). Where ( x => ! string . IsNullOrWhiteSpace ( x ) && x . Trim (). Length > 0 ). Select ( x => new Guid ( x )). ToArray ();
if ( excludedWriters . Contains ( HyperVUtility . HyperVWriterGuid ))
{
2018-03-12 14:07:11 +01:00
Logging . Log . WriteWarningMessage ( LOGTAG , "CannotExcludeHyperVVSSWriter" , null , "Excluded writers for VSS cannot contain Hyper-V writer when backuping Hyper-V virtual machines. Removing \"{0}\" to continue" , HyperVUtility . HyperVWriterGuid . ToString ());
2017-01-09 11:35:38 +01:00
changedOptions [ "vss-exclude-writers" ] = string . Join ( ";" , excludedWriters . Where ( x => x != HyperVUtility . HyperVWriterGuid ));
}
}
if (! commandlineOptions . Keys . Contains ( "snapshot-policy" ) || ! commandlineOptions [ "snapshot-policy" ]. Equals ( "required" , StringComparison . OrdinalIgnoreCase ))
{
2018-03-12 14:07:11 +01:00
Logging . Log . WriteWarningMessage ( LOGTAG , "MustSetSnapshotPolicy" , null , "Snapshot policy have to be set to \"required\" when backuping Hyper-V virtual machines. Changing to \"required\" to continue" , Logging . LogMessageType . Warning );
2017-01-09 11:35:38 +01:00
changedOptions [ "snapshot-policy" ] = "required" ;
}
if (! hypervUtility . IsVSSWriterSupported )
2018-03-12 14:07:11 +01:00
Logging . Log . WriteWarningMessage ( LOGTAG , "HyperVOnServerOnly" , null , "This is client version of Windows. Hyper-V VSS writer is present only on Server version. Backup will continue, but will be crash consistent only in opposite to application consistent in Server version" );
2017-01-09 11:35:38 +01:00
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "StartingHyperVQuery" , "Starting to gather Hyper-V information" );
2017-01-09 11:35:38 +01:00
hypervUtility . QueryHyperVGuestsInfo ( true );
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "HyperVMachineCount" , "Found {0} virtual machines on Hyper-V" , hypervUtility . Guests . Count );
2017-01-09 11:35:38 +01:00
foreach ( var guest in hypervUtility . Guests )
2018-03-12 14:07:11 +01:00
Logging . Log . WriteProfilingMessage ( LOGTAG , "FoundHyperVMachine" , "Found VM name {0}, ID {1}, files {2}" , guest . Name , guest . ID , string . Join ( ";" , guest . DataPaths ));
2017-01-09 11:35:38 +01:00
List < HyperVGuest > guestsForBackup = new List < HyperVGuest >();
if ( paths . Contains ( m_HyperVPathAllRegExp , StringComparer . OrdinalIgnoreCase ))
guestsForBackup = hypervUtility . Guests ;
else
foreach ( var guestID in paths . Where ( x => Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ))
. Select ( x => Regex . Match ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ). Groups [ 1 ]. Value ). ToArray ())
{
var foundGuest = hypervUtility . Guests . Where ( x => x . ID == new Guid ( guestID ));
if ( foundGuest . Count () != 1 )
2018-03-12 14:07:11 +01:00
throw new Duplicati . Library . Interface . UserInformationException ( string . Format ( "Hyper-V guest specified in source with ID {0} cannot be found" , guestID ), "HyperVGuestNotFound" );
2017-01-09 11:35:38 +01:00
guestsForBackup . Add ( foundGuest . First ());
}
if ( filtersInclude . Count > 0 )
foreach ( var guestID in filtersInclude )
{
var foundGuest = hypervUtility . Guests . Where ( x => x . ID == new Guid ( guestID ));
if ( foundGuest . Count () != 1 )
2018-03-12 14:07:11 +01:00
throw new Duplicati . Library . Interface . UserInformationException ( string . Format ( "Hyper-V guest specified in include filter with ID {0} cannot be found" , guestID ), "HyperVGuestNotFound" );
2017-01-09 11:35:38 +01:00
guestsForBackup . Add ( foundGuest . First ());
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "IncludeByFilter" , "Including {0} based on including filters" , guestID );
2017-01-09 11:35:38 +01:00
}
guestsForBackup = guestsForBackup . Distinct (). ToList ();
if ( filtersExclude . Count > 0 )
foreach ( var guestID in filtersExclude )
{
var foundGuest = guestsForBackup . Where ( x => x . ID == new Guid ( guestID ));
if ( foundGuest . Count () != 1 )
2018-03-12 14:07:11 +01:00
throw new Duplicati . Library . Interface . UserInformationException ( string . Format ( "Hyper-V guest specified in exclude filter with ID {0} cannot be found" , guestID ), "HyperVGuestNotFound" );
2017-01-09 11:35:38 +01:00
guestsForBackup . Remove ( foundGuest . First ());
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "ExcludeByFilter" , "Excluding {0} based on excluding filters" , guestID );
2017-01-09 11:35:38 +01:00
}
var pathsForBackup = new List < string >( paths );
var filterhandler = new Utility . FilterExpression (
2017-11-26 10:53:14 -08:00
filter . Split ( new string [] { System . IO . Path . PathSeparator . ToString () }, StringSplitOptions . RemoveEmptyEntries ). Where ( x => x . StartsWith ( "-" , StringComparison . Ordinal )). Select ( x => x . Substring ( 1 )). ToList ());
2017-01-09 11:35:38 +01:00
foreach ( var guestForBackup in guestsForBackup )
foreach ( var pathForBackup in guestForBackup . DataPaths )
{
2019-04-16 21:35:02 -07:00
if (! filterhandler . Matches ( pathForBackup , out _ , out _ ))
2017-01-09 11:35:38 +01:00
{
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "IncludeHyperV" , "For VM {0} - adding {1}" , guestForBackup . Name , pathForBackup );
2017-01-09 11:35:38 +01:00
pathsForBackup . Add ( pathForBackup );
}
else
2018-03-12 14:07:11 +01:00
Logging . Log . WriteInformationMessage ( LOGTAG , "ExcludeByFilter" , "Excluding {0} based on excluding filters" , pathForBackup );
2017-01-09 11:35:38 +01:00
}
paths = pathsForBackup . Where ( x => ! x . Equals ( m_HyperVPathAllRegExp , StringComparison . OrdinalIgnoreCase ) && ! Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ))
. Distinct ( Utility . Utility . ClientFilenameStringComparer ). OrderBy ( a => a ). ToArray ();
return changedOptions ;
}
public bool ContainFilesForBackup ( string [] paths )
{
2018-11-02 22:13:25 +01:00
if ( paths == null || ! Platform . IsClientWindows )
2017-01-09 11:35:38 +01:00
return false ;
2018-10-06 15:50:05 -07:00
return paths . Where ( x => ! string . IsNullOrWhiteSpace ( x )). Any ( x => x . Equals ( m_HyperVPathAllRegExp , StringComparison . OrdinalIgnoreCase ) || Regex . IsMatch ( x , m_HyperVPathGuidRegExp , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ));
2017-01-09 11:35:38 +01:00
}
#endregion
}
}