The ability to manage the configuration and availability of file systems is an important system administration skill, particularly for systems heavily involved in a network's infrastructure. At various times, you might need to configure and add an additional hard drive to a server system or provide system services with access to files on a separate file server. For these types of tasks, you need to know how to create, mount, and unmount file systems. Creating a file system refers to the process by which a hard drive or other storage device is made ready to store files (this process is often called formatting in other operating systems). Mounting a file system refers to the process of making the contents of a device or network share available to users and processes within the Linux file system. Unmounting a file system refers to the process of disconnecting a device or network share and its contents from the Linux file system.
Creating File SystemsBefore you can mount and use a new or empty storage device like a hard drive, Zip or Jaz disk, or other removable storage device with Linux, you must create a file system on it. For purposes of example, let's build an ext2 file system in this chapter.
Two major steps are involved in creating a new ext2 file system for use with Linux:
The name of the device where the new file system and partition are to be created depends on the type of device and the type of connection through which the device communicates with your computer system. Most often, the device is connected either through an IDE channel or a SCSI controller.
After you identify the device on which you want to create a new file system for use with Linux, you call parted with the device name as an argument followed by the print option to display information about the device's size and any existing partitions that might reside on it: [root@workstation20 you]# /sbin/parted /dev/sdb print Disk geometry for /dev/sdb: 0.000-3992.717 megabytes Disk label type: msdos Minor Start End Type Filesystem Flags 1 0.031 3992.717 primary FAT Information: Don't forget to update /etc/fstab, if necessary. [root@workstation20 ~]$ In this particular example, one existing partition of the FAT (Windows) type must be removed before a new partition or file system can be created. The first column indicates that this is partition 1 on the device /dev/sdb. To remove it, use parted with the rm option and the number of the partition to remove as an argument. Then call parted with the print option to verify that the partition has been removed: [root@workstation20 you]# /sbin/parted /dev/sdb rm 1 [root@workstation20 you]# /sbin/parted /dev/sdb print Disk geometry for /dev/sdb: 0.000-3992.717 megabytes Disk label type: msdos Minor Start End Type Filesystem Flags Information: Don't forget to update /etc/fstab, if necessary. [root@workstation20 you]#
With existing partitions removed, you are free to create new partitions. This is done using the parted command as follows: parted device mkpartfs primary fstype begin end Replace device with the device name where you're creating a new file system, fstype with either fat (for a Windows file system) or ext2 (for a Linux file system), and begin and end with the begin and end positions of the new partition, in megabytes. For example, to create a Linux file system the size of the entire disk shown in the examples thus far, you issue the following parted command: [root@workstation20 you]# /sbin/parted /dev/sdb mkpartfs primary ext2 0 3992.717 [root@workstation20 you]# /sbin/parted /dev/sdb print Disk geometry for /dev/sdb: 0.000-3992.717 megabytes Disk label type: msdos Minor Start End Type Filesystem Flags 1 0.031 3992.717 primary ext2 Information: Don't forget to update /etc/fstab, if necessary. [root@workstation20 you]# You can create up to four partitions on any device using this technique; Linux and Windows partitions can be freely intermixed.
File systems you create this way are referred to by appending their partition number to the device name. For example, when you learn to mount file systems in the next section, you mount the first partition on /dev/sdb by referring to /dev/sdb1, the second partition by referring to /dev/sdb2, and so on.
Mounting and Unmounting File SystemsWhether you want to access a newly created file system, the data on an existing device or file system, or the data on a network share, in the Linux operating system, you must first mount a data storage area before you are able to read from or write to it. Some more common data storage areas, like those on USB keys or CD-ROM drives, are automatically mounted by Fedora Core 4 and appear instantly on the desktop when connected. For those that aren't automatically mounted, however, the following four steps must be performed:
The format of the mount command is as follows: mount -t fstype [-o options] datasource mountpoint Replace fstype with the type of file system (vfat, ext2, ext3, reiserfs, nfs, iso9660, udf, or other type you have been instructed to use). Replace datasource with the device name (/dev/hda1, /dev/sda1, and so forth) or network path (address:/path). Replace mountpoint with the place in your local file system where this data or this storage area is to appear. The -o options option is not required but can be used to provide additional options, usually one of the following:
Additional options can be found in the man page for the mount command. To illustrate, if you want to mount the ext2 file system you created on /dev/sdb1 in the previous section to a new directory called /publicspace, you first create /publicspace and then issue the mount command supplying all three as arguments: [root@workstation20 you]# mkdir /publicspace [root@workstation20 you]# mount -t ext2 /dev/sdb1 /publicspace [root@workstation20 you]# The contents of the device /dev/sdb1 can now be accessed or modified by reading or writing to files or directories in the /publicspace tree. To verify that this is the case, you can print the list of mounted file systems by entering the mount command without arguments: [root@workstation20 you]# mount /dev/sda2 on / type ext3 (rw) none on /proc type proc (rw) usbdevfs on /proc/bus/usb type usbdevfs (rw) /dev/sda1 on /boot type ext3 (rw) none of /dev/pts type devpts (rw,gid=5,mode=620) none on /dev/shm type tmpfs (rw) /dev/sdb1 on /publicpsace type ext2 (rw) [root@workstation20 you]# When the mount command is called without arguments, each line of output contains the device or resource, the mountpoint, the file system type, and any options the device has been mounted with, in that order. Complete documentation for file system types and options can be found in the mount manual page.
You can unmount a mounted file system with the umount command. The umount command simply needs one argument: either the name of the device or resource to unmount, or the name of the mountpoint where the device or resource was mounted. For example, considering the mount commands we've issued in this section, the following two umount commands are equivalent: umount /dev/sdb1 umount /publicspace After you unmount a device or resource, you can verify that it no longer appears in the mounted file systems list by using the mount or df commands without arguments.
Maintaining the /etc/fstab FileThe /etc/fstab file is a list of devices and resources, file system types, and mountpoints that control which file systems are mounted when the computer starts. This file also lists which file systems are mountable and unmountable by regular users. Each line in the /etc/fstab file represents one mounted or mountable file system, and each line is formatted as follows: device mountpoint fstype option1,option2,.. dump fscklvl Each of the fields in this file is described in Table 28.4.
If you would like to cause a file system such as the one created in the previous section to automatically be mounted when the system starts, add an entry to the bottom of the /etc/fstab file containing the device, mountpoint, file system type, and other information. Following the sample file system created in the previous section, you add the following line: /dev/sdb1 /publicspace ext2 defaults 0 2 By editing the /etc/fstab file, you can cause Linux to automatically mount added storage devices or networked file systems each time you boot. You can also add additional removable storage devices so that users can mount and unmount them as needed. For further documentation of the /etc/fstab file or the options that can be used in it, see the manual pages for fstab(5) and mount. |