+
+ // 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( "Successful 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( "Successful 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!" ) ;
+ }
+ }
+