summaryrefslogtreecommitdiffstats
path: root/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/virtualization/LibvirtVirtualMachine.java
blob: 781bd938c0520eb22ad8bf24a99c388c0c78c15e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package org.openslx.runvirt.virtualization;

import org.libvirt.Domain;
import org.libvirt.LibvirtException;

public class LibvirtVirtualMachine
{
	private Domain domain;

	LibvirtVirtualMachine( Domain vm )
	{
		this.domain = vm;
	}

	public Domain getLibvirtDomain()
	{
		return this.domain;
	}

	public boolean isRunning() throws LibvirtVirtualMachineException
	{
		int state = 0;

		try {
			state = this.domain.isActive();
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}

		return ( state == 0 ) ? false : true;
	}

	public void start() throws LibvirtVirtualMachineException
	{
		if ( !this.isRunning() ) {
			try {
				this.domain.create();
			} catch ( LibvirtException e ) {
				throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
			}
		}
	}

	public void stop() throws LibvirtVirtualMachineException
	{
		if ( this.isRunning() ) {
			try {
				this.domain.shutdown();
			} catch ( LibvirtException e ) {
				throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
			}
		}
	}

	public void suspend() throws LibvirtVirtualMachineException
	{
		try {
			this.domain.suspend();
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}
	}

	public void resume() throws LibvirtVirtualMachineException
	{
		try {
			this.domain.resume();
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}
	}

	public void reboot() throws LibvirtVirtualMachineException
	{
		try {
			this.domain.reboot( 0 );
		} catch ( LibvirtException e ) {
			throw new LibvirtVirtualMachineException( e.getLocalizedMessage() );
		}
	}
}