///<summary>Initializes a new instance of the <see cref="VirtualHardDisk"/> class of the specified format, type, size, and path.</summary> ///<param name="format">The file format of the virtual hard disk file.</param> ///<param name="type">The type of virtual hard disk.</param> ///<param name="size">The size in GB of the virtual hard disk.</param> ///<param name="path">The path for the virtual hard disk file.</param> public VirtualHardDisk(VirtualHardDiskFormat format, VirtualHardDiskType type, UInt64 size, string path) { Format = format; Path = path; Size = size; Type = type; }
public VHDFooter(byte[] buffer) { Cookie = ByteReader.ReadAnsiString(buffer, 0x00, 8); Features = BigEndianConverter.ToUInt32(buffer, 0x08); FileFormatVersion = BigEndianConverter.ToUInt32(buffer, 0x0C); DataOffset = BigEndianConverter.ToUInt64(buffer, 0x10); TimeStamp = BigEndianConverter.ToUInt32(buffer, 0x18); CreatorApplication = ByteReader.ReadAnsiString(buffer, 0x1C, 4); CreatorVersion = BigEndianConverter.ToUInt32(buffer, 0x20); CreatorHostOS = BigEndianConverter.ToUInt32(buffer, 0x24); OriginalSize = BigEndianConverter.ToUInt64(buffer, 0x28); CurrentSize = BigEndianConverter.ToUInt64(buffer, 0x30); DiskGeometry = BigEndianConverter.ToUInt32(buffer, 0x38); DiskType = (VirtualHardDiskType)BigEndianConverter.ToUInt32(buffer, 0x3C); uint checksum = BigEndianConverter.ToUInt32(buffer, 0x40); UniqueId = BigEndianConverter.ToGuid(buffer, 0x44); SavedState = ByteReader.ReadByte(buffer, 0x54); byte[] temp = (byte[])buffer.Clone(); BigEndianWriter.WriteInt32(temp, 0x40, 0); uint expectedChecksum = CalculateChecksum(temp); m_isValid = String.Equals(Cookie, VirtualHardDiskCookie) && (checksum == expectedChecksum) && (FileFormatVersion == 0x00010000); }
public VHDFooter() { Cookie = VirtualHardDiskCookie; FileFormatVersion = 0x00010000; DataOffset = 0xFFFFFFFFFFFFFFFF; CreatorApplication = "DXSL"; // Disk Access Library CreatorHostOS = 0x5769326B; DiskType = VirtualHardDiskType.Fixed; UniqueId = Guid.NewGuid(); }
public JobResult CreateVirtualHardDisk(string destinationPath, VirtualHardDiskType diskType, uint blockSizeBytes, UInt64 sizeGB) { try { Log.WriteStart("'{0}' CreateVirtualHardDisk", ProviderSettings.ProviderName); JobResult result = VirtualizationProvider.CreateVirtualHardDisk(destinationPath, diskType, blockSizeBytes, sizeGB); Log.WriteEnd("'{0}' CreateVirtualHardDisk", ProviderSettings.ProviderName); return(result); } catch (Exception ex) { Log.WriteError(String.Format("'{0}' CreateVirtualHardDisk", ProviderSettings.ProviderName), ex); throw; } }
CreateVirtualHardDisk( string ServerName, string VirtualHardDiskPath, string ParentPath, VirtualHardDiskType Type, VirtualHardDiskFormat Format, Int64 FileSize, Int32 BlockSize, Int32 LogicalSectorSize, Int32 PhysicalSectorSize) { ManagementScope scope = new ManagementScope("\\\\" + ServerName + "\\root\\virtualization\\v2"); VirtualHardDiskSettingData settingData = new VirtualHardDiskSettingData( Type, Format, VirtualHardDiskPath, ParentPath, FileSize, BlockSize, LogicalSectorSize, PhysicalSectorSize); using (ManagementObject imageManagementService = StorageUtilities.GetImageManagementService(scope)) { using (ManagementBaseObject inParams = imageManagementService.GetMethodParameters("CreateVirtualHardDisk")) { inParams["VirtualDiskSettingData"] = settingData.GetVirtualHardDiskSettingDataEmbeddedInstance( ServerName, imageManagementService.Path.Path); using (ManagementBaseObject outParams = imageManagementService.InvokeMethod( "CreateVirtualHardDisk", inParams, null)) { WmiUtilities.ValidateOutput(outParams, scope); } } } }
VirtualHardDiskSettingData( VirtualHardDiskType diskType, VirtualHardDiskFormat diskFormat, string path, string parentPath, Int64 maxInternalSize, Int32 blockSize, Int32 logicalSectorSize, Int32 physicalSectorSize) { m_DiskType = diskType; m_DiskFormat = diskFormat; m_Path = path; m_ParentPath = parentPath; m_MaxInternalSize = maxInternalSize; m_BlockSize = blockSize; m_LogicalSectorSize = logicalSectorSize; m_PhysicalSectorSize = physicalSectorSize; }
public VirtualHardDiskInfo(long fileSize, bool inSavedState, bool inUse, long maxInternalSize, string parentPath, VirtualHardDiskType diskType, bool supportPersistentReservations, ulong maximumIOPS, ulong minimumIOPS, ControllerType vhdControllerType, int controllerNumber, int controllerLocation, string name, string path, VirtualHardDiskFormat diskFormat, bool attached, uint blockSizeBytes) { FileSize = fileSize; InSavedState = inSavedState; InUse = inUse; MaxInternalSize = maxInternalSize; ParentPath = parentPath; DiskType = diskType; SupportPersistentReservations = supportPersistentReservations; MaximumIOPS = maximumIOPS; MinimumIOPS = minimumIOPS; VHDControllerType = vhdControllerType; ControllerNumber = controllerNumber; ControllerLocation = controllerLocation; Name = name; Path = path; DiskFormat = diskFormat; Attached = attached; BlockSizeBytes = blockSizeBytes; }
public JobResult ConvertVirtualHardDisk(string sourcePath, string destinationPath, VirtualHardDiskType diskType) { // check source file if (!FileExists(sourcePath)) throw new Exception("Source VHD cannot be found: " + sourcePath); // check destination folder string destFolder = Path.GetDirectoryName(destinationPath); if (!DirectoryExists(destFolder)) CreateFolder(destFolder); sourcePath = FileUtils.EvaluateSystemVariables(sourcePath); destinationPath = FileUtils.EvaluateSystemVariables(destinationPath); try { Command cmd = new Command("Convert-VHD"); cmd.Parameters.Add("Path", sourcePath); cmd.Parameters.Add("DestinationPath", destinationPath); cmd.Parameters.Add("VHDType", diskType.ToString()); PowerShell.Execute(cmd, true, true); return JobHelper.CreateSuccessResult(ReturnCode.JobStarted); } catch (Exception ex) { HostedSolutionLog.LogError("ConvertVirtualHardDisk", ex); throw; } }
Parse( string embeddedInstance) { VirtualHardDiskType type = VirtualHardDiskType.Unknown; VirtualHardDiskFormat format = VirtualHardDiskFormat.Unknown; string path = string.Empty; string parentPath = string.Empty; Int64 maxInternalSize = 0; Int32 blockSize = 0; Int32 logicalSectorSize = 0; Int32 physicalSectorSize = 0; XmlDocument doc = new XmlDocument(); doc.LoadXml(embeddedInstance); XmlNodeList nodelist = doc.SelectNodes(@"/INSTANCE/@CLASSNAME"); if (nodelist.Count != 1) { throw new FormatException(); } if (nodelist[0].Value != "Msvm_VirtualHardDiskSettingData") { throw new FormatException(); } // Disk type nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'Type']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } int itype = int.Parse(nodelist[0].Value, NumberStyles.None, CultureInfo.InvariantCulture); type = (VirtualHardDiskType)itype; if (type != VirtualHardDiskType.Differencing && type != VirtualHardDiskType.DynamicallyExpanding && type != VirtualHardDiskType.FixedSize) { // The type integer returned is of an unrecognized type. throw new FormatException(); } // Disk format nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'Format']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } int iformat = int.Parse(nodelist[0].Value, NumberStyles.None, CultureInfo.InvariantCulture); format = (VirtualHardDiskFormat)iformat; if (format != VirtualHardDiskFormat.Vhd && format != VirtualHardDiskFormat.Vhdx) { //The format integer returned is of an unrecognized type. throw new FormatException(); } // Path nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'Path']/VALUE/child::text()"); if (nodelist.Count != 1) { // There can not be multiple parents. throw new FormatException(); } path = nodelist[0].Value; // ParentPath nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'ParentPath']/VALUE/child::text()"); // A nodeList.Count == 0 is okay and indicates that there is no parent. if (nodelist.Count == 1) { parentPath = nodelist[0].Value; } else if (nodelist.Count != 0) { // There can not be multiple parents. throw new FormatException(); } if (type == VirtualHardDiskType.Differencing && string.IsNullOrEmpty(parentPath)) { // Parent path must be set if this is a differencing disk. throw new FormatException(); } // MaxInternalSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'MaxInternalSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } maxInternalSize = Int64.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); // BlockSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'BlockSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } blockSize = Int32.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); // LogicalSectorSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'LogicalSectorSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } logicalSectorSize = Int32.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); // PhysicalSectorSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'PhysicalSectorSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } physicalSectorSize = Int32.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); return(new VirtualHardDiskSettingData(type, format, path, parentPath, maxInternalSize, blockSize, logicalSectorSize, physicalSectorSize)); }
public JobResult ConvertVirtualHardDisk(string sourcePath, string destinationPath, VirtualHardDiskType diskType) { sourcePath = FileUtils.EvaluateSystemVariables(sourcePath); destinationPath = FileUtils.EvaluateSystemVariables(destinationPath); // check source file if (!FileExists(sourcePath)) throw new Exception("Source VHD cannot be found: " + sourcePath); // check destination folder string destFolder = Path.GetDirectoryName(destinationPath); if (!DirectoryExists(destFolder)) CreateFolder(destFolder); ManagementObject objImgSvc = GetImageManagementService(); // get method params ManagementBaseObject inParams = objImgSvc.GetMethodParameters("ConvertVirtualHardDisk"); inParams["SourcePath"] = sourcePath; inParams["DestinationPath"] = destinationPath; inParams["Type"] = (UInt16)diskType; ManagementBaseObject outParams = (ManagementBaseObject)objImgSvc.InvokeMethod("ConvertVirtualHardDisk", inParams, null); return CreateJobResultFromWmiMethodResults(outParams); }
/// <remarks/> public void ConvertVirtualHardDiskAsync(string sourcePath, string destinationPath, VirtualHardDiskType diskType, object userState) { if ((this.ConvertVirtualHardDiskOperationCompleted == null)) { this.ConvertVirtualHardDiskOperationCompleted = new System.Threading.SendOrPostCallback(this.OnConvertVirtualHardDiskOperationCompleted); } this.InvokeAsync("ConvertVirtualHardDisk", new object[] { sourcePath, destinationPath, diskType}, this.ConvertVirtualHardDiskOperationCompleted, userState); }
/// <remarks/> public void ConvertVirtualHardDiskAsync(string sourcePath, string destinationPath, VirtualHardDiskType diskType) { this.ConvertVirtualHardDiskAsync(sourcePath, destinationPath, diskType, null); }
public JobResult ConvertVirtualHardDisk(string sourcePath, string destinationPath, VirtualHardDiskType diskType) { try { Log.WriteStart("'{0}' ConvertVirtualHardDisk", ProviderSettings.ProviderName); JobResult result = VirtualizationProvider.ConvertVirtualHardDisk(sourcePath, destinationPath, diskType); Log.WriteEnd("'{0}' ConvertVirtualHardDisk", ProviderSettings.ProviderName); return(result); } catch (Exception ex) { Log.WriteError(String.Format("'{0}' ConvertVirtualHardDisk", ProviderSettings.ProviderName), ex); throw; } }
public static Collection <PSObject> CreateVirtualHardDisk(PowerShellManager powerShellwithJobs, string destinationPath, VirtualHardDiskType diskType, uint blockSizeBytes, UInt64 sizeGB, string serverNameSettings) { string destFolder = Path.GetDirectoryName(destinationPath); if (!DirectoryExists(destFolder, serverNameSettings)) { CreateFolder(destFolder, serverNameSettings); } destinationPath = FileUtils.EvaluateSystemVariables(destinationPath); Command cmd = new Command("New-VHD"); cmd.Parameters.Add("SizeBytes", sizeGB * Constants.Size1G); cmd.Parameters.Add("Path", destinationPath); cmd.Parameters.Add(diskType.ToString()); if (blockSizeBytes > 0) { cmd.Parameters.Add("BlockSizeBytes", blockSizeBytes); } return(powerShellwithJobs.TryExecuteAsJob(cmd, true)); }
Main( string[] args) { if (args.Length == 3) { if (string.Equals(args[0], "GetVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; StorageGetSample.GetVirtualHardDisk( serverName, virtualHardDiskPath); } else if (string.Equals(args[0], "ValidateVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; StorageValidateSample.ValidateVirtualHardDisk( serverName, virtualHardDiskPath); } else if (string.Equals(args[0], "DetachVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; StorageDetachSample.DetachVirtualHardDisk( serverName, virtualHardDiskPath); } else if (string.Equals(args[0], "CreateVirtualFloppyDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; StorageFloppySample.CreateVirtualFloppyDisk( serverName, virtualHardDiskPath); } else { ShowUsage(); } } else if (args.Length == 4) { if (string.Equals(args[0], "CompactVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; string mode = args[3]; if (string.Equals(mode, "2", StringComparison.OrdinalIgnoreCase)) { StorageAttachSample.AttachVirtualHardDisk( serverName, virtualHardDiskPath, "false", "true"); } StorageCompactSample.CompactVirtualHardDisk( serverName, virtualHardDiskPath, mode); if (string.Equals(mode, "2", StringComparison.OrdinalIgnoreCase)) { StorageDetachSample.DetachVirtualHardDisk( serverName, virtualHardDiskPath); } } else if (string.Equals(args[0], "MergeVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string sourcePath = args[2]; string destinationPath = args[3]; StorageMergeSample.MergeVirtualHardDisk( serverName, sourcePath, destinationPath); } else if (string.Equals(args[0], "ResizeVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; UInt64 fileSize = UInt64.Parse(args[3], CultureInfo.CurrentCulture); StorageResizeSample.ResizeVirtualHardDisk( serverName, virtualHardDiskPath, fileSize); } else if (string.Equals(args[0], "SetVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; Int32 sectorSize; string parentPath = args[3]; if (Int32.TryParse(args[3], NumberStyles.None, CultureInfo.CurrentCulture, out sectorSize)) { StorageSetSample.SetVirtualHardDisk( serverName, virtualHardDiskPath, null, sectorSize); } else { StorageSetSample.SetVirtualHardDisk( serverName, virtualHardDiskPath, parentPath, 0); } } else { ShowUsage(); } } else if (args.Length == 5) { if (string.Equals(args[0], "AttachVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; string assignDriveLetter = args[3]; string readOnly = args[4]; StorageAttachSample.AttachVirtualHardDisk( serverName, virtualHardDiskPath, assignDriveLetter, readOnly); } else if (string.Equals(args[0], "CreateDifferencingVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; string parentPath = args[3]; VirtualHardDiskType type = VirtualHardDiskType.Differencing; VirtualHardDiskFormat format; if (string.Equals(args[4], "vhdx", StringComparison.OrdinalIgnoreCase)) { format = VirtualHardDiskFormat.Vhdx; } else { format = VirtualHardDiskFormat.Vhd; } StorageCreateSample.CreateVirtualHardDisk( serverName, virtualHardDiskPath, parentPath, type, format, 0, 0, 0, 0); } else if (string.Equals(args[0], "ConvertVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string sourcePath = args[2]; string destinationPath = args[3]; VirtualHardDiskFormat format; if (string.Equals(args[4], "vhdx", StringComparison.OrdinalIgnoreCase)) { format = VirtualHardDiskFormat.Vhdx; } else { format = VirtualHardDiskFormat.Vhd; } StorageConvertSample.ConvertVirtualHardDisk( serverName, sourcePath, destinationPath, format); } else { ShowUsage(); } } else if (args.Length == 6) { if (string.Equals(args[0], "SetParentVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string ChildPath = args[2]; string ParentPath = args[3]; string LeafPath = args[4]; string IgnoreIDMismatch = args[5]; if (string.Equals(LeafPath, "null", StringComparison.OrdinalIgnoreCase)) { // Only valid if VHD is not online. LeafPath = null; } StorageSetParentSample.SetParentVirtualHardDisk( serverName, ChildPath, ParentPath, LeafPath, IgnoreIDMismatch); } else { ShowUsage(); } } else if (args.Length == 8) { if (string.Equals(args[0], "CreateFixedVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; string parentPath = null; VirtualHardDiskType type = VirtualHardDiskType.FixedSize; VirtualHardDiskFormat format; if (string.Equals(args[3], "vhdx", StringComparison.OrdinalIgnoreCase)) { format = VirtualHardDiskFormat.Vhdx; } else { format = VirtualHardDiskFormat.Vhd; } Int64 fileSize = Int64.Parse(args[4], CultureInfo.CurrentCulture); Int32 blockSize = Int32.Parse(args[5], CultureInfo.CurrentCulture); Int32 logicalSectorSize = Int32.Parse(args[6], CultureInfo.CurrentCulture); Int32 physicalSectorSize = Int32.Parse(args[7], CultureInfo.CurrentCulture); StorageCreateSample.CreateVirtualHardDisk( serverName, virtualHardDiskPath, parentPath, type, format, fileSize, blockSize, logicalSectorSize, physicalSectorSize); } else if (string.Equals(args[0], "CreateDynamicVirtualHardDisk", StringComparison.OrdinalIgnoreCase)) { string serverName = args[1]; string virtualHardDiskPath = args[2]; string parentPath = null; VirtualHardDiskType type = VirtualHardDiskType.DynamicallyExpanding; VirtualHardDiskFormat format; if (string.Equals(args[3], "vhdx", StringComparison.OrdinalIgnoreCase)) { format = VirtualHardDiskFormat.Vhdx; } else { format = VirtualHardDiskFormat.Vhd; } Int64 fileSize = Int64.Parse(args[4], CultureInfo.CurrentCulture); Int32 blockSize = Int32.Parse(args[5], CultureInfo.CurrentCulture); Int32 logicalSectorSize = Int32.Parse(args[6], CultureInfo.CurrentCulture); Int32 physicalSectorSize = Int32.Parse(args[7], CultureInfo.CurrentCulture); StorageCreateSample.CreateVirtualHardDisk( serverName, virtualHardDiskPath, parentPath, type, format, fileSize, blockSize, logicalSectorSize, physicalSectorSize); } else { ShowUsage(); } } else { ShowUsage(); } }
public JobResult ConvertVirtualHardDisk(string sourcePath, string destinationPath, VirtualHardDiskType diskType) { throw new NotImplementedException(); }
public JobResult ConvertVirtualHardDisk(string sourcePath, string destinationPath, VirtualHardDiskType diskType) { object[] results = this.Invoke("ConvertVirtualHardDisk", new object[] { sourcePath, destinationPath, diskType}); return ((JobResult)(results[0])); }
public JobResult ConvertVirtualHardDisk(string sourcePath, string destinationPath, VirtualHardDiskType diskType) { try { Log.WriteStart("'{0}' ConvertVirtualHardDisk", ProviderSettings.ProviderName); JobResult result = VirtualizationProvider.ConvertVirtualHardDisk(sourcePath, destinationPath, diskType); Log.WriteEnd("'{0}' ConvertVirtualHardDisk", ProviderSettings.ProviderName); return result; } catch (Exception ex) { Log.WriteError(String.Format("'{0}' ConvertVirtualHardDisk", ProviderSettings.ProviderName), ex); throw; } }
/// <remarks/> public System.IAsyncResult BeginConvertVirtualHardDisk(string sourcePath, string destinationPath, VirtualHardDiskType diskType, System.AsyncCallback callback, object asyncState) { return this.BeginInvoke("ConvertVirtualHardDisk", new object[] { sourcePath, destinationPath, diskType}, callback, asyncState); }