1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Text; |
4 |
using System.IO; |
5 |
using System.CodeDom.Compiler; |
6 |
using System.Windows.Forms; |
7 |
using System.Diagnostics; |
8 |
using log4net; |
9 |
|
10 |
namespace AnywhereTS |
11 |
{ |
12 |
// Represents an ATS image to be used by thin clients |
13 |
// Currrently only configuration data is stored and some configuration data is not included. |
14 |
public class ATSImage |
15 |
{ |
16 |
// Configuration data that can be changed runtime, after the client has booted |
17 |
public ATSImageRuntimeConfig RuntimeConfig; |
18 |
public ATSImageDesigntimeConfig DesigntimeConfig; |
19 |
public TempFileCollection objTempdir; // Temp file collection, to be deleted afterwards |
20 |
|
21 |
|
22 |
// Configuration data that can only be changed designtime, before the client has booted |
23 |
|
24 |
|
25 |
// Constructor |
26 |
public ATSImage() |
27 |
{ // Create the two types of configurations |
28 |
RuntimeConfig = new ATSImageRuntimeConfig(); |
29 |
DesigntimeConfig = new ATSImageDesigntimeConfig(); |
30 |
objTempdir = new TempFileCollection(); |
31 |
if (!Directory.Exists(objTempdir.BasePath)) |
32 |
{ |
33 |
Directory.CreateDirectory(objTempdir.BasePath); |
34 |
} |
35 |
} |
36 |
|
37 |
|
38 |
public bool WriteDesigntimeConfigFile(string pathfile) |
39 |
{ |
40 |
System.IO.StreamWriter writer = null; |
41 |
bool result = false; // Assume we have a problem. |
42 |
|
43 |
// Write a designtime new client config file. |
44 |
try |
45 |
{ |
46 |
writer = new StreamWriter(pathfile); |
47 |
writer.NewLine = "\n"; // Unix style line terminators |
48 |
writer.WriteLine("# This is a client file generated by AnywhereTS. Do not change."); |
49 |
DesigntimeConfig.WriteParameters(writer); |
50 |
RuntimeConfig.WriteParameters(writer); |
51 |
result = true; // It went ok. |
52 |
} |
53 |
finally |
54 |
{ |
55 |
writer.Close(); |
56 |
|
57 |
} |
58 |
return result; |
59 |
} |
60 |
|
61 |
public void ReadDefaultFromDatabase() |
62 |
{ |
63 |
RuntimeConfig.ReadDefaultFromDatabase(); |
64 |
DesigntimeConfig.ReadDefaultFromDatabase(); |
65 |
} |
66 |
|
67 |
public void WriteDefaultToDatabase() |
68 |
{ |
69 |
RuntimeConfig.WriteDefaultsToDatabase(); |
70 |
DesigntimeConfig.WriteDefaultsToDatabase(); |
71 |
} |
72 |
|
73 |
// Reset all properties that to the state they should have in a non-licensed version of ATS. |
74 |
// This is useful when switching from the Pro version to a free version image, to make |
75 |
// sure no pro features remain enabled. |
76 |
public void ResetProProperties() |
77 |
{ |
78 |
RuntimeConfig.ResetProProperties(); |
79 |
} |
80 |
|
81 |
|
82 |
public void Make() |
83 |
{ |
84 |
TempFileCollection objTempRoot = new TempFileCollection(); //Temp file collection, to be deleted afterwards |
85 |
|
86 |
// Clean temp collection, as there might be files here from previous runs |
87 |
// Needed when going back in wizard and rebuild |
88 |
if (objTempdir.Count > 0) |
89 |
{ |
90 |
objTempdir.Delete(); |
91 |
} |
92 |
|
93 |
string strBase; |
94 |
strBase = objTempRoot.BasePath; |
95 |
|
96 |
try |
97 |
{ |
98 |
objTempRoot.AddFile(strBase + @"\defaults", false); |
99 |
} |
100 |
catch { } |
101 |
|
102 |
// Create tree with selected drivers |
103 |
CopyGraphicsDrv(objTempRoot); |
104 |
CopyNetworkDrv(objTempRoot); |
105 |
CopySoundDrv(objTempRoot); |
106 |
|
107 |
// Copy the resolution support file |
108 |
File.Copy(DesigntimeConfig.DataDirectory + @"\modelines", strBase + @"\modelines", true); |
109 |
|
110 |
try |
111 |
{ |
112 |
WriteDesigntimeConfigFile(strBase + @"\defaults"); |
113 |
} |
114 |
catch (Exception e) |
115 |
{ |
116 |
MessageBox.Show("Error: Could not write configuration to disk (67732). Do you have sufficient rights? Error:" + e.Message); |
117 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString()))) |
118 |
{ |
119 |
Logging.ATSAdminLog.Error("Could not write configuration to disk (67732)"); |
120 |
} |
121 |
} |
122 |
|
123 |
// Save designtime parameters to database, so they will be remembered to next time |
124 |
DesigntimeConfig.WriteDefaultsToDatabase(); |
125 |
|
126 |
// Save defaults to database, so they can be used by new clients |
127 |
|
128 |
// If client is managed or there is no server in default settings, then update default settings |
129 |
ATSImageRuntimeConfig defaultRuntimeConfig = new ATSImageRuntimeConfig(); |
130 |
defaultRuntimeConfig.ReadDefaultFromDatabase(); |
131 |
if (defaultRuntimeConfig.ServerName == "" // There is no server name in default config, update default record. |
132 |
|| DesigntimeConfig.ConfigFile) // Client is managed, update default record |
133 |
{ |
134 |
RuntimeConfig.WriteDefaultsToDatabase(); |
135 |
ProSupport.WriteConfigFiles(RuntimeConfig, "network", true); |
136 |
} |
137 |
|
138 |
string strRoot = objTempRoot.BasePath; |
139 |
|
140 |
CreateInitrd(strRoot); |
141 |
objTempRoot.Delete(); |
142 |
|
143 |
} |
144 |
|
145 |
private void CopyGraphicsDrv(TempFileCollection objTempRoot) |
146 |
{ |
147 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
148 |
string strDriverDestDir; // The destination directory for all drivers |
149 |
strDriverDestDir = objTempRoot.BasePath + @"\x11_drv"; |
150 |
|
151 |
if (DesigntimeConfig.GraphicsDriver == "") |
152 |
{ // Driver autodetect selected |
153 |
// Copy all graphics driver files |
154 |
Directory.CreateDirectory(strDriverDestDir); |
155 |
CopyAllFiles(DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedGraphicsAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot); |
156 |
} |
157 |
else |
158 |
{ // Specific driver selected |
159 |
// Copy only the files for the selected graphics adapter |
160 |
Directory.CreateDirectory(strDriverDestDir); |
161 |
CopyFiles(DesigntimeConfig.GraphicsDriver, DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedGraphicsAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot); |
162 |
} |
163 |
} |
164 |
|
165 |
|
166 |
private void CopySoundDrv(TempFileCollection objTempRoot) |
167 |
{ |
168 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
169 |
string strDriverDestDir; // The destination directory for all drivers |
170 |
strDriverDestDir = objTempRoot.BasePath + @"\drivers"; |
171 |
|
172 |
if (DesigntimeConfig.SoundType == ATSImageDesigntimeConfig.ATSSoundType.ATSAutodetect) |
173 |
{ |
174 |
// Autodetect sound driver selected |
175 |
// Copy all sound driver files |
176 |
Directory.CreateDirectory(strDriverDestDir); |
177 |
CopyAllFiles(DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedSoundAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot); |
178 |
|
179 |
} |
180 |
else if (DesigntimeConfig.SoundType == ATSImageDesigntimeConfig.ATSSoundType.ATSSelectedDriver) |
181 |
{ |
182 |
// Specific sound driver selected, copy only the files for the selected sound adapter |
183 |
Directory.CreateDirectory(strDriverDestDir); |
184 |
CopyFiles(DesigntimeConfig.SoundDriver, DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedSoundAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot); |
185 |
} |
186 |
else |
187 |
{ |
188 |
// No sound |
189 |
} |
190 |
} |
191 |
|
192 |
private void CopyNetworkDrv(TempFileCollection objTempRoot) |
193 |
{ |
194 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
195 |
string strDriverDestDir; // The destination directory for all drivers |
196 |
strDriverDestDir = objTempRoot.BasePath + @"\drivers"; |
197 |
|
198 |
if (DesigntimeConfig.NetworkDriver == "") |
199 |
{ // Autodetect network driver selected |
200 |
// Copy all network driver files |
201 |
Directory.CreateDirectory(strDriverDestDir); |
202 |
CopyAllFiles(DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedNicAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot); |
203 |
} |
204 |
else |
205 |
{ // One network driver selcted, copy only the files for the selected network adapter |
206 |
Directory.CreateDirectory(strDriverDestDir); |
207 |
CopyFiles(DesigntimeConfig.NetworkDriver, DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedNicAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot); |
208 |
} |
209 |
} |
210 |
|
211 |
|
212 |
// Copies all files specified in strFile from strSourcePath to strDestPath. |
213 |
// The first paramter on each line is skipped the following parameters on the line is iterpreted as file names |
214 |
private void CopyAllFiles(string strFile, string strSourcePath, string strDestPath, TempFileCollection objTempRoot) |
215 |
{ |
216 |
using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(strFile)) |
217 |
{ |
218 |
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; |
219 |
MyReader.SetDelimiters(";"); |
220 |
string[] currentRow; |
221 |
while (!MyReader.EndOfData) |
222 |
{ |
223 |
try |
224 |
{ |
225 |
currentRow = MyReader.ReadFields(); |
226 |
for (int i = 2; i <= currentRow.GetUpperBound(0); i++) |
227 |
{ |
228 |
if (!File.Exists(strDestPath + @"\" + currentRow[i])) |
229 |
{ |
230 |
File.Copy(strSourcePath + @"\" + currentRow[i], strDestPath + @"\" + currentRow[i], true); |
231 |
try |
232 |
{ |
233 |
objTempRoot.AddFile(strDestPath + @"\" + currentRow[i], false); |
234 |
} |
235 |
catch { } |
236 |
} |
237 |
} |
238 |
} |
239 |
catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) |
240 |
{ |
241 |
MessageBox.Show("Error in file " + strFile + ", line: '" + ex.Message + "'is not valid and will be skipped (77224)."); |
242 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString()))) |
243 |
{ |
244 |
using (log4net.NDC.Push(string.Format("file={0} line={1}", strFile, ex.Message))) |
245 |
{ |
246 |
Logging.ATSAdminLog.Error("Encountered invalid file"); |
247 |
} |
248 |
} |
249 |
} |
250 |
} // While |
251 |
} // using |
252 |
} // CopyAllFiles |
253 |
|
254 |
|
255 |
// Does a look-up in the file strFile on the line numbered by intIndex (zero based), then skips the first parameter on the line, |
256 |
// and interprets all the following parameters on the row as files to be copied from strSourcePath to strDestPath |
257 |
private void CopyFiles(string strDriver, string strFile, string strSourcePath, string strDestPath, TempFileCollection objTempRoot) |
258 |
{ |
259 |
using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(strFile)) |
260 |
{ |
261 |
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; |
262 |
MyReader.SetDelimiters(";"); |
263 |
string[] currentRow; |
264 |
currentRow = new string[2]; |
265 |
currentRow[1] = ""; |
266 |
// Read rows until we found the row |
267 |
while (!MyReader.EndOfData && currentRow[1] != strDriver) |
268 |
{ |
269 |
try |
270 |
{ |
271 |
currentRow = MyReader.ReadFields(); |
272 |
|
273 |
} |
274 |
catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) |
275 |
{ |
276 |
MessageBox.Show("Error in file " + strFile + ", line: '" + ex.Message + "'is not valid and will be skipped."); |
277 |
using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString()))) |
278 |
{ |
279 |
using (log4net.NDC.Push(string.Format("file={0} line={1}", strFile, ex.Message))) |
280 |
{ |
281 |
Logging.ATSAdminLog.Error("Encountered invalid file"); |
282 |
} |
283 |
} |
284 |
} |
285 |
} // While |
286 |
// We have found the right row |
287 |
for (int i = 2; i <= currentRow.GetUpperBound(0); i++) |
288 |
{ |
289 |
File.Copy(strSourcePath + @"\" + currentRow[i], strDestPath + @"\" + currentRow[i], true); |
290 |
try |
291 |
{ |
292 |
objTempRoot.AddFile(strDestPath + @"\" + currentRow[i], false); |
293 |
} |
294 |
catch { } |
295 |
} |
296 |
} // using |
297 |
} // CopyFiles |
298 |
|
299 |
public string KernelAppendString() |
300 |
{ |
301 |
string strString; |
302 |
|
303 |
strString = "initrd=initrd load_ramdisk=1 ramdisk_blocksize=4096 console=ttyS3 root=/dev/ram0 ramdisk_size=262144"; |
304 |
|
305 |
// color depth | 640x480 800x600 1024x768 1280x1024 |
306 |
// 256 (8bit)| 769 771 773 775 |
307 |
// 32000 (15bit)| 784 787 790 793 |
308 |
// 65000 (16bit)| 785 788 791 794 |
309 |
// 16.7 Mil. (24bit)| 786 789 792 795 |
310 |
|
311 |
if ((DesigntimeConfig.BootPicture.Type == 1) || (DesigntimeConfig.BootPicture.Type > 10)) |
312 |
{ // BootImage selected |
313 |
|
314 |
strString = strString + " splash=silent"; |
315 |
switch (DesigntimeConfig.BootPicture.Width) |
316 |
{ |
317 |
case 640: |
318 |
strString += " vga=785"; // 640x480 |
319 |
break; |
320 |
case 800: |
321 |
strString += " vga=788"; // 800x600 |
322 |
break; |
323 |
case 1024: |
324 |
strString += " vga=791"; // 1024x768 |
325 |
break; |
326 |
case 1280: |
327 |
strString += " vga=794"; // 1280x1024 |
328 |
break; |
329 |
default: |
330 |
MessageBox.Show("ERROR unknown boot picture resolution (49488)"); |
331 |
strString += " vga=791"; // Default to 1024x768 |
332 |
|
333 |
using (log4net.NDC.Push(string.Format("width=", DesigntimeConfig.BootPicture.Width))) |
334 |
{ |
335 |
Logging.ATSAdminLog.Error("ERROR unknown boot picture resolution (49488)"); |
336 |
} |
337 |
|
338 |
break; |
339 |
} |
340 |
} |
341 |
return strString; |
342 |
} |
343 |
|
344 |
// Create an ISO file containing a bootable client |
345 |
public void CreateClientISO() |
346 |
{ |
347 |
CreateISO(objTempdir.BasePath + @"\initrd"); |
348 |
} |
349 |
|
350 |
public void CreateISO(string pathInitrd) |
351 |
{ // pathInitrd = Path to the initrd file that should be put on the CD |
352 |
string strCDroot = objTempdir.BasePath + @"\cd"; |
353 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
354 |
CreateClientRoot(pathInitrd, strCDroot); |
355 |
|
356 |
// Lauch mkisofs.exe to create the ISO |
357 |
Process p = new Process(); |
358 |
p.StartInfo = new ProcessStartInfo(strAppdir + @"\mkisofs\mkisofs.exe"); |
359 |
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
360 |
p.StartInfo.Arguments = @" -o " + @"""" + objTempdir.BasePath + @"\client.iso""" + " -b isolinux.bin -f -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table " + @"""" + strCDroot + @""""; |
361 |
p.Start(); |
362 |
p.WaitForExit(); |
363 |
try |
364 |
{ |
365 |
objTempdir.AddFile(objTempdir.BasePath + @"\client.iso", false); |
366 |
} |
367 |
catch {} |
368 |
} |
369 |
|
370 |
public void CreateEtherboot() |
371 |
{ |
372 |
string strBase; |
373 |
|
374 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
375 |
strBase = objTempdir.BasePath; |
376 |
|
377 |
// Launch wraplinux.exe to create the etherboot image |
378 |
Process p = new Process(); |
379 |
p.StartInfo = new ProcessStartInfo(strAppdir + @"\tools\wraplinux.exe"); |
380 |
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
381 |
p.StartInfo.Arguments = @"-p " + @"""" + KernelAppendString() + @"""" + @" -i " + @"""" + strBase + @"\initrd" + @"""" + @" -N -o " + @"""" + strBase + @"\client" + @"""" + " " + @"""" + DesigntimeConfig.DataDirectory + @"\vmlinuz" + @""""; |
382 |
p.Start(); |
383 |
p.WaitForExit(); |
384 |
if (!File.Exists(strBase + @"\client")) |
385 |
{ |
386 |
MessageBox.Show("Internal error: Failed to create Network Boot Image. Aborting (22399).", "AnywhereTS", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); |
387 |
|
388 |
using (log4net.NDC.Push(string.Format("image=", strBase + @"\client"))) |
389 |
{ |
390 |
Logging.ATSAdminLog.Error("Failed to create Network Boot Image."); |
391 |
} |
392 |
|
393 |
Application.Exit(); |
394 |
} |
395 |
try |
396 |
{ |
397 |
objTempdir.AddFile(strBase + @"\client", false); |
398 |
} |
399 |
catch { } |
400 |
} |
401 |
|
402 |
|
403 |
// Create client root |
404 |
// Creates a directory containing a client, made from an initrd file. |
405 |
// This directory can be put on CD, Hard disk or USB. |
406 |
// All that is then needed is a booter, that varies with the media |
407 |
// IN pathIntird: Path to the Initrd file that contains the client |
408 |
// IN strClientDirectory: Path to the directory where the client will be put. If not existing, directory will be created. |
409 |
public void CreateClientRoot (string pathInitrd, string strClientDirectory) |
410 |
{ // pathInitrd = Path to the initrd file that should be put on the CD |
411 |
string strBase; |
412 |
|
413 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
414 |
strBase = objTempdir.BasePath; |
415 |
|
416 |
// Make a temp directory for the client root |
417 |
Directory.CreateDirectory(strClientDirectory); |
418 |
|
419 |
// Create the isolinux.cfg file |
420 |
System.IO.StreamWriter writer = null; |
421 |
bool writeok = false; // Assume we have a problem. |
422 |
|
423 |
string isolinuxCfg = strClientDirectory + @"\isolinux.cfg"; // Path to isolinux.cfg |
424 |
|
425 |
// Write the isolinux.cfg file. |
426 |
try |
427 |
{ |
428 |
writer = new StreamWriter(isolinuxCfg); |
429 |
writer.NewLine = "\n"; // Unix style line terminators |
430 |
writer.WriteLine(@"timeout 0"); |
431 |
writer.WriteLine(@"default vmlinuz"); |
432 |
writer.WriteLine(@"append " + KernelAppendString()); |
433 |
writeok = true; // It went ok. |
434 |
} |
435 |
finally |
436 |
{ |
437 |
writer.Close(); |
438 |
} |
439 |
|
440 |
if (writeok) |
441 |
{ |
442 |
try |
443 |
{ |
444 |
// There was no prevoius version of the file, add it to the temp collection. |
445 |
objTempdir.AddFile(isolinuxCfg, false); |
446 |
} |
447 |
catch { } |
448 |
} |
449 |
else |
450 |
{ |
451 |
MessageBox.Show("Error cannot write to file (10065)", "AnywhereTS"); |
452 |
return; |
453 |
} |
454 |
|
455 |
// Copy the files from the template directory to the temp directory |
456 |
File.Copy(DesigntimeConfig.DataDirectory + @"\vmlinuz", strClientDirectory + @"\vmlinuz", true); |
457 |
try |
458 |
{ |
459 |
objTempdir.AddFile(strClientDirectory + @"\vmlinuz", false); |
460 |
} |
461 |
catch |
462 |
{} // File already exists in the collection |
463 |
|
464 |
File.Copy(DesigntimeConfig.DataDirectory + @"\isolinux.bin", strClientDirectory + @"\isolinux.bin", true); |
465 |
try |
466 |
{ |
467 |
objTempdir.AddFile(strClientDirectory + @"\isolinux.bin", false); |
468 |
} |
469 |
catch |
470 |
{ } // File already exists in the collection |
471 |
|
472 |
File.Copy(pathInitrd, strClientDirectory + @"\initrd", true); |
473 |
try |
474 |
{ |
475 |
objTempdir.AddFile(strClientDirectory + @"\initrd", false); |
476 |
} |
477 |
catch |
478 |
{ } // File already exists in the collection |
479 |
|
480 |
|
481 |
} |
482 |
|
483 |
public void CreateCfg(string strCfgName,string strDestPath) |
484 |
{ |
485 |
// Create the isolinux.cfg file |
486 |
System.IO.StreamWriter writer = null; |
487 |
bool writeok = false; // Assume we have a problem. |
488 |
string isolinuxCfg = strDestPath + @"\" + strCfgName; // Path and filename |
489 |
|
490 |
// Write the isolinux.cfg file. |
491 |
try |
492 |
{ |
493 |
writer = new StreamWriter(isolinuxCfg); |
494 |
writer.NewLine = "\n"; // Unix style line terminators |
495 |
writer.WriteLine(@"timeout 0"); |
496 |
writer.WriteLine(@"default vmlinuz"); |
497 |
writer.WriteLine(@"append " + KernelAppendString()); |
498 |
writeok = true; // It went ok. |
499 |
} |
500 |
finally |
501 |
{ |
502 |
writer.Close(); |
503 |
} |
504 |
|
505 |
if (writeok) |
506 |
{ |
507 |
try |
508 |
{ |
509 |
// Add it to the temp collection. |
510 |
objTempdir.AddFile(isolinuxCfg, false); |
511 |
} |
512 |
catch { } |
513 |
} |
514 |
else |
515 |
{ |
516 |
MessageBox.Show("Error cannot write to file (10565)", "AnywhereTS"); |
517 |
|
518 |
using (log4net.NDC.Push(string.Format("file={0}", isolinuxCfg))) |
519 |
{ |
520 |
Logging.ATSAdminLog.Error("Cannot write to file"); |
521 |
} |
522 |
|
523 |
return; |
524 |
} |
525 |
} |
526 |
|
527 |
// Create an initrd file |
528 |
// strRoot: |
529 |
public void CreateInitrd(string strRoot) |
530 |
{ |
531 |
string strBase; |
532 |
|
533 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
534 |
strBase = objTempdir.BasePath; |
535 |
|
536 |
File.Copy(DesigntimeConfig.DataDirectory + @"\xorg", strBase + @"\initrd", true); |
537 |
try |
538 |
{ |
539 |
objTempdir.AddFile(strBase + @"\initrd", false); |
540 |
} |
541 |
catch { } |
542 |
|
543 |
// Lauch mksqfs.exe.exe to append files to initrd |
544 |
Process p = new Process(); |
545 |
p.StartInfo = new ProcessStartInfo(strAppdir + @"\mksquash\mksqfs.exe"); |
546 |
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
547 |
p.StartInfo.Arguments = @"""" + strRoot + @"""" + " " + @"""" + strBase + @"\initrd" + @""""; |
548 |
p.Start(); |
549 |
p.WaitForExit(); |
550 |
} |
551 |
|
552 |
// Create an ISO file containing a hard disk boot client including a hard disk writer that writes the client to hard disk |
553 |
public void CreateHardDiskBootISO() |
554 |
{ |
555 |
string strBase, strHDroot; |
556 |
|
557 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
558 |
strBase = objTempdir.BasePath; |
559 |
|
560 |
// Make a temp directory for the HD root |
561 |
strHDroot = strBase + @"\hd"; |
562 |
Directory.CreateDirectory(strHDroot); |
563 |
|
564 |
// Write a signature to the initrd file, to make it bootable with ATS protection |
565 |
WriteHarddiskBootSignature(strBase + @"\initrd"); |
566 |
|
567 |
// Copy HDimage to HDimage directory |
568 |
File.Copy(strAppdir + @"\tools\hdimage", strHDroot + @"\initrd", true); |
569 |
try |
570 |
{ |
571 |
objTempdir.AddFile(strHDroot + @"\initrd", false); |
572 |
} |
573 |
catch { } |
574 |
// Make a syslinux.cfg to be added to hard disk |
575 |
CreateCfg(@"\syslinux.cfg", strBase); |
576 |
|
577 |
// Add Initrd to HDImage |
578 |
// Lauch mksqfs.exe.exe to add initrd file to hdboot |
579 |
Process p = new Process(); |
580 |
p.StartInfo = new ProcessStartInfo(strAppdir + @"\mksquash\mksqfs.exe"); |
581 |
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
582 |
p.StartInfo.Arguments = @"""" + strBase + @"\initrd" + @"""" + " " + @"""" + strBase + @"\syslinux.cfg" + @"""" + " " + @"""" + strHDroot + @"\initrd" + @""""; |
583 |
p.Start(); |
584 |
p.WaitForExit(); |
585 |
|
586 |
// Prepare CD with an initrd that is the HD burner |
587 |
CreateISO(strHDroot + @"\initrd"); |
588 |
} |
589 |
|
590 |
// Make a hard disk boot client including a hard disk writer that writes the client to hard disk, to be written to an USB drive |
591 |
// pathUsbRoot = The path to the USB removable drive that the client will be written to. Should end with a backslash! |
592 |
// Return: true = went ok. False = error. |
593 |
public void CreateHardDiskBootUSB() |
594 |
{ |
595 |
string strBase, strHDroot; |
596 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
597 |
strBase = objTempdir.BasePath; |
598 |
|
599 |
string strPathTempUsbRoot = objTempdir.BasePath + @"\usb"; // Temp Directory for the USB root files. |
600 |
|
601 |
// Make a temp directory for the HD root |
602 |
strHDroot = strBase + @"\hd"; |
603 |
Directory.CreateDirectory(strHDroot); |
604 |
|
605 |
// Make a syslinux.cfg to be added to hard disk |
606 |
CreateCfg(@"\syslinux.cfg", strBase); |
607 |
|
608 |
// Write a signature to the har disk boot initrd file, to make it bootable |
609 |
WriteHarddiskBootSignature(strBase + @"\initrd"); |
610 |
|
611 |
// Copy HDimage to HDimage directory |
612 |
File.Copy(strAppdir + @"\tools\hdimage", strHDroot + @"\initrd", true); |
613 |
try |
614 |
{ |
615 |
objTempdir.AddFile(strHDroot + @"\initrd", false); |
616 |
} |
617 |
catch { } |
618 |
|
619 |
// Add Initrd to HDImage |
620 |
// Lauch mksqfs.exe.exe to add initrd file to hdboot |
621 |
Process p = new Process(); |
622 |
p.StartInfo = new ProcessStartInfo(strAppdir + @"\mksquash\mksqfs.exe"); |
623 |
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; |
624 |
p.StartInfo.Arguments = @"""" + strBase + @"\initrd" + @"""" + " " + @"""" + strBase + @"\syslinux.cfg" + @"""" + " " + @"""" + strHDroot + @"\initrd" + @""""; |
625 |
|
626 |
p.Start(); |
627 |
p.WaitForExit(); |
628 |
|
629 |
// Write a signature to the har USB boot initrd file, to make it bootable |
630 |
WriteHarddiskBootSignature(strHDroot + @"\initrd"); |
631 |
|
632 |
CreateClientRoot(strHDroot + @"\initrd", strPathTempUsbRoot); |
633 |
} |
634 |
|
635 |
// Make a bootable USB memory that contains a hard disk boot client including a hard disk writer that writes the client to hard disk. |
636 |
// pathUsbRoot = The path to the USB removable drive that the client will be written to. Should end with a backslash! |
637 |
// Return: true = went ok. False = error. |
638 |
// Assumes the files are in objTempdir.BasePath + @"\usb" |
639 |
public bool WriteHardDiskBootUSB(string pathUsbMemoryRoot) |
640 |
{ |
641 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
642 |
string strPathTempUsbRoot = objTempdir.BasePath + @"\usb"; // Temp Directory for the USB root files. |
643 |
|
644 |
// Copy client root fles to USB |
645 |
try |
646 |
{ |
647 |
File.Copy(strPathTempUsbRoot + @"\initrd", pathUsbMemoryRoot + @"initrd", true); |
648 |
File.Copy(strPathTempUsbRoot + @"\isolinux.cfg", pathUsbMemoryRoot + @"syslinux.cfg", true); // On a HD/USB, the name should be syslinux.cfg |
649 |
File.Copy(strPathTempUsbRoot + @"\vmlinuz", pathUsbMemoryRoot + @"vmlinuz", true); |
650 |
// Make USB bootable |
651 |
MakeUsbMemoryBootable(pathUsbMemoryRoot); |
652 |
} |
653 |
catch |
654 |
{ |
655 |
MessageBox.Show("Error: Could write to removable drive. Please check write protection and rights.", "AnywhereTS"); |
656 |
return false; |
657 |
} |
658 |
|
659 |
return true; |
660 |
} |
661 |
|
662 |
// Install a bootble AnywhereTS client to a USB memory. |
663 |
// pathUsbRoot = The path to the USB removable drive that the client will be written to. |
664 |
public void CreateUSBBootedClient() |
665 |
{ |
666 |
string strPathTempUsbRoot = objTempdir.BasePath + @"\usb"; |
667 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
668 |
|
669 |
// Write a signature to the USB boot initrd file, to make it bootable |
670 |
WriteHarddiskBootSignature(objTempdir.BasePath + @"\initrd"); |
671 |
|
672 |
CreateClientRoot(objTempdir.BasePath + @"\initrd", strPathTempUsbRoot); |
673 |
} |
674 |
|
675 |
// Install a bootble AnywhereTS client to a USB memory. |
676 |
// pathUsbRoot = The path to the USB removable drive that the client will be written to. |
677 |
// Return: true = went ok. False = error. |
678 |
// Source files will be found in objTempdir.BasePath + @"\usb |
679 |
public bool WriteUSBBootedClient(string pathUsbMemoryRoot) |
680 |
{ |
681 |
string strPathTempUsbRoot = objTempdir.BasePath + @"\usb"; |
682 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
683 |
// Copy client root fles to USB |
684 |
try |
685 |
{ |
686 |
File.Copy(strPathTempUsbRoot + @"\initrd", pathUsbMemoryRoot + @"initrd", true); |
687 |
File.Copy(strPathTempUsbRoot + @"\isolinux.cfg", pathUsbMemoryRoot + @"syslinux.cfg", true); // On a HD/USB, the name should be syslinux.cfg |
688 |
File.Copy(strPathTempUsbRoot + @"\vmlinuz", pathUsbMemoryRoot + @"vmlinuz", true); |
689 |
// Make USB bootable |
690 |
MakeUsbMemoryBootable(pathUsbMemoryRoot); |
691 |
} |
692 |
catch |
693 |
{ |
694 |
//MessageBox.Show(ex.ToString()+ " " + ex.Message); |
695 |
MessageBox.Show("Error: Could not write to removable drive. Please check write protection and rights.", "AnywhereTS"); |
696 |
return false; |
697 |
} |
698 |
return true; |
699 |
} |
700 |
|
701 |
|
702 |
// Writes a boot sector to an USB flash memory |
703 |
public void MakeUsbMemoryBootable(string pathUsbMemoryRoot) |
704 |
{ |
705 |
string strAppdir = Path.GetDirectoryName(Application.ExecutablePath); |
706 |
string strUsbDrive = pathUsbMemoryRoot.Remove(2); |
707 |
// Lauch syslinux.exe to write boot sector to USB drive |
708 |
Process p = new Process(); |
709 |
p.StartInfo = new ProcessStartInfo(strAppdir + @"\tools\syslinux.exe"); |
710 |
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal; |
711 |
p.StartInfo.Arguments = @"-m -a -d /boot/syslinux " + strUsbDrive; |
712 |
p.Start(); |
713 |
if (!p.WaitForExit(20000)) // Wait 20 sek for the process to terminate |
714 |
{ // Process terminated because of timeout |
715 |
MessageBox.Show("Error: Could not make removable drive bootable (27611)", ATSGlobals.ApplicationName); |
716 |
p.Close(); |
717 |
|
718 |
} |
719 |
} |
720 |
|
721 |
// Appends a a two byte signature to a client that should be booted from harddisk. |
722 |
// This acts as a license protection for harddisk boot, and eliminates the possibility of copying a free client to a hard disk. |
723 |
// IN strPathInitrd: Path to the initrd file that the signature should be appended to |
724 |
private void WriteHarddiskBootSignature(string strPathInitrd) |
725 |
{ |
726 |
{ // Append the image file to the inird file |
727 |
// Create a signature in memory |
728 |
byte[] fileSignature = new byte[2]; |
729 |
fileSignature[0] = 19; |
730 |
fileSignature[1] = 71; |
731 |
// Append to initrd file |
732 |
FileStream objDest = File.Open(strPathInitrd, FileMode.Append, FileAccess.Write); |
733 |
objDest.Write(fileSignature,0,2); |
734 |
objDest.Close(); |
735 |
} |
736 |
} |
737 |
|
738 |
} // ATSImage |
739 |
} // Namespace |