PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » File Formats » File_Fstab » Manual

This package reads and writes fstab files, or other files sharing their format, such as /proc/mounts on Linux systems.

Introduction

Introduction – Introduction to File_Fstab.

Overview

This package allows you to read, manipulate, and write fstab-format files, such as /etc/fstab, /etc/mtab, and /proc/mounts.

Examples

Getting the filesystem type of the root device.

<?php
require_once 'File/Fstab.php';

$fstab =& new File_Fstab();

$root =& $fstab->getEntryForPath('/');
echo "Root FS type: " . $root->fsType . "\n";
?>

Determine if a user may mount the CD-ROM

The user option in your fstab determines whether users may mount a given device or not.

<?php
require_once 'File/Fstab.php';

$fstab =& new File_Fstab();

$cd =& $fstab->getEntryForDevice('/dev/cdrom');
if ($cd->hasOption('user')) {
    echo "Users may mount the CD-ROM\n";
} else {
    echo "Users may not mount the CD-ROM\n";
}
?>

Entries

Entries – Working with entries

Entry overview

The File_Fstab_Entry class represents all the information available about a particular entry in a Fstab file.

Entry properties

The entry has a number of properties which represent the information in the fstab file.

$device
This is the path to the block device for this entry. $device, $uuid, and $label are mutually exclusive; only one of the three may be set.
$uuid
The UUID of the device.
$label
The label for this device.
$mountPoint
The directory this device is mounted on.
$fsType
The type of filesystem on $device.
$mountOptions
Array of mount options for this device.
$dumpFrequency
How often / if this filesystem should be backed up by dump.
$fsckPassNo
Order of / if this device should be checked by fsck when the system boots.

You may want to read fstab(5) for more information about what these fields mean.

Finding entries

There are a number of ways of finding a specific entry from the fstab. You may find based on device, mountpoint, filesystem label, or UUID.

Finding by device

To find by device, you want to use the getEntryForDevice() function. The single argument this function accepts is the path to the block device for an entry.

Get entry by device

<?php
require_once 'File/Fstab.php';
$fstab =& new File_Fstab();
$dev =& $fstab->getEntryForDevice('/dev/hda1');
if (PEAR::isError($dev)) {
    die($dev->getMessage());
}
?>

Finding by path (mountpoint)

You may want to find a device based on the path it is mounted on; for example, you may want to get the entry for /cdrom, without caring if the CD device is /dev/hdb, /dev/cdrom, or some other device. To do this, use the getEntryForPath() function.

Get entry by path

<?php
require_once 'File/Fstab.php';
$fstab =& new File_Fstab();
$dev =& $fstab->getEntryForPath('/cdrom');
if (PEAR::isError($dev)) {
    die($dev->getMessage());
}
?>

Finding by UUID

Some systems use a filesystem UUID to specify the device to mount. A UUID may look like this: b46ad2ee-01f3-4041-96ca-91d35d059417. The getEntryForUUID() function handles this.

Get entry by UUID

<?php
require_once 'File/Fstab.php';
$fstab =& new File_Fstab();
$dev =& $fstab->getEntryForUUID('b46ad2ee-01f3-4041-96ca-91d35d059417');
if (PEAR::isError($dev)) {
    die($dev->getMessage());
}
?>

Finding by label

Some filesystems allow you to specify a textual label to a filesystem. For example, you may label your root device rootdev, the device you mount on /home could be named homedirs and so forth. File_Fstab supports getting entries based on the device label. This is accomplished by using the getEntryForLabel() function.

Get entry by label

<?php
require_once 'File/Fstab.php';
$fstab =& new File_Fstab();
$dev =& $fstab->getEntryForLabel('homedirs');
if (PEAR::isError($dev)) {
    die($dev->getMessage());
}
?>

Adding entries

In addition to reading fstab files, you may modify them as well.

Add an entry for a floppy disk

<?php
require_once 'File/Fstab.php';
$fstab =& new File_Fstab();
$floppy =& new File_Fstab_Entry();
$floppy->fsType = 'vfat';
$floppy->device = '/dev/fd0';
$floppy->mountPoint = '/floppy';
$fstab->addEntry($floppy);
?>

Saving

Saving – Saving your changes

Saving your changes

After modifying a fstab file, you will want to save your changes. The save() function does this.

Comments from the loaded file are not preserved when saving, and whitespace may change. This has no effect on the functionality of the fstab file, but you may lose helpful comments.

Save to the same file

This will save your changes back to the file you loaded, overwriting the old file.

<?php
require_once 'File/Fstab.php';
$fstab =& new File_Fstab();
$floppy =& new File_Fstab_Entry();
$floppy->fsType = 'vfat';
$floppy->device = '/dev/fd0';
$floppy->mountPoint = '/floppy';
$fstab->addEntry($floppy);
$res = $fstab->save();
if (PEAR::isError($res)) {
    die($res->getMessage());
}
?>

Save to a different file

This will save your changes to a different file than the one you originally loaded.

<?php
require_once 'File/Fstab.php';
$fstab =& new File_Fstab();
$floppy =& new File_Fstab_Entry();
$floppy->fsType = 'vfat';
$floppy->device = '/dev/fd0';
$floppy->mountPoint = '/floppy';
$fstab->addEntry($floppy);
$res = $fstab->save('/tmp/fstab.new');
if (PEAR::isError($res)) {
    die($res->getMessage());
}
?>