/// <summary>
 /// Initializes a new instance of the PhysicalVolumeInfo class.
 /// </summary>
 /// <param name="diskId">The containing disk's identity</param>
 /// <param name="disk">The disk containing the partition</param>
 /// <param name="partitionInfo">Information about the partition</param>
 /// <remarks>Use this constructor to represent a (BIOS or GPT) partition</remarks>
 internal PhysicalVolumeInfo(
     string diskId,
     VirtualDisk disk,
     PartitionInfo partitionInfo)
 {
     _diskId = diskId;
     _disk = disk;
     _streamOpener = partitionInfo.Open;
     _type = partitionInfo.VolumeType;
     _partitionInfo = partitionInfo;
 }
Example #2
0
        private string GetDiskId(int ordinal)
        {
            VirtualDisk disk = _disks[ordinal];

            if (disk.IsPartitioned)
            {
                Guid guid = disk.Partitions.DiskGuid;
                if (guid != Guid.Empty)
                {
                    return("DG" + guid.ToString("B"));
                }
            }

            int sig = disk.Signature;

            if (sig != 0)
            {
                return("DS" + sig.ToString("X8", CultureInfo.InvariantCulture));
            }

            return("DO" + ordinal);
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the VolumeManager class.
 /// </summary>
 /// <param name="initialDisk">The initial disk to add.</param>
 public VolumeManager(VirtualDisk initialDisk)
     : this()
 {
     AddDisk(initialDisk);
 }
Example #4
0
 /// <summary>
 /// Gets the physical volumes held on a disk.
 /// </summary>
 /// <param name="disk">The disk to inspect</param>
 /// <returns>An array of volumes</returns>
 /// <remarks>If the disk isn't partitioned, this method returns the entire disk contents
 /// as a single volume.</remarks>
 public static PhysicalVolumeInfo[] GetPhysicalVolumes(VirtualDisk disk)
 {
     return(new VolumeManager(disk).GetPhysicalVolumes());
 }
 /// <summary>
 /// Initializes a new instance of the PhysicalVolumeInfo class.
 /// </summary>
 /// <param name="diskId">The identity of the disk</param>
 /// <param name="disk">The disk itself</param>
 /// <remarks>Use this constructor to represent an entire disk as a single volume.</remarks>
 internal PhysicalVolumeInfo(
     string diskId,
     VirtualDisk disk)
 {
     _diskId = diskId;
     _disk = disk;
     _streamOpener = delegate { return new SubStream(disk.Content, Ownership.None, 0, disk.Capacity); };
     _type = PhysicalVolumeType.EntireDisk;
 }
Example #6
0
        /// <summary>
        /// Opens an existing virtual disk.
        /// </summary>
        /// <param name="path">The path of the virtual disk to open, can be a URI</param>
        /// <param name="forceType">Force the detected disk type (<c>null</c> to detect)</param>
        /// <param name="access">The desired access to the disk</param>
        /// <param name="user">The user name to use for authentication (if necessary)</param>
        /// <param name="password">The password to use for authentication (if necessary)</param>
        /// <returns>The Virtual Disk, or <c>null</c> if an unknown disk format</returns>
        /// <remarks>
        /// The detected disk type can be forced by specifying a known disk type:
        /// RAW, VHD, VMDK, etc.
        /// </remarks>
        public static VirtualDisk OpenDisk(string path, string forceType, FileAccess access, string user, string password)
        {
            Uri         uri    = PathToUri(path);
            VirtualDisk result = null;

            Type transportType;

            if (!DiskTransports.TryGetValue(uri.Scheme.ToUpperInvariant(), out transportType))
            {
                throw new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "Unable to parse path '{0}'", path), path);
            }

            VirtualDiskTransport transport = (VirtualDiskTransport)Activator.CreateInstance(transportType);

            try
            {
                transport.Connect(uri, user, password);

                if (transport.IsRawDisk)
                {
                    result = transport.OpenDisk(access);
                }
                else
                {
                    bool foundFactory;
                    VirtualDiskFactory factory;

                    if (!string.IsNullOrEmpty(forceType))
                    {
                        foundFactory = TypeMap.TryGetValue(forceType, out factory);
                    }
                    else
                    {
                        string extension = Path.GetExtension(uri.AbsolutePath).ToUpperInvariant();
                        if (extension.StartsWith(".", StringComparison.Ordinal))
                        {
                            extension = extension.Substring(1);
                        }

                        foundFactory = ExtensionMap.TryGetValue(extension, out factory);
                    }

                    if (foundFactory)
                    {
                        result = factory.OpenDisk(transport.GetFileLocator(), transport.GetFileName(), access);
                    }
                }

                if (result != null)
                {
                    result._transport = transport;
                    transport         = null;
                }

                return(result);
            }
            finally
            {
                if (transport != null)
                {
                    transport.Dispose();
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the VolumeManager class.
 /// </summary>
 /// <param name="initialDisk">The initial disk to add.</param>
 public VolumeManager(VirtualDisk initialDisk)
     : this()
 {
     AddDisk(initialDisk);
 }
 /// <summary>
 /// Adds a disk to the volume manager.
 /// </summary>
 /// <param name="disk">The disk to add</param>
 /// <returns>The GUID the volume manager will use to identify the disk</returns>
 public string AddDisk(VirtualDisk disk)
 {
     _needScan = true;
     int ordinal = _disks.Count;
     _disks.Add(disk);
     return GetDiskId(ordinal);
 }
 /// <summary>
 /// Gets the physical volumes held on a disk.
 /// </summary>
 /// <param name="disk">The disk to inspect</param>
 /// <returns>An array of volumes</returns>
 /// <remarks>If the disk isn't partitioned, this method returns the entire disk contents
 /// as a single volume.</remarks>
 public static PhysicalVolumeInfo[] GetPhysicalVolumes(VirtualDisk disk)
 {
     return new VolumeManager(disk).GetPhysicalVolumes();
 }