public UploadParameters ValidateParameters() { BlobUri destinationUri; if (!BlobUri.TryParseUri(Destination, out destinationUri)) { throw new ArgumentOutOfRangeException("Destination", this.Destination.ToString()); } BlobUri baseImageUri = null; if (this.BaseImageUriToPatch != null) { if (!BlobUri.TryParseUri(BaseImageUriToPatch, out baseImageUri)) { throw new ArgumentOutOfRangeException("BaseImageUriToPatch", this.BaseImageUriToPatch.ToString()); } if (!String.IsNullOrEmpty(destinationUri.Uri.Query)) { var message = String.Format(Rsrc.AddAzureVhdCommandSASUriNotSupportedInPatchMode, destinationUri.Uri); throw new ArgumentOutOfRangeException("Destination", message); } } var storageCredentialsFactory = CreateStorageCredentialsFactory(); PathIntrinsics currentPath = SessionState.Path; var filePath = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString())); using (var vds = new VirtualDiskStream(filePath.FullName)) { if (vds.DiskType == DiskType.Fixed) { long divisor = Convert.ToInt64(Math.Pow(2, 9)); long rem = 0; Math.DivRem(filePath.Length, divisor, out rem); if (rem != 0) { throw new ArgumentOutOfRangeException("LocalFilePath", "Given vhd file is a corrupted fixed vhd"); } } } var parameters = new UploadParameters( destinationUri, baseImageUri, filePath, OverWrite.IsPresent, (NumberOfUploaderThreads) ?? DefaultNumberOfUploaderThreads) { Cmdlet = this, BlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1)) }; return(parameters); }
public UploadParameters ValidateParameters() { BlobUri destinationUri; if (!BlobUri.TryParseUri(Destination, out destinationUri)) { throw new ArgumentOutOfRangeException("Destination", this.Destination.ToString()); } BlobUri baseImageUri = null; if (this.BaseImageUriToPatch != null) { if (!BlobUri.TryParseUri(BaseImageUriToPatch, out baseImageUri)) { throw new ArgumentOutOfRangeException("BaseImageUriToPatch", this.BaseImageUriToPatch.ToString()); } if (!String.IsNullOrEmpty(destinationUri.Uri.Query)) { var message = String.Format(Rsrc.AddAzureVhdCommandSASUriNotSupportedInPatchMode, destinationUri.Uri); throw new ArgumentOutOfRangeException("Destination", message); } } var storageCredentialsFactory = CreateStorageCredentialsFactory(); PathIntrinsics currentPath = SessionState.Path; var filePath = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString())); var parameters = new UploadParameters( destinationUri, baseImageUri, filePath, OverWrite.IsPresent, (NumberOfUploaderThreads) ?? DefaultNumberOfUploaderThreads) { Cmdlet = this, BlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1)) }; return(parameters); }
private void CheckForInvalidVhd() { PathIntrinsics currentPath = SessionState.Path; FileInfo filePath = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString())); try { bool resizeNeeded = false; long resizeTo = 0; using (VirtualDiskStream vds = new VirtualDiskStream(filePath.FullName)) { if (vds.Length < 20971520 || vds.Length > 4396972769280) { throw new InvalidOperationException("The VHD must be between 20 MB and 4095 GB."); } if (!this.SkipResizing.IsPresent && (vds.Length - 512) % 1048576 != 0) { resizeNeeded = true; resizeTo = Convert.ToInt64(1048576 * Math.Ceiling((vds.Length - 512) / 1048576.0)); } else if (this.SkipResizing.IsPresent) { WriteVerbose("Skipping VHD resizing."); } FixedSize = vds.Length; } if (resizeNeeded) { resizeVhdFile(resizeTo); } } catch (VhdParsingException) { throw new InvalidOperationException("The VHD file is corrupted."); } return; }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ExecuteClientAction(() => { try { WriteVerbose("To be compatible with Azure, Add-AzVhd will automatically try to convert VHDX files to VHD and resize VHD files to N * Mib using Hyper-V Platform, a Windows native virtualization product. During the process, the cmdlet will temporarily create a converted/resized file in the same directory as the provided VHD/VHDX file. \nFor more information visit https://aka.ms/usingAdd-AzVhd \n"); Program.SyncOutput = new PSSyncOutputEvents(this); PathIntrinsics currentPath = SessionState.Path; vhdFileToBeUploaded = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString())); // 1. CONVERT VHDX TO VHD if (vhdFileToBeUploaded.Extension == ".vhdx") { vhdFileToBeUploaded = ConvertVhd(); } // 2. RESIZE VHD long vdsLength = GetVirtualDiskStreamLength(); if (!this.SkipResizing.IsPresent && (vdsLength - 512) % 1048576 != 0) { long resizeTo = Convert.ToInt64(1048576 * Math.Ceiling((vdsLength - 512) / 1048576.0)); vhdFileToBeUploaded = ResizeVhdFile(vdsLength, resizeTo); vdsLength = resizeTo + 512; } else if (this.SkipResizing.IsPresent) { WriteVerbose("Skipping VHD resizing."); } if (this.ParameterSetName == DirectUploadToManagedDiskSet) { // 3. DIRECT UPLOAD TO MANAGED DISK // 3-1. CREATE DISK CONFIG CheckForExistingDisk(this.ResourceGroupName, this.DiskName); var diskConfig = CreateDiskConfig(vdsLength); // 3-2: CREATE DISK CreateManagedDisk(this.ResourceGroupName, this.DiskName, diskConfig); // 3-3: GENERATE SAS WriteVerbose("Generating SAS"); var accessUri = GenerateSAS(); Uri sasUri = new Uri(accessUri.AccessSAS); WriteVerbose("SAS generated: " + accessUri.AccessSAS); // 3-4: UPLOAD WriteVerbose("Preparing for Upload"); ComputeTokenCredential tokenCredential = null; if (this.DataAccessAuthMode == "AzureActiveDirectory") { // get token tokenCredential = new ComputeTokenCredential(DefaultContext, "https://disk.azure.com/"); } PSPageBlobClient managedDisk = new PSPageBlobClient(sasUri, tokenCredential); DiskUploadCreator diskUploadCreator = new DiskUploadCreator(); var uploadContext = diskUploadCreator.Create(vhdFileToBeUploaded, managedDisk, false); var synchronizer = new DiskSynchronizer(uploadContext, this.NumberOfUploaderThreads ?? DefaultNumberOfUploaderThreads); WriteVerbose("Uploading"); if (synchronizer.Synchronize(tokenCredential)) { var result = new VhdUploadContext { LocalFilePath = vhdFileToBeUploaded, DestinationUri = sasUri }; WriteObject(result); } else { RevokeSAS(); this.ComputeClient.ComputeManagementClient.Disks.Delete(this.ResourceGroupName, this.DiskName); Exception outputEx = new Exception("Upload failed. Please try again later."); ThrowTerminatingError(new ErrorRecord( outputEx, "Error uploading data.", ErrorCategory.NotSpecified, null)); } // 3-5: REVOKE SAS WriteVerbose("Revoking SAS"); RevokeSAS(); WriteVerbose("SAS revoked."); WriteVerbose("\nUpload complete."); } else { var parameters = ValidateParameters(); var vhdUploadContext = VhdUploaderModel.Upload(parameters); WriteObject(vhdUploadContext); } } finally { if (temporaryFileCreated) { WriteVerbose("Deleting file: " + vhdFileToBeUploaded.FullName); File.Delete(vhdFileToBeUploaded.FullName); } } }); }
private void checkForCorruptedAndDynamicallySizedVhd() { // checking for corrupted vhd PathIntrinsics currentPath = SessionState.Path; var filePath = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString())); using (var vds = new VirtualDiskStream(filePath.FullName)) { if (vds.DiskType == DiskType.Fixed) { long divisor = Convert.ToInt64(Math.Pow(2, 9)); long rem = 0; Math.DivRem(filePath.Length, divisor, out rem); if (rem != 0) { throw new ArgumentOutOfRangeException("LocalFilePath", "Given vhd file is a corrupted fixed vhd"); } } else { convertDynamicVhdToStatic(); } } return; }