Пример #1
0
        public void Resize_Vhd_Blob_Shrink()
        {
            var firstSizeInGb = 2;
            var newSizeInGb   = 1;
            var vhdFilePath   = "TestDisk-Shrink.vhd";
            var containerName = "test-container";

            // First create the fixed VHD file
            VhdHelper.CreateVhdDisk(false, firstSizeInGb, vhdFilePath, "Testing Shrink Disk");
            var vhdBlobUri = AzureStorageEmulatorHelper.UploadVhdFileToContainer(containerName, vhdFilePath);

            // Then resize the VHD file
            var resizeVhdHelper = new ResizeVhdHelper();
            var firstResult     = resizeVhdHelper.ResizeVhdBlob(newSizeInGb, vhdBlobUri, accountName, accountKey);
            var finalResult     = ResizeResult.Error;

            if (firstResult == ResizeResult.Shrink)
            {
                resizeVhdHelper.IsExpand = false;
                finalResult = resizeVhdHelper.DoResizeVhdBlob();
            }
            var length = AzureStorageEmulatorHelper.GetVhdSizeInContainer(vhdBlobUri);

            // Clean the files in the container and the local file system
            AddCleanupAction(() => AzureStorageEmulatorHelper.DeleteVhdFileInContainer(containerName, vhdBlobUri));
            AddCleanupAction(() => File.Delete(vhdFilePath));

            Assert.IsTrue(firstResult == ResizeResult.Shrink);
            Assert.IsTrue(finalResult == ResizeResult.Success);
            Assert.IsTrue(newSizeInGb == (int)ByteSize.FromBytes(length).GigaBytes);
        }
Пример #2
0
        private static int Main(string[] args)
        {
            WriteHeader();

            // Check argument count
            if (args.Length < 2)
            {
                WriteUsage();
                return(-1);
            }

            // Parse arguments
            if (!long.TryParse(args[0], out long newSizeInGb) || (newSizeInGb * 1024 * 1024 * 1024) % 512 != 0)
            {
                Console.WriteLine("Argument size invalid. Please specify a valid disk size in GB (must be a whole number).");
                return(-1);
            }
            if (!Uri.TryCreate(args[1], UriKind.Absolute, out Uri blobUri))
            {
                Console.WriteLine("Argument bloburl invalid. Please specify a valid URL with an HTTP or HTTPS schema.");
                return(-1);
            }

            var accountName = "";
            var accountKey  = "";

            if (args.Length == 4)
            {
                accountName = args[2];
                accountKey  = args[3];
            }
            else if (!blobUri.Query.Contains("sig="))
            {
                Console.WriteLine("Please specify either a blob URL with a shared access signature that allows write access or provide full storage credentials.");
                return(-1);
            }

            // Verify size. Size for disk must be <= 1023 GB
            if (newSizeInGb > 1023)
            {
                Console.WriteLine("The given disk size exceeds 1023 GB. Microsoft Azure will not be able to start the virtual machine stored on this disk if you continue.");
                Console.WriteLine("See https://msdn.microsoft.com/en-us/library/azure/dn197896.aspx for more information.");
                return(-1);
            }

            // Start the resize process
            var resizeVhdHelper = new ResizeVhdHelper();
            var result          = resizeVhdHelper.ResizeVhdBlob((int)newSizeInGb, blobUri, accountName, accountKey);

            if (result != ResizeResult.Shrink)
            {
                return((int)result);
            }

            Console.WriteLine("The specified VHD blob is larger than the specified new size. Shrinking disks is a potentially dangerous operation.");
            Console.WriteLine("Do you want to continue with shrinking the disk? (y/n)");
            while (true)
            {
                var consoleKey = Console.ReadKey().KeyChar;
                if (consoleKey == 'n')
                {
                    Console.WriteLine("Aborted.");
                    return(-1);
                }
                if (consoleKey == 'y')
                {
                    resizeVhdHelper.IsExpand = false;
                    var finalResult = resizeVhdHelper.DoResizeVhdBlob();
                    return((int)finalResult);
                }
            }
        }