-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathProgram.cs
More file actions
456 lines (368 loc) · 16.2 KB
/
Program.cs
File metadata and controls
456 lines (368 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
// Copyright (c) 2008-2011, Kenneth Bell
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using DiscUtils;
using DiscUtils.Common;
using DiscUtils.Ntfs;
using DiscUtils.Partitions;
using DiscUtils.Streams;
namespace DiskClone
{
class CloneVolume
{
public NativeMethods.DiskExtent SourceExtent;
public string Path;
public Guid SnapshotId;
public VssSnapshotProperties SnapshotProperties;
}
class Program : ProgramBase
{
private CommandLineEnumSwitch<GeometryTranslation> _translation;
private CommandLineMultiParameter _volumes;
private CommandLineParameter _destDisk;
static void Main(string[] args)
{
Program program = new Program();
program.Run(args);
}
protected override StandardSwitches DefineCommandLine(CommandLineParser parser)
{
_translation = new CommandLineEnumSwitch<GeometryTranslation>("t", "translation", "mode", GeometryTranslation.Auto,"Indicates the geometry adjustment to apply. Set this parameter to match the translation configured in the BIOS of the machine that will boot from the disk - auto should work in most cases for modern BIOS.");
_volumes = new CommandLineMultiParameter("volume", "Volumes to clone. The volumes should all be on the same disk.", false);
_destDisk = new CommandLineParameter("out_file", "Path to the output disk image.", false);
parser.AddSwitch(_translation);
parser.AddMultiParameter(_volumes);
parser.AddParameter(_destDisk);
return StandardSwitches.OutputFormatAndAdapterType;
}
protected override string[] HelpRemarks
{
get
{
return new string[]
{
"DiskClone clones a live disk into a virtual disk file. The volumes cloned must be formatted with NTFS, and partitioned using a conventional partition table.",
"Only Windows 7 is supported.",
"The tool must be run with administrator privilege."
};
}
}
protected override void DoRun()
{
if (!IsAdministrator())
{
Console.WriteLine("\nThis utility must be run as an administrator!\n");
Environment.Exit(1);
}
DiskImageBuilder builder = DiskImageBuilder.GetBuilder(OutputDiskType, OutputDiskVariant);
builder.GenericAdapterType = AdapterType;
string[] sourceVolume = _volumes.Values;
uint diskNumber;
List<CloneVolume> cloneVolumes = GatherVolumes(sourceVolume, out diskNumber);
if (!Quiet)
{
Console.WriteLine("Inspecting Disk...");
}
// Construct a stream representing the contents of the cloned disk.
BiosPartitionedDiskBuilder contentBuilder;
Geometry biosGeometry;
Geometry ideGeometry;
long capacity;
using (Disk disk = new Disk(diskNumber))
{
contentBuilder = new BiosPartitionedDiskBuilder(disk);
biosGeometry = disk.BiosGeometry;
ideGeometry = disk.Geometry;
capacity = disk.Capacity;
}
// Preserve the IDE (aka Physical) geometry
builder.Geometry = ideGeometry;
// Translate the BIOS (aka Logical) geometry
GeometryTranslation translation = _translation.EnumValue;
if (builder.PreservesBiosGeometry && translation == GeometryTranslation.Auto)
{
// If the new format preserves BIOS geometry, then take no action if asked for 'auto'
builder.BiosGeometry = biosGeometry;
translation = GeometryTranslation.None;
}
else
{
builder.BiosGeometry = ideGeometry.TranslateToBios(0, translation);
}
if (translation != GeometryTranslation.None)
{
contentBuilder.UpdateBiosGeometry(builder.BiosGeometry);
}
IVssBackupComponents backupCmpnts;
int status;
if (Marshal.SizeOf(typeof(IntPtr)) == 4)
{
status = NativeMethods.CreateVssBackupComponents(out backupCmpnts);
}
else
{
status = NativeMethods.CreateVssBackupComponents64(out backupCmpnts);
}
Guid snapshotSetId = CreateSnapshotSet(cloneVolumes, backupCmpnts);
if (!Quiet)
{
Console.Write("Copying Disk...");
}
foreach (var sv in cloneVolumes)
{
Volume sourceVol = new Volume(sv.SnapshotProperties.SnapshotDeviceObject, sv.SourceExtent.ExtentLength);
SnapshotStream rawVolStream = new SnapshotStream(sourceVol.Content, Ownership.None);
rawVolStream.Snapshot();
byte[] volBitmap;
int clusterSize;
using (NtfsFileSystem ntfs = new NtfsFileSystem(rawVolStream))
{
ntfs.NtfsOptions.HideSystemFiles = false;
ntfs.NtfsOptions.HideHiddenFiles = false;
ntfs.NtfsOptions.HideMetafiles = false;
// Remove VSS snapshot files (can be very large)
foreach (string filePath in ntfs.GetFiles(@"\System Volume Information", "*{3808876B-C176-4e48-B7AE-04046E6CC752}"))
{
ntfs.DeleteFile(filePath);
}
// Remove the page file
if (ntfs.FileExists(@"\Pagefile.sys"))
{
ntfs.DeleteFile(@"\Pagefile.sys");
}
// Remove the hibernation file
if (ntfs.FileExists(@"\hiberfil.sys"))
{
ntfs.DeleteFile(@"\hiberfil.sys");
}
using (Stream bitmapStream = ntfs.OpenFile(@"$Bitmap", FileMode.Open))
{
volBitmap = new byte[bitmapStream.Length];
int totalRead = 0;
int numRead = bitmapStream.Read(volBitmap, 0, volBitmap.Length - totalRead);
while (numRead > 0)
{
totalRead += numRead;
numRead = bitmapStream.Read(volBitmap, totalRead, volBitmap.Length - totalRead);
}
}
clusterSize = (int)ntfs.ClusterSize;
if (translation != GeometryTranslation.None)
{
ntfs.UpdateBiosGeometry(builder.BiosGeometry);
}
}
List<StreamExtent> extents = new List<StreamExtent>(BitmapToRanges(volBitmap, clusterSize));
SparseStream partSourceStream = SparseStream.FromStream(rawVolStream, Ownership.None, extents);
for (int i = 0; i < contentBuilder.PartitionTable.Partitions.Count; ++i)
{
var part = contentBuilder.PartitionTable.Partitions[i];
if (part.FirstSector * 512 == sv.SourceExtent.StartingOffset)
{
contentBuilder.SetPartitionContent(i, partSourceStream);
}
}
}
SparseStream contentStream = contentBuilder.Build();
// Write out the disk images
string dir = Path.GetDirectoryName(_destDisk.Value);
string file = Path.GetFileNameWithoutExtension(_destDisk.Value);
builder.Content = contentStream;
DiskImageFileSpecification[] fileSpecs = builder.Build(file);
for (int i = 0; i < fileSpecs.Length; ++i)
{
// Construct the destination file path from the directory of the primary file.
string outputPath = Path.Combine(dir, fileSpecs[i].Name);
// Force the primary file to the be one from the command-line.
if (i == 0)
{
outputPath = _destDisk.Value;
}
using (SparseStream vhdStream = fileSpecs[i].OpenStream())
using (FileStream fs = new FileStream(outputPath, FileMode.Create, FileAccess.ReadWrite))
{
StreamPump pump = new StreamPump()
{
InputStream = vhdStream,
OutputStream = fs,
};
long totalBytes = 0;
foreach (var se in vhdStream.Extents)
{
totalBytes += se.Length;
}
if (!Quiet)
{
Console.WriteLine();
DateTime now = DateTime.Now;
pump.ProgressEvent += (o, e) => { ShowProgress(fileSpecs[i].Name, totalBytes, now, o, e); };
}
pump.Run();
if (!Quiet)
{
Console.WriteLine();
}
}
}
// Complete - tidy up
CallAsyncMethod(backupCmpnts.BackupComplete);
long numDeleteFailed;
Guid deleteFailed;
backupCmpnts.DeleteSnapshots(snapshotSetId, 2 /*VSS_OBJECT_SNAPSHOT_SET*/, true, out numDeleteFailed, out deleteFailed);
Marshal.ReleaseComObject(backupCmpnts);
}
private static bool IsAdministrator()
{
WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
private static IEnumerable<StreamExtent> BitmapToRanges(byte[] bitmap, int bytesPerCluster)
{
long numClusters = bitmap.Length * 8;
long cluster = 0;
while (cluster < numClusters && !IsSet(bitmap, cluster))
{
++cluster;
}
while (cluster < numClusters)
{
long startCluster = cluster;
while (cluster < numClusters && IsSet(bitmap, cluster))
{
++cluster;
}
yield return new StreamExtent((long)(startCluster * (long)bytesPerCluster), (long)((cluster - startCluster) * (long)bytesPerCluster));
while (cluster < numClusters && !IsSet(bitmap, cluster))
{
++cluster;
}
}
}
private static bool IsSet(byte[] buffer, long bit)
{
int byteIdx = (int)(bit >> 3);
if (byteIdx >= buffer.Length)
{
return false;
}
byte val = buffer[byteIdx];
byte mask = (byte)(1 << (int)(bit & 0x7));
return (val & mask) != 0;
}
private List<CloneVolume> GatherVolumes(string[] sourceVolume, out uint diskNumber)
{
diskNumber = uint.MaxValue;
List<CloneVolume> cloneVolumes = new List<CloneVolume>(sourceVolume.Length);
if (!Quiet)
{
Console.WriteLine("Inspecting Volumes...");
}
for (int i = 0; i < sourceVolume.Length; ++i)
{
using (Volume vol = new Volume(sourceVolume[i], 0))
{
NativeMethods.DiskExtent[] sourceExtents = vol.GetDiskExtents();
if (sourceExtents.Length > 1)
{
Console.Error.WriteLine("Volume '{0}' is made up of multiple extents, which is not supported", sourceVolume[i]);
Environment.Exit(1);
}
if (diskNumber == uint.MaxValue)
{
diskNumber = sourceExtents[0].DiskNumber;
}
else if (diskNumber != sourceExtents[0].DiskNumber)
{
Console.Error.WriteLine("Specified volumes span multiple disks, which is not supported");
Environment.Exit(1);
}
string volPath = sourceVolume[i];
if (volPath[volPath.Length - 1] != '\\')
{
volPath += @"\";
}
cloneVolumes.Add(new CloneVolume { Path = volPath, SourceExtent = sourceExtents[0] });
}
}
return cloneVolumes;
}
private Guid CreateSnapshotSet(List<CloneVolume> cloneVolumes, IVssBackupComponents backupCmpnts)
{
if (!Quiet)
{
Console.WriteLine("Snapshotting Volumes...");
}
backupCmpnts.InitializeForBackup(null);
backupCmpnts.SetContext(0 /* VSS_CTX_BACKUP */);
backupCmpnts.SetBackupState(false, true, 5 /* VSS_BT_COPY */, false);
CallAsyncMethod(backupCmpnts.GatherWriterMetadata);
Guid snapshotSetId;
try
{
backupCmpnts.StartSnapshotSet(out snapshotSetId);
foreach (var vol in cloneVolumes)
{
backupCmpnts.AddToSnapshotSet(vol.Path, Guid.Empty, out vol.SnapshotId);
}
CallAsyncMethod(backupCmpnts.PrepareForBackup);
CallAsyncMethod(backupCmpnts.DoSnapshotSet);
}
catch
{
backupCmpnts.AbortBackup();
throw;
}
foreach (var vol in cloneVolumes)
{
vol.SnapshotProperties = GetSnapshotProperties(backupCmpnts, vol.SnapshotId);
}
return snapshotSetId;
}
private static VssSnapshotProperties GetSnapshotProperties(IVssBackupComponents backupComponents, Guid snapshotId)
{
VssSnapshotProperties props = new VssSnapshotProperties();
IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VssSnapshotProperties)));
backupComponents.GetSnapshotProperties(snapshotId, buffer);
Marshal.PtrToStructure(buffer, props);
NativeMethods.VssFreeSnapshotProperties(buffer);
return props;
}
private delegate void VssAsyncMethod(out IVssAsync result);
private static void CallAsyncMethod(VssAsyncMethod method)
{
IVssAsync async;
int reserved = 0;
uint hResult;
method(out async);
async.Wait(60 * 1000);
async.QueryStatus(out hResult, ref reserved);
if (hResult != 0 && hResult != 0x0004230a /* VSS_S_ASYNC_FINISHED */)
{
Marshal.ThrowExceptionForHR((int)hResult);
}
}
}
}