Пример #1
0
        public override VirtualDisk CreateDisk(FileLocator locator, string variant, string path, VirtualDiskParameters diskParameters)
        {
            DiskParameters vmdkParams = new DiskParameters(diskParameters);

            vmdkParams.CreateType = VariantToCreateType(variant);
            return(Disk.Initialize(locator, path, vmdkParams));
        }
Пример #2
0
        public override VirtualDisk CreateDisk(FileLocator locator, string variant, string path, long capacity, Geometry geometry, Dictionary <string, string> parameters)
        {
            DiskParameters vmdkParams = new DiskParameters();

            vmdkParams.Capacity = capacity;
            vmdkParams.Geometry = geometry;

            switch (variant)
            {
            case "fixed":
                vmdkParams.CreateType = DiskCreateType.MonolithicFlat;
                break;

            case "dynamic":
                vmdkParams.CreateType = DiskCreateType.MonolithicSparse;
                break;

            case "vmfsfixed":
                vmdkParams.CreateType = DiskCreateType.Vmfs;
                break;

            case "vmfsdynamic":
                vmdkParams.CreateType = DiskCreateType.VmfsSparse;
                break;

            default:
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Unknown VMDK disk variant '{0}'", variant), "variant");
            }

            return(Disk.Initialize(locator, path, vmdkParams));
        }
        public void ReadWriteLarge()
        {
            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.TwoGbMaxExtentSparse))
            {
                byte[] content = new byte[3 * 1024 * 1024];
                for (int i = 0; i < content.Length / 4; ++i)
                {
                    content[i * 4 + 0] = (byte)((i >> 24) & 0xFF);
                    content[i * 4 + 1] = (byte)((i >> 16) & 0xFF);
                    content[i * 4 + 2] = (byte)((i >> 8) & 0xFF);
                    content[i * 4 + 3] = (byte)(i & 0xFF);
                }

                Stream s = disk.Content;
                s.Position = 10;
                s.Write(content, 0, content.Length);

                byte[] buffer = new byte[content.Length];
                s.Position = 10;
                s.Read(buffer, 0, buffer.Length);

                for (int i = 0; i < content.Length; ++i)
                {
                    if (buffer[i] != content[i])
                    {
                        Assert.Fail();
                    }
                }
            }
        }
Пример #4
0
        public void InitializeDynamic()
        {
            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.MonolithicSparse))
            {
                Assert.IsNotNull(disk);
                Assert.That(disk.Geometry.Capacity > 15.8 * 1024L * 1024 * 1024 && disk.Geometry.Capacity <= 16 * 1024L * 1024 * 1024);
                Assert.That(disk.Content.Length == 16 * 1024L * 1024 * 1024);
            }

            Assert.Greater(fs.GetFileLength("a.vmdk"), 2 * 1024 * 1024);
            Assert.Less(fs.GetFileLength("a.vmdk"), 4 * 1024 * 1024);

            using (Disk disk = new Disk(fs, "a.vmdk", FileAccess.Read))
            {
                Assert.That(disk.Geometry.Capacity > 15.8 * 1024L * 1024 * 1024 && disk.Geometry.Capacity <= 16 * 1024L * 1024 * 1024);
                Assert.That(disk.Content.Length == 16 * 1024L * 1024 * 1024);

                List <DiskImageFile> links = new List <DiskImageFile>(disk.Links);
                List <string>        paths = new List <string>(links[0].ExtentPaths);
                Assert.AreEqual(1, paths.Count);
                Assert.AreEqual("a.vmdk", paths[0]);
            }
        }
Пример #5
0
        public void ReadOnlyHosted()
        {
            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.MonolithicSparse))
            {
            }

            Disk d2 = new Disk(fs, "a.vmdk", FileAccess.Read);

            Assert.IsFalse(d2.Content.CanWrite);
        }
        public void Attributes()
        {
            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.MonolithicSparse))
            {
                Stream s = disk.Content;
                Assert.IsTrue(s.CanRead);
                Assert.IsTrue(s.CanWrite);
                Assert.IsTrue(s.CanSeek);
            }
        }
Пример #7
0
        public void InitializeFixedIDE()
        {
            using (Disk disk = Disk.Initialize(new InMemoryFileSystem(), "a.vmdk", 8 * 1024 * 1024, DiskCreateType.MonolithicFlat, DiskAdapterType.Ide))
            {
                Assert.IsNotNull(disk);
                Assert.That(disk.Geometry.Capacity > 7.9 * 1024 * 1024 && disk.Geometry.Capacity < 8.1 * 1024 * 1024);
                Assert.That(disk.Geometry.Capacity == disk.Content.Length);

                List <DiskImageFile> links = new List <DiskImageFile>(disk.Links);
                List <string>        paths = new List <string>(links[0].ExtentPaths);
                Assert.AreEqual(1, paths.Count);
                Assert.AreEqual("a-flat.vmdk", paths[0]);
            }
        }
        public void ReadWriteSmall()
        {
            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.TwoGbMaxExtentSparse))
            {
                byte[] content = new byte[100];
                for (int i = 0; i < content.Length; ++i)
                {
                    content[i] = (byte)i;
                }

                Stream s = disk.Content;
                s.Write(content, 10, 40);
                Assert.AreEqual(40, s.Position);
                s.Write(content, 50, 50);
                Assert.AreEqual(90, s.Position);
                s.Position = 0;

                byte[] buffer = new byte[100];
                s.Read(buffer, 10, 60);
                Assert.AreEqual(60, s.Position);
                for (int i = 0; i < 10; ++i)
                {
                    Assert.AreEqual(0, buffer[i]);
                }
                for (int i = 10; i < 60; ++i)
                {
                    Assert.AreEqual(i, buffer[i]);
                }
            }

            // Check the data persisted
            using (Disk disk = new Disk(fs, "a.vmdk", FileAccess.Read))
            {
                Stream s = disk.Content;

                byte[] buffer = new byte[100];
                s.Read(buffer, 10, 20);
                Assert.AreEqual(20, s.Position);
                for (int i = 0; i < 10; ++i)
                {
                    Assert.AreEqual(0, buffer[i]);
                }
                for (int i = 10; i < 20; ++i)
                {
                    Assert.AreEqual(i, buffer[i]);
                }
            }
        }
        public void DisposeTestVmfs()
        {
            Stream contentStream;

            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.VmfsSparse))
            {
                contentStream = disk.Content;
            }

            try
            {
                contentStream.Position = 0;
                Assert.Fail();
            }
            catch (ObjectDisposedException) { }
        }
        public void ReadNotPresentVmfs()
        {
            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.VmfsSparse))
            {
                byte[] buffer = new byte[100];
                disk.Content.Seek(2 * 1024 * 1024, SeekOrigin.Current);
                disk.Content.Read(buffer, 0, buffer.Length);

                for (int i = 0; i < 100; ++i)
                {
                    if (buffer[i] != 0)
                    {
                        Assert.Fail();
                    }
                }
            }
        }
        public void Extents()
        {
            // Fragile - this is the grain size in bytes of the VMDK file, so dependant on algorithm that
            // determines grain size for new VMDKs...
            const int unit = 128 * 512;

            DiscFileSystem fs = new InMemoryFileSystem();

            using (Disk disk = Disk.Initialize(fs, "a.vmdk", 16 * 1024L * 1024 * 1024, DiskCreateType.TwoGbMaxExtentSparse))
            {
                disk.Content.Position = 20 * unit;
                disk.Content.Write(new byte[4 * unit], 0, 4 * unit);

                // Starts before first extent, ends before end of extent
                List <StreamExtent> extents = new List <StreamExtent>(disk.Content.GetExtentsInRange(0, 21 * unit));
                Assert.AreEqual(1, extents.Count);
                Assert.AreEqual(20 * unit, extents[0].Start);
                Assert.AreEqual(1 * unit, extents[0].Length);

                // Limit to disk content length
                extents = new List <StreamExtent>(disk.Content.GetExtentsInRange(21 * unit, 20 * unit));
                Assert.AreEqual(1, extents.Count);
                Assert.AreEqual(21 * unit, extents[0].Start);
                Assert.AreEqual(3 * unit, extents[0].Length);

                // Out of range
                extents = new List <StreamExtent>(disk.Content.GetExtentsInRange(25 * unit, 4 * unit));
                Assert.AreEqual(0, extents.Count);

                // Non-unit multiples
                extents = new List <StreamExtent>(disk.Content.GetExtentsInRange(21 * unit + 10, 20 * unit));
                Assert.AreEqual(1, extents.Count);
                Assert.AreEqual(21 * unit + 10, extents[0].Start);
                Assert.AreEqual(3 * unit - 10, extents[0].Length);
            }
        }