* 'master' of http://github.com/andreaturli/jclouds:
  README improved
  fix runNodeWithTagAndNameAndStoreCredentials: it shoud run the domain not only define it
  improvement on libvirt
This commit is contained in:
Adrian Cole 2010-11-02 09:04:03 -07:00
commit 90e83c256a
6 changed files with 166 additions and 85 deletions

View File

@ -1,2 +1,73 @@
The libvirt library is used to interface with different virtualization technologies (http://libvirt.org/)
libvirt supports:
The Xen hypervisor on Linux and Solaris hosts.
The QEMU emulator
The KVM Linux hypervisor
The LXC Linux container system
The OpenVZ Linux container system
The User Mode Linux paravirtualized kernel
The VirtualBox hypervisor
The VMware ESX and GSX hypervisors
Storage on IDE/SCSI/USB disks, FibreChannel, LVM, iSCSI, NFS and filesystems
Getting Started Guide for jclouds-libvirt
install libvirt on your os install libvirt on your os
* if os/x, see http://github.com/justinclift/libvirt * if os/x, see http://github.com/justinclift/libvirt
* if you are using Linux, let's suppose you want to use KVM:
- install libvirt and KVM (http://www.linux-kvm.org/page/Main_Page).
Remember to run
egrep '(vmx|svm)' /proc/cpuinfo
If nothing is printed, it means that your cpu does not support hardware virtualization.
Verify Installation
$ virsh -c qemu:///system list
Id Name State
----------------------------------
(for Ubuntu users: look also at this good turorial https://help.ubuntu.com/community/KVM)
Create your first guest
- download, for example, an ubuntu 10.04 LTS ISO
- create a libvirt domain by using:
virt-manager: a GUI tool at http://virt-manager.et.redhat.com/
virt-install, a python script developed by Red Hat (sudo apt-get install python-virtinst)
ubuntu-vm-builder, developed by Canonical. (sudo apt-get install ubuntu-vm-builder)
NB: use Javascript tool that generates the parameters for ubuntu-vm-builder: http://people.ubuntu.com/~kirkland/ubuntu-vm-builder.html
Now that you have a libvirt domain, your workstation is ready to use jclouds-libvirt.
You can now download jclouds-libvirt and give a try by running
ComputeServiceContext context = null;
try {
context = new ComputeServiceContextFactory()
.createContext(new StandaloneComputeServiceContextSpec<Domain, Domain, Image, Datacenter>("libvirt",
endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet
.<Module> of()));
Template defaultTemplate = context.getComputeService().templateBuilder()
.hardwareId("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1").build();
context.getComputeService().runNodesWithTag(domainName, 1, defaultTemplate);
} catch (RunNodesException e) {
e.printStackTrace();
} finally {
if (context != null)
context.close();
}
where identity=your_name, endpoint=qemu:///system
and domainName equals to the name chosen during the creation of libvirt domain
NB: apiversion, credential can be null

View File

@ -29,6 +29,7 @@ import org.jclouds.compute.ComputeServiceAdapter;
import org.jclouds.compute.config.StandaloneComputeServiceContextModule; import org.jclouds.compute.config.StandaloneComputeServiceContextModule;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.suppliers.DefaultLocationSupplier; import org.jclouds.compute.suppliers.DefaultLocationSupplier;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.libvirt.Datacenter; import org.jclouds.libvirt.Datacenter;
@ -45,37 +46,58 @@ import org.libvirt.LibvirtException;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.TypeLiteral; import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
/** /**
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class LibvirtComputeServiceContextModule extends public class LibvirtComputeServiceContextModule extends
StandaloneComputeServiceContextModule<Domain, Domain, Image, Datacenter> { StandaloneComputeServiceContextModule<Domain, Domain, Image, Datacenter> {
@Override @Override
protected void configure() { protected void configure() {
super.configure(); super.configure();
bind(new TypeLiteral<ComputeServiceAdapter<Domain, Domain, Image, Datacenter>>() { bind(new TypeLiteral<ComputeServiceAdapter<Domain, Domain, Image, Datacenter>>() {
}).to(LibvirtComputeServiceAdapter.class); }).to(LibvirtComputeServiceAdapter.class);
bind(new TypeLiteral<Supplier<Location>>() { bind(new TypeLiteral<Supplier<Location>>() {
}).to(DefaultLocationSupplier.class); }).to(DefaultLocationSupplier.class);
bind(new TypeLiteral<Function<Domain, NodeMetadata>>() { bind(new TypeLiteral<Function<Domain, NodeMetadata>>() {
}).to(DomainToNodeMetadata.class); }).to(DomainToNodeMetadata.class);
bind(new TypeLiteral<Function<Image, org.jclouds.compute.domain.Image>>() { bind(new TypeLiteral<Function<Image, org.jclouds.compute.domain.Image>>() {
}).to(LibvirtImageToImage.class); }).to(LibvirtImageToImage.class);
bind(new TypeLiteral<Function<Domain, Hardware>>() { bind(new TypeLiteral<Function<Domain, Hardware>>() {
}).to(DomainToHardware.class); }).to(DomainToHardware.class);
bind(new TypeLiteral<Function<Datacenter, Location>>() { bind(new TypeLiteral<Function<Datacenter, Location>>() {
}).to(DatacenterToLocation.class); }).to(DatacenterToLocation.class);
} }
@Provides @Provides
@Singleton @Singleton
protected Connect createConnection(@Provider URI endpoint, @Named(Constants.PROPERTY_IDENTITY) String identity, protected Connect createConnection(@Provider URI endpoint, @Named(Constants.PROPERTY_IDENTITY) String identity,
@Named(Constants.PROPERTY_CREDENTIAL) String credential) throws LibvirtException { @Named(Constants.PROPERTY_CREDENTIAL) String credential) throws LibvirtException {
// ConnectAuth connectAuth = null; // ConnectAuth connectAuth = null;
return new Connect(endpoint.toASCIIString()); return new Connect(endpoint.toASCIIString());
} }
@Override
protected TemplateBuilder provideTemplate(Injector injector, TemplateBuilder template) {
String domainDir = injector.getInstance(Key.get(String.class, Names.named("jclouds.libvirt.domain-dir")));
String hardwareId = searchForHardwareIdInDomainDir(domainDir);
String image = searchForImageIdInDomainDir(domainDir);
return template.hardwareId(hardwareId).imageId(image) ;
}
private String searchForImageIdInDomainDir(String domainDir) {
// TODO
return "1";
}
private String searchForHardwareIdInDomainDir(String domainDir) {
// TODO
return "c7ff2039-a9f1-a659-7f91-e0f82f59d52e";
}
} }

View File

@ -77,6 +77,7 @@ public class DomainToHardware implements Function<Domain, Hardware> {
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
String diskFileName = nodes.item(0).getNodeValue(); String diskFileName = nodes.item(0).getNodeValue();
for (int i = 0; i < nodes.getLength(); i++) { for (int i = 0; i < nodes.getLength(); i++) {
System.out.println("disk " + diskFileName);
StorageVol storageVol = from.getConnect().storageVolLookupByPath(diskFileName); StorageVol storageVol = from.getConnect().storageVolLookupByPath(diskFileName);
String id = storageVol.getKey(); String id = storageVol.getKey();
float size = new Long(storageVol.getInfo().capacity).floatValue(); float size = new Long(storageVol.getInfo().capacity).floatValue();

View File

@ -52,7 +52,7 @@ public class LibvirtImageToImage implements Function<org.jclouds.libvirt.Image,
OsFamily family = null; OsFamily family = null;
try { try {
family = OsFamily.fromValue(from.name); family = OsFamily.fromValue(from.name);
builder.operatingSystem(new OperatingSystemBuilder().name(from.name).family(family).description("ubuntu").build()); builder.operatingSystem(new OperatingSystemBuilder().name(from.name).family(family).description(from.name).build());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
logger.debug("<< didn't match os(%s)", from); logger.debug("<< didn't match os(%s)", from);
} }

View File

@ -83,47 +83,47 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
// Domain from = client.createDomainInDC(template.getLocation().getId(), name, // Domain from = client.createDomainInDC(template.getLocation().getId(), name,
// Integer.parseInt(template.getImage().getProviderId()), // Integer.parseInt(template.getImage().getProviderId()),
// Integer.parseInt(template.getHardware().getProviderId())); // Integer.parseInt(template.getHardware().getProviderId()));
// store the credentials so that later functions can use them // store the credentials so that later functions can use them
// credentialStore.put(from.id + "", new Credentials(from.loginUser, from.password)); // credentialStore.put(from.id + "", new Credentials(from.loginUser, from.password));
String[] domains; //String[] domains;
try { try {
domains = client.listDefinedDomains(); //domains = client.listDefinedDomains();
String xmlDesc = "";
Domain domain = null; Domain domain = null;
String xmlDesc = ""; //for (String domainName : domains) {
for (String domainName : domains) { // domain = client.domainLookupByName(domainName);
domain = client.domainLookupByName(domainName); // if (domainName.equals(tag)) {
if (domainName.equals("ttylinux")) { String domainName = tag;
domain = client.domainLookupByName(domainName); domain = client.domainLookupByName(domainName);
xmlDesc = domain.getXMLDesc(0); System.out.println("domain name " + domain.getName());
System.out.println("domain: " + domain.getUUIDString()); XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(domain.getXMLDesc(0))));
Document doc = builder.getDocument();
XMLBuilder builder = XMLBuilder.parse(new InputSource(new StringReader(xmlDesc))); XPathExpression expr = null;
Document doc = builder.getDocument();
XPathExpression expr = null;
NodeList nodes = null; NodeList nodes = null;
String xpathString = "//devices/disk[@device='disk']/source/@file"; // + String xpathString = "//devices/disk[@device='disk']/source/@file"; // +
expr = XPathFactory.newInstance().newXPath().compile(xpathString); expr = XPathFactory.newInstance().newXPath().compile(xpathString);
nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
String diskFileName = nodes.item(0).getNodeValue(); String diskFileName = nodes.item(0).getNodeValue();
System.out.println("\n *** diskFileName " + diskFileName);
StorageVol storageVol = client.storageVolLookupByPath(diskFileName); StorageVol storageVol = client.storageVolLookupByPath(diskFileName);
System.out.println(storageVol.getXMLDesc(0));
// cloning volume // cloning volume
String poolName = "default"; String poolName = "default";
StoragePool storagePool = client.storagePoolLookupByName(poolName); StoragePool storagePool = client.storagePoolLookupByName(poolName);
StorageVol clonedVol = cloneVolume(storagePool, storageVol); StorageVol clonedVol = cloneVolume(storagePool, storageVol);
// System.out.println(generateClonedDomainXML(xmlDesc)); System.out.println(clonedVol.getXMLDesc(0));
domain = client.domainDefineXML(generateClonedDomainXML(xmlDesc)); // define Domain
} String xmlFinal = generateClonedDomainXML(domain.getXMLDesc(0));
} domain = client.domainDefineXML(xmlFinal);
domain.create();
// store the credentials so that later functions can use them
credentialStore.put(domain.getUUIDString() + "", new Credentials("identity", "credential"));
//}
//}
return domain; return domain;
} catch (LibvirtException e) { } catch (LibvirtException e) {
return propogate(e); return propogate(e);
@ -142,26 +142,19 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
// return ImmutableSet.of(); // return ImmutableSet.of();
// TODO // TODO
// return client.listImages(); // return client.listImages();
int i = 1;
List<Image> images = Lists.newArrayList(); try {
images.add(new Image(1, "ubuntu")); String[] domains = client.listDefinedDomains();
return images; List<Image> images = Lists.newArrayList();
for (String domainName : domains) {
images.add(new Image(i++, domainName));
}
return images;
} catch (Exception e) {
return propogate(e);
}
} }
// @Override
// public Iterable<Domain> listNodes() {
// try {
// List<Domain> domains = Lists.newArrayList();
// for (int domain : client.listDomains()) {
// domains.add(client.domainLookupByID(domain));
// }
// return domains;
// } catch (LibvirtException e) {
// return propogate(e);
// }
// }
@Override @Override
public Iterable<Domain> listNodes() { public Iterable<Domain> listNodes() {
try { try {
@ -219,20 +212,10 @@ public class LibvirtComputeServiceAdapter implements ComputeServiceAdapter<Domai
} }
} }
public void createDomain() throws LibvirtException {
Domain domain = client.domainDefineXML("<domain type='test' id='2'>" + " <name>deftest</name>"
+ " <uuid>004b96e1-2d78-c30f-5aa5-f03c87d21e70</uuid>" + " <memory>8388608</memory>"
+ " <vcpu>2</vcpu>" + " <os><type arch='i686'>hvm</type></os>" + " <on_reboot>restart</on_reboot>"
+ " <on_poweroff>destroy</on_poweroff>" + " <on_crash>restart</on_crash>" + "</domain>");
}
private static StorageVol cloneVolume(StoragePool storagePool, StorageVol from) throws LibvirtException, private static StorageVol cloneVolume(StoragePool storagePool, StorageVol from) throws LibvirtException,
XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException { XPathExpressionException, ParserConfigurationException, SAXException, IOException, TransformerException {
String fromXML = from.getXMLDesc(0); String fromXML = from.getXMLDesc(0);
String clonedXML = generateClonedVolumeXML(fromXML); String clonedXML = generateClonedVolumeXML(fromXML);
System.out.println(clonedXML);
// return null;
return storagePool.storageVolCreateXMLFrom(clonedXML, from, 0); return storagePool.storageVolCreateXMLFrom(clonedXML, from, 0);
} }

View File

@ -65,24 +65,28 @@ public class LibvirtExperimentLiveTest {
endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet endpoint, apiversion, identity, credential, new LibvirtComputeServiceContextModule(), ImmutableSet
.<Module> of())); .<Module> of()));
/*
System.out.println("images " + context.getComputeService().listImages()); System.out.println("images " + context.getComputeService().listImages());
System.out.println("nodes " + context.getComputeService().listNodes()); System.out.println("nodes " + context.getComputeService().listNodes());
System.out.println("hardware profiles " + context.getComputeService().listHardwareProfiles()); System.out.println("hardware profiles " + context.getComputeService().listHardwareProfiles());
*/
/*
Template defaultTemplate = context.getComputeService().templateBuilder() Template defaultTemplate = context.getComputeService().templateBuilder()
.hardwareId("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1") //.locationId("") .hardwareId("c7ff2039-a9f1-a659-7f91-e0f82f59d52e").imageId("1") //.locationId("")
.build(); .build();
*/
/* /*
* We will probably make a default template out of properties at some point * We will probably make a default template out of properties at some point
* You can control the default template via overriding a method in standalonecomputeservicexontextmodule * You can control the default template via overriding a method in standalonecomputeservicexontextmodule
*/ */
context.getComputeService().runNodesWithTag("ttylinux", 1/*, defaultTemplate*/);
// context.getComputeService().templateOptions().; } catch (RunNodesException e) {
context.getComputeService().runNodesWithTag("ttylinux", 1, defaultTemplate); // TODO Auto-generated catch block
} catch (RunNodesException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (context != null) if (context != null)