5

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?

Mark B
  • 4,768
  • 2
  • 25
  • 30
  • So basically this ins't a problem with PHP, which means it's not a programming question which means it's best suited for [Super User](http://superuser.com) ? – HamZa Aug 12 '13 at 12:18
  • 1
    It looks exactly like a problem with PHP to me. disk_total_space should return the size of a partition, but under some circumstances it doesn't. I'm happy to be corrected on this, but it looks like a programming question to me. – Mark B Aug 12 '13 at 13:13
  • What's the behaviour in the command line ? If it's the same, then it has nothing to do with PHP. – HamZa Aug 12 '13 at 13:14
  • This is all from the command line, but I get the same behaviour through Apache. – Mark B Aug 12 '13 at 13:15
  • I'm removing my close vote, but I really think you could get more help at Super User. You might enter a chatroom and ask it if you're not sure. – HamZa Aug 12 '13 at 13:19

1 Answers1

3

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;
}
keithhatfield
  • 3,273
  • 1
  • 17
  • 24