Sunday, November 7, 2010

Managing Filesystems: fstab

Understanding how the BSD filesystem manages disk space is critical to successfully managing a BSD server or workstation. However, this topic is generally overlooked since it is rarely used outside of installation and upgrades. It is also a very simple topic and most people assume you understand how it all works.

This article gives a quick synopsis on filesystem layout and tries to briefly explain how to understand /etc/fstab. The fstab(5) man pages, while good, do little to teach the basics to new sysadmins.

The first thing to realize when dealing with the Unix filesystem is that everything can be addressed as a file. Even system hardware has a file representation that is used to access it. Those special files live in /dev.

The second thing to understand is the starting location for all these files is / and pronounced "root". Those are the two points of Unix doctrine that everyone expects you to already understand before even thinking about trying BSD.

But what does that mean to me? First, if you have used a Microsoft operating system, you will notice there is no "C:" drive or any other drive letters. Unix has no concept of the drive letter mentality. With Unix all things exist in one directory structure. This may seem very unusual at first given the way you may be used to dealing with removable media. Instead of assigning a drive letter to removable media, you must find a place in the existing file structure to attach it.

Think of it like docking a module on to a very large space station. You find an empty docking station, connect the module. While the module is connected, the entire contents of the module are accessible for storage or removal by authorized personnel.


The Unix filesystem works in a similar fashion. Any empty directory can be used as the attach point and any readable filesystem can be used as the module. So local hard drives, partitons, removable media, and even networked storage are all attached to the existing filesystem.

The route you would take to get to your module on the space station is like the directory path that is used to access the filesystem. So, if you attached your module to docking station Alpha, your contents would be available there. If you disengaged the module and moved it to docking station Beta, the contents would no longer be available at Alpha, but now at Beta. Then everybody who expected to find your stuff in Alpha would need to be re-routed to Beta to find it. On the filesystem it would be as though you had mounted your partition on /usr/Alpha and then unmounted it and attached it to /usr/Beta. The contents would be the same, but the path different.

Disk partitions that are always available can be automatically mounted when the computer boots up. Which filesystems get mounted at boot-up is controlled by /etc/fstab. All of the mounted filesystems get unmounted at shutdown and must be reconnected at startup. The kernel reads /etc/fstab and creates the file structure based on its contents.



# Device                Mountpoint      FStype  Options         Dump    Pass#
/dev/da0s1b             none            swap    sw              0       0
/dev/da0s1a             /               ufs     rw              1       1
/dev/da0s1f             /tmp            ufs     rw              2       2
/dev/da0s1g             /usr            ufs     rw              2       2
/dev/da0s1e             /var            ufs     rw              2       2
/dev/acd0               /cdrom          cd9660  ro,noauto       0       0

The layout of /etc/fstab is very simple. It lists the device name, followed by where in the file structure it gets mounted along with some options. Order in the file is important. Just like if you were really attaching modules, which one comes first can be important to its location.

The swap partition is not part of the filesystem since it is used exclusively by the system memory as a temporary storage area and is not mounted, therefore it does not have a mount point. The FStype tells the system how the partition is formatted. A type of ufs is a standard BSD filesystem. Other types would be network filesystems, DOS, NTFS, etc. The options rw specify that the partion is read/write-able. The ro option on the CD drive declares it read only. The additional option of noauto indicates that this partition should not be automatically mounted at boot up.

If a partition is not available for attaching during boot up, the BSD kernel thinks it has encountered an error and stops booting to wait for operator intervention. If you have a CD set to attach automatically and you forget to put a CD in the drive it will boot to single user mode and wait for you to fix the problem.

You can see which filesystems are attached by using df at the command line.



# df
Filesystem  1K-blocks     Used    Avail Capacity  Mounted on
/dev/da0s1a    126702   125298    -8732   107%    /
devfs               1        1        0   100%    /dev
/dev/da0s1f    253678     2866   230518     1%    /tmp
/dev/da0s1g  33075218 10144970 20284232    33%    /usr
/dev/da0s1e    253678    13946   219438     6%    /var

This will give you an idea of whether or not your /etc/fstab is working properly and also allow you to monitor the capacity of your partitions. You will notice that the / partition is just a bit full. The filesystem always reserves 10% or so of extra space for just this sort of occasion and only lets root write to it. Looks like I have to go clean out some stuff.

This should be enough information to help you understand the basics of filesystem structure. Generally you won't need to edit/etc/fstab, but you will need to understand it in case you ever upgrade or re-install.

No comments:

Post a Comment