Disk Storage
************

All files and directories on a Linux-based system are stored on a Linux filesystem, which is a disk device (such as a hard drive) that is formatted to store a directory tree (see Files and Directories).

There are two kinds of disk storage on a Linux system: fixed and removable. Fixed storage refers to a disk that is firmly attached to the computer system, and is not intended for casual removal (except when upgrading). Your hard drive (sometimes called ``hard disk''), used to store the operating system, application software, and user data, is the prime example of a fixed disk.

The second kind of disk storage is removable storage, disks that are intended to be removed for archiving or transfer to another system. Common examples of removable storage are floppy disk (or ``diskette'') and CD-ROM drives, where you typically remove the storage media from its drive bay when you're done using it.

On Linux systems, disks are used by mounting them to a directory, which makes the directory tree the disk contains available at that given directory mount point. Disks can be mounted on any directory on the system, but any divisions between disks are transparent---so a system which has, aside from the root filesystem disk mounted on /, separate physical hard disks for the /home, /usr, and /usr/local directory trees will look and feel no different from the system that only has one physical disk.

System administrators often mount high-capacity drives on directory trees that will contain a lot of data (such as a /home directory tree on a system with a lot of users), and for purposes of fault tolerance, administrators often use several physical hard disks on one system---if there is a disk failure, only the data in that disk is lost.

This chapter describes tools and techniques for manipulating disks and storage media.

Listing a Disk's Free Space
===========================

To see how much free space is left on a disk, use df. Without any options, df outputs a list of all mounted filesystems. Six columns are output, displaying information about each disk: the name of its device file in /dev; the number of 1024-byte blocks the system uses; the number of blocks in use; the number of blocks available; the percent of the device used; and the name of the directory tree the device is mounted on.

  * To see how much free space is left on the system's disks, type:

          $ df RET
          Filesystem         1024-blocks  Used Available Capacity Mounted on
          /dev/hda1             195167   43405   141684     23%   /
          /dev/hda2            2783807  688916  1950949     26%   /usr
          /dev/hdb1            2039559 1675652   258472     87%   /home/webb
          $

This example shows that three filesystems are mounted on the system---the filesystem mounted on / is at 23 percent capacity, the filesystem mounted on /usr is at 26 percent capacity, and the filesystem mounted on /home/webb, a home directory, is at 87 percent capacity.

Listing a File's Disk Usage
===========================

Use du to list the amount of space on disk used by files. To specify a particular file name or directory tree, give it as an argument. With no arguments, du works on the current directory.

It outputs a line for each subdirectory in the tree, listing the space used and the subdirectory name; the last line lists the total amount of space used for the entire directory tree.

  * To output the disk usage for the directory tree whose root is the current directory, type:

          $ du RET
          8       ./projects/documentation
          12      ./projects/source
          4       ./projects/etc
          24      ./projects
          3       ./tmp
          27      .
          $

This example shows two subdirectories in the directory tree: projects and tmp; projects contains three additional directories. The amount of disk space used by the individual directories is the total on the last line, 27K.

By default, output is in 1K blocks, but you can specify another unit to use as an option: -k for kilobytes and -m for megabytes.

  * To output the disk usage, in kilobytes, of the /usr/local directory tree, type:

          $ du -k /usr/local RET

  * To show the number of megabytes used by the file /tmp/cache, type:

          $ du -m /tmp/cache RET

Use the -s option (``summarize'') to output only the last line containing the total for the entire directory tree. This is useful when you are only interested in the total disk usage of a directory tree.

  * To output only the total disk usage of the /usr/local directory tree, type:

          $ du -s /usr/local RET

  * To output only the total disk usage, in kilobytes, of the /usr/local directory tree, type:

          $ du -s -k /usr/local RET

Floppy Disks
============

Before you can use a floppy disk for the first time, it must be formatted, which creates an empty filesystem on the disk.

To read or write files to a formatted disk, you mount the floppy on an empty directory, making its filesystem available in the specified directory. Usually, Linux systems have an empty /floppy directory for this purpose. (Another general-purpose directory for mounting filesystems is the /mnt directory.)

NOTE: While you cannot mount a filesystem on a directory containing other files, you can always create a new directory somewhere to mount a filesystem.

When you mount a disk on a directory, that directory contains all the files and directories of the disk's filesystem; when you later unmount the disk, that directory will be empty---all the files and directories on the disk are still on the disk's filesystem, but the filesystem is no longer mounted.

When you're done using a floppy, you must unmount it first before you remove it from the drive. If you don't, you risk corrupting or deleting some of the files on it---Linux may still be using the mounted files when you remove the disk (see Turning Off the System).

The following sections show you how to format, mount, and unmount floppies. On many systems, you need superuser privileges to do any one of these actions.

NOTE: For recipes describing use of MS-DOS (and Microsoft Windows) formatted disks under Linux, see Using DOS and Windows Disks.

Formatting a Floppy Disk
------------------------

Use mke2fs to format a floppy and make a Linux filesystem. Give the name of the device file of the floppy drive as an argument---usually the first removable disk drive, /dev/fd0. The floppy must be in the drive when you give the format command, and any data already on it will be lost.

  * To format a floppy disk in the first removable floppy drive, type:

          $ mke2fs /dev/fd0 RET

Mounting a Floppy Disk
----------------------

To mount a floppy, use mount with the /floppy option. (This works if your administrator has set up the floppy drive filesystem for user access---see Letting Users Access Hardware Peripherals.)

  * To mount a floppy, type:

          $ mount /floppy RET

To mount a floppy to a specific directory, use mount and give as arguments the device name of the floppy drive (usually /dev/fd0 for one-floppy systems) and the name of the directory to mount to.

  * To mount the floppy in the first floppy drive to ~/tmp, type:

          $ mount /dev/fd0 ~/tmp RET

Once you have mounted a floppy, its contents appear in the directory you specify, and you can use any file command on them.

  * To list the contents of the base directory of the floppy mounted on /floppy, type:

          $ ls /floppy RET

  * To list the contents of the entire directory tree on the floppy mounted on /floppy, type:

          $ ls -lR /floppy RET

NOTE: You can copy files to and from the directory tree that the floppy is mounted on, make and remove directories, and do anything else you could on any other directory tree. But remember, before you remove it, you must first unmount it.

Unmounting a Floppy Disk
------------------------

Use umount to unmount a floppy disk, using the name of the directory it is mounted on as an argument.

  * To umount the floppy that is mounted on /floppy, type:

          $ umount /floppy RET

NOTE: You can't unmount a disk if your current working directory, the directory you are in, is somewhere in that disk's directory tree. (This is sometimes called being ``under the mount point'' of the disk.) In this case, trying to unmount the disk will give the error that the /floppy filesystem is in use; change to a different directory that isn't in the /floppy directory tree, and then you can unmount the disk.

Sometimes when you unmount a floppy, the light on the floppy drive will go on and remain on for a few seconds after it has been unmounted. This is because Linux sometimes keeps changes to files in memory before it writes them to disk; it's making sure that the files on the floppy are up-to-date. Simply wait until the light goes off before you remove the floppy from the drive.

CD-ROMs
=======

As with a floppy disk, before you can use a data CD (compact disc) on your system, you must first mount it on an empty directory. You then unmount it from the directory before you can eject the CD from the CD-ROM drive (you can also eject the disc using software---see Ejecting an Audio CD).

NOTE: To use audio CDs, see Audio Compact Discs.

Mounting a CD-ROM
-----------------

To mount a CD-ROM on the system, use mount with the /cdrom option. (This works if your administrator has set up the CD-ROM drive filesystem for user access---see Letting Users Access Hardware Peripherals.)

  * To mount a CD-ROM on the system, type:

          $ mount /cdrom RET

This command makes the contents of the CD-ROM available from the /cdrom directory tree. You can use any Linux file command on the files and directories on a CD-ROM, but you can't write to a CD-ROM---the CD-ROM format is read-only, so you can read the disc but not write to it.

Like the /floppy directory, the use of the /cdrom directory is a standard practice and convenient, but not necessary---you can mount disks in whatever empty directory you like. (You could even, for example, mount discs from the CD-ROM drive to /floppy and mount floppy disks to /cdrom, but why would anyone do that!)

To mount a CD-ROM to a specific directory, use mount and give as arguments the name of the device file in /dev corresponding to the CD-ROM drive, and the name of the directory to mount to. This directory must already exist on the filesystem, and must be empty. If it doesn't exist, use mkdir to create it first (see Making a Directory).

Most Linux systems are set up so that the device file of the first CD-ROM drive is /dev/cdrom, but the name of the device file may be different, especially if you have a SCSI CD-ROM drive.

  * To mount the disc in the CD-ROM drive to the /usr/local/share/clipart directory, type:

          $ mount /dev/cdrom /usr/local/share/clipart RET

The contents of the disc in the CD-ROM drive will then be available in the /usr/local/share/clipart directory tree, and you can then use the files and directories on the CD-ROM as you would any other files. For example:

  * To peruse a directory tree graph of the CD-ROM's contents, type:

          $ tree /usr/local/share/clipart | less RET

  * To change to the root directory of the CD-ROM, type:

          $ cd /usr/local/share/clipart RET

  * To list the contents of the root directory of the CD-ROM, type:

          $ ls /usr/local/share/clipart RET

Unmounting a CD-ROM
-------------------

Use umount to unmount a CD-ROM; give as an argument the name of the directory it's mounted on.

  * To unmount the disc in the CD-ROM drive mounted on /cdrom, type:

          $ umount /cdrom RET

NOTE: As with unmounting any kind of filesystem, make sure that none of the files on the disc are in use, or you won't be able to unmount it. For example, if the current working directory in a shell is somewhere inside the /cdrom directory tree, you won't be able to unmount the CD-ROM until you change to a different directory.