Now that you have seen several aspects of the filesystem from a user s perspective, let s look at some of the administrative aspects of the filesystem, such as the different drives on a system and its partitions. Apart from the hard disks on a system, there is almost always a floppy disk drive and a CD-ROM drive, both of which are treated as filesystem drives. In addition, certain other peripheral devices, such as digital cameras or removable storage devices, may present the storage available on them as filesystem drives. A drive may be mounted , making it available for use by the filesystem, or unmounted , making it unavailable for the filesystem to use.
You can list the currently mounted drives on a system using the df or mount commands. Let s look at the output of the df command:
$ df k File system 1K-blocks Used Available Use% Mounted on /dev/hda2 5716172 3426868 1998932 64% / /dev/hda1 101089 19137 76733 20% /boot none 94752 0 94752 0% /dev/shm /dev/fd0 1423 105 1318 8% /mnt/floppy /dev/cdrom 661728 661728 0 100% /mnt/cdrom
The “k option to df causes it to report partition sizes in kilobytes.
In the preceding listing, you see the following:
The first IDE hard disk, /dev/had , has two partitions, /dev/hda1 and /dev/hda2 .
The floppy drive is the /dev/fd0 drive.
The /dev/cdrom drive represents a CD-ROM drive.
In addition, a drive marked none (in this case) represents a temporary filesystem ( tmpfs ) necessary for certain shared-memory operations.
With the other drives, in addition to the space-usage numbers on each of them, the thing to note is the Mounted on column. This represents the directory that these filesystems are mapped on to. Therefore, the following is true:
The /dev/hda2 partition contains a filesystem that is mapped on to the / or the root directory.
The /dev/hda 1 partition contains another filesystem that is mapped on to the /boot directory.
What this means is that each time you execute the cd / command or the cd /etc command, you are actually accessing a filesystem on the second partition of the only hard disk on this system. Also, executing ls “l /mnt/cdrom will list the top-level files and subdirectories of the currently mounted CD in the CD-ROM drive.
A device such as a floppy drive may be detected by the operating system when it boots up. However, for the floppy drive to be usable , it needs to be mounted first (this is like grafting the drive onto a filesystem entry point).
To mount a drive, you must first create a directory (or use the already available /mnt/floppy and /mnt/cdrom directories for floppy disks and CD-ROMs, respectively). You then mount the floppy drive /dev/fd0 to that directory. Drives can be mounted in two different ways:
Automatic mounting of the drive at boot time
Manual mounting at any time after the system has come up
For a drive to be mounted automatically by the system, you must make an entry for it in the file /etc/fstab or the filesystem table file. Here is a typical /etc/fstab file:
LABEL=/ / ext3 defaults 1 1 LABEL=/boot /boot ext3 defaults 1 2 none /dev/pts devpts gid=5,mode=620 0 0 none /proc proc defaults 0 0 none /dev/shm tmpfs defaults 0 0 /dev/hda3 swap swap defaults 0 0 /dev/cdrom /mnt/cdrom iso9660 noauto,owner,kudzu,ro 0 0 /dev/fd0 /mnt/floppy auto noauto,owner,kudzu 0 0
Notice that there are more entries in this file than were listed by the df command because there are drives of several other types that do not have true filesystem characteristics. For example, disk usage of the proc filesystem does not make much sense because the proc filesystem actually represents a set of processes currently executing on the system. Similarly, the /dev/pts entry indicates a pseudo-terminal device, which is not really associated with commonly used files and directories; rather, it is used for managing terminal-based input and output. However, these special types of filesystems are also listed in /etc/fstab so that they may be mounted automatically at boot time.
Also, notice that besides the floppy and CD-ROM drives, there is also a swap device. This is a device used by the operating system to juggle processes and chunks of memory content between the RAM and the hard disk, therefore allowing arbitrarily large programs to be loaded and executed. In general, swap partitions are critical to the operating system because they are necessary for multitasking ”that is, running multiple tasks at (almost) the same time.
It is also possible to manage drives at runtime using the Disk Management utility. It may be accessed from the Start Menu>System Tools>Disk Management (see Figure 5-2).
You can mount or unmount specific devices or even format a new device from this tool.
You can also check for the hard disks on a system and their partitions by using the Hardware Browser application, which you ll find under the Main Menu>System Tools menu. When you re there, select the Hardware Browser option to view the hard drives on your system, as shown in Figure 5-3.
In Figure 5-3, you can see that on this machine the disk /dev/hda has three partitions: hda1 is an NTFS partition, hda2 is a Linux ext3 partition and hda3 is a Linux swap partition,
You can mount a drive (such as the CD-ROM drive) manually by using the mount command. To unmount a drive manually, use the umount command.
You can also use the Bluecurve desktop to mount floppies and CD-ROMs. To do this, right-click on the workspace, choose Disks from the co
As a demonstration, let s try to mount a floppy disk using the mount command and later unmount it to understand how the process of mounting and unmounting drives works (you ll need a floppy disk for this).
Start out by trying to list the /mnt/floppy directory. No files are listed because the floppy drive device has not yet been mounted to /mnt/floppy :
$ ls /mnt/floppy/ before mounting the floppy drive no files listed
We attempt to mount floppy drive device /dev/fd0 to /mnt/floppy . However, we are prevented from doing so because this is a privileged operation allowed only for administrators or programs that have the setuid bit set. So we switch to the root user and try again to mount the drive. This time we succeed in mounting the /dev/fd0 device to /mnt/floppy :
$ mount /dev/fd0 /mnt/floppy mount: only root can do that $ su Password: # mount /dev/fd0 /mnt/floppy # df -kgrep floppy /dev/fd0 1423 105 1318 8% /mnt/floppy
The mount command minimally takes two arguments. The first is the name of the device containing a filesystem to mount, and the second is a directory to map the device on to. Verify this using the df command whose output is piped to the grep command, which searches for the string floppy; sure enough, the drive is listed as being mounted to /mnt/floppy .
Once this is done, non-root users may also access the /mnt/floppy directory. You can exit from the root shell and assume your previous identity of a regular user:
# exit $ ls /mnt/floppy/ aliases.txt df.txt ff.html ps_A.txt testcmd.sh which.txt crontab.txt env.txt parted.txt ps_auxww.txt top.jpeg
Now, as a regular user, you can list the files under the /mnt/floppy directory:
$ umount /dev/fd0 umount: only root can unmount /dev/fd0 from /mnt/floppy $ su Password: # umount /dev/fd0 # exit exit $ ls /mnt/floppy/ bash$
However, as a regular user, you are not permitted to unmount the device. To do this, you once again must assume the identity of the root user and run the umount command. The umount command takes the name of the device ” /dev/fd0 , in this case. After the device is unmounted, the ls listing is not available any more because there is no device with files and subdirectories grafted to the filesystem at this point.
If a program has a file open on a mounted device or a reference to a directory, the system will not permit the device to be unmounted. This is characterized by an error message that reads as unmount: /mnt/floppy: device is busy . If you see this error, ensure that you do not have a shell window open that is accessing a directory or file on the device in question.
In Fedora 2, CDs are mounted automatically; manually mounting them is not required. The Nautilus file manager automatically pops up a dialog box if the CD has a runnable program on it. Another tidbit to take note of is the eject command. You can use the eject command to unmount the CD and cause the CD-ROM tray to be opened.
Recently, key-chain storage drives have gained popularity. These are small, inexpensive (at least the not-so high-end ones) flash memory cards that can be connected to a computer via a USB port. You can use the key drive to copy files back and forth between computers. Because most of them start with a minimum of 32MB, going up to 1GB or more, they are much more practical than floppy disks or burning a CD when you need to carry with you important information such as documents or presentations. Fedora supports these USB mass-storage devices and treats them as storage devices.
To use the USB flash device, you need to mount the USB mass-storage device. After you plug in the device, you can either use the right-click menu as before, or mount the device from the command line using the mount command.
$ /sbin/mount -t vfat /dev/sda0 /mnt/usbstick
You may also choose to mount the drive during boot time by adding the following entry to /etc/fstab :
/dev/sda0 /mnt/usbstick vfat user,noauto,umask=0 0 0
If you already have a USB mass-storage device connected to the computer, the system may recognize the device to be /dev/sda1 and not /dev/sda0 . Also, if you have a pre-existing SCSI hard disk on the computer, /dev/sda will be allotted to the hard disk. In this case, the USB mass-storage device is assigned the device /dev/sdb .
You can use partitions to divide a disk drive into a number of filesystems. For example, consider the hard disk discussed earlier that had one partition dedicated to the root filesystem ( / ), another for the /boot directory (which houses the various kernels that can be used to boot up the system), and the swap partition.
On dual boot systems , partitions allow you to dedicate certain partitions to one operating system and the remaining to a second operating system. The partitions are indicated as numbers next to the disk drive ”for example, /dev/hda1 , /dev/hda2 ,and so on, or /dev/sda1 , /dev/sda2 , and so on. The first IDE hard disk is usually named as /dev/hda , the second one as /dev/hdb , and so on. SCSI hard disks are named in the order /dev/sda , /dev/sdb , and so on.
A disk s partition information is stored in a partition table that is read by the operating system. The tools that you can use to manipulate the partition table to change partition information on hard disks are discussed later in this chapter.
Now that you know how disks are organized into partitions, it is time to explore filesystem types . Various operating systems have their own notion of how data should be organized on disk partitions. For example, Microsoft DOS uses the file allocation table (FAT) filesystem to organize files on the disk. With the advent of Windows NT, NT filesystem (NTFS) became another filesystem supported by Microsoft operating systems.
The de facto filesystem on Linux is the extended (ext) filesystem. A second version of this filesystem was called ext2; Fedora 2 supports the latest version, ext3. Usually, when a system is shut down abruptly, (without allowing it to save all of the in-memory filesystem data), the operating system performs a lengthy sanity check of the disk and sometimes also repairs the disk. With ext3, the time taken for the check (and repair) in such an event is significantly reduced due to a new feature supported by ext3 known as journaling, which maintains disk consistency by constantly logging disk activity.
Fedora 2 currently supports a host of filesystems, and many simultaneously . For example, it is possible to have a system with one FAT partition and the remaining partitions in the ext3 format. The file /proc/filesystems contains a list of the filesystems supported on a particular machine:
$ cat /proc/filesystems nodev rootfs nodev bdev nodev proc nodev sockfs nodev tmpfs nodev shm nodev pipefs ext2 nodev ramfs iso9660 nodev devpts ext3 nodev usbdevfs nodev usbfs nodev autofs vfat
The nodev entry against a filesystem type indicates that no device is currently using that filesystem type (although the filesystem type itself is supported). In the preceding sample listing, the filesystem types currently used by devices are as follows :
The extended filesystem types, ext2 and ext3, used by the Linux partitions
The CD-ROM filesystem ISO-9660 for the CD-ROM device
The VFAT filesystem for the floppy device
Support for additional filesystems can be compiled into the kernel or loaded as a kernel module. Chapter 12 has information on recompiling the kernel and setting up loadable kernel modules.
You might wonder how it is possible that a file residing on a Linux ext3 partition can be copied onto a floppy that uses the VFAT filesystem of Microsoft Windows? Historically, filesystems have been designed with a certain operating system or class of operating systems in mind. For this reason, filesystem types are subject to the idiosyncrasies of the operating systems. However, Linux handles a multitude of filesystems, several of which have been designed to allow interoperation with other operating systems.
Do programmers who write programs on Linux write code to handle each and every supported filesystem that Linux supports? If that were true, wouldn t these programs break when a new filesystem type was supported?
In reality, Linux application programmers do not write filesystem-specific code (this would make most programs too complex and unmaintainable, besides being very hard to extend when a new filesystem type is supported by the operating system). They write code assuming a simple, generic filesystem. The abstraction of multiple filesystems is handled by the Virtual Filesystem (VFS) . VFS is not really a filesystem in itself; rather, it is a way to abstract the common aspects of most filesystem types.
For example, almost all of the operations that relate to the manipulation of files can be reduced to a few basic actions: create , delete , rename , open , read , write , and close . The VFS layer in the kernel supports these generic interfaces in the form of functions that higher-level programs can use. Internally, VFS figures out the real implementation to invoke.
For example, while attempting to copy a file from a FAT partition to an ext3 partition, the copy program would invoke system calls (among other things) such as read() and write() . Because the VFS layer understands that the target filesystem type is FAT, it ends up invoking a fat_read() , and because the destination filesystem type is ext3, it invokes ext3_write() (see Figure 5-5).
In the diagram, the copy command invokes the read() system call. The read() internally translates into the appropriate filesystem specific call, fat_read() , for example.
Because Fedora supports and recognizes a large number of filesystem types, including several Microsoft filesystems, it is possible to access files residing on disk partitions formatted as Microsoft filesystems, such as FAT32 or NTFS. Here is what you need to do to mount a FAT32 partition:
$ mount -t vfat /dev/hda1 /mnt/winfiles
This would effectively mount the /dev/hda1 device (presumably the C: drive on Microsoft Windows) to the directory /mnt/winfiles . From this point, it is possible to work with the Microsoft Windows files using standard Linux commands.
To always mount the drive at boot time, you must add the following entry to /etc/fstab :
/dev/hda1 /mnt/winfiles auto noauto,users,exec 0 0
However, there is a catch when it comes to accessing NTFS filesystems. Fedora does not come with support for NTFS, presumably due to licensing or legal issues concerning this proprietary filesystem. That can, however, be remedied by installing the necessary kernel support for NTFS. The tedious way to achieve this is to recompile the kernel with NTFS support. However, a simpler option exists, which is to install the NTFS support available as RPMS from http://linux-ntfs. sourceforge .net/rpm/ .
See Chapter 4 for details on installing RPMs.
Once support for NTFS is installed, you can choose to mount an NTFS partition with the mount command, as illustrated in the following example:
$ mount -t ntfs /dev/hda4 /mnt/myntfs
Optionally, you may choose to have the drive mounted at boot time by adding an entry similar to the following to /etc/fstab .
/dev/hda4 /mnt/myntfs ro 0 0
Even after you install NTFS support, there is still a catch; NTFS partitions may be accessed only as read-only partitions. You can copy or open files on NTFS partitions, but you cannot create, modify, delete, or rename files and folders on these partitions.
Surprisingly, it is possible to access Linux filesystem partitions from certain Microsoft Windows platforms. This is achieved by plugging in support for reading the ext filesystem of Linux. Explore2fs is one such project that provides support for Microsoft Windows. More details on Explore2fs can be found at http://uranus.it.swin.edu.au/~jn/linux/explore2fs.htm . Please note that this software offers write support for Linux filesystems in addition to read support. However, the write support may not be very stable.
Even with the limitations of the aforementioned drivers, it is possible to copy files between Linux partitions and NTFS partitions. For example, to copy a file from Windows to Linux, you would boot into Linux and, using the NTFS support, copy the file to a Linux partition. To copy a file from a Linux partition, you would boot into Windows and, using the ext filesystem support offered by Explore2fs, copy the file from a Linux partition to a Windows partition.
This section explains how to manipulate a partition table ”to create, verify, delete, and resize partitions, and to manage partitions in general. It is inevitable that sooner or later most people will have to deal with the task of partitioning or repartitioning hard disks, either as part of a fresh installation or a disk upgrade.
You have several tools at your disposal. The fdisk command is the oldest of this genre of tools, although these days the parted command seems to be the preference of an increasing number of users. The cfdisk and sfdisk commands are other partition manipulation tools that are available as part of the Fedora 2 distribution. The disk druid utility is also supplied by Red Hat, and can be employed if the user chooses to perform custom partitioning.
Because parted is fast emerging as the popular choice for editing partitions, let s use it here to experiment with partition management. The parted program, unlike fdisk, does not gather all the modifications during an editing session and apply changes to the partition table at the end.
Inexperienced users of parted need to be aware of the following:
The parted program writes changes immediately to the partition table, so users are warned against curious experimentation using parted on disks containing any significant information.
When resizing a partition, the partition in question must not be in use. That is, it must be unmounted using the umount command.
Having said that, parted is definitely much more function-packed and user-friendly in comparison to fdisk .
In the following example, because you will be resizing the root partition, the root partition cannot be mounted. Therefore, you need to run parted from a boot disk, as described here:
A boot disk image with parted is available at ftp://ftp.gnu.org/gnu/parted/bootdisk/partboot.img . Download a copy if you re not confident about making a boot disk yourself.
Save the image as partboot.img . The image file needs to be written onto a floppy disk (as the root user) by running the dd command, (as shown in the following code) from the same directory as the file:
# dd if=partboot.img of=/dev/fd0 bs=1440k
This step prepares the book disk. For more information about this, refer to the Parted manual at www.auth.gr/mirrors/gnu/manual/parted-1.6.1/html_mono/parted.html .
The system needs to be shut down and restarted by booting from this boot disk.
After the system comes up, run the parted command from the boot disk, as shown. When editing partitions other than the root or boot partitions, you need not use a boot disk. Simply ensure that the partition is unmounted and invoke parted as /sbin/parted .
# parted /dev/hda
Start parted as the root user, specifying the hard disk whose partition table you wish to modify. The parted command allows you to edit the partition table interactively:
(parted) print Disk geometry for /dev/hda: 0.000-6149.882 megabytes Disk label type: msdos Minor Start End Type Filesystem Flags 1 0.031 101.975 primary ext3 boot 2 101.975 5773.359 primary ext3 3 5773.359 6149.882 primary linux-swap
The print command prints the current disk geometry with the partition information. Note that parted can specify the size of partitions as a size range (0 to about 100MB for partition 1 and roughly 101MB to about 5,773MB for partition 2). This is a marked improvement over some of the earlier programs that required you to manually calculate disk geometry using cylinders , sectors, and so on. Here, you see that you have two extended filesystem partitions and one swap partition:
(parted) resize 2 101.975 5000 (parted) print Disk geometry for /dev/hda: 0.000-6149.882 megabytes Disk label type: msdos Minor Start End Type Filesystem Flags 1 0.031 101.975 primary ext3 boot 2 101.975 4996.779 primary ext3 3 5773.359 6149.882 primary linux-swap
The first thing you do is attempt to resize the biggest partition of the three (that is, the second partition), which currently extends from 101.975MB to 5773.359MB. You use the resize command by specifying the partition to resize (Partition 2), and the new size range (101.975 to 5,000). With this resizing, you shrink the partition by 773.395MB (that is 5773.359MB to 5000MB). On printing the partition table, the new disk geometry is evident:
(parted) mkpartfs primary ext2 4996.779 5773.359 (parted) print Disk geometry for /dev/hda: 0.000-6149.882 megabytes Disk label type: msdos Minor Start End Type Filesystem Flags 1 0.031 101.975 primary ext3 boot 2 101.975 4996.779 primary ext3 4 4996.780 5773.359 primary ext2 3 5773.359 6149.882 primary linux-swap
You now create a new partition, using the space taken from Partition 2. You use the mkpartfs command to do this, indicating that this is a primary partition (partitions can be primary, extended, or logical). You also indicate that this is a partition that will house an ext2 filesystem because ext3 is not yet supported by parted . You specify the new partition range from 4996.779MB to 5773.359MB. Printing the partition table confirms that the new partition has been created:
(parted) rm 4 (parted) resize 2 101.975 5773.359
Now that you have learned to resize partitions, you can try and restore things to their original state (at the start of this example). Delete the newly created partition using the rm command, specifying the partition number of the partition you wish to remove. Finally, resize Partition 2 to its original size:
(parted) print Disk geometry for /dev/hda: 0.000-6149.882 megabytes Disk label type: msdos Minor Start End Type Filesystem Flags 1 0.031 101.975 primary ext3 boot 2 101.975 5773.359 primary ext3 3 5773.359 6149.882 primary linux-swap (parted) quit
The print command confirms the latest attempt to resize the partition. When you are done experimenting, exit the parted program using the quit command.
Regardless of the hard disk real estate that you start out with, it is very likely that you will run out of disk space before the end of the lifecycle of our machines. When this happens, you will most likely purchase an additional hard disk or replace the current disk with one that has more storage capacity.
When you first installed Fedora 2 on the system, you did not have to bother about explicitly setting up your hard disk ”the installer took care of this behind the scenes. But, if you need to add a disk yourself, there are a few manual steps that you need to perform.
After hooking up the new hard disk to the system, and before going any further, it is necessary to ensure that the disk has been recognized by the operating system. Usually, Red Hat s kudzu configuration tool will allow you to configure your new hardware when it is detected during the bootup process.
To ensure that the new disk has been recognized, you need to examine the kernel startup messages ”you can view these messages with the dmesg command. You need to look for detection messages corresponding to your new hard disk. Here is a typical dmesg line, which indicates that the new hard disk has been detected:
$ dmesg grep i maxtor hdb: Maxtor 90651U2, ATA DISK drive
An alternative is to list the contents of /proc/partitions , which lists all the currently available partitions, including those that have not yet been mounted. Here is the listing of a typical /proc/partitions file. For our purposes, it is sufficient to look for a new row in the table and note the name column, in this case, hdb.
$ cat /proc/partitions major minor #blocks name rio rmerge rsect ruse wio wmerge wsect wuse running use aveq 22 0 661728 hdb 40 32 280 500 0 0 0 0 -24865 2297424 1939878 3 0 6297480 hda 17031 21392 305170 253707 11959 16564 228632 422367 -1 6577905 2337991 3 1 104391 hda1 35 85 240 408 14 6 40 244 0 636 652 3 2 5807497 hda2 16967 21086 304394 252974 11788 14238 208776 250927 0 188263 503912 3 3 385560 hda3 9 25 104 146 157 2320 19816 171195 0 3453 171341
If there are no kernel startup messages or listings in /proc/partitions , it is quite likely that the disk was not detected. There are several possible reasons for the system s failure to detect the disk. To eliminate the possibility of a hardware setup problem, you should check all hardware settings, such as the master “slave mode settings of the hard disk, and compare them with the manufacturer s recommended settings. The detection failure may have occurred because the correct driver is missing from the kernel; this problem can be remedied by recompiling a kernel, or by using a dynamically loadable kernel module that has support for the new hard disk. Chapter 12 has details on how to perform these steps.
Once the disk has been detected, it is usually assigned a /dev name (such as /dev/hdb ) automatically by the system. After you know what this name is, as you can see in the dmesg output or the /proc/partitions file, you need to partition the device.
If you intend to create a swap partition on this disk, the rule of thumb is to create a partition that is at least two and a half times the size of the available physical RAM. You can use parted , fdisk , sfdisk (or another similar command) to partition the new drive. During the partitioning process, it is necessary to specify the intended filesystem for this hard disk.
After partitioning, the next logical step is to format the disk. The formatting process prepares the partition for a certain filesystem type to create files and directories. During this process, filesystem-specific information may be created on the partitions. Formatting is very much specific to the filesystem that will be installed on the partition. For example, to format the first partition of the /dev/hdb disk to use the ext3 filesystem, the following command is used:
# mkfs.ext3 c /dev/hdb1
The “c option forces a check for disk errors.
Now that you have partitioned and formatted the disk, it is ready for use and needs to be mounted to a filesystem mount point. For example, suppose you intend to mount the new partition to the directory /public . For this, you can use the following command:
# mount -t ext3 /dev/hdb1 /public
Although this command mounts the partition for now, it is not enough to mount the partition automatically when the system reboots. For automatic mounting, you need to add an entry to the /etc/fstab file. A typical entry would look like the following:
/dev/hdb1 /public ext3 defaults 1 1
The first entry (/dev/hdb1 ) represents the partition containing the filesystem to be mounted. The second entry ( /public ) represents the directory that this filesystem should be mounted to. The third entry ( ext3 ) indicates the filesystem type. The fourth entry indicates the permissions for the device; permissions determine the type of operations that may be performed on the filesystem. In this case, the default permissions (that is, allowing both read and write access) is in effect. The last two entries indicate operating system parameters (dump frequency and parallel fsck number) that may be left set to 1 .
The parted command has a copy command that enables entire filesystems to be copied from one partition to another. This is particularly useful during disk upgrades that involve the replacement of an existing disk with a larger one. It is possible to add the new disk to the system without removing the older disk ”possibly in a master “slave configuration. If you want to set this up, you should look at the disk manufacturer s documentation.
After both disks are hooked up to the system, you can use the parted copy command to copy the old filesystems to the new partitions. Once the copy is complete, the old disk can be removed from the system or reused (even perhaps as a swap disk) on the same system.
From this point, you can use the /public directory (in this case) just like any other directory on the system.