Site hosted by Angelfire.com: Build your free website today!

UNIX Filesystem Management

<< Back to module 4 index

Table of Contents


Summary

Files on a UNIX system can be anything: terminals for users to login, programs that are running or waiting to be executed, etc. These files exist in a virtual world called a filesystem. The UNIX filesystem is the infrastructure that the operating system depends on to operate. Disk devices support the UNIX filesystem, these devices are most commonly a hard disk, but they are also removeable storage or any piece of hardware that supports file access. As an administrator, you must maintain these devices.

Maintenance of the filesystem involves indepth knowledge of the contents of your filesystem and what duties must be performed to properly govern this community of software, hardware and file objects. By the end of this module you should be able to:

The Layout of a Linux/UNIX Filesystem

The technical definition of a filesystem is the way that a disk device has been formatted to allow data to be arranged and accessed on some form of physical media. You must know how the filesystem is arranged on a structural level to understand the kind of files you might find on a filesystem.

Each filesystem will have three main elements:

superblock
The data structure that contains general information about the filesystem such as, its label, size in Kilobytes, number of inodes and data blocks.
inode table
Contains the inodes (AKA. information nodes) for each file in the filesystem. Each inode contains information about a file's size, data block location, last date modified, permissions, and ownership. The inode is just a pointer to the actual data on disk, which are the data blocks. All files must have an inode.
data blocks
The file's contents and filename is stored in the data blocks. The only file type that does not have data blocks are device files. The data blocks of a directory merely contain the names of the files within.

What can be found in the filesystem

A filesystem in the UNIX operating system environment is a single hierarchy beginning with the root directory (/). The filesystem contains more than just files. In the filesystem you will also find:

The ls command's -l option, which is most commonly associated with viewing a file's permissions is also useful for determining a file's type. The first character presented on a file's long listing is the file type character this line will show six different file types:

File types displayed by the ls -l command
Type character Description
d Directory file - a special file on the filesystem used to organize files into a logical tree structure.
l Symbolically linked file - a pointer to another file on a filesystem, AKA. a shortcut.
b or c Device file - a file that represents a specific device connected to the system.
n Named pipe file - a temporary connection that sends information from one command or process in memory to another; it can also be represented by a file on the filesystem.
s Socket file - a named pipe connecting processes on two different computers.
- Regular text or binary file - a regular file is a file that stores information in a human-readable format, a binary file is a file that stores machine-understandable data (0s and 1s) for compiled programs.

Software Resources

The core a computer's operation is its software. Software developers have taken advantage of the UNIX filesystem to store objects important to programming efforts. A programmer will use the UNIX filesystem in these ways:

Without the proper arrangement of files in a consistent manner, software may not be able to find the supporting program files, processes, and library resources necessary to execute. However, sometimes this is unavoidable and a system administrator must edit a configuration file and specify where a particular resource may be found on their system and/or install a resource (usually done with an rpm).

Different Linux systems once were known to have common configuration files in widely variant directory locations. Linux developers decided to work with a Filesystem Hierarchy Standard (FHS). Because of this FHS system Linux users could find files on any distribution.

Directories specified in the Filesystem Hierarchy Standard
Directory Description
/bin Contains binary commands for use by all users
/boot Contains the Linux kernel and files used by the boot loader
/dev Contains device files
/etc Contains binary commands for use by all users
/home Default location for user home directories
/lib Contains shared program libraries (used by the commands in /bin and /sbin) as well as kernel modules
/mnt Empty directory used for accessing (mounting) disks such as floppy disks and CD-ROMs
/opt Stores additional software programs
/proc Contains process and kernel information
/root The root user's home directory
/sbin Contains system binary commands (used for administration)
/tmp Holds temporary files created by programs
/usr Contains most system commands and utilities - will contain these directories:
  • /usr/bin - user binary commands
  • /usr/games - educational programs and games
  • /usr/include - C program header files
  • /usr/lib - libraries
  • /usr/local - local programs
  • /usr/sbin - system binary commands
  • /usr/share - files that are architecture independant
  • /usr/src - source code
  • /usr/X11R6 - the XWindow system
/usr/local Location for most additional programs
/var Contains log files and spools

Device Files and Device Drivers

Not all files in the UNIX filesystem are handled the same. Some portions of the filesystem are handled in a disk-based implementation, others by drivers for other implementations. For example, NFS (network filesystems) are accessing a driver that forwards requests to a remote computer and device files forward requests to drivers in the kernel.

Device files in UNIX communicate with drivers in the kernel. They are not really data files, device files do not have data blocks. They have a major and minor number to reference the driver that enables them to user the specific type of hardware they control. Drivers are programs that control devices such as, printers, keyboards, etc. They serve as translators between the devices themselves and the programs that use them.

The /dev Directory

Devices connected to a Linux system like, terminals, disks, and serial ports are placed in the /dev directory as a file.

Types of devices

Device files can be either character (c) devices or block (b) devices. The type of device specify how data should be transferred.

Character devices
Character devices transfer data character-by-character to and from devices.
Block devices
Devices also use another method, which involves transferring files via a buffer in chunks from device to device.

A typical Linux system will have the following device files in its /dev directory:

File commonly found in the /dev directory
Device File Description Device Type (Block or Character)
/dev/fd0 First floppy disk Block
/dev/fd1 Second floppy disk Block
/dev/hda1 First primary partition on the first IDE hard disk controller (primary master) Block
/dev/hdb1 First primary partition on the second IDE hard disk controller (primary slave) Block
/dev/hdc1 First primary partition on the third IDE hard disk controller (secondary master) Block
/dev/hdd1 First primary partition on the fourth IDE hard disk controller (primary slave) Block
/dev/sda1 First primary partition on the first SCSI hard disk Block
/dev/sdb1 First primary partition on the second SCSI hard disk Block
/dev/tty1 First local terminal on the system Character
/dev/tty2 Second local terminal on the system Character
/dev/ttyS0 First serial port on the system (COM1) Character
/dev/ttyS1 Second serial port on the system (COM2) Character
/dev/psaux PS/2 mouse port Character
/dev/lp0 First parallel port on the system (LPT1) Character
/dev/null A device that represents nothing; void Character
/dev/st0 The first SCSI tape device in the system Character
/dev/usb/* USB device files Character

Example - listing the first SCSI hard disk and local terminal attached the academic server.

		
Device files
------------------------------------
[lwheeler@academic ~]$  ls -l /dev/sda1 /dev/tty0
brw-rw----    1 root     disk       8,   1 Mar 23  2001 /dev/sda1
crw--w----    1 root     root       4,   0 Mar 23  2001 /dev/tty0
------------------------------------
Regular files
------------------------------------
[lwheeler@academic ~]$ ls -l file1
--wx--xrwx    1 lwheeler lwheeler     5739 Mar  7 12:37 file1

Take special notice of the above ls -l output. Device files display slightly different information than the long listing of other files. The fifth column, which is normally reserved for the file's size in kilobytes, contains the device file's associated driver's major and minor numbers.

The major number for the device driver identifies it to the kernel. Two (or more) devices of similar types can share the same major number. The minor number indicates the device itself.

Example - Display floppy drive 1 and 2's long listing to observe the version numbers.

[lwheeler@academic ~]$ ls -l /dev/fd0 /dev/fd1
brw-rw----    1 root     floppy     2,   0 Mar 23  2001 /dev/fd0
brw-rw----    1 root     floppy     2,   1 Mar 23  2001 /dev/fd1

In the above listing the major number (listed first) is two for both floppy drives. The minor number listed second refers to the device's number (fd0 and fd1). It is tempting to jump to the conclusion that the device number will always be the number ending the device file, but this is not the case.

Example - Displaying the device files and version numbers of the IDE hard drives.

[lwheeler@academic ~]$ ls -l /dev/hd*1 | more
brw-rw----    1 root     disk       3,   1 Mar 23  2001 /dev/hda1
brw-rw----    1 root     disk       3,  11 Mar 23  2001 /dev/hda11
brw-rw----    1 root     disk       3,  65 Mar 23  2001 /dev/hdb1
brw-rw----    1 root     disk       3,  75 Mar 23  2001 /dev/hdb11

Fixing Device Files

Occasionally, device files get corrupted. A common symptom that a device file is corrupted is that it will become listed as a regular file, not a character or block file. Running the find command within the /dev directory to search for any regular files can discover and signs of trouble.

[lwheeler@academic ~]$ find /dev -type f

Creating Devices

If a device file is corrupted or is accidentally deleted, you can recreate the device file if you know the type, major, and minor numbers with the mknod command.

In the following example, I do a long listing of my floppy disk drive 1 (/dev/fd0) to get the type of device, major and minor numbers. I then delete it, and verify it is gone. Next, I use mknod to recreate it and then once again prove that it was successful.

Example - Creating a device file with mknod.

[root@linux_box /]# ls -l /dev/fd0
brw-rw----    1 root     floppy     2,   0 Mar 23  2001 /dev/fd0
[root@linux_box /]# rm -f /dev/fd0
[root@linux_box /]# ls -l /dev/fd0
ls: /dev/fd0: No such file or directory
[root@linux_box /]# mknod /dev/fd0 b 2 0
[root@linux_box /]# ls -l /dev/fd0
brw-rw-r--    1 root     root       2,   0 Mar  8 06:19 /dev/fd0

If you do not know the type, major or minor numbers for a device all is not lost! Linux provides you with the handy /dev/MAKEDEV program that let's you recreate the device, based on the common name, like fd0 for your first floppy.

Example - Creating a device using /dev/MAKEDEV.

[root@linux_box /]# ls -l /dev/fd0
brw-rw----    1 root     floppy     2,   0 Mar 23  2001 /dev/fd0
[root@linux_box /]# rm -f /dev/fd0
[root@linux_box /]# ls -l /dev/fd0
ls: /dev/fd0: No such file or directory
[root@linux_box /]# /dev/MAKEDEV fd0
[root@linux_box /]# ls -l /dev/fd0
brw-rw-r--    1 root     root       2,   0 Mar  8 06:19 /dev/fd0

Viewing devices in use

If you wish to view the devices that are currently in use you need only view the devices file within the /proc directory.

Example - Displaying devices in use on my machine.

[lwheeler@academic ~]$ cat /proc/devices
Character devices:
  1 mem
  2 pty
  3 ttyp
  4 ttyS
  5 cua
  7 vcs
 10 misc
 29 fb
 36 netlink
128 ptm
129 ptm
130 ptm
131 ptm
132 ptm
133 ptm
134 ptm
135 ptm
136 pts
137 pts
138 pts
139 pts
140 pts
141 pts
142 pts
143 pts
162 raw

Block devices:
  1 ramdisk
  2 fd
  3 ide0
  8 sd
  9 md
 65 sd
 66 sd
 72 ida0

Filesystem Creation and Manipulation

Storage media are devices that can be formatted with a filesystem. There are several different types of filesystems, each with different features that make one the choice for some and not for others. The good thing about a Linux system is that you need not choose one filesystem type exclusively. You can use several different devices, each formatted differently under the same file tree.

Types of filesystems common to Linux/UNIX
Filesystem Description
bfs Boot filesystem - a small bootable filesystem used to hold the files necessary for system startup; common to UNIX systems.
cdfs Compact disc filesystem - used to view all tracks and data on a CD-ROM as normal files.
ext2 Second extended filesystem - currently the most common filesystem used on Linux, it supports Access Control Lists (user permissions). The name is based on the Minix operating system's extended filesystem.
ext3 Third extended filesystem - a variant of the ext2 system, this version allows journalling to enhance faster startup and recovery.
hfs Hierarchical filesystem - the native filesystem on Apple Macintoshes.
hpfs High Performance filesystem - the format that supports long filenames and large volume disks created for use on IBM's OS/2 operating system.
iso9660 The CD-ROM filesystem - used to access data on CD-ROMs.
minix The Minix filesystem - the filesystem used in the early days of Linux development.
msdos The DOS FAT filesystem (8.3 name format).
ntfs New Technology File System - the filesystem for Windows NT and 2000 high-performance use; Linux can mount devices formatted with this type as read-only.
reiserfs The REISERFS filesystem - another journalling filesystem that is designed for use with databases.
vfat The DOS FAT filesystem with support for long names.
vxfs Teh Veritas filesystem - yet another journalling filesystem that also can handle very large files. This filesystem support Access Control Lists for individual user permissions.

Mounting Filesystems

The term mounting refers to the action taken that makes data available. You mount a device to make it accessible to users on the file tree. Devices are connected at a mount point. Commonly people use the mount point /mnt for mounting removeable storage devices, this directory usually contains just two directories cdrom and floppy. Once mounted, the floppy disk's filesystem is the filesystem branching down from the mount point. Any files saved on the /mnt directory will them be saved physically on the floppy disk.

To place a filesystem on the file tree the root user must use the mount command. To uncover directories previously mounted you use the umount command.

Working with floppy disks

In order to make a storage device available for adding files and directories, it must be formatted. Formatting a disk involves using the mkfs command.

The mkfs command

mkfs builds a filesystem on a device. The syntax for mkfs is:

 mkfs  [ -V ] [ -t fstype ] [ fs-options ] filesys [ blocks ]

Formatting a disk device with the ext2 filesystem will make that disk readable by most UNIX system, but not by Windows systems. You can specify files that can be read by both Windows and UNIX systems by using the -t option to format the disk with either msdos or vfat filesystems.

Example - formatting a disk with a msdos filesystem.

[root@linux_box root]# mkfs -t msdos /dev/fd0
  mkfs.msdos 2.2 (06 Jul 1999)

Without any options being used the mkfs command will simply format the disk with the ext2 filesystem type.

Example - using mkfs without any arguments to format a disk with the ext2 filesystem.

[root@linux_box /root]# mkfs /dev/fd0
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
184 inodes, 1440 blocks
72 blocks (5.00%) reserved for the super user
First data block=1
1 block group
8192 blocks per group, 8192 fragments per group
184 inodes per group

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

The fuser command

Before mounting a filesystem you will need to know if the mount point directory is being used, or else your mounting attempt will fail. To see if a filesystem is being used by a particular user or process you can use the fuser command.

In the following example, I will check to see if the /mnt/floppy command is being used by any processes

[root@linux_box floppy]# fuser -u /mnt/floppy
/mnt/floppy:          6507c(kwheeler)

Using the mount command

Again, mounting a filesystem makes a disk device available for use. To do so you need to use the mount command. The mount command's syntax is as follows:

mount [-lhV]

mount -a [-fFnrsvw] [-t vfstype]
mount [-fnrsvw] [-o options [,...]] device | dir
mount [-fnrsvw] [-t vfstype] [-o options] device dir
Some of the mount command options
Command and option Description
mount list mounted filesystems
mount -a mount all devices from /etc/fstab
mount device dir mount specified device (device) at specified mount point (dir)
mount -t type device dir mounts the device on the mount point with a particular filesystem type (type)
mount -t type lists all mounted device of type (type)

Example - mounting a floppy disk device with an ext2 filesystem.

[root@linux_box /root]# mount -t ext2 /dev/fd0 /mnt/floppy
[root@linux_box /root]# mount
/dev/hda5 on / type ext2 (rw)
none on /proc type proc (rw)
/dev/hda1 on /boot type ext2 (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
automount(pid570) on /misc type autofs (rw,fd=5,pgrp=570,minproto=2,maxproto=3)
/dev/fd0 on /mnt/floppy type ext2 (rw)

Using mount without any arguments will display to you the currently mounted devices. This command will read the same information made available to you in the /etc/mtab (mount table) file. Note that the output the mount command displays the devices mounted on parts of your file tree. For example, you can see that the first primary IDE hard disk partition is mounted on the /boot directory.

The /etc/fstab file

Typing man 5 fstab will give you a succinct listing of the types of filesystems you may expect on your machine. This file is used on boot to automount filesystems with the mount -a command. If mounting a device mentioned in fstab it is ok to simply give the device or the mount point, fstab supplies the missing information.

Example - my /etc/fstab file.

[root@linux_box /root]# cat /etc/fstab
LABEL=/                 /                       ext2    defaults        1 1
LABEL=/boot             /boot                   ext2    defaults        1 2
/dev/fd0                /mnt/floppy             auto    noauto,owner    0 0
none                    /proc                   proc    defaults        0 0
none                    /dev/pts                devpts  gid=5,mode=620  0 0
/dev/hda6               swap                    swap    defaults        0 0
/dev/cdrom              /mnt/cdrom              iso9660 noauto,owner,kudzu,ro 0
0
Fields of the fstab file
Number Keyword Description
1 fs_spec the block special device or remote filesystem to be mounted.
2 fs_file the mount point for the filesystem. For swap partitions, this field should be specified as 'none'.
3 fs_vfstype the type of the filesystem.
4 fs_mntops the mount options associated with the filesystem.
5 fs_freq used for these filesystems by the dump(8) command to determine which filesystems need to be dumped. If the fifth field is not present, a value of zero is returned and dump will assume that the filesystem does not need to be dumped.
6 fs_passno used by the fsck(8) program gram to determine the order in which filesystem checks are done at reboot time. Filesystems within a drive will generally be checked sequentially. The root filesystem should be specified with a fs_passno of 1, and other filesystems should have a fs_passno of 2. If the sixth field is not present or zero, a value of zero is returned and fsck will assume that the filesystem does not need to be checked.

Removing a filesystem with the umount command

Removing a filesystem can be done by entering the umount command and a mount point directory. Like the mkfs command you must be certain prior to executing that no process is actively interacting with the device. To do so, use fuser -u and specify the mount point of the device.

Example - umounting the floppy disk device.

[root@linux_box /root]# mount /mnt/floppy
[root@linux_box /root]# cd /mnt/floppy
[root@linux_box floppy]# ls
lost+found
[root@linux_box floppy]# umount /mnt/floppy
umount: /mnt/floppy: device is busy
[root@linux_box /root]# fuser /mnt/floppy
/mnt/floppy:          6627c(root)
[root@linux_box floppy]# cd /root
[root@linux_box /root]# umount /mnt/floppy
[root@linux_box /root]# mount
/dev/hda5 on / type ext2 (rw)
none on /proc type proc (rw)
/dev/hda1 on /boot type ext2 (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
automount(pid570) on /misc type autofs (rw,fd=5,pgrp=570,minproto=2,maxproto=3)

CD-ROMs in Linux

CD-ROMs and the Linux filesystem offer some interesting differences in use when compared to use in Windows operating system environments. CD-ROMs, like hard drives, usually are designed to connect to your computer's mainboard with either an IDE or SCSI cabling configuration. They are thus considered read-only hard disks by the system and can be viewed as a mounted hard disk.

Example - viewing the long listing of the CD-ROM device file.

[kwheeler@linux_box kwheeler]$ ls -l /dev/cdrom
lrwxrwxrwx    1 root     root            8 Feb  7 20:23 /dev/cdrom -> /dev/hdc

CD-ROMs normally use the iso9660 filesystem format for accessing read-only CD-ROM files. The following command line will show the mounting of the read-only filesystem of an IDE CD-ROM device.

[root@linux_box kwheeler]# mount -r -t iso9660 /dev/cdrom /mnt/cdrom
[root@linux_box kwheeler]# mount
/dev/hda5 on / type ext2 (rw)
none on /proc type proc (rw)
/dev/hda1 on /boot type ext2 (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
automount(pid570) on /misc type autofs (rw,fd=5,pgrp=570,minproto=2,maxproto=3)
/dev/hdc on /mnt/cdrom type iso9660 (ro)
[root@linux_box kwheeler]# ls -F /mnt/cdrom
autorun*   COPYING  dosutils/  misc/   RedHat/      rr_moved/
boot.cat*  doc/     images/    README  RPM-GPG-KEY  TRANS.TBL

Why won't my #!?@&! drive door open!

Unmounting (umount) filesystems first, then removing the media is the proper sequence to follow when removing any storage device, however Linux actually will refuse to open the CD-ROM drive door before a proper umount command is issued for it.

Hard Disks

Hard disks on a computer can be arranged based on their connection to the mainboard's technology - SCSI or IDE. SCSI devices are considered superior to IDE because they are faster because they have their own bus transfer, but they can be "daisy chained" to connect several drives and thus provide the high volumes of storage common to the needs of file servers. However, IDE devices are not obsolete, they are very inexpensive and can transfer data at speeds of 15 MB/per second.

SCSI drives are numbered in relation to thier order of connection to the SCSI controller on the motherboard; SCSI drives need not be placed on individual controllers. The number of IDE drives you employ are limited to the number of controllers you have available on your motherboard. They are arranged somewhat differently than SCSI drives in thier recognition by the Linux operating system.

As you recall from the list of common files in the /dev directory, all IDE drives begin with the hd* prefix the letters displayed after the d usually refer to:

and, so on...

Hard Disk Internals

When discussing the physical nature of a hard disk one may hear the terms: platters, tracks, sectors, blocks, and cyclinders.

Hard Disk Components
Component Description
platters A platter is a circular magnetic disk that composes the functional part of a hard disk's storage. Platters can be double-sided and there are usually 12 platters found in the average hard disk.
tracks Tracks are the concentric circles arranged on the platters of the hard drive where data can be written.
sectors Sectors divide tracks into pieces of information on a track. The operating system retrieves information by locating data by track and sector.
cylinder Cylinders are the track locations that cut through the same location on all platters of the hard drive.

The first readable sector of the hard disk stores the MBR (master boot record). The MBR contains the information about the partitions on the disk. If this sector is damaged the hard disk may be unreadable.

Partitioning Drives

Creating partitions is important because:

Recall that installing Linux required that you consider the hard disk's partitioning arrangement. You were asked to, at minimum, create a swap partition for use by your system as virtual memory and a root (/) partition for the system and user files.

Organizing Hard Drives

Organizing a disk involves file names, directory structures and speedy indexing. Some hard drives today offer single drives exceeding 120 GBs on lightly used home systems! This can be a nightmare on a system where filenames must be unique. For this reason (amongst others) it is a good idea to partition (AKA - split up into digestable compenents) your hard disks.

Partition Configurations

Hard drives can be divided into a maximum of 4 primary partitions. In order to provide more partitions developers created extended/logical partitions. You actually place extra filesystems on the logical partitions.

Hard Disk Partitioning Configurations
Partition IDE Device (/dev/hda) SCSI Device (/dev/sda)
1st primary partition /dev/hda1 /dev/sda1
2nd primary partition /dev/hda2 /dev/sda2
3rd primary partition /dev/hda3 /dev/sda3
4st primary partition /dev/hda4 /dev/sda4
1st logical partition /dev/hda5 /dev/sda5
2nd primary partition /dev/hda6 /dev/sda6
3rd primary partition /dev/hda7 /dev/sda7

Creating a Partition

Creating a partition usually follows adding an additional hard drive to your system. Physically adding a drive to your system requires that you refer to your system's documentation for the proper installment guidelines. However, one thing you must remember after you have installed the drive is the interface of which controller you attached it to and whether you assigned it as master or slave. This will help you determine what device file to refer to when preparing the drive to be placed into service.

Example - viewing the /proc/ide to see if my new hard drive has been added.

[root@linux_box /root]# ls -F /proc/ide
drivers  hda@  hdb@  hdc@  ide0/  ide1/
Popular Partition Creation Utilities
Utility Description
parted A disk partitioning and partition resizing program that allows you to create, destroy, resize, move and copy ext2, FAT and FAT32 partitions. Good for creating space for new operating systems, reorganizing disk usage, and copying data to new hard disks.
fdisk Effective, but destructive partitioning option. Can be used after initial installation. The most common utility used to prepare disks newly added to a Linux system.
dd Disk Druid - easy to use utility specially designed for installation only.
FIPS A partitioning software that works on a variety of hardware software platforms that can "re-partition" drives without losing data! Does the job for free that expensive software packages like Partition Magic and System Commander do for a somewhat considerable cost. This software is used to prepare Windows operating systems for dual-booting EXCLUSIVELY.

* The parted utility has been envogue ever since I have been working on this level with Linux, but because it'll normally be RPM'ed after common installation (you may not know how to do that yet) and that this is a UNIX course and fdisk is much more likely to be found on other UNIX family OS'es I will explain partitioning with fdisk.

For a detailed explanation on how to use parted, see the current Red Hat Linux manual's Chapter 5. Managing Disk Storage.

The fdisk utility

The ubiquitous fdisk. We've seen this tool in Windows and DOS... Who knew it had its origins in UNIX?

It's not quite the same in the UNIX world though. It is used for the same purposes though, to remove, create and label disk device partitions. fdisk is a utility like vi, but it does have a few command line options that are useful.

Common fdisk command line options
Option Description
fdisk -l device_file Lists the partition table for the specified device.
fdisk -lu device_file Lists the partition table for the specified device in sectors instead of cylinders.
fdisk -s partition_file Lists the size of a partition in blocks in the standard output.

Example - The follow example shows the difference between the -l and -lu to view a device's capacity and the size of an individual partition using the -s option.

[root@linux_box /root]# fdisk -ul /dev/hdc

Disk /dev/hdc: 128 heads, 63 sectors, 620 cylinders
Units = sectors of 1 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdc1            63   4999679   2499808+  83  Linux
[root@linux_box /root]# fdisk -l /dev/hdc

Disk /dev/hdc: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdc1             1       620   2499808+  83  Linux
[root@linux_box /root]# fdisk -s /dev/hdc1
2499808

Viewing a drive's contents

To determine the contents of one's new hard drive is truly empty you can either:

  1. type, fdisk -l /dev/hdc, if your new drive is an IDE device connected to the second IDE controller's master interface (/dev/hdc).
[root@linux_box /root]# fdisk -l /dev/hdc

Disk /dev/hdc: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes
If the drive is not empty the output would look something like this:
Disk /dev/hdb: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdc1             1       620   2499808+   c  Win95 FAT32 (LBA)
Partition 1 has different physical/logical endings:
     phys=(617, 127, 63) logical=(619, 127, 63)
  1. Enter the interactive fdisk utility and press the p key to view the existing partition table.
[root@linux_box /root]# fdisk /dev/hdc

Command (m for help): p

Disk /dev/hdb: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdc1             1       620   2499808+   c  Win95 FAT32 (LBA)
Partition 1 has different physical/logical endings:
     phys=(617, 127, 63) logical=(619, 127, 63)

Command (m for help):

Deleting partitions

If you wanted to delete a partition with fdisk you merely need to enter fdisk, press the d command and supply a partition identity.

[root@linux_box /root]# fdisk /dev/hdb

Command (m for help): p

Disk /dev/hdc: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdc1             1       620   2499808+   c  Win95 FAT32 (LBA)
Partition 1 has different physical/logical endings:
     phys=(617, 127, 63) logical=(619, 127, 63)

Command (m for help):

In the interest of safety fdisk has two methods for exiting the utility after making changes to a disk device - exiting without saving changes or writing to the partition table then exiting.

Example - To exit and save after deleting the partition above press w, you will see output similar to the below.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Re-read table failed with error 16: Device or resource busy.
Reboot your system to ensure the partition table is updated.

WARNING: If you have created or modified any DOS 6.x partitions, please see the fdisk manual page for additional information. Syncing disks.

Creating new partitions

The n command creates new partitions. When creating a partition you must know what type of partition you are going to create, what partition id you are going to give it, and how much space you wish to assign to it.

Example - Below I take my newly blank drive and create a new primary partition (hdc1) and give it approximately half of the space available on the drive by specifying the number of cylinders.

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-620, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-620, default 620): 310

Command (m for help): p

Disk /dev/hdc: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdc1             1       310   1249888+  83  Linux

You could've specified the size in megabytes by using notation like below, where I make the first partition only a gigabyte (1000 megabytes).

Last cylinder or +size or +sizeM or +sizeK (1-620, default 620): +1000M

Command (m for help): p

Disk /dev/hdb: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdb1             1       254   1024096+  83  Linux

Changing the partition's system id

In the above examples, you may have noticed that the new partitions created are by default Linux partitions. If you wanted to create a new Linux swap partition or a partition for Windows you need to change the partition's system id with the t command.

Example - creating a Window 95 (LBA) partition with fdisk.

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): L

 0  Empty           1b  Hidden Win95 FA 63  GNU HURD or Sys b7  BSDI fs
 1  FAT12           1c  Hidden Win95 FA 64  Novell Netware  b8  BSDI swap
 2  XENIX root      1e  Hidden Win95 FA 65  Novell Netware  c1  DRDOS/sec (FAT-
 3  XENIX usr       24  NEC DOS         70  DiskSecure Mult c4  DRDOS/sec (FAT-
 4  FAT16 <32M      39  Plan 9          75  PC/IX           c6  DRDOS/sec (FAT-
 5  Extended        3c  PartitionMagic  80  Old Minix       c7  Syrinx
 6  FAT16           40  Venix 80286     81  Minix / old Lin da  Non-FS data
 7  HPFS/NTFS       41  PPC PReP Boot   82  Linux swap      db  CP/M / CTOS / .
 8  AIX             42  SFS             83  Linux           de  Dell Utility
 9  AIX bootable    4d  QNX4.x          84  OS/2 hidden C:  e1  DOS access
 a  OS/2 Boot Manag 4e  QNX4.x 2nd part 85  Linux extended  e3  DOS R/O
 b  Win95 FAT32     4f  QNX4.x 3rd part 86  NTFS volume set e4  SpeedStor
 c  Win95 FAT32 (LB 50  OnTrack DM      87  NTFS volume set eb  BeOS fs
 e  Win95 FAT16 (LB 51  OnTrack DM6 Aux 8e  Linux LVM       ee  EFI GPT
 f  Win95 Ext'd (LB 52  CP/M            93  Amoeba          ef  EFI (FAT-12/16/
10  OPUS            53  OnTrack DM6 Aux 94  Amoeba BBT      f1  SpeedStor
11  Hidden FAT12    54  OnTrackDM6      9f  BSD/OS          f4  SpeedStor
12  Compaq diagnost 55  EZ-Drive        a0  IBM Thinkpad hi f2  DOS secondary
14  Hidden FAT16 <3 56  Golden Bow      a5  BSD/386         fd  Linux raid auto
16  Hidden FAT16    5c  Priam Edisk     a6  OpenBSD         fe  LANstep
17  Hidden HPFS/NTF 61  SpeedStor       a7  NeXTSTEP        ff  BBT
18  AST Windows swa
Hex code (type L to list codes): c
Changed system type of partition 1 to c (Win95 FAT32 (LBA))

Command (m for help): p

Disk /dev/hdb: 128 heads, 63 sectors, 620 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdb1             1       310   1249888+   c  Win95 FAT32 (LBA)

Command (m for help):

A word to the wise about fdisk

As appropriate for some tasks as fdisk might be, I seriously suggest learning the other tools such as parted. Even fdisk's man pages will say that it has strange errors associated with it.

Completing the preparation of a new disk for Linux

If you have prepared a partition for Linux when you have completed and saved your work in fdisk, exit with w and reboot your system as instructed. When the system comes backup you will have a clean drive partition just waiting for a filesystem so it can be put to work storing all of your system's important files.

Example - formatting the new drive partition with the ext2 filesystem.

[root@linux_box /root]# mkfs -t ext2 /dev/hdb1
mke2fs 1.19, 13-Jul-2000 for EXT2 FS 0.5b, 95/08/09
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
312960 inodes, 624952 blocks
31247 blocks (5.00%) reserved for the super user
First data block=0
20 block groups
32768 blocks per group, 32768 fragments per group
15648 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

To finish up, edit your system's filesystem table (/etc/fstab) to make the drive automatically mounted to a specified part of the file tree on boot.

Example - I decided that this new space should be used for my mysql database so I created a directory (/data) and mounted the device, then edited /etc/fstab to automount the device next boot.

[root@linux_box /root]# cat session_log
[root@linux_box /root]# mkdir /data
[root@linux_box /root]# mount -t ext2 /dev/hdb1 /data
[root@linux_box /root]# cp /etc/fstab backups/fstab
[root@linux_box /root]# cat >> /etc/fstab
/dev/hdb1               /data                   ext2    defaults        0 0

Management of the Linux Filesystem

Your system administration runbooks should specify a regular schedule for a systematic check of disk devices for errors, disk space usage, and inode usage. A system maintained this way is much less likely to cause you problems due to damaged filesystems and/or running out of disk space when a user's doctoral thesis is in the middle of saving (thus, causing you to get fired).

System Robustness

The buzzword, robustness, is common today. Webopedia defines robustness as a quality of computer systems that acheive these criteria:

Partitioning your hard drive to provide for different filesystems can make your system more robust

File Systems and Disk Usage

Now that you know that you should be striving for a robust system, let's figure out a game plan.

Filesystems would benefit by having disk space allocated with the future in mind. When installing their systems, many seasoned system administrators create filesystems for the /, /usr, and /var directories. This is because the /usr directory is used for most utilities and programs installed on your system, keep these safe and give this directory plenty of space. The /var directory is a living thing, logs are generated everytime your system does something - not only should you partition a filesystem for this directory and give it space, you should purge old log files on a regular basis. The / (root) directory is the most important directory (well, I guess without the /boot directory contains the kernel, but the root is in the name). It needs space so that the operating system can have space to do its job. You must monitor the space on this directory to assure your system is in good shape. Wisdom shows that any / filesystem that falls below 10% capacity is in trouble.

How do you monitor disk space?

The df command

The df command is a good way to monitor the free space remaining on your system.

Example - output of the df utility.

[root@linux_box /root]# df
Filesystem           1k-blocks      Used Available Use% Mounted on
/dev/hda5               878996    641708    192636  77% /
/dev/hda1                48770      3479     42773   8% /boot
/dev/hdc1              2460480        20   2335472   1% /data

The -h option can simplify the mental math by showing volumes in units familiar to computer users giga- and mega-bytes.

[root@linux_box /root]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda5             858M  627M  188M  77% /
/dev/hda1              48M  3.4M   41M   8% /boot
/dev/hdb1             2.3G   20k  2.2G   1% /data
Directory-level monitoring with du

When you begin to run out of space you may need to view which directory is taking up the space. To do this page a directory with the du command using less (or more).

[root@linux_box /root]# du /root | sort -n | less
4       /root/bin
4       /root/.gnome/apps
4       /root/.gnome-desktop/Trash
4       /root/.gnome/gnorpm.d/resources/distribs
4       /root/.gnome_private
4       /root/.netscape/archive
4       /root/nsmail
4       /root/second_hdb
4       /root/.xauth
8       /root/backups
8       /root/.gnome/gnorpm.d/resources
8       /root/.netscape/xover-cache/host-news
8       /root/.sawfish/sessions
12      /root/.gnome/accels
12      /root/.gnome/gnorpm.d
12      /root/.netscape/xover-cache
12      /root/.sawfish
16      /root/.gnome-desktop
16      /root/.mc
20      /root/.netscape/cache
40      /root/.gnome/panel.d/default
44      /root/.gnome/panel.d
144     /root/.gnome
312     /root/.netscape
676     /root

The du command also can use the -h option to display units. You can also view the directory specified's total size with the -s option.

[root@linux_box /root]# du -s /etc
4236    /etc
[root@linux_box /root]# du -sh /etc
4.2M    /etc

Account for inodes with dumpe2fs

Earlier you may have read that the inode table of a filesystem is used to keep track of files and directories by pointing to their locations on the physical media (their blocks). In most cases, the number of inodes available are similar in size to the filesystem. Occaisionally a system will have a dirty trick played on it by a soon-to-be-disabled user account who is silly enough to write this script:

while 1
	mkdir adir
	cd adir
	touch afile
end

This script will run infinitely until the space allocated to the inode table has run out and though you may have tons of disk space left, your users will not be able to create files because there is no more space in the inode table.

Viewing the capacity of the inode table is done with dumpe2fs -h command.

[root@linux_box /root]# dumpe2fs -h /dev/hda5
dumpe2fs 1.19, 13-Jul-2000 for EXT2 FS 0.5b, 95/08/09
Filesystem volume name: /
Last mounted on: <not available>
Filesystem UUID: ac281fec-3ad4-11d7-86e7-b0e77d6d3d1d
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: filetype sparse_super
Filesystem state: not clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 111776
Block count: 223264
Reserved block count: 11163
Free blocks: 59321
Free inodes: 63340
First block: 0
Block size: 4096
Fragment size: 4096
Blocks per group: 32768
Fragments per group: 32768
Inodes per group: 15968
Inode blocks per group: 499
Last mount time: Sat Mar 15 00:52:57 2003
Last write time: Sat Mar 15 02:18:03 2003
Mount count: 4
Maximum mount count: 20
Last checked: Sun Feb 23 14:12:41 2003
Check interval: 15552000 (6 months)
Next check after: Fri Aug 22 15:12:41 2003
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128

In the above example you can see that I have 63340 inodes available yet on my filesystem. No problem.

Checking for Filesystem Errors

Filesystems get corrupted over time. This can happen when power is cut without shutting down properly or similar circumstances where activity in memory does not acheive permanance on the hard drive. The process on UNIX systems where memory is saved on the hard drive is called syncing and normally occurs when a system is being shutdown or rebooted.

What is a bad block?

Hard drives are only a collection of fast-spinning metal disks that can be magnetized to represent the present/not-present binary format that computers read. A bad block is a situation where a part of the disk is unable to hold an electric charge and is therefore not able to be magnetized. These situations are unavoidable, the operating system simply takes note of that block's location and avoids it. Heavy activity on the hard disk, like what goes on in your typically heavy-traffic RDBMS system can cause bad blocks to turn up on your disk.

Checking for disk errors with fsck

The fsck command checks your filesystem for errors. Your system is configured normally to automatically sync and fsck when booting and shutting down, but you can run the utility if need be manually. To use fsck you need to umount the filesystem in question and specify filesystem you are checking.

Example - fsck'ing my floppy disk in device /dev/fd0.

[root@linux_box /root]# mount
/dev/hda5 on / type ext2 (rw)
none on /proc type proc (rw)
/dev/hda1 on /boot type ext2 (rw)
none on /dev/pts type devpts (rw,gid=5,mode=620)
automount(pid579) on /misc type autofs (rw,fd=5,pgrp=579,minproto=2,maxproto=3)
/dev/hdb1 on /data type ext2 (rw)
/dev/fd0 on /mnt/floppy type ext2 (rw)
[root@linux_box /root]# ls /mnt/floppy
fdisk_out  inittab.copy  io_out      mkfs_out     sorted      XF86Config.bak
fstab      inode_out     io_outerr   sample.c     telnet.bak
fuser_out  io_err        lost+found  session_log  users
[root@linux_box /root]# umount /mnt/floppy
[root@linux_box /root]# fsck -t ext2 /dev/fd0
Parallelizing fsck version 1.19 (13-Jul-2000)
e2fsck 1.19, 13-Jul-2000 for EXT2 FS 0.5b, 95/08/09
/dev/fd0: clean, 26/184 files, 74/1440 blocks

The -f option of fsck will do a more thorough check of your device if used.

Common options used with the fsck command
Option Description
fsck -f device_file Full filesystem check on device.
fsck -a device_file Repairs errors automatically.
fsck -A Check all filesystems in the /etc/fstab file.
fsck -Cf device_file Does a full filesystem check with a process bar.
fsck -AR Checks all filesystems in the /etc/fstab file with a 1 or 2 in 6th field (skips the / filesystem).
fsck -V device_file fsck with verbose output.

When correcting filesystem errors fsck will prompt you to correct them unless you have specified tha -a option, which will fix any errors automatically. Any problems fsck cannot fix is placed in the lost+found directory on the filesystem's root and the file is renamed to its inode number. The lost+found directory applies only to ext2 or ext3 filesystems.

Rationing out Hard Disks

When a UNIX system has users, space issues must be judiciously handled. Allocate plenty of space the /home directory and create a policy that lets your users know what should be stored on the system and how much space they are allotted. The allotted space is often referred to as a hard disk quota. When referring to hard disk quotas there are two types hard and soft limits.

Hard Limit
Prevents users from going past their limit on disk allowance.
Soft Limit
Allows users to exceed their limits for a certain amount of time.

The steps to creating quotas for users and groups on your system can viewed at the Red Hat Linux Manuals - Implementing Disk Quotas chapter.