コード例 #1
0
        private void CreateDynamicVirtualHardDisk()
        {
            long   maximumVhdSizeInMb = migrationData.TemporaryVhdFileMaxSize >> 20;// / (1024 * 1024);
            string vhdTypeString      = migrationData.VhdFileType == VhdType.VhdDynamic || migrationData.VhdFileType == VhdType.VhdxDynamic
                ? "expandable" : "fixed";

            string vhdFullName = $"{migrationData.VhdFileTemporaryFolder}\\{migrationData.VhdFileName}";

            logger.LogInformation($"Vhd temporary file path: {vhdFullName}");

            List <string> diskpartScriptContent = new List <string>()
            {
                $"create vdisk file={vhdFullName} maximum={maximumVhdSizeInMb} type={vhdTypeString}",
                $"select vdisk file={vhdFullName}",
                "attach vdisk",
                "convert mbr",
                "detach vdisk",
                "exit"
            };
            string diskpartScriptLocation = $"{migrationData.VhdFileTemporaryFolder}\\diskpartScriptContent.txt";

            using (var sw = fileSystem.File.CreateText(diskpartScriptLocation))
            {
                foreach (string line in diskpartScriptContent)
                {
                    sw.WriteLine(line);
                }
            }

            fileSystemHelper.ExecuteDiskpart(diskpartScriptLocation, logger);

            //cleanup
            fileSystem.File.Delete(diskpartScriptLocation);
        }
コード例 #2
0
        private void ShrinkVolume()
        {
            var systemDriveInfo = fileSystemHelper.GetSystemDriveInfo();

            long   desiredShrinkSize = migrationData.DesiredTempVhdShrinkSize >> 20; // / (1024 * 1024);
            string vhdFullName       = $"{migrationData.VhdFileTemporaryFolder}\\{migrationData.VhdFileName}";

            logger.LogDebug($"Shrinking volume '{vhdFullName}' by {desiredShrinkSize} MB");

            List <string> diskpartScriptContent = new List <string>()
            {
                $"select vdisk file={vhdFullName}",
                "attach vdisk",
                "select partition 1",
                $"shrink desired={desiredShrinkSize}",
                "detach vdisk",
                "exit"
            };

            Policy retryPolicy = Policy
                                 .Handle <Exception>()
                                 .Retry(onRetry: (exception, retry) =>
            {
                this.logger.LogWarning(exception, $"Shrinking volume failed {retry} time.");
                diskpartScriptContent = new List <string>()
                {
                    $"select vdisk file={vhdFullName}",
                    "select partition 1",
                    $"shrink desired={desiredShrinkSize}",
                    "detach vdisk",
                    "exit"
                };
            });

            string diskpartScriptLocation = $"{migrationData.VhdFileTemporaryFolder}\\diskpartScriptContent.txt";

            retryPolicy.Execute(() =>
            {
                using (var sw = fileSystem.File.CreateText(diskpartScriptLocation))
                {
                    foreach (string line in diskpartScriptContent)
                    {
                        sw.WriteLine(line);
                    }
                }

                fileSystemHelper.ExecuteDiskpart(diskpartScriptLocation, logger);
            });

            //cleanup
            fileSystem.File.Delete(diskpartScriptLocation);

            this.ValidateVhdVolumeSize();
        }