/// <summary> /// Extracts an a VHD file /// </summary> /// <param name="fileEntry"> </param> /// <returns> </returns> public IEnumerable <FileEntry> Extract(FileEntry fileEntry, ExtractorOptions options, ResourceGovernor governor) { using var disk = new DiscUtils.Vhd.Disk(fileEntry.Content, Ownership.None); LogicalVolumeInfo[]? logicalVolumes = null; try { var manager = new VolumeManager(disk); logicalVolumes = manager.GetLogicalVolumes(); } catch (Exception e) { Logger.Debug("Error reading {0} disk at {1} ({2}:{3})", disk.GetType(), fileEntry.FullPath, e.GetType(), e.Message); } if (logicalVolumes != null) { foreach (var volume in logicalVolumes) { foreach (var entry in DiscCommon.DumpLogicalVolume(volume, fileEntry.FullPath, options, governor, Context, fileEntry)) { yield return(entry); } } } else { if (options.ExtractSelfOnFail) { yield return(fileEntry); } } }
void InitializeVhdManually(DiscUtils.Vhd.Disk vhdDisk) { BiosPartitionTable.Initialize(vhdDisk, WellKnownPartitionType.WindowsNtfs); // GuidPartitionTable.Initialize(vhdDisk, WellKnownPartitionType.WindowsNtfs); var volMgr = new VolumeManager(vhdDisk); var logicalVolume = volMgr.GetLogicalVolumes()[0]; var label = $"XVDTool conversion"; using (var destNtfs = NtfsFileSystem.Format(logicalVolume, label, new NtfsFormatOptions())) { destNtfs.NtfsOptions.ShortNameCreation = ShortFileNameOption.Disabled; // NOTE: For VHD creation we just assume a single partition foreach (var file in IterateFilesystem(partitionNumber: 0)) { var fh = file.OpenRead(); if (!destNtfs.Exists(file.DirectoryName)) { destNtfs.CreateDirectory(file.DirectoryName); } using (Stream dest = destNtfs.OpenFile(file.FullName, FileMode.Create, FileAccess.ReadWrite)) { fh.CopyTo(dest); dest.Flush(); } fh.Close(); } } }
void InitializeVhdViaPump(DiscUtils.Vhd.Disk vhdDisk) { if (SectorSize != XvdFile.LEGACY_SECTOR_SIZE) { throw new InvalidOperationException( "Initializing VHD via pump is only supported for 512 byte sectors"); } var pump = new StreamPump(_fs, vhdDisk.Content, (int)XvdFile.LEGACY_SECTOR_SIZE); pump.Run(); }
public DiskBuilderTest() { MemoryStream fileStream = new MemoryStream(); DiscUtils.Vhd.Disk baseFile = DiscUtils.Vhd.Disk.InitializeDynamic(fileStream, Ownership.Dispose, 16 * 1024L * 1024); for (int i = 0; i < 8; i += 1024 * 1024) { baseFile.Content.Position = i; baseFile.Content.WriteByte((byte)i); } baseFile.Content.Position = 15 * 1024 * 1024; baseFile.Content.WriteByte(0xFF); diskContent = baseFile.Content; }
private void _copydisks(EnvelopeType ovfEnv, string label, string targetPath) { m_iscsi = new iSCSI { UpdateHandler = iscsi_UpdateHandler, Cancel = Cancel //in case it has already been cancelled }; m_iscsi.ConfigureTvmNetwork(m_networkUuid, m_isTvmIpStatic, m_tvmIpAddress, m_tvmSubnetMask, m_tvmGateway); try { foreach (XenRef <VDI> vdiuuid in _vdiRefs) { string uuid = ""; string destinationFilename = ""; try { uuid = VDI.get_uuid(XenSession, vdiuuid); destinationFilename = Path.Combine(targetPath, string.Format(@"{0}.vhd", uuid)); if (File.Exists(destinationFilename)) { destinationFilename = Path.Combine(targetPath, string.Format(@"{0}_{1}.vhd", uuid, Thread.CurrentThread.ManagedThreadId)); OVF.UpdateFilename(ovfEnv, string.Format(@"{0}.vhd", uuid), string.Format(@"{0}_{1}.vhd", uuid, Thread.CurrentThread.ManagedThreadId)); log.InfoFormat("{0}: VHD Name collision, renamed {1}.vhd to {1}_{2}.vhd", label, uuid, Thread.CurrentThread.ManagedThreadId); } OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOn, "Export", string.Format(Messages.FILES_TRANSPORT_SETUP, uuid + ".vhd"))); using (Stream source = m_iscsi.Connect(XenSession, uuid, true)) { OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOff, "Export", "")); using (FileStream fs = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)) { // Create a geometry to give to DiscUtils.Vhd.Disk.InitializeDynamic() just so it doesn't override capacity // when initializing the . DiscUtils.Geometry geometry = DiscUtils.Geometry.FromCapacity(source.Length); using (DiscUtils.Vhd.Disk destination = DiscUtils.Vhd.Disk.InitializeDynamic(fs, Ownership.None, source.Length, geometry)) { m_iscsi.Copy(source, destination.Content, Path.GetFileName(destinationFilename), ShouldVerifyDisks); } } } if (ShouldVerifyDisks) { using (var target = new DiscUtils.Vhd.Disk(destinationFilename, FileAccess.Read)) { m_iscsi.Verify(target.Content, destinationFilename); } } } catch (Exception ex) { if (ex is OperationCanceledException) { throw; } var msg = string.Format(Messages.ISCSI_COPY_ERROR, destinationFilename); log.Error(msg); OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.Failure, "Export", msg, ex)); throw new Exception(msg, ex); } finally { OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOn, "Export", string.Format(Messages.FILES_TRANSPORT_CLEANUP, uuid + ".vhd"))); m_iscsi.Disconnect(XenSession); OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOff, "Export", "")); } } } finally { _vdiRefs.Clear(); } }
private void _copydisks(EnvelopeType ovfEnv, string label, string targetPath) { m_iscsi = new iSCSI { UpdateHandler = iscsi_UpdateHandler, Cancel = Cancel//in case it has already been cancelled }; m_iscsi.ConfigureTvmNetwork(m_networkUuid, m_isTvmIpStatic, m_tvmIpAddress, m_tvmSubnetMask, m_tvmGateway); try { foreach (XenRef<VDI> vdiuuid in _vdiRefs) { string uuid = ""; string destinationFilename = ""; try { uuid = VDI.get_uuid(XenSession, vdiuuid); destinationFilename = Path.Combine(targetPath, string.Format(@"{0}.vhd", uuid)); if (File.Exists(destinationFilename)) { destinationFilename = Path.Combine(targetPath, string.Format(@"{0}_{1}.vhd", uuid, Thread.CurrentThread.ManagedThreadId)); OVF.UpdateFilename(ovfEnv, string.Format(@"{0}.vhd", uuid), string.Format(@"{0}_{1}.vhd", uuid, Thread.CurrentThread.ManagedThreadId)); log.InfoFormat("{0}: VHD Name collision, renamed {1}.vhd to {1}_{2}.vhd", label, uuid, Thread.CurrentThread.ManagedThreadId); } OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOn, "Export", string.Format(Messages.FILES_TRANSPORT_SETUP, uuid + ".vhd"))); using (Stream source = m_iscsi.Connect(XenSession, uuid, true)) { OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOff, "Export", "")); using (FileStream fs = new FileStream(destinationFilename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)) { // Create a geometry to give to DiscUtils.Vhd.Disk.InitializeDynamic() just so it doesn't override capacity // when initializing the . DiscUtils.Geometry geometry = DiscUtils.Geometry.FromCapacity(source.Length); using (DiscUtils.Vhd.Disk destination = DiscUtils.Vhd.Disk.InitializeDynamic(fs, Ownership.None, source.Length, geometry)) { m_iscsi.Copy(source, destination.Content, Path.GetFileName(destinationFilename), ShouldVerifyDisks); } } } if (ShouldVerifyDisks) { using (var target = new DiscUtils.Vhd.Disk(destinationFilename, FileAccess.Read)) { m_iscsi.Verify(target.Content, destinationFilename); } } } catch (Exception ex) { if (ex is OperationCanceledException) throw; var msg = string.Format(Messages.ISCSI_COPY_ERROR, destinationFilename); log.Error(msg); OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.Failure, "Export", msg, ex)); throw new Exception(msg, ex); } finally { OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOn, "Export", string.Format(Messages.FILES_TRANSPORT_CLEANUP, uuid + ".vhd"))); m_iscsi.Disconnect(XenSession); OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.MarqueeOff, "Export", "")); } } } finally { _vdiRefs.Clear(); } }