Simply PHP and Libvirt api examples

200px-Libvirt_logo.svg

 

 

<?php
echo “WebAdmin 0.1Beta”;
echo “<br/>”;

$uri=”qemu+tcp:///system”;
$credentials=Array(VIR_CRED_AUTHNAME=>”root”,VIR_CRED_PASSPHRASE=>”xxxx”);
echo (“Connecting to libvirt (URI:$uri)\n”.”<br/>”.”<br/>”);
$conn=libvirt_connect($uri,false,$credentials);
if ($conn==false)
{
echo (“Libvirt last error: “.libvirt_get_last_error().”\n”);
exit;
}
$pools=libvirt_list_storagepools($conn);

echo “Storage Pool :”; echo “<br/>”;

for ($x = 0, $m = count($pools) ; $x < $m ; $x++)
{
$storeinfo= $pools[$x] ;
echo $storeinfo ;
echo “<br/>”;
$pool=libvirt_storagepool_lookup_by_name($conn,$storeinfo);
$volumes=libvirt_storagepool_list_volumes($pool);

echo “Storage Pool Details:”;
echo “<br/>”;
$storifoext=libvirt_storagepool_get_info($pool);

foreach($storifoext as $key => $value)
{
echo $key. ” : ” . $value . “<br/>”;
}

echo “<br/>”;
echo “Volumes”;
echo “<br/>”;
for ($y = 0, $o = count($volumes) ; $y < $o ; $y++)
{
echo $volumes[$y];  echo “<br/>”;

}
}
echo “<br/>”;
$doms = libvirt_list_domains($conn);
echo (“Domains :”.”<br/>”);
echo “<br/>”;
for ($i = 0, $n = count($doms) ; $i < $n ; $i++)
{
echo $doms[$i] ; echo “<br/>”;

}

echo “<br/>” ;
$cdoms= libvirt_domain_get_counts($conn);
echo (“Total    : ”  . $cdoms[total]); echo “<br/>”;
echo (“Active : ”  . $cdoms[active]); echo “<br/>”;
echo (“Inactive : ”  . $cdoms[inactive]); echo “<br/>”;

?>

This is the output :

WebAdmin 0.1Beta
Connecting to libvirt (URI:qemu+tcp:///system)

Storage Pool :
default
Storage Pool Details:
state : 2
capacity : 371561320448
allocation : 306486804480
available : 65074515968

Volumes
w7.img
Fedora_17_x86_64-2.img
OST_01-2.img
ad-01.img
Cloudify.img
Fedora_17_x86_64.img
Fedora_17_x86_64-1.img
Foreman-02-clone-1.img
Foreman-02-clone.qcow2
Foreman-02.vmdk
OST_01-3.img
Centos-6_clean.img
Centos_01.img
Centos-6_clean-1.img
Fedora_17_x86_64-3.img
Foreman-02.img
Foreman-02-clone-2.img
Foreman-02-clone.img
Centos_02.img
OST_01-1.img
Centos-6-x86_64.img
Centos-6-Puppet.img
OST_01.img

Domains :

Centos_02
Foreman-02-V1-clone
Foreman-02-V1
Centos_01
Fedora_17_x86_64
Foreman-02-clone
w7
Cloudify
OST_01

Total : 9
Active : 0
Inactive : 9

Fedora 18 and Systemd

f18_fpo_screenshot

Show all the services :

chkconfig –list

with systemctl :
systemctl list-unit-files –type=service

start or stop a service

/etc/init.d/httpd start or service httpd start

/etc/init.d/httpd stop or service httpd stop

with systemctl :

systemctl start httpd.service

systemctl stop httpd.service

enable or disable services :

chkconfig –add httpd && chkconfig httpd on

chkconfig httpd off

with systemctl :

systemct enable httpd.service

systemct disable httpd.service

JBoss 7 as Service on linux Redhat

jboss-at-7

Create a file under /etc/init.d/ called jboss

chmod 755 jboss
vi /etc/init.d/jboss and add the following text :

#!/bin/sh
### BEGIN INIT INFO
# Reference: http://stackoverflow.com/questions/6880902/start-jboss-7-as-a-service-on-linux
# Provides: jboss
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop JBoss AS v7.0.0
### END INIT INFO
#
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH
#[ -r /etc/profile.d/java.sh ] && . /etc/profile.d/java.sh
#[ -r /etc/profile.d/jboss.sh ] && . /etc/profile.d/jboss.sh

export JBOSS_HOME=/opt/jboss-as-web-7.0.2.Final/

case “$1” in
start)
echo “Starting JBoss AS 7.0.2”
sudo -u jboss sh ${JBOSS_HOME}/bin/standalone.sh -b 0.0.0.0 > /dev/null 2>&1 &
;;
stop)
echo “Stopping JBoss AS 7.0.0”
sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-admin.sh –connect command=:shutdown > /dev/null 2>&1 &
;;
*)
echo “Usage: /etc/init.d/jboss {start|stop}”
exit 1
;;
esac

exit 0

with standalone.sh -b 0.0.0.0 means that jbossis binded on all ip’s

chkconfig –add jboss && chkconfig jboss on

Jboss 7 AS on Redhat 6

Image

vi /etc/hosts and add the hostname

yum install java-1.7.0-openjdk.x86_64

tar xzvf jboss-as-7.0.2.Final.tar.gz

mv jboss-as-7.0.2.Final jboss-as

adduser jboss

chown jboss:jboss /opt/jboss-as

add the following lines to jboss .bash_profile :

JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64

JBOSS_HOME=/opt/jboss-as

export JAVA_HOME

export JBOSS_HOME

cd /opt/jboss-as/bin

You must type “unset MODULEPATH” beacuse it’s in conflict with default MODULEPATH environment.

./standalone.sh and jboss should start correctly