private XenRef<VDI> UploadiSCSIbyWimFile(Session xenSession, string sruuid, string label, WimFile wimDisk, int imageindex, long capacity, ulong wimFileCount, int arch, string description) { log.Debug("OVF.Import.UploadiSCSIbyWimFile Enter"); log.DebugFormat("OVF.Import.UploadiSCSIbyWimFile SRUUID: {0}", sruuid); log.DebugFormat("OVF.Import.UploadiSCSIbyWimFile Label: {0}", label); log.DebugFormat("OVF.Import.UploadiSCSIbyWimFile ImageIndex: {0}", imageindex); log.DebugFormat("OVF.Import.UploadiSCSIbyWimFile Capacity: {0}", capacity); string vdilabel = string.Format("{0}{1}", label, imageindex); XenRef<VDI> vdiRef = CreateVDI(xenSession, sruuid, vdilabel, capacity, description); byte[] mbr = null; byte[] boot = null; //byte[] bcd = null; //byte[] bootmgr = null; string vhdfile = FindReferenceVHD(Properties.Settings.Default.ReferenceVHDName); if (File.Exists(vhdfile)) { mbr = ExtractMBRFromVHD(vhdfile); boot = ExtractBootFromVHD(vhdfile); //bcd = ExtractBCDFromVHD(vhdfile, arch); //bootmgr = ExtractBootmgrFromVHD(vhdfile, arch); } else { log.WarnFormat("Refernce VHD not found [{0}]", Properties.Settings.Default.ReferenceVHDName); } #region UPLOAD iSCSI STREAM OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.FileStart, "Import", string.Format(Messages.FILES_TRANSPORT_SETUP, _currentfilename))); 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 { wimFileIndex = 0; string vdiuuid = VDI.get_uuid(xenSession, vdiRef); Stream iSCSIStream = m_iscsi.Connect(xenSession, vdiuuid, false); WimFileSystem w = wimDisk.GetImage(imageindex); if (mbr != null) { m_iscsi.ScsiDisk.SetMasterBootRecord(mbr); } else { log.WarnFormat("System will not be bootable, cannot find [{0}] to extract master boot record.", vhdfile); OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.FileStart, "Import", Messages.WARNING_TARGET_NOT_BOOTABLE)); } m_iscsi.ScsiDisk.Signature = new Random().Next(); BiosPartitionTable table = BiosPartitionTable.Initialize(m_iscsi.ScsiDisk, WellKnownPartitionType.WindowsNtfs); VolumeManager volmgr = new VolumeManager(m_iscsi.ScsiDisk); NtfsFileSystem ntfs = null; if (wimDisk.BootImage == imageindex && boot != null) { table.SetActivePartition(0); ntfs = NtfsFileSystem.Format(volmgr.GetLogicalVolumes()[0], "New Volume", boot); } else { ntfs = NtfsFileSystem.Format(volmgr.GetLogicalVolumes()[0], "New Volume"); } //AddBCD(ntfs, bcd); //AddBootMgr(ntfs, bootmgr); // If it's not there it'll be created if it is it will not.. not below filecopy will overwrite if one it exists. FileCopy(m_iscsi, w.Root.GetFiles(), w, ntfs); FileCopy(m_iscsi, w.Root.GetDirectories(), w, ntfs); FixBCD(ntfs, volmgr); ntfs.Dispose(); } catch (Exception ex) { if (ex is OperationCanceledException) throw; log.ErrorFormat("{0} {1}", Messages.ERROR_ISCSI_UPLOAD_FAILED, ex.Message); vdiRef = null; throw new Exception(Messages.ERROR_ISCSI_UPLOAD_FAILED, ex); } finally { OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.FileStart, "Import", string.Format(Messages.FILES_TRANSPORT_CLEANUP, _currentfilename))); m_iscsi.Disconnect(xenSession); } #endregion log.Debug("OVF.Import.UploadiSCSIbyWimFile Leave"); return vdiRef; }
private XenRef<VDI> UploadiSCSI(Session xenSession, string sruuid, string label, Stream filestream, long capacity, string description, string vdiuuid) { log.Debug("OVF.Import.UploadiSCSI Enter"); log.DebugFormat("OVF.Import.UploadiSCSI SRUUID: {0}", sruuid); log.DebugFormat("OVF.Import.UploadiSCSI Label: {0}", label); log.DebugFormat("OVF.Import.UploadiSCSI Capacity: {0}", capacity); XenRef<VDI> vdiRef = null; //If no VDI uuid is provided create a VDI, otherwise use the one provided as //the target for the import. Used for SRs such as Lun per VDI (PR-1544) if(String.IsNullOrEmpty(vdiuuid)) { vdiRef = CreateVDI(xenSession, sruuid, label, capacity, description); vdiuuid = VDI.get_uuid(xenSession, vdiRef); } else { vdiRef = new XenRef<VDI>(VDI.get_by_uuid(xenSession, vdiuuid)); } #region UPLOAD iSCSI STREAM OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.FileStart, "Import", string.Format(Messages.FILES_TRANSPORT_SETUP, _currentfilename))); 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 { using (Stream iSCSIStream = m_iscsi.Connect(xenSession, vdiuuid, false)) { m_iscsi.Copy(filestream, iSCSIStream, label, false); iSCSIStream.Flush(); } } catch (Exception ex) { if (ex is OperationCanceledException) throw; log.ErrorFormat("{0} {1}", Messages.ERROR_ISCSI_UPLOAD_FAILED, ex.Message); vdiRef = null; throw new Exception(Messages.ERROR_ISCSI_UPLOAD_FAILED, ex); } finally { OnUpdate(new XenOvfTranportEventArgs(XenOvfTranportEventType.FileStart, "Import", string.Format(Messages.FILES_TRANSPORT_CLEANUP,_currentfilename))); m_iscsi.Disconnect(xenSession); } #endregion log.Debug("OVF.Import.UploadiSCSI Leave"); return vdiRef; }
private void FileCopy(iSCSI iscsi, DiscFileInfo[] FileInfos, WimFileSystem w, NtfsFileSystem ntfs) { foreach (DiscFileInfo file in FileInfos) { if (IsExcluded(file.FullName)) { log.InfoFormat("File Skip {0}", file.FullName); continue; } FileAttributes attr = file.Attributes; if ((attr & FileAttributes.ReparsePoint) == 0) { using (Stream source = w.OpenFile(file.FullName, FileMode.Open, FileAccess.Read)) { using (Stream destin = ntfs.OpenFile(file.FullName, FileMode.Create, FileAccess.Write)) { iscsi.WimCopy(source, destin, Path.GetFileName(file.FullName), false, wimFileIndex++, wimFileCount); } } if ((attr & FileAttributes.Temporary) == FileAttributes.Temporary) attr = attr & ~FileAttributes.Temporary; if ((attr & FileAttributes.Offline) == FileAttributes.Offline) attr = attr & ~FileAttributes.Offline; ntfs.SetAttributes(file.FullName, attr); } else { traceLog.InfoFormat("Reparse Point: {0}", file.FullName); ReparsePoint rp = w.GetReparsePoint(file.FullName); ntfs.SetReparsePoint(file.FullName, rp); } } }
private void FileCopy(iSCSI iscsi, DiscDirectoryInfo[] DirInfos, WimFileSystem w, NtfsFileSystem ntfs) { foreach (DiscDirectoryInfo dir in DirInfos) { if (IsExcluded(dir.FullName)) { log.InfoFormat("Directory Skip {0}", dir.FullName); continue; } FileAttributes attr = dir.Attributes; if ((dir.Attributes & FileAttributes.ReparsePoint) == 0) { ntfs.CreateDirectory(dir.FullName); if ((attr & FileAttributes.Temporary) == FileAttributes.Temporary) attr = attr & ~FileAttributes.Temporary; if ((attr & FileAttributes.Offline) == FileAttributes.Offline) attr = attr & ~FileAttributes.Offline; ntfs.SetAttributes(dir.FullName, attr); FileCopy(iscsi, dir.GetDirectories(), w, ntfs); FileCopy(iscsi, dir.GetFiles(), w, ntfs); } else { traceLog.InfoFormat("Directory ReparsePoint {0}", dir.FullName); ReparsePoint rp = w.GetReparsePoint(dir.FullName); ntfs.CreateDirectory(dir.FullName); ntfs.SetReparsePoint(dir.FullName, rp); } } }
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(); } }