X-Git-Url: http://bilbo.iut-bm.univ-fcomte.fr/pub/gitweb/hpcvm.git/blobdiff_plain/a5033b66e150c1f5a03008abb71408b1700337aa..d83793da6e881ebab006a50a47fbba914e65f9cf:/src/and/hpcvm/VirtualMachine.java diff --git a/src/and/hpcvm/VirtualMachine.java b/src/and/hpcvm/VirtualMachine.java index 1cba1f1..95a6683 100644 --- a/src/and/hpcvm/VirtualMachine.java +++ b/src/and/hpcvm/VirtualMachine.java @@ -1,6 +1,10 @@ package and.hpcvm ; import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; @@ -12,8 +16,8 @@ public class VirtualMachine private String initial_archive_name ; private String directory ; private String vmx_name ; - private String vmx_name_normal ; - private String vmx_name_crash ; + private String vmx_name_first ; + private String vmx_name_initial ; private String save_current ; private String save_last ; private int no_save ; @@ -25,6 +29,9 @@ public class VirtualMachine private String clientInVM ; private String vm_user ; private String vm_user_passwd ; + private boolean deploy ; + private boolean first ; + private boolean deployFault ; public VirtualMachine() { @@ -33,8 +40,8 @@ public class VirtualMachine initial_archive_name = "VmTest.tgz" ; directory = "VmTest" ; vmx_name = "VmTest.vmx" ; - vmx_name_normal = "VmTest.vmx.normal" ; - vmx_name_crash = "VmTest.vmx.crash" ; + vmx_name_first = "VmTest.vmx.first" ; + vmx_name_initial = "VmTest.vmx.initial" ; save_current = "VmTest.tgz" ; save_last = "VmTest.tgz" ; working_directory = "/localhome/vmware" ; @@ -47,6 +54,15 @@ public class VirtualMachine clientInVM = "/home/mpi/InGuest" ; vm_user = "mpi" ; vm_user_passwd = "mpi" ; + first = false ; + deploy = false ; + deployFault = false ; + } + + + public void setDeployFault( boolean _bool ) + { + deployFault = _bool ; } @@ -170,21 +186,21 @@ public class VirtualMachine this.vmx_name = vmx_name; } - public String getVmx_name_normal() { - return vmx_name_normal; - } +// public String getVmx_name_normal() { +// return vmx_name_normal; +// } - public void setVmx_name_normal(String vmx_name_normal) { - this.vmx_name_normal = vmx_name_normal; - } +// public void setVmx_name_normal(String vmx_name_normal) { +// this.vmx_name_normal = vmx_name_normal; +// } - public String getVmx_name_crash() { - return vmx_name_crash; - } +// public String getVmx_name_crash() { +// return vmx_name_crash; +// } - public void setVmx_name_crash(String vmx_name_crash) { - this.vmx_name_crash = vmx_name_crash; - } +// public void setVmx_name_crash(String vmx_name_crash) { +// this.vmx_name_crash = vmx_name_crash; +// } public String getSave_current() { return save_current; @@ -230,7 +246,7 @@ public class VirtualMachine { computation_id = _id ; } - + public int deployLastSave() { @@ -256,18 +272,8 @@ public class VirtualMachine { System.out.println( "Successful deletion of current VM." ) ; } else { - System.err.println( "Error: " + pr.exitValue() ) ; - BufferedReader b = new BufferedReader( new InputStreamReader( pr.getErrorStream() ) ) ; - - String l ; - try { - while( (l = b.readLine()) != null ) - { - System.err.println( l ) ; - } - } catch( IOException e ) { - e.printStackTrace() ; - } + System.err.println( "Unssuccessful deletion of current VM!" ) ; + printProcessError( pr ) ; } System.out.print( "Deploying the last save ... " ) ; @@ -295,22 +301,575 @@ public class VirtualMachine System.out.println( "Successful extraction of the save archive." ) ; return 0 ; } else { - System.err.println( "Error: " + pr.exitValue() ) ; - BufferedReader b = new BufferedReader( new InputStreamReader( pr.getErrorStream() ) ) ; + System.err.println( "unSuccessful extraction of the save archive!" ) ; + printProcessError( pr ) ; + } + + return 1 ; + } + + + public int deployInitialVM() + { + System.out.print( "Removing current VM ... " ) ; + + String[] command = new String[] { "/bin/rm", "-rf", + working_directory + "/" + directory } ; + Process pr = null ; + try { + pr = Runtime.getRuntime().exec( command ) ; +// synchronized( pr ){ + pr.waitFor() ; //;} + } catch( IOException e ) { + System.err.println( "Error while removing current VM!" ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + if( pr.exitValue() == 0 ) + { + System.out.println( "Successful deletion of current VM." ) ; + } else { + System.err.println( "Unsuccessful deletion of current VM!" ) ; + printProcessError( pr ) ; + } + + System.out.print( "Deploying the initial archive ... " ) ; + + command = new String[] { "/bin/tar", "-xzf", + working_directory + "/" + save_last, + "-C", working_directory } ; + pr = null ; + try { + pr = Runtime.getRuntime().exec( command ) ; +// synchronized( pr ){ + pr.waitFor() ; //;} + } catch( IOException e ) { + System.err.println( "Error while deploying the initial archive!" ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + + if( pr.exitValue() == 0 ) + { + System.out.println( "Successful extraction of the initial archive." ) ; + deploy = true ; + } else { + System.err.println( "Unsuccessful extraction of the initial archive!" ) ; + printProcessError( pr ) ; + return 1 ; + } + + try { + FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/initial.hpcvm" ) ) ; + fw.write( "initial!" ) ; + fw.flush() ; + fw.close() ; + fw = null ; + return 0 ; + } catch( IOException e1 ) { + e1.printStackTrace() ; + System.err.println( "Unable to mark the initial deployment!" ) ; + } + + return 1 ; + } + + + public int checkVmx() + { + // Initial deployment + File file = new File( working_directory + "/" + directory + "/initial.hpcvm" ) ; + + if( file.exists() ) + { + deploy = true ; + } + + // First execution + file = null ; + file = new File( working_directory + "/" + directory + "/first.hpcvm" ) ; + + if( file.exists() ) + { + first = true ; + } + + // Redeployment after a fault + file = null ; + file = new File( working_directory + "/" + directory + "/fault.hpcvm" ) ; + + if( file.exists() ) + { + deploy = false ; + first = false ; + deployFault = true ; + } + + file = null ; + + // Deployment of the VM + + if( deploy ) + { + System.out.print( "Saving the initial VMX file ... " ) ; + + String[] command = new String[]{ "/bin/cp", + working_directory + "/" + directory + "/" + vmx_name, + working_directory + "/" + directory + "/" + vmx_name_initial } ; - String l ; try { - while( (l = b.readLine()) != null ) + Process p = Runtime.getRuntime().exec( command ) ; + p.waitFor() ; + + if( p.exitValue() == 0 ) { - System.err.println( l ) ; + System.out.println( "Successfully saved initial the VMX file." ) ; + } else { + System.err.println( "Unsuccessful save of the initial VMX file!" ) ; + printProcessError( p ) ; + return 1 ; } } catch( IOException e ) { - e.printStackTrace() ; + System.err.println( "Error during initial VMX file save: " ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + System.out.print( "Rewritting WMX file ... " ) ; + + + FileReader fin = null ; + try { + fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_initial ) ); + } catch( FileNotFoundException e ) { + System.err.println( "Unable to open the initial VMX file!" ) ; + e.printStackTrace() ; + return 1 ; } + + BufferedReader bin = new BufferedReader( fin ) ; + String line = "" ; + FileWriter fout = null ; + + try { + fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ; + } catch( IOException e ) { + System.err.println( "Unable to open the VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + // Keeping only some information + // -- this force VmWare to generate new identifiers and MAC address + while( line != null ) + { + try { + line = bin.readLine() ; + if( line != null ) + if( ! line.contains( "ethernet0.addressType" ) + && ! line.contains( "uuid.location " ) + && ! line.contains( "uuid.bios " ) + && ! line.contains( "ethernet0.generatedAddress " ) + && ! line.contains( "ethernet0.generatedAddressOffset " ) + ) + { + fout.write( line + "\n" ) ; + } + } catch( IOException e ) {} + } + + try { + bin.close() ; + } catch (IOException e) { + System.err.println( "Unable to close the initial VMX file!" ) ; + e.printStackTrace() ; + } + + // Adding the copy information + try + { + fout.write( "msg.autoAnswer = \"TRUE\"\n" ) ; + fout.write( "answer.msg.checkpoint.cpufeaturecheck.fail = \"_Yes\"\n" ) ; + fout.write( "checkpoint.vmState = \"\"\n" ) ; + fout.write( "answer.msg.uuid.altered = \"I _copied it\"\n" ) ; + } catch( IOException e ) { + System.err.println( "Unable to add infortion to VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + try { + fout.close() ; + } catch( IOException e ) { + System.err.println( "Unable to close the VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + System.out.println( "Successful rewrite of the VMX file." ) ; + + // Removing the initial mark + System.out.print( "Removing the deployment mark ... " ) ; + + command = new String[]{ "/bin/rm", + working_directory + "/" + directory + "/initial.hpcvm" } ; + + try { + Process p = Runtime.getRuntime().exec( command ) ; + p.waitFor() ; + + if( p.exitValue() == 0 ) + { + System.out.println( "Successfully deletion of the deployment mark." ) ; + } else { + System.err.println( "Unsuccessful deletion of the deployment mark!" ) ; + printProcessError( p ) ; + return 1 ; + } + } catch( IOException e ) { + System.err.println( "Error during deletion of the deployment mark: " ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + deploy = false ; + + // Writing the first start mark + try { + FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ; + fw.write( "first!\n" ) ; + fw.flush() ; + fw.close() ; + fw = null ; + return 0 ; + } catch( IOException e1 ) { + e1.printStackTrace() ; + System.err.println( "Unable to mark the first run of the VM!" ) ; + } + } + + + // First start of the VM + + if( first ) + { + System.out.print( "Saving the first start VMX file ... " ) ; + + String[] command = new String[]{ "/bin/cp", + working_directory + "/" + directory + "/" + vmx_name, + working_directory + "/" + directory + "/" + vmx_name_first } ; + + try { + Process p = Runtime.getRuntime().exec( command ) ; + p.waitFor() ; + + if( p.exitValue() == 0 ) + { + System.out.println( "Successfully saved the first start VMX file." ) ; + } else { + System.err.println( "Unsuccessful save of the first start VMX file!" ) ; + printProcessError( p ) ; + return 1 ; + } + } catch( IOException e ) { + System.err.println( "Error during first start VMX file save: " ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + System.out.print( "Rewritting WMX file ... " ) ; + + + FileReader fin = null ; + try { + fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) ); + } catch( FileNotFoundException e ) { + System.err.println( "Unable to open the first start VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + BufferedReader bin = new BufferedReader( fin ) ; + String line = "" ; + FileWriter fout = null ; + + try { + fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ; + } catch( IOException e ) { + System.err.println( "Unable to open the VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + // Keeping all information + // -- except copy information + while( line != null ) + { + try { + line = bin.readLine() ; + if( line != null ) + if( ! line.contains( "answer.msg.uuid.altered " ) ) + { + fout.write( line + "\n" ) ; + } + } catch( IOException e ) {} + } + + try { + bin.close() ; + } catch (IOException e) { + System.err.println( "Unable to close the first start VMX file!" ) ; + e.printStackTrace() ; + } + + try { + fout.close() ; + } catch( IOException e ) { + System.err.println( "Unable to close the VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + System.out.println( "Successful rewrite of the VMX file." ) ; + + // Removing the initial mark + System.out.print( "Removing the first start mark ... " ) ; + + command = new String[]{ "/bin/rm", + working_directory + "/" + directory + "/first.hpcvm" } ; + + try { + Process p = Runtime.getRuntime().exec( command ) ; + p.waitFor() ; + + if( p.exitValue() == 0 ) + { + System.out.println( "Successfully deletion of the first start mark." ) ; + } else { + System.err.println( "Unsuccessful deletion of the first start mark!" ) ; + printProcessError( p ) ; + return 1 ; + } + } catch( IOException e ) { + System.err.println( "Error during deletion of the first start mark: " ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + first = false ; + + return 0 ; } + + // Redeployment after a fault of a VM + + if( deployFault ) + { + System.out.print( "Saving the original VMX file ... " ) ; + + String[] command = new String[]{ "/bin/cp", + working_directory + "/" + directory + "/" + vmx_name, + working_directory + "/" + directory + "/" + vmx_name_first } ; + + try { + Process p = Runtime.getRuntime().exec( command ) ; + p.waitFor() ; + + if( p.exitValue() == 0 ) + { + System.out.println( "Successfully saved the original VMX file." ) ; + } else { + System.err.println( "Unsuccessful save of the original VMX file!" ) ; + printProcessError( p ) ; + return 1 ; + } + } catch( IOException e ) { + System.err.println( "Error during original VMX file save: " ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + System.out.print( "Rewritting WMX file ... " ) ; + + + FileReader fin = null ; + try { + fin = new FileReader( new File( working_directory + "/" + directory + "/" + vmx_name_first ) ); + } catch( FileNotFoundException e ) { + System.err.println( "Unable to open the original VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + BufferedReader bin = new BufferedReader( fin ) ; + String line = "" ; + FileWriter fout = null ; + + try { + fout = new FileWriter( new File( working_directory + "/" + directory + "/" + vmx_name ) ) ; + } catch( IOException e ) { + System.err.println( "Unable to open the VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + // Keeping all information + while( line != null ) + { + try { + line = bin.readLine() ; + if( line != null ) + if( ! line.contains( "answer.msg.uuid.altered " ) ) + { + fout.write( line + "\n" ) ; + } + } catch( IOException e ) {} + } + + try { + bin.close() ; + } catch (IOException e) { + System.err.println( "Unable to close the original VMX file!" ) ; + e.printStackTrace() ; + } + + // Writing information to indicate that the VM has been moved + try { + fout.write( "answer.msg.uuid.altered = \"I _moved it\"\n" ) ; + } catch (IOException e1) { + System.err.println( "Unable to indicate that the VM has been moved in the VMX file!" ) ; + e1.printStackTrace() ; + } + + try { + fout.close() ; + } catch( IOException e ) { + System.err.println( "Unable to close the VMX file!" ) ; + e.printStackTrace() ; + return 1 ; + } + + System.out.println( "Successful rewrite of the VMX file." ) ; + + // Removing lock files + System.out.print( "Removing lock files ... " ) ; + + command = new String[]{ "/bin/rm", "-rf", + working_directory + "/" + directory + + "/" + vmx_name + ".lck" } ; + + try { + Process p = Runtime.getRuntime().exec( command ) ; + p.waitFor() ; + + if( p.exitValue() == 0 ) + { + System.out.println( "Successfully deleted lock files." ) ; + } else { + System.err.println( "Unsuccessful deletion of lock files!" ) ; +// printProcessError( p.getErrorStream() ) ; + printProcessError( p ) ; + + return 1 ; + } + } catch( IOException e ) { + System.err.println( "Error during lock files deletion: " ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + // Removing the initial mark + System.out.print( "Removing the fault mark ... " ) ; + + command = new String[]{ "/bin/rm", + working_directory + "/" + directory + "/fault.hpcvm" } ; + + try { + Process p = Runtime.getRuntime().exec( command ) ; + p.waitFor() ; + + if( p.exitValue() == 0 ) + { + System.out.println( "Successfully deletion of the fault mark." ) ; + } else { + System.err.println( "Unsuccessful deletion of the fault mark!" ) ; + printProcessError( p ) ; + return 1 ; + } + } catch( IOException e ) { + System.err.println( "Error during deletion of the fault mark: " ) ; + e.printStackTrace() ; + return 1 ; + } catch( InterruptedException e ) { + e.printStackTrace() ; + return 1 ; + } + + deployFault = false ; + + // Writing the first start mark + try { + FileWriter fw = new FileWriter( new File( working_directory + "/" + directory + "/first.hpcvm" ) ) ; + fw.write( "first!" ) ; + fw.flush() ; + fw.close() ; + fw = null ; + first = true ; + return 0 ; + } catch( IOException e1 ) { + e1.printStackTrace() ; + System.err.println( "Unable to mark the first run of the VM!" ) ; + } + } + return 1 ; } + + + private void printProcessError( Process _p ) + { + if( _p != null ) + { + System.err.println( "Error: " + _p.exitValue() ) ; + BufferedReader br = new BufferedReader( new InputStreamReader( _p.getErrorStream() ) ) ; + String line ; + try { + while( (line = br.readLine()) != null ) + { + System.err.println( line ) ; + } + } catch( IOException e ) { + e.printStackTrace() ; + } + } + } }