package org.openslx.util.vm;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.Virtualizer;
public class VboxMetaData extends VmMetaData
{
private static final Logger LOGGER = Logger.getLogger( VboxMetaData.class );
private static final Virtualizer virtualizer = new Virtualizer( "virtualbox", "VirtualBox" );
private final VboxConfig config;
public VboxMetaData( List<OperatingSystem> osList, File file ) throws IOException, UnsupportedVirtualizerFormatException
{
super( osList );
this.config = new VboxConfig( file );
init();
}
public VboxMetaData( List<OperatingSystem> osList, byte[] vmContent, int length ) throws IOException, UnsupportedVirtualizerFormatException
{
super( osList );
this.config = new VboxConfig( vmContent, length );
setTypeOf();
init();
}
private void init()
{
this.config.init();
displayName = config.getDisplayName();
setOs( "virtualbox", config.getOsName() );
for ( HardDisk hardDisk : config.getHdds() ) {
hdds.add( hardDisk );
}
try {
addFloppy( 0, null, true );
WriteToFile();
} catch ( TransformerFactoryConfigurationError | TransformerException e ) {
// WriteToFile exceptions here...not important for the the LOGGER
e.printStackTrace();
}
}
// --TODO will be needed later again
private void WriteToFile() throws TransformerFactoryConfigurationError, TransformerException
{
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StreamResult output = new StreamResult( new File( "output.xml" ) );
Source input = new DOMSource( config.getConfigDoc() );
transformer.transform( input, output );
}
@Override
public Virtualizer getVirtualizer()
{
return new Virtualizer( "virtualbox", "VirtualBox" );
}
@Override
public void enableUsb( boolean enabled )
{
// TODO Auto-generated method stub
}
@Override
public void applySettingsForLocalEdit()
{
// TODO Auto-generated method stub
}
@Override
public byte[] getFilteredDefinitionArray()
{
return config.toString().getBytes( StandardCharsets.UTF_8 );
}
@Override
public boolean addHddTemplate( String diskImage, String hddMode, String redoDir ) {
config.changeAttribute( "HardDisk", "location", diskImage );
return true;
}
@Override
public boolean addHddTemplate( File diskImage, String hddMode, String redoDir )
{
String diskImagePath = diskImage.getName();
config.changeAttribute( "HardDisk", "location", diskImagePath );
UUID newuuid = UUID.randomUUID();
LOGGER.debug( newuuid );
// patching the new uuid in the vbox config file here
String vboxUUid = "{" + newuuid.toString() + "}";
config.changeAttribute( "HardDisk", "uuid", vboxUUid );
config.changeAttribute( "Image", "uuid", vboxUUid );
Long longl = newuuid.getLeastSignificantBits();
Long longr = newuuid.getMostSignificantBits();
byte l56 = (byte) (longl >>> 56);
byte l48 = (byte) (longl >>> 48);
byte l40 = (byte) (longl >>> 40);
byte l32 = (byte) (longl >>> 32);
byte l24 = (byte) (longl >>> 24);
byte l16 = (byte) (longl >>> 16);
byte l8 = (byte) (longl >>> 8);
byte l0 = (byte) (longl >>> 0);
// switcharoo next 4
byte r56 = (byte) (longr >>> 32);
byte r48 = (byte) (longr >>> 40);
byte r40 = (byte) (longr >>> 48);
byte r32 = (byte) (longr >>> 56);
// switcharoo next 2
byte r24 = (byte) (longr >>> 16);
byte r16 = (byte) (longr >>> 24);
// switcharoo next 2
byte r8 = (byte) (longr >>> 0);
byte r0 = (byte) (longr >>> 8);
byte[] bytesToWrite = {r56, r48, r40, r32, r24, r16, r8, r0, l56, l48, l40, l32, l24, l16, l8, l0};
try( RandomAccessFile file = new RandomAccessFile( diskImage, "rw" ) ) {
file.seek( 392 );
file.write( bytesToWrite, 0, 16 );
} catch (Exception e) {
LOGGER.warn( "could not patch new uuid in the vdi", e );
}
return true;
}
@Override
public boolean addDefaultNat()
{
config.addNewNode( "Adapter", "NAT", false );
return true;
}
public void reWrite()
{
try {
WriteToFile();
} catch ( TransformerFactoryConfigurationError | TransformerException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void setOs( String vendorOsId )
{
// TODO Auto-generated method stub
}
@Override
public boolean addDisplayName( String name )
{
config.changeAttribute( "Machine", "name", name );
return true;
}
@Override
public boolean addRam( int mem )
{
config.changeAttribute( "Memory", "RAMSize", Integer.toString( mem ) );
return true;
}
@Override
public void addFloppy( int index, String image, boolean readOnly )
{
if ( image == null ) {
} else {
}
}
@Override
public boolean addCdrom( String image )
{
// TODO Auto-generated method stub
return false;
}
@Override
public void setTypeOf()
{
typeOf = TypeOf.VBOX;
}
}