Red Hat Linux 7 Unleashed

Red Hat Linux 7 Unleashed

By William Ball

Various Kinds of Hardware

There are block devices under Linux for representing all sorts of random access devices: diskettes, hard disks (XT, EIDE, and SCSI), Zip drives, CD-ROM drives, RAM disks, and loopback devices.

Hard Disks

Hard disks are large enough to make it useful to keep different filesystems on different parts of the hard disk. The scheme for dividing these disks is called partitioning. Although it is common for computers running MS-DOS to have only one partition, it is possible to have several different partitions on each disk. The summary of how the disk is partitioned is kept in its partition table.

The Partition Table

A hard disk might be divided like this:

$ fdisk –l
Disk /dev/hda: 128 heads, 63 sectors, 970 cylinders
Units = cylinders of 8064 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *         1       177    713632+   6  FAT16
/dev/hda2           179       970   3193344    5  Extended
/dev/hda3           178       178      4032    a  OS/2 Boot Manager
/dev/hda5           179       696   2088544+   6  FAT16
/dev/hda6           920       970    205600+   6  FAT16
/dev/hda7           697       762    266080+  82  Linux swap
/dev/hda8           763       919    632992+  83  Linux

Disk /dev/hdb: 255 heads, 63 sectors, 523 cylinders
Units = cylinders of 16065 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdb1   *         2       383   3068415    5  Extended
/dev/hdb2           384       523   1124550   83  Linux
/dev/hdb5             2       192   1534176    6  FAT16
/dev/hdb6           193       383   1534176    6  FAT16

Note that the partitions on the first disk have names starting with /dev/hda and those on the second have names starting with /dev/hdb. The number of the partition follows these prefixes.

Disk Geometry

The units of the table in the last section are cylinders. The partition table allocates a consecutive block of cylinders to each partition. The term cylinder itself dates from the days when it was possible to remove a disk pack from a UNIX machine and point to the various parts. That can't be done here, so here's another way of looking at it.

Imagine that a hard disk is in fact a stack of pizzas. Each of the pizzas is a platter, a disk-shaped surface with a magnetic coating designed to hold magnetic encodings. Both sides of these platters are used. These platters rotate around the spindle, like the spindle in a record player. The hard disk has a movable arm containing several disk heads. Each side of each platter has a separate disk head. If you were to put your fingers between the pizzas while keeping them straight, your fingers are the same as the arrangement of the heads on the arm. All the parts of the platters that the heads pass over in one rotation of the disk is called a cylinder. The parts of a single platter that one head passes over in one rotation is called a track. Each track is divided into sectors, as if the pizzas had been already sliced for you. The layout of a disk, its geometry, is described by the number of cylinders, heads, and sectors comprising the disk. Another important feature is the rotational speed of the disk; generally, the faster it is, the faster the hard disk can read or write data.

Floppy Disk Drivers

Diskettes, sometimes referred to as floppy disks, are removable low-capacity storage media. As storage devices, they are far slower than hard disks, but they have the advantage of being removable, which makes them good media for transporting modest amounts of data.

The block devices corresponding to the floppy disks drivers, as they are called for legacy reasons, begin with the letters fd; /dev/fd0 is the first, and any additional ones have increasing numbers. There are many possible formats for a diskette, and the kernel needs to know the format (geometry) of a disk to access it properly. Linux can usually work out the correct format, so the automatic devices /dev/fd0 (plus /dev/fd1 and so on for extra floppy drives) are usually sufficient. If for some reason it is necessary to specify the exact format, further device names are provided. The device /dev/fd0H1440, for example, denotes a 1.44MB high-density diskette. There are many more devices indicating obscure formats, both older lower-capacity formats and other nonstandard extra–high-capacity formats. You can even create your own diskette formats using the setfdprm program.

The most common reason to use the specific-format device names is that you are formatting a diskette for the first time. In this situation, the disk is not yet readable, so the kernel will not be able to autoprobe it for an existing format. You need to use the name, for example /dev/fd0H1440, to denote a high-density 3.5-inch disk in the first floppy drive. For device names representing other formats, refer to the fd manual page. Section 4 of the manual is devoted to devices.

The process of formatting a diskette is completely destructive to the data on it, and because it requires writing to the actual device itself, it requires root privileges. It is done like this:

# fdformat /dev/fd0H1440
Double-sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.
Formatting ... done
Verifying ... done

After you have formatted a floppy, don't forget to use mkfs to build a filesystem on it. (See the section Creating New Filesystems earlier in this chapter.)

If you have the X Window System running, you can use the user mount tool previously discussed in this chapter to accomplish the same tasks.

CD-ROM Drives

CD-ROM drives are fundamentally another kind of read-only block device. They are mounted in just the same way as other block devices. CD-ROMs almost always contain standard ISO 9660 filesystems, often with some optional extensions. There is no reason, however, why you should not use any other filesystem. Once you have mounted your CD-ROM, it behaves like any other read-only filesystem.

You can set up and mount your CD-ROM drive using the Red Hat File System Manager, as explained previously, or by using the mount command:

# mount -t iso9660 /dev/cdrom /mnt/cdrom

The directory /mnt/cdrom is a common place to mount your CD-ROM drive under Red Hat Linux.

The device name /dev/cdrom is commonly used as a symbolic link to the actual device name corresponding to the CD-ROM. This is because at the time the CD-ROM drive became available for the PC, there was no cheap standard interface for these devices. Each manufacturer chose or invented an interfacing scheme that was incompatible with everyone else's. For this reason, there are about a dozen different drivers for CD-ROM drives available in the Linux kernel. SCSI would have been a sensible standard to use, but although SCSI CD-ROM drives are available, they're not particularly popular.

The ATAPI standard arrived in time to ensure that all non-SCSI CD-ROM drives at quad speed or faster use a standard interface, so the situation is far simpler for new CD-ROM drives. Support for ATAPI CD-ROMs is handled by one driver for all drives. The ATAPI standard also provides for very large hard disk drives and tape drives. ATAPI CD-ROM drives are attached to IDE interfaces, just like hard disks, and they have the same set of device names as hard disk devices.

Because CD-ROMs come already written, there is no need to partition them. They are accessed using the device names for whole-disk devices: /dev/hda, /dev/hdb, and so on.

The ISO 9660 standard specifies a standard format for the layout of data on CD-ROMs. It restricts filenames to no more than 32 characters, for example. Most CD-ROMs are written with very short filenames for compatibility with MS-DOS. To support certain UNIX features such as symbolic links and long filenames, developers created a set of extensions called Rock Ridge, and the Linux kernel will automatically detect and use the Rock Ridge extensions.

CD-ROM drives also usually support the playing of audio CDs, and there are many Linux programs for controlling the CD-ROM drive in the same way as you might control a CD player. The multimedia package on the Red Hat 7 CD-ROMs contains the xplaycd program for playing CDs. To make it work, you need to set the /dev/cdrom symbolic link to point to your real CD-ROM device.

If you have the X Window System running, you can use the user mount tool previously discussed in this chapter to accomplish the same tasks by clicking buttons.

RAID

RAID is an acronym for Redundant Array of Inexpensive Disks. Using RAID it is possible to combine several disks in various ways and access them as one. In fact, under Linux it is possible to include just about any kind of block devices in a RAID set. There are several different levels of RAID; each level offers different ways of combining the underlying devices. The following RAID levels are the most used with Linux:

Linux supports both hardware and software RAID. To use hardware RAID you will need a RAID controller, which is a specialized hard drive interface. This controller creates an array of disks and presents these as a single drive to the operating system. Having these abilities in hardware reduces the load on the system. These kinds of controllers are generally rather expensive. Linux supports a number of RAID controllers. If you are about to purchase one or have access to one, be sure to check /usr/src/linux/Documentation to see whether it is supported by Linux. Because different RAID controllers have different user interfaces, we will not cover such controllers here.

As mentioned, Linux also supports software RAID. This offers the same capabilities as the hardware version. The main difference is that it puts a bigger load on the system. Apart from having software RAID support in the Linux kernel, there is a suite of tools to set up and maintain RAID disks on your system.

Don't include IDE disks that are on the same controller in a RAID set. Switching between a slave and a master IDE disk is quite slow. If you have such disks in a RAID set they will constantly need to switch back and forth. This will result in a huge performance hit. Should you also be using disk space on the same disks or controller for swap memory as well, your system can become excruciatingly slow. If you have to use IDE disks for your RAID set, be sure to put them on different IDE interfaces.

The following commands are used to create and use software RAID sets:

The configuration file for a RAID set is not at all hard to set up. Take a look at Listing 18.1, which is one of the sample configuration files that comes with the RAID tool suite. The usage and format of the options should be pretty obvious.

Example 18.1. raid0.conf.sample

# Sample raid-0 configuration

raiddev                 /dev/md0

raid-level              0    # it's not obvious but this *must* be
                             # right after raiddev

persistent-superblock   0    # set this to 1 if you want autostart,
                             # BUT SETTING TO 1 WILL DESTROY PREVIOUS
                             # CONTENTS if this is a RAID0 array created
                             # by older raidtools (0.40-0.51) or mdtools!

chunk-size              16

nr-raid-disks           2
nr-spare-disks          0

device                  /dev/hda1
raid-disk               0

device                  /dev/hdb1
raid-disk               1

After you have created a RAID set you can use it like you would an ordinary hard disk under Linux. For example, suppose you had just created the RAID set from the previous listing. You would use the mke2fs, if you wanted to use that filesystem, to create a filesystem on your new RAID device. After this you can mount the newly create device on a mount point and use it as you would any other device under Linux.

For more exhaustive coverage of how to use software RAID with Red Hat Linux, see http://www.redhat.com/support/docs/tips/raid/raid-index.html.

Loopback Devices

Loopback devices enable you to store new filesystems inside regular files. You might want to do this to prepare an emulated hard disk image for DOSEMU, an install disk, or just to try a filesystem of a new type or an ISO9660 CD-ROM image before writing it to the CD writer.

Mounting Filesystems on Files

Under UNIX, you need root permissions to change the system's filesystem structure; even if you own a file and the mount point on which you want to mount it, only root can do this, unless the user option has been specified in /etc/fstab for this filesystem.

When a filesystem is mounted using the loopback driver, the file containing the filesystem plays the role of the block device in the mount command and /etc/fstab. The kernel talks to the block device interface provided by the loopback device driver, and the driver forwards operations to the file:

# mount $(pwd)/rtems.iso -t iso9660 -o ro,loop /mnt/test
# ls -F /mnt/test
INSTALL   LICENSE   README   SUPPORT   c/   doc/   rr_moved/
# mount | grep loop | fold -s
/home/james/documents/books/Sams/Linux-Unleashed-2/ch9/tmp/rtems.iso on
/mnt/test type iso9660 (ro,loop=/dev/loop0)
# umount /mnt/test

After the loopback filesystem is mounted, it's a normal filesystem.

Using Encrypted Filesystems

Loopback filesystems offer even more features—encryption, for example. A loopback filesystem can be configured to decrypt data from the block device on-the-fly so that the data on the device is useless to people even if they can read it—unless they have the password. The mount command prompts for the password at the appropriate time. To make this work, first you have to use mkfs to generate a filesystem on the encrypted block device; losetup is used to associate a loop device and encryption method with the block device you want to use (in the following case, a floppy drive):

# /sbin/losetup -e DES /dev/loop0 /dev/fd1
Password:
Init (up to 16 hex digits):
# /sbin/mkfs -t ext2 -m0 /dev/loop0
mke2fs 1.10, 24-Apr-97 for EXT2 FS 0.5b, 95/08/09
Linux ext2 filesystem format
Filesystem label=
360 inodes, 1440 blocks
0 blocks (0.00) reserved for the super user
First data block=1
Block size=1024 (log=0)
Fragment size=1024 (log=0)
1 block group
8192 blocks per group, 8192 fragments per group
360 inodes per group

Writing inode tables: done
Writing superblocks and filesystem accounting information: done
# losetup -d /dev/loop0

As shown previously, losetup's -e option associates an encryption method and block device with a loopback device. The -d option deletes this association and erases the stored encryption key.

When the filesystem has been created on the encrypted device, it can be mounted in a manner similar to the normal case:

# /sbin/losetup -d /dev/loop0
# mount /dev/fd1 -t ext2 -o loop=/dev/loop0,encryption=DES /mnt/test
Password:
Init (up to 16 hex digits):
# ls /mnt/test
lost+found

Usually, the whole process of using an encrypted filesystem can be set up for ordinary users by adding the appropriate line to /etc/fstab:

$ mount /mnt/test
Password:
Init (up to 16 hex digits):
$ ls -ld /mnt/test
drwxrwxrwx  3 james  root    1024 Sep 14 22:04 /mnt/test

In this example, root has enabled users to mount encrypted filesystems by including this line in /etc/fstab:

/dev/fd1   /mnt/test   ext2   user,loop,encryption=DES

Additionally, ownership of the top-level directory on the diskette has been given to the user james because it is presumably his diskette. If root had not done this, james would have been able to mount his filesystem but not read it. It was an essential step, but it turns out that in this example, root has made a fatal mistake. As well as changing the ownership of the filesystem's root directory, root has changed the directory's mode as well. This means that once the unsuspecting james has supplied his secret password, any user on the system can read and write the files on the diskette. This underlines the fact that encryption alone is not sufficient for safety. Careful thought is also essential.

In the previous case, the file ownerships and permissions have turned out to be more of a hindrance than a help. It is probably better to use an MS-DOS filesystem on the encrypted device because ownership is automatically given away to the user mounting the disk and the file modes are set correctly:

$ ls -ld  /mnt/floppy/
drwxr-xr-x 2 james  users   7168 Jan  1  1970 /mnt/floppy/

However, there are still two problems with this strategy. First, it is not possible to make an encrypted filesystem easily on a diskette because the mkfs.msdos program needs to know the geometry for the device on which it is creating the filesystem, and the loopback device drivers don't really have geometries. Second, once your encrypted ext2 filesystem is mounted, the superuser can still read your data.

The encryption methods outlined previously are not available in standard kernels because most useful forms of encryption technology are not legally exportable from the United States. However, they are already available outside the United States at http://www.kerneli.org/

You need to apply these patches to your kernel and recompile it in order to use the DES and IDEA encryption methods with loopback devices.

To summarize, encrypted filesystems can be useful for some kinds of data (for example, for storing digital signatures for important system binaries in such a way that they can't be tampered with), but their usefulness to users other than root is limited. Of course, all the ordinary file encryption mechanisms are still available to and useful for ordinary users.

Other Block Devices

Although hard disks, floppy disks drivers, and CD-ROM drives are probably the most heavily used block devices, there are other kinds of block devices, including RAM disks and Zip drives.

RAM Disks

RAM disks are block devices that store their data in RAM rather than on a disk. This means they are very fast; nevertheless, RAM disks are rarely used with Linux because Linux has a very good disk-caching scheme, which provides most of the speed benefit of a RAM disk but not the fixed cost in memory.

The most common use for RAM disks is to serve as a root filesystem while Linux is being installed. A compressed filesystem image is loaded into a RAM disk, and the installation process is run from this disk. The RAM disk's filesystem can be larger than a single diskette because the image is compressed on the diskette.

Although RAM disks are useful with operating systems lacking effective disk buffering, they offer little performance advantage under Linux. If you want to try a RAM disk, they work just like any other block device. For example, to mount a RAM disk as /tmp, you add this line to /etc/fstab:

/dev/ram    /tmp        ext2    defaults    0 0

Then you create and mount an ext filesystem with the following:


   /sbin/mkfs -t ext2 /dev/ram

   mount /tmp

Any performance benefits from doing this are hard to find, but you might find that this helps in unusual circumstances.

The principal advantage of RAM disks is that they provide great flexibility in the boot process. Although it is possible to recompile a kernel including support for your hardware, it makes the initial installation process difficult. Historically, programmers worked around this problem by providing dozens of different installation boot disks, each with support for one or two items of boot hardware (SCSI cards and CD-ROM drives, for example).

A simpler solution is to exploit loadable kernel modules. Instead of having separate boot disks for each type of hardware, all containing different kernels, it is simple to provide just one boot disk containing a modular kernel and the module utilities themselves.

A compressed filesystem is loaded from the diskette into a RAM disk by the kernel loader, LILO, at the same time the kernel is loaded. The kernel mounts this filesystem and runs a program (/linuxrc) from it. This program then mounts the "real" root filesystem and exits, enabling the kernel to remount the real root filesystem on /. This system is convenient to set up, and the process of creating initial RAM disks has been automated by Red Hat Software (see the manual page for mkinitrd). Red Hat Linux systems whose root filesystem is on a SCSI device have a modular kernel and boot by this method.

Zip Drives

Zip drives are drives providing removable cartridges, available in various sizes from 100MB to 250MB. They come in three varieties: parallel port (PPA), IDE, and SCSI. All are supported by Linux. The parallel version is emulated as a SCSI drive but with a proprietary parallel port interface. Hence, it also appears as a SCSI disk.

Because they're just standard (but removable) SCSI or IDE disks, most aspects of their use are similar to those for other block devices. Red Hat Linux 7 comes with support for both the SCSI and PPA varieties. You can find further information in the Zip-Drive mini HOWTO (which explains how to install your Zip drive), and the Zip-Install mini HOWTO, which explains how to install Red Hat Linux onto a Zip drive.

USB Devices

USB is a new standard for connecting devices to your computer. It allows you to connect a variety of devices via a fast serial bus. It can be used with keyboards, Web cameras, network cards, and so on.

USB support was only recently added to Linux. Therefore it is at the moment in a state of flux. There is a lot of work going on to get as many USB devices as possible to work with Linux. Unfortunately, support is slightly immature and there is no way to be sure that a USB device will work with Linux. Therefore, you should check on Linux compatibility before purchasing a USB device. A good resource about Linux USB can be found at http://www.linux-usb.org/. There you will also find a list of USB devices known to work with Linux.

If you have USB devices connected to your computer, you can use the usbview command to get information about them. This command will list all USB devices that Linux can access and print information about them.

Share ThisShare This

Informit Network