Skip to content

Commit

Permalink
Restore functionality of lxfile_get_ntfs_disk_usage into getDiskUsage…
Browse files Browse the repository at this point in the history
…Windows() optimized. Adapt the calls to is_windows and root_path params of getDiskUsage()
  • Loading branch information
Ángel Guzmán Maeso committed Feb 21, 2012
1 parent 3774f93 commit 7dc2efd
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 41 deletions.
155 changes: 122 additions & 33 deletions hypervm/httpdocs/lib/vps/driver/vps__xenlib.php
Expand Up @@ -327,6 +327,77 @@ public static function getStatus($virtual_machine_name, $rootdir)
}
}

/**
* Get the disk usage for a given disk on a windows based Xen virtual machine.
*
* Get the data from ntfscluster output processing the bytes per volume,
* and bytes of user data.
*
* Calculate the total disk space and total disk space used.
*
* @author Anonymous <anonymous@lxcenter.org>
* @author Ángel Guzmán Maeso <angel.guzman@lxcenter.org>
*
* @param string $disk The disk on a xen virtual machine. Default NULL.
* @param string $root_path The location for root path needed for windows based Xen virtual machine.
* @return array[string] The total and used integer space indexed as string
*/
private static function getDiskUsageWindows($disk = NULL, $root_path = NULL)
{
$root_path = fix_vgname($root_path);
$partition = get_partition($disk, $root_path);

// @todo Check if the ntfscluster it's available to use and exists (never trusts on users)
$output = lxshell_output('ntfscluster', '-f', $partition);

// Disconnect the partition from the file on device mapper.
// @todo Check if the kpartx it's available to use and exists (never trusts on users)
$base = basename($disk);
$image_file = '/dev/mapper/' . $root_path . '-' . $base;
lxshell_return('kpartx', '-d', $image_file);

if(!empty($output)) {
// If no output returned we return 0 MBytes (fallback mode)
$ouput_lines = explode(PHP_EOL, $output);

// Process the ntfscluster output
if(!empty($ouput_lines)) // Ensure not process truncate output
{
foreach($ouput_lines as $line) {
$line = trim($line);

// Only process lines with :
if (char_search_a($line, ':')) {
list($variable_header, $value) = explode(':', $line);

$variable_header = trim($variable_header);
$value = trim($val);

// Get the bytes per volume line
if ($variable_header === 'bytes per volume') {
$total_disk = $value;
}

// Get the bytes of user data line
if ($variable_header === 'bytes of user data') {
$total_disk_used = $value;
}
}
}
}

// Round total and used to MBytes with 2 decimals
$result['total'] = round($total_disk / (1024 * 1024), 1);
$result['used'] = round($total_disk_used / (1024 * 1024), 1);
}
else { // Fallback mode
$result['total'] = 0;
$result['used'] = 0;
}

return $result;
}

/**
* Get the disk usage for a given disk on a Xen virtual machine.
*
Expand All @@ -339,9 +410,11 @@ public static function getStatus($virtual_machine_name, $rootdir)
* @author Ángel Guzmán Maeso <angel.guzman@lxcenter.org>
*
* @param string $disk The disk on a xen virtual machine. Default NULL.
* @param boolean $is_windows TRUE if the Xen virtual machine is windows based.
* @param string $root_path The location for root path needed for windows based Xen virtual machine.
* @return array[string] The total and used integer space indexed as string
*/
public static function getDiskUsage($disk = NULL)
public static function getDiskUsage($disk = NULL, $is_windows = FALSE, $root_path = NULL)
{
global $global_dontlogshell;

Expand All @@ -352,41 +425,49 @@ public static function getDiskUsage($disk = NULL)
// @todo Check if it is a valid disk path (never trusts on users)
$disk = expand_real_root($disk);

// @todo Check if the dumpe2fs it's available to use and exists (never trusts on users)
$global_dontlogshell = TRUE;
$output = lxshell_output('dumpe2fs', '-h', $disk);
$global_dontlogshell = FALSE;

if(!empty($output)) { // If no output returned we return 0 MBytes (fallback mode)
$ouput_lines = explode(PHP_EOL, $output);
// Check if the Xen virtual machine is windows based
if($is_windows)
{
$result = $this->getDiskUsageWindows($disk, $root_path);
}
else { // For Unix based Xen virtual machine
// @todo Check if the dumpe2fs it's available to use and exists (never trusts on users)
$global_dontlogshell = TRUE;
$output = lxshell_output('dumpe2fs', '-h', $disk);
$global_dontlogshell = FALSE;

// Process the dumpe2fs output
if(!empty($ouput_lines)) // Ensure not process truncate output
{
foreach($ouput_lines as $line) {
// Get the Block size line (on bytes)
if (char_search_beg($line, 'Block size:')) {
$blocksize = intval(trim(strfrom($line, 'Block size:'))) / 1024; // Convert total bytes to KBytes
if(!empty($output)) { // If no output returned we return 0 MBytes (fallback mode)
$ouput_lines = explode(PHP_EOL, $output);

// Process the dumpe2fs output
if(!empty($ouput_lines)) // Ensure not process truncate output
{
foreach($ouput_lines as $line) {
// Get the Block size line (on bytes)
if (char_search_beg($line, 'Block size:')) {
$blocksize = intval(trim(strfrom($line, 'Block size:')));
}

// Get the Block count number line
if (char_search_beg($line, 'Block count:')) {
$block_count = intval(trim(strfrom($line, 'Block count:')));
}

// Get the Free blocks number line
if (char_search_beg($line, 'Free blocks:')) {
$free_blocks = intval(trim(strfrom($line, 'Free blocks:')));
}
}

// Get the Block count number line
if (char_search_beg($line, 'Block count:')) {
$block_count = intval(trim(strfrom($line, 'Block count:')));
}
$blocksize = $blocksize / 1024; // Convert total bytes to KBytes
$total_disk_space = $block_count * $blocksize;
$total_free_blocks = $free_blocks * $blocksize;
$total_disk_used = $total_disk_space - $total_free_blocks;

// Get the Free blocks number line
if (char_search_beg($line, 'Free blocks:')) {
$free_blocks = intval(trim(strfrom($line, 'Free blocks:')));
}
// Round total and used to MBytes with 2 decimals
$result['total'] = round($total_disk_space / 1024, 2);
$result['used'] = round($total_disk_used / 1024, 2);
}

$total_disk_space = $block_count * $blocksize;
$total_free_blocks = $free_blocks * $blocksize;
$total_disk_used = $total_disk_space - $total_free_blocks;

// Round total and used to MBytes with 2 decimals
$result['total'] = round($total_disk_space / 1024, 2);
$result['used'] = round($total_disk_used / 1024, 2);
}
}

Expand Down Expand Up @@ -2150,12 +2231,20 @@ public static function getCompleteStatus($list)
{
foreach($list as $l) {
$virtual_machine_name = $l['nname'];
$root_dir = '/home/xen';
$root_dir = '/home/xen';

$r['status'] = self::getStatus($virtual_machine_name, $root_dir);
$disk = self::getDiskUsage($l['diskname']);

$disk_name = $l['diskname'];
$is_windows = $l['winflag'];
$root_path = $l['corerootdir'];

$disk = self::getDiskUsage($disk_name, $is_windows, $root_path);

$r['ldiskusage_f'] = $disk['used'];
$res[$l['nname']] = $r;
}

return $res;
}
}
30 changes: 22 additions & 8 deletions hypervm/httpdocs/lib/vps/vpslib.php
Expand Up @@ -2251,13 +2251,23 @@ function hasFunctions() { return true; }
function getHardProperty()
{
global $gbl, $sgbl, $login, $ghtml;
$driverapp = $gbl->getSyncClass('localhost', $this->syncserver, 'vps');

$master_server = $this->__masterserver;
$slave_server = $this->syncserver;
$driverapp = $gbl->getSyncClass('localhost', $slave_server, 'vps');

if ($this->isXen()) {
$maindisk = $this->getXenMaindiskName();
$disk = rl_exec_get($this->__masterserver, $this->syncserver, array("vps__$driverapp", "getDiskUsage"), array($maindisk));
// Build the params
$maindisk = $this->getXenMaindiskName();
$is_windows = $this->isWindows();
$root_path = $this->corerootdir;

$parameters = array($maindisk, $is_windows, $root_path);

$disk = rl_exec_get($master_server, $slave_server, array("vps__$driverapp", "getDiskUsage"), $parameters);
$this->used->disk_usage = $disk['used'];
} else {
$l = rl_exec_get($this->__masterserver, $this->syncserver, array("vps__$driverapp", "vpsInfo"), array($this->getIid(), $this->corerootdir));
$l = rl_exec_get($master_server, $slave_server, array("vps__$driverapp", "vpsInfo"), array($this->getIid(), $this->corerootdir));
$this->used->disk_usage = $l['used_s_disk'];
$this->used->disk_inode = $l['used_s_inode'];
$this->used->memory_usage = $l['used_s_memory'];
Expand Down Expand Up @@ -2533,10 +2543,14 @@ function createShowRlist($subaction)
$disk['used'] = '300';
$disk['total'] = '6000';
} else {

$maindisk = $this->getXenMaindiskName();

$disk = rl_exec_get($master_server, $slave_server, array("vps__$driverapp", "getDiskUsage"), array($maindisk));
// Build the params
$maindisk = $this->getXenMaindiskName();
$is_windows = $this->isWindows();
$root_path = $this->corerootdir;

$parameters = array($maindisk, $is_windows, $root_path);

$disk = rl_exec_get($master_server, $slave_server, array("vps__$driverapp", "getDiskUsage"), $parameters);
}
if (!$this->priv->disk_usage) {
$this->priv->disk_usage = $disk['total'];
Expand Down

0 comments on commit 7dc2efd

Please sign in to comment.