/[AnywhereTS-MSSQL]/trunk/TSAdminTool/Image.cs
ViewVC logotype

Annotation of /trunk/TSAdminTool/Image.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4 - (hide annotations) (download)
Wed Jul 11 14:19:13 2012 UTC (8 years, 11 months ago) by william
File size: 31326 byte(s)
imported from https://anywherets.svn.sourceforge.net/svnroot/anywherets/trunk/
Revision: 1
Author: beartown
Date: 3:10:40 AM, Monday, June 21, 2010
Message:
First upload. Still todo in order to get a fully working product: Replace Thinstation images with vanilla Thinstation, modify ImageDesigntimeConfig.cs, ImageRuntimeConfig.cs in order to support the Thinstation image
1 william 4 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    
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     }
118    
119     // Save designtime parameters to database, so they will be remembered to next time
120     DesigntimeConfig.WriteDefaultsToDatabase();
121    
122     // Save defaults to database, so they can be used by new clients
123    
124     // If client is managed or there is no server in default settings, then update default settings
125     ATSImageRuntimeConfig defaultRuntimeConfig = new ATSImageRuntimeConfig();
126     defaultRuntimeConfig.ReadDefaultFromDatabase();
127     if (defaultRuntimeConfig.ServerName == "" // There is no server name in default config, update default record.
128     || DesigntimeConfig.ConfigFile) // Client is managed, update default record
129     {
130     RuntimeConfig.WriteDefaultsToDatabase();
131     ProSupport.WriteConfigFiles(RuntimeConfig, "network", true);
132     }
133    
134     string strRoot = objTempRoot.BasePath;
135    
136     CreateInitrd(strRoot);
137     objTempRoot.Delete();
138    
139     }
140    
141     private void CopyGraphicsDrv(TempFileCollection objTempRoot)
142     {
143     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
144     string strDriverDestDir; // The destination directory for all drivers
145     strDriverDestDir = objTempRoot.BasePath + @"\x11_drv";
146    
147     if (DesigntimeConfig.GraphicsDriver == "")
148     { // Driver autodetect selected
149     // Copy all graphics driver files
150     Directory.CreateDirectory(strDriverDestDir);
151     CopyAllFiles(DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedGraphicsAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot);
152     }
153     else
154     { // Specific driver selected
155     // Copy only the files for the selected graphics adapter
156     Directory.CreateDirectory(strDriverDestDir);
157     CopyFiles(DesigntimeConfig.GraphicsDriver, DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedGraphicsAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot);
158     }
159     }
160    
161    
162     private void CopySoundDrv(TempFileCollection objTempRoot)
163     {
164     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
165     string strDriverDestDir; // The destination directory for all drivers
166     strDriverDestDir = objTempRoot.BasePath + @"\drivers";
167    
168     if (DesigntimeConfig.SoundType == ATSImageDesigntimeConfig.ATSSoundType.ATSAutodetect)
169     {
170     // Autodetect sound driver selected
171     // Copy all sound driver files
172     Directory.CreateDirectory(strDriverDestDir);
173     CopyAllFiles(DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedSoundAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot);
174    
175     }
176     else if (DesigntimeConfig.SoundType == ATSImageDesigntimeConfig.ATSSoundType.ATSSelectedDriver)
177     {
178     // Specific sound driver selected, copy only the files for the selected sound adapter
179     Directory.CreateDirectory(strDriverDestDir);
180     CopyFiles(DesigntimeConfig.SoundDriver, DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedSoundAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot);
181     }
182     else
183     {
184     // No sound
185     }
186     }
187    
188     private void CopyNetworkDrv(TempFileCollection objTempRoot)
189     {
190     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
191     string strDriverDestDir; // The destination directory for all drivers
192     strDriverDestDir = objTempRoot.BasePath + @"\drivers";
193    
194     if (DesigntimeConfig.NetworkDriver == "")
195     { // Autodetect network driver selected
196     // Copy all network driver files
197     Directory.CreateDirectory(strDriverDestDir);
198     CopyAllFiles(DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedNicAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot);
199     }
200     else
201     { // One network driver selcted, copy only the files for the selected network adapter
202     Directory.CreateDirectory(strDriverDestDir);
203     CopyFiles(DesigntimeConfig.NetworkDriver, DesigntimeConfig.DataDirectory + @"\" + ATSGlobals.SelectedNicAdaptersFile, DesigntimeConfig.DataDirectory, strDriverDestDir, objTempRoot);
204     }
205     }
206    
207    
208     // Copies all files specified in strFile from strSourcePath to strDestPath.
209     // The first paramter on each line is skipped the following parameters on the line is iterpreted as file names
210     private void CopyAllFiles(string strFile, string strSourcePath, string strDestPath, TempFileCollection objTempRoot)
211     {
212     using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(strFile))
213     {
214     MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
215     MyReader.SetDelimiters(";");
216     string[] currentRow;
217     while (!MyReader.EndOfData)
218     {
219     try
220     {
221     currentRow = MyReader.ReadFields();
222     for (int i = 2; i <= currentRow.GetUpperBound(0); i++)
223     {
224     if (!File.Exists(strDestPath + @"\" + currentRow[i]))
225     {
226     File.Copy(strSourcePath + @"\" + currentRow[i], strDestPath + @"\" + currentRow[i], true);
227     try
228     {
229     objTempRoot.AddFile(strDestPath + @"\" + currentRow[i], false);
230     }
231     catch { }
232     }
233     }
234     }
235     catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
236     {
237     MessageBox.Show("Error in file " + strFile + ", line: '" + ex.Message + "'is not valid and will be skipped (77224).");
238     }
239     } // While
240     } // using
241     } // CopyAllFiles
242    
243    
244     // Does a look-up in the file strFile on the line numbered by intIndex (zero based), then skips the first parameter on the line,
245     // and interprets all the following parameters on the row as files to be copied from strSourcePath to strDestPath
246     private void CopyFiles(string strDriver, string strFile, string strSourcePath, string strDestPath, TempFileCollection objTempRoot)
247     {
248     using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = new Microsoft.VisualBasic.FileIO.TextFieldParser(strFile))
249     {
250     MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
251     MyReader.SetDelimiters(";");
252     string[] currentRow;
253     currentRow = new string[2];
254     currentRow[1] = "";
255     // Read rows until we found the row
256     while (!MyReader.EndOfData && currentRow[1] != strDriver)
257     {
258     try
259     {
260     currentRow = MyReader.ReadFields();
261    
262     }
263     catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
264     {
265     MessageBox.Show("Error in file " + strFile + ", line: '" + ex.Message + "'is not valid and will be skipped.");
266     }
267     } // While
268     // We have found the right row
269     for (int i = 2; i <= currentRow.GetUpperBound(0); i++)
270     {
271     File.Copy(strSourcePath + @"\" + currentRow[i], strDestPath + @"\" + currentRow[i], true);
272     try
273     {
274     objTempRoot.AddFile(strDestPath + @"\" + currentRow[i], false);
275     }
276     catch { }
277     }
278     } // using
279     } // CopyFiles
280    
281     public string KernelAppendString()
282     {
283     string strString;
284    
285     strString = "initrd=initrd load_ramdisk=1 ramdisk_blocksize=4096 console=ttyS3 root=/dev/ram0 ramdisk_size=262144";
286    
287     // color depth | 640x480 800x600 1024x768 1280x1024
288     // 256 (8bit)| 769 771 773 775
289     // 32000 (15bit)| 784 787 790 793
290     // 65000 (16bit)| 785 788 791 794
291     // 16.7 Mil. (24bit)| 786 789 792 795
292    
293     if ((DesigntimeConfig.BootPicture.Type == 1) || (DesigntimeConfig.BootPicture.Type > 10))
294     { // BootImage selected
295    
296     strString = strString + " splash=silent";
297     switch (DesigntimeConfig.BootPicture.Width)
298     {
299     case 640:
300     strString += " vga=785"; // 640x480
301     break;
302     case 800:
303     strString += " vga=788"; // 800x600
304     break;
305     case 1024:
306     strString += " vga=791"; // 1024x768
307     break;
308     case 1280:
309     strString += " vga=794"; // 1280x1024
310     break;
311     default:
312     MessageBox.Show("ERROR unknown boot picture resolution (49488)");
313     strString += " vga=791"; // Default to 1024x768
314     break;
315     }
316     }
317     return strString;
318     }
319    
320     // Create an ISO file containing a bootable client
321     public void CreateClientISO()
322     {
323     CreateISO(objTempdir.BasePath + @"\initrd");
324     }
325    
326     public void CreateISO(string pathInitrd)
327     { // pathInitrd = Path to the initrd file that should be put on the CD
328     string strCDroot = objTempdir.BasePath + @"\cd";
329     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
330     CreateClientRoot(pathInitrd, strCDroot);
331    
332     // Lauch mkisofs.exe to create the ISO
333     Process p = new Process();
334     p.StartInfo = new ProcessStartInfo(strAppdir + @"\mkisofs\mkisofs.exe");
335     p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
336     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 + @"""";
337     p.Start();
338     p.WaitForExit();
339     try
340     {
341     objTempdir.AddFile(objTempdir.BasePath + @"\client.iso", false);
342     }
343     catch {}
344     }
345    
346     public void CreateEtherboot()
347     {
348     string strBase;
349    
350     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
351     strBase = objTempdir.BasePath;
352    
353     // Launch wraplinux.exe to create the etherboot image
354     Process p = new Process();
355     p.StartInfo = new ProcessStartInfo(strAppdir + @"\tools\wraplinux.exe");
356     p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
357     p.StartInfo.Arguments = @"-p " + @"""" + KernelAppendString() + @"""" + @" -i " + @"""" + strBase + @"\initrd" + @"""" + @" -N -o " + @"""" + strBase + @"\client" + @"""" + " " + @"""" + DesigntimeConfig.DataDirectory + @"\vmlinuz" + @"""";
358     p.Start();
359     p.WaitForExit();
360     if (!File.Exists(strBase + @"\client"))
361     {
362     MessageBox.Show("Internal error: Failed to create Network Boot Image. Aborting (22399).", "AnywhereTS", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
363     Application.Exit();
364     }
365     try
366     {
367     objTempdir.AddFile(strBase + @"\client", false);
368     }
369     catch { }
370     }
371    
372    
373     // Create client root
374     // Creates a directory containing a client, made from an initrd file.
375     // This directory can be put on CD, Hard disk or USB.
376     // All that is then needed is a booter, that varies with the media
377     // IN pathIntird: Path to the Initrd file that contains the client
378     // IN strClientDirectory: Path to the directory where the client will be put. If not existing, directory will be created.
379     public void CreateClientRoot (string pathInitrd, string strClientDirectory)
380     { // pathInitrd = Path to the initrd file that should be put on the CD
381     string strBase;
382    
383     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
384     strBase = objTempdir.BasePath;
385    
386     // Make a temp directory for the client root
387     Directory.CreateDirectory(strClientDirectory);
388    
389     // Create the isolinux.cfg file
390     System.IO.StreamWriter writer = null;
391     bool writeok = false; // Assume we have a problem.
392    
393     string isolinuxCfg = strClientDirectory + @"\isolinux.cfg"; // Path to isolinux.cfg
394    
395     // Write the isolinux.cfg file.
396     try
397     {
398     writer = new StreamWriter(isolinuxCfg);
399     writer.NewLine = "\n"; // Unix style line terminators
400     writer.WriteLine(@"timeout 0");
401     writer.WriteLine(@"default vmlinuz");
402     writer.WriteLine(@"append " + KernelAppendString());
403     writeok = true; // It went ok.
404     }
405     finally
406     {
407     writer.Close();
408     }
409    
410     if (writeok)
411     {
412     try
413     {
414     // There was no prevoius version of the file, add it to the temp collection.
415     objTempdir.AddFile(isolinuxCfg, false);
416     }
417     catch { }
418     }
419     else
420     {
421     MessageBox.Show("Error cannot write to file (10065)", "AnywhereTS");
422     return;
423     }
424    
425     // Copy the files from the template directory to the temp directory
426     File.Copy(DesigntimeConfig.DataDirectory + @"\vmlinuz", strClientDirectory + @"\vmlinuz", true);
427     try
428     {
429     objTempdir.AddFile(strClientDirectory + @"\vmlinuz", false);
430     }
431     catch
432     {} // File already exists in the collection
433    
434     File.Copy(DesigntimeConfig.DataDirectory + @"\isolinux.bin", strClientDirectory + @"\isolinux.bin", true);
435     try
436     {
437     objTempdir.AddFile(strClientDirectory + @"\isolinux.bin", false);
438     }
439     catch
440     { } // File already exists in the collection
441    
442     File.Copy(pathInitrd, strClientDirectory + @"\initrd", true);
443     try
444     {
445     objTempdir.AddFile(strClientDirectory + @"\initrd", false);
446     }
447     catch
448     { } // File already exists in the collection
449    
450    
451     }
452    
453     public void CreateCfg(string strCfgName,string strDestPath)
454     {
455     // Create the isolinux.cfg file
456     System.IO.StreamWriter writer = null;
457     bool writeok = false; // Assume we have a problem.
458     string isolinuxCfg = strDestPath + @"\" + strCfgName; // Path and filename
459    
460     // Write the isolinux.cfg file.
461     try
462     {
463     writer = new StreamWriter(isolinuxCfg);
464     writer.NewLine = "\n"; // Unix style line terminators
465     writer.WriteLine(@"timeout 0");
466     writer.WriteLine(@"default vmlinuz");
467     writer.WriteLine(@"append " + KernelAppendString());
468     writeok = true; // It went ok.
469     }
470     finally
471     {
472     writer.Close();
473     }
474    
475     if (writeok)
476     {
477     try
478     {
479     // Add it to the temp collection.
480     objTempdir.AddFile(isolinuxCfg, false);
481     }
482     catch { }
483     }
484     else
485     {
486     MessageBox.Show("Error cannot write to file (10565)", "AnywhereTS");
487     return;
488     }
489     }
490    
491     // Create an initrd file
492     // strRoot:
493     public void CreateInitrd(string strRoot)
494     {
495     string strBase;
496    
497     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
498     strBase = objTempdir.BasePath;
499    
500     File.Copy(DesigntimeConfig.DataDirectory + @"\xorg", strBase + @"\initrd", true);
501     try
502     {
503     objTempdir.AddFile(strBase + @"\initrd", false);
504     }
505     catch { }
506    
507     // Lauch mksqfs.exe.exe to append files to initrd
508     Process p = new Process();
509     p.StartInfo = new ProcessStartInfo(strAppdir + @"\mksquash\mksqfs.exe");
510     p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
511     p.StartInfo.Arguments = @"""" + strRoot + @"""" + " " + @"""" + strBase + @"\initrd" + @"""";
512     p.Start();
513     p.WaitForExit();
514     }
515    
516     // Create an ISO file containing a hard disk boot client including a hard disk writer that writes the client to hard disk
517     public void CreateHardDiskBootISO()
518     {
519     string strBase, strHDroot;
520    
521     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
522     strBase = objTempdir.BasePath;
523    
524     // Make a temp directory for the HD root
525     strHDroot = strBase + @"\hd";
526     Directory.CreateDirectory(strHDroot);
527    
528     // Write a signature to the initrd file, to make it bootable with ATS protection
529     WriteHarddiskBootSignature(strBase + @"\initrd");
530    
531     // Copy HDimage to HDimage directory
532     File.Copy(strAppdir + @"\tools\hdimage", strHDroot + @"\initrd", true);
533     try
534     {
535     objTempdir.AddFile(strHDroot + @"\initrd", false);
536     }
537     catch { }
538     // Make a syslinux.cfg to be added to hard disk
539     CreateCfg(@"\syslinux.cfg", strBase);
540    
541     // Add Initrd to HDImage
542     // Lauch mksqfs.exe.exe to add initrd file to hdboot
543     Process p = new Process();
544     p.StartInfo = new ProcessStartInfo(strAppdir + @"\mksquash\mksqfs.exe");
545     p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
546     p.StartInfo.Arguments = @"""" + strBase + @"\initrd" + @"""" + " " + @"""" + strBase + @"\syslinux.cfg" + @"""" + " " + @"""" + strHDroot + @"\initrd" + @"""";
547     p.Start();
548     p.WaitForExit();
549    
550     // Prepare CD with an initrd that is the HD burner
551     CreateISO(strHDroot + @"\initrd");
552     }
553    
554     // 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
555     // pathUsbRoot = The path to the USB removable drive that the client will be written to. Should end with a backslash!
556     // Return: true = went ok. False = error.
557     public void CreateHardDiskBootUSB()
558     {
559     string strBase, strHDroot;
560     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
561     strBase = objTempdir.BasePath;
562    
563     string strPathTempUsbRoot = objTempdir.BasePath + @"\usb"; // Temp Directory for the USB root files.
564    
565     // Make a temp directory for the HD root
566     strHDroot = strBase + @"\hd";
567     Directory.CreateDirectory(strHDroot);
568    
569     // Make a syslinux.cfg to be added to hard disk
570     CreateCfg(@"\syslinux.cfg", strBase);
571    
572     // Write a signature to the har disk boot initrd file, to make it bootable
573     WriteHarddiskBootSignature(strBase + @"\initrd");
574    
575     // Copy HDimage to HDimage directory
576     File.Copy(strAppdir + @"\tools\hdimage", strHDroot + @"\initrd", true);
577     try
578     {
579     objTempdir.AddFile(strHDroot + @"\initrd", false);
580     }
581     catch { }
582    
583     // Add Initrd to HDImage
584     // Lauch mksqfs.exe.exe to add initrd file to hdboot
585     Process p = new Process();
586     p.StartInfo = new ProcessStartInfo(strAppdir + @"\mksquash\mksqfs.exe");
587     p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
588     p.StartInfo.Arguments = @"""" + strBase + @"\initrd" + @"""" + " " + @"""" + strBase + @"\syslinux.cfg" + @"""" + " " + @"""" + strHDroot + @"\initrd" + @"""";
589    
590     p.Start();
591     p.WaitForExit();
592    
593     // Write a signature to the har USB boot initrd file, to make it bootable
594     WriteHarddiskBootSignature(strHDroot + @"\initrd");
595    
596     CreateClientRoot(strHDroot + @"\initrd", strPathTempUsbRoot);
597     }
598    
599     // Make a bootable USB memory that contains a hard disk boot client including a hard disk writer that writes the client to hard disk.
600     // pathUsbRoot = The path to the USB removable drive that the client will be written to. Should end with a backslash!
601     // Return: true = went ok. False = error.
602     // Assumes the files are in objTempdir.BasePath + @"\usb"
603     public bool WriteHardDiskBootUSB(string pathUsbMemoryRoot)
604     {
605     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
606     string strPathTempUsbRoot = objTempdir.BasePath + @"\usb"; // Temp Directory for the USB root files.
607    
608     // Copy client root fles to USB
609     try
610     {
611     File.Copy(strPathTempUsbRoot + @"\initrd", pathUsbMemoryRoot + @"initrd", true);
612     File.Copy(strPathTempUsbRoot + @"\isolinux.cfg", pathUsbMemoryRoot + @"syslinux.cfg", true); // On a HD/USB, the name should be syslinux.cfg
613     File.Copy(strPathTempUsbRoot + @"\vmlinuz", pathUsbMemoryRoot + @"vmlinuz", true);
614     // Make USB bootable
615     MakeUsbMemoryBootable(pathUsbMemoryRoot);
616     }
617     catch
618     {
619     MessageBox.Show("Error: Could write to removable drive. Please check write protection and rights.", "AnywhereTS");
620     return false;
621     }
622    
623     return true;
624     }
625    
626     // Install a bootble AnywhereTS client to a USB memory.
627     // pathUsbRoot = The path to the USB removable drive that the client will be written to.
628     public void CreateUSBBootedClient()
629     {
630     string strPathTempUsbRoot = objTempdir.BasePath + @"\usb";
631     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
632    
633     // Write a signature to the USB boot initrd file, to make it bootable
634     WriteHarddiskBootSignature(objTempdir.BasePath + @"\initrd");
635    
636     CreateClientRoot(objTempdir.BasePath + @"\initrd", strPathTempUsbRoot);
637     }
638    
639     // Install a bootble AnywhereTS client to a USB memory.
640     // pathUsbRoot = The path to the USB removable drive that the client will be written to.
641     // Return: true = went ok. False = error.
642     // Source files will be found in objTempdir.BasePath + @"\usb
643     public bool WriteUSBBootedClient(string pathUsbMemoryRoot)
644     {
645     string strPathTempUsbRoot = objTempdir.BasePath + @"\usb";
646     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
647     // Copy client root fles to USB
648     try
649     {
650     File.Copy(strPathTempUsbRoot + @"\initrd", pathUsbMemoryRoot + @"initrd", true);
651     File.Copy(strPathTempUsbRoot + @"\isolinux.cfg", pathUsbMemoryRoot + @"syslinux.cfg", true); // On a HD/USB, the name should be syslinux.cfg
652     File.Copy(strPathTempUsbRoot + @"\vmlinuz", pathUsbMemoryRoot + @"vmlinuz", true);
653     // Make USB bootable
654     MakeUsbMemoryBootable(pathUsbMemoryRoot);
655     }
656     catch
657     {
658     //MessageBox.Show(ex.ToString()+ " " + ex.Message);
659     MessageBox.Show("Error: Could not write to removable drive. Please check write protection and rights.", "AnywhereTS");
660     return false;
661     }
662     return true;
663     }
664    
665    
666     // Writes a boot sector to an USB flash memory
667     public void MakeUsbMemoryBootable(string pathUsbMemoryRoot)
668     {
669     string strAppdir = Path.GetDirectoryName(Application.ExecutablePath);
670     string strUsbDrive = pathUsbMemoryRoot.Remove(2);
671     // Lauch syslinux.exe to write boot sector to USB drive
672     Process p = new Process();
673     p.StartInfo = new ProcessStartInfo(strAppdir + @"\tools\syslinux.exe");
674     p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
675     p.StartInfo.Arguments = @"-m -a -d /boot/syslinux " + strUsbDrive;
676     p.Start();
677     if (!p.WaitForExit(20000)) // Wait 20 sek for the process to terminate
678     { // Process terminated because of timeout
679     MessageBox.Show("Error: Could not make removable drive bootable (27611)", ATSGlobals.ApplicationName);
680     p.Close();
681    
682     }
683     }
684    
685     // Appends a a two byte signature to a client that should be booted from harddisk.
686     // This acts as a license protection for harddisk boot, and eliminates the possibility of copying a free client to a hard disk.
687     // IN strPathInitrd: Path to the initrd file that the signature should be appended to
688     private void WriteHarddiskBootSignature(string strPathInitrd)
689     {
690     { // Append the image file to the inird file
691     // Create a signature in memory
692     byte[] fileSignature = new byte[2];
693     fileSignature[0] = 19;
694     fileSignature[1] = 71;
695     // Append to initrd file
696     FileStream objDest = File.Open(strPathInitrd, FileMode.Append, FileAccess.Write);
697     objDest.Write(fileSignature,0,2);
698     objDest.Close();
699     }
700     }
701    
702     } // ATSImage
703     } // Namespace

  ViewVC Help
Powered by ViewVC 1.1.22