Mounting File Systems Directly: mount and umount

 < Day Day Up > 



You can also mount or unmount any file system using the mount and umount commands, directly (notice that umount lacks a n). The mount operations discussed in the previous sections use the mount command to mount a file system. Normally, file systems can be mounted on hard disk partitions only by the root user, whereas CD-ROMs and floppies can be mounted by any user. Table 30-9 lists the different options for the mount command.

The mount Command

The mount command takes two arguments: the storage device through which Linux accesses the file system, and the directory in the file structure to which the new file system is attached. The mountpoint is the directory on your main directory tree where you want the files on the storage device attached. The device is a special device file that connects your system to the hardware device. The syntax for the mount command is as follows:

# mount device mountpoint 

As noted previously, device files are located in the /dev directories and usually have abbreviated names ending with the number of the device. For example, fd0 may refer to the first floppy drive attached to your system. The following example mounts a floppy disk in the first floppy drive device (fd0) to the /mnt/floppy directory. The mountpoint directory needs to be empty. If you already have a file system mounted there, you will receive a message that another file system is already mounted there and that the directory is busy. If you mount a file system to a directory that already has files and subdirectories in it, those will be bypassed, giving you access only to the files in the mounted file system. Unmounting the file system, of course, restores access to the original directory files.

Table 30-9: The mount Command

Mount Options

Description

-f

Fakes the mounting of a file system. Use it to check if a file system can be mounted.

-v

Verbose mode. mount displays descriptions of the actions it is taking. Use with -f to check for any problems mounting a file system, -fv.

-w

Mounts the file system with read/write permission.

-r

Mounts the file system with read-only permission.

-n

Mounts the file system without placing an entry for it in the mstab file.

-t type

Specifies the type of file system to be mounted. See Table 30-7 for valid file system types.

-a

Mounts all file systems listed in /etc/fstab.

-o option-list

Mounts the file system using a list of options. This is a comma-separated list of options following -o. See Table 30-8 for a list of the options.

# mount /dev/fd0 /mnt/floppy

For any partition with an entry in the /etc/fstab file, you can mount the partition using only the mount directory specified in its fstab entry; you needn't enter the device filename. The mount command looks up the entry for the partition in the fstab file, using the directory to identify the entry and, in that way, find the device name. For example, to mount the /dev/hda1 Windows partition in the preceding example, the mount command only needs to know the directory it is mounted to—in this case, /mnt/windows.

# mount /mnt/windows

If you are unsure as to the type of file system that the floppy disk holds, you can mount it specifying the auto file system type with the -t option. Given the auto file system type, mount attempts to detect the type of file system on the floppy disk automatically.

# mount -t auto /dev/fd0 /mnt/floppy

The umount Command

If you want to replace one mounted file system with another, you must first explicitly unmount the one already mounted. Say you have mounted a floppy disk, and now you want to take it out and put in a new one. You must unmount that floppy disk before you can put in and mount the new one. You unmount a file system with the umount command. The umount command can take as its argument either a device name or the directory where it was mounted. Here is the syntax:

# umount device-or-mountpoint 

The following example unmounts the floppy disk wherever it is mounted:

# umount /dev/fd0

Using the example where the device was mounted on the /mydir directory, you could use that directory to unmount the file system:

# umount /mydir

One important constraint applies to the umount command. You can never unmount a file system in which you are currently working. If you change to a directory within a file system that you then try to unmount, you receive an error message stating that the file system is busy. For example, suppose you mount a CD-ROM on the /mnt/cdrom directory and then change to that /mnt/cdrom directory. If you decide to change CD-ROMs, you first have to unmount the current one with the umount command. This will fail because you are currently in the directory in which it is mounted. You have to leave that directory before you can unmount the CD-ROM.

# mount /dev/hdc /mnt/cdrom # cd /mnt/cdrom # umount /mnt/cdrom umount: /dev/hdd: device is busy # cd /root # umount /mnt/cdrom
Tip 

If other users are using a file system you are trying to unmount, you can use the lsof or fuser command to find out who they are.

Mounting Floppy Disks

As noted previously, to access a file on a floppy disk, the disk first has to be mounted on your Linux system. The device name for your floppy drive is fd0, and it is located in the directory /dev. Entering /dev/fd0 references your floppy drive. Notice the number 0 after fd. If you have more than one floppy drive, the additional drives are represented by fd1, fd2, and so on. You can mount to any directory you want. Red Hat creates a convenient directory to use for floppy disks, /mnt/floppy. The following example mounts the floppy disk in your floppy drive to the /mnt/floppy directory:

# mount /dev/fd0 /mnt/floppy
Tip 

On GNOME, you can mount a floppy drive by right-clicking the desktop background to display the desktop menu and then selecting Floppy in the Disk entry. To unmount, right-click the Floppy icon and select Eject from the pop-up menu.

Remember, you are mounting a particular floppy disk, not the floppy drive. You cannot simply remove the floppy disk and put in another one. The mount command has attached those files to your main directory tree, and your system expects to find those files on a floppy disk in your floppy drive. If you take out the disk and put another one in, you get an error message when you try to access it.

To change disks, you must first unmount the floppy disk already in your disk drive. Then, after putting in the new disk, you must explicitly mount that new disk. To do this, use the umount command.

# umount /dev/fd0

For the umount or mount operations, you can specify either the directory it is mounted on or the /dev/fd0 device.

# umount /mnt/floppy

You can now remove the floppy disk, put in the new one, and then mount it:

# mount /mnt/floppy 

When you shut down your system, any disk you have mounted is automatically unmounted. You do not have to unmount it explicitly.

Mounting CD-ROMs

Remember, when you mount a CD-ROM or floppy disk, you cannot then simply remove it to put another one in the drive. You first have to unmount it, detaching the file system from the overall directory tree. In fact, the CD-ROM drive remains locked until you unmount it. Once you unmount a CD-ROM, you can then take it out and put in another one, which you then must mount before you can access it. When changing several CD-ROMs or floppy disks, you are continually mounting and unmounting them. For a CD-ROM, instead of using the umount command, you can use the eject command with the device name or mount point, which will unmount and then eject the CD-ROM from the drive.

You can also mount CD-ROM disks to your Linux system using the mount command. On many distributions, the directory /mnt/cdrom has been reserved for CD-ROM file systems. You can see an entry for this in the /etc/fstab file presented earlier in the chapter. With such an entry, to mount a CD-ROM, all you have to do is enter the command mount and the directory /mnt/cdrom. You needn't specify the device name. Once it is mounted, you can access the CD-ROM through the /mnt/cdrom directory.

# mount /mnt/cdrom
Tip 

On GNOME, CD-ROMs are automatically mounted, though you can manually mount them by right-clicking the desktop background to display the desktop menu and then selecting CD-ROM in the Disk entry. To unmount, right-click the CD-ROM icon and select Eject from the pop-up menu.

# umount /mnt/cdrom

If you want to mount a CD-ROM to another directory, you have to include the device name in the mount command. The following example mounts the disc in your CD-ROM drive to the /mydir directory. The particular device name for the CD-ROM in this example is /dev/hdc.

# mount /dev/hdc /mydir

When you burn a CD, you may need to create a CD image file. You could access such an image file from your hard drive, mounting it as if it were another file system (even ripped images could be mounted in this way). For this, you use the loop option, specifying an open loop device such as /dev/loop0. If no loop device is indicated, mount will try to find a open one. The file system type is iso9660, a CD-ROM iso image file type.

# mount -t iso9660 -o loop=/dev/loop0 image-file  mount-directory 

To mount the image file mymusic.cdimage to the /mnt/mystuff directory and make it read-only, you would use:

# mount -t iso9660 -o ro,loop=/dev/loop0 mymusic.cdimage /mnt/mystuff 

Once it is mounted, you can access files on the CD-ROM as you would in any directory.

Tip 

You use mkisofs to create a CD-ROM image made up from your files or another CD-ROM.

Mounting Hard Drive Partitions: Linux and Windows

You can mount either Linux or Windows hard drive partitions with the mount command. However, it is much more practical to have them mounted automatically using the /etc/fstab file as described previously. The Linux hard disk partitions you created during installation are already automatically mounted for you. As noted previously, to mount a Linux hard disk partition, enter the mount command with the device name of the partition and the directory to which you want to mount it. IDE hard drives use the prefix hd, and SCSI hard drives use the prefix sd. The next example mounts the Linux hard disk partition on /dev/hda4 to the directory /mnt/mydata:

# mount -t ext3 /dev/hda4 /mnt/mydata

You can also mount a Windows partition and directly access the files on it. As with a Linux partition, you use the mount command, but you also have to specify the file system type as Windows. For that, use the -t option, and then type vfat for Windows 95/98/ME (msdos for MS-DOS). For Windows XP, 2000, and NT, you would use ntfs (limited read-only access; write access is experimental and not recommended). In the next example, the user mounts the Windows hard disk partition /dev/hda1 to the Linux file structure at directory /mnt/windows. The /mnt/windows directory is a common designation for Windows file systems, though you can mount it in any directory (such as /mnt/dos for MS-DOS). If you have several Windows partitions, you could create a Windows directory and then a subdirectory for each drive using the drive's label or letter, such as /mnt/windows/a or /mnt/windows/mystuff. Be sure you have already created the directory before mounting the file system.

# mount -t vfat /dev/hda1 /mnt/windows



 < Day Day Up > 



Red Hat(c) The Complete Reference
Red Hat Enterprise Linux & Fedora Edition (DVD): The Complete Reference
ISBN: 0072230754
EAN: 2147483647
Year: 2004
Pages: 328

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net