Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP disk_total_space won't measure LVM partition size

Tags:

php

lvm

I've got a PHP script which needs to check the total size of a disk. I've been using disk_total_space successfully for a while, and have just moved to a new server which uses LVM. disk_total_space now reports a completely different size. I've recreated this on a second LVM server.

df -h on the first server (CentOS 6.4, PHP v5.3.27) shows

Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/vg-vg_root    99G   47G   47G  50% /
tmpfs                     32G     0   32G   0% /dev/shm
/dev/sda1                194M   65M  120M  36% /boot
/dev/mapper/vg-vg_backup 400G   33M  400G   1% /var/tmp
/dev/mapper/vg-vg_mysql  950G   81G  870G   9% /data

but disk_total_space('/dev/mapper/vg-vg_mysql') returns 32G. In fact, it returns 32G whatever partition I enter in the command.

On a second server (Ubuntu 10.04.4LTS, PHP v5.3.6), I get the same kind of behaviour:

Filesystem              Size  Used Avail Use% Mounted on
/dev/mapper/batty-root  258G  217G   29G  89% /
none                    4.0G  208K  4.0G   1% /dev
none                    4.0G     0  4.0G   0% /dev/shm
none                    4.0G   88K  4.0G   1% /var/run
none                    4.0G     0  4.0G   0% /var/lock
none                    4.0G     0  4.0G   0% /lib/init/rw
none                    258G  217G   29G  89% /var/lib/ureadahead/debugfs

and disk_total_space('/dev/mapper/batty-root') returns 4G.

In both cases it seems to be returning the amount of disk space which isn't handled by LVM. Is there any way to get the total size of an LVM partition using PHP?

like image 443
Mark B Avatar asked Oct 22 '22 03:10

Mark B


1 Answers

I had a similar issue at one point ... I needed to get the total and used space on LVM logical volumes from a PHP script ...

I wrote an ugly little function that returns a multi-dimensional array with the filesystem data from df ...

Each array contains:

  • Filesystem
  • Mount Point
  • Total Space
  • Used Space

Like I said, it's ugly, but it works ...

function disk_space()
{
    $cmd    = 'df -P | gawk \'{ printf "%s\t%s\t%s\t%s\n", $1, $6, $2, $3 }\'';
    $data   = trim(shell_exec($cmd));

    $return = array();
    $lines  = explode("\n", $data);
    unset($lines[0]);

    foreach($lines as $line){
        $return[] = explode("\t", $line);
    }

    return $return;
}
like image 65
keithhatfield Avatar answered Nov 03 '22 04:11

keithhatfield