Using dmesg To Find Drives
Before you can mount drives, you need to know how to reference them. Each device under UNIX is listed under the /dev/ directory. Therefore a tape drive might be /dev/st0, and a SCSI hard drive might be /dev/sda.
You cannot access these directories directly to access your files. Hence why you must mount devices.
To see a list of your devices, it's best to call up your kernel boot messages. This can be done using the "dmesg" program. Here is a sample output from typing "dmesg" as root:
hda: Maxtor 91728D8, ATA DISK drive
hda: hda1 hda2 hda3
hdc: SONY CDU4811, ATAPI CD/DVD-ROM drive
Attached scsi tape st0 at scsi0, channel 0, id 5, lun 0
As you can see there's a hard drive under /dev/hda, a CD-ROM drive under /dev/hdc, and a tape drive under /dev/st0. Additionally, the hard drive has multiple partitions (1, 2, and 3). Thus to access each partition we would mount as /dev/hda1, /dev/hda2, or /dev/hda3.
Using the Mount Command
The mount command is what allows devices to be tied in to directories. If typed by itself, it will give a listing of how drives are mounted. Here's a brief example:
/dev/hda3 on / type ext3 (rw)
/dev/hda1 on /boot type ext3 (rw)
In this example, both hda3 and hda1 are already mounted, and can be accessed by going typing "cd /" or "cd /boot" respectively.
Mounting a CD-ROM Drive
CD-ROM drives that are IDE are labeled as follows in Linux:
If one were to guess, /dev/hdc is usually a good bet for CD-ROM drives in a typical system. Although it's still best to do a dmesg to find out where your CD-ROM drive really is located. To mount a CD-ROM drive that resides under /dev/hdc, we would type the following command:
mount /dev/hdc /mnt/cdrom
This would tell the system to take device /dev/hdc (our CD-ROM drive) and make its contents accessible under the directory /mnt/cdrom.*
* If the /mnt/cdrom directory does not exist, create it by typing:
Mounting Hard Drives
Mount hard drives often means you will deal with partitions. Partitions are different storage areas on your hard drive. Much like rooms in a house. Partitions are set up by you, or the person/company that set up your hard drive. Sometimes there aren't multiple partitions on a drive. But in the Linux world, this would be extremely rare.
Again, we would look for our hard drive in the dmesg log. On most IDE systems, the hard drive is going to be /dev/hda. Next we need a directory to mount to. We might make the following directories:
From there we could mount each partition to a directory. Be sure not to mount a drive that's already mounted! Chances are, some, if not all partitions of you Linux system are already mounted! Read Using the Mount Command above.
mount /dev/hda1 /mnt/hda1
mount /dev/hda2 /mnt/hda2
mount /dev/hda3 /mnt/hda3
mount /dev/hda4 /mnt/hda4
Advanced - Loopback / Image File Mounts
Sometimes entire file systems are stored in one file. This is called an "image." When an image file is mounted, it will appear as an entire hard drive partition. Uses of images vary, but often they are used for embedded systems, or small Linux-on-disk distributions.
Mounting an image requires the use of the loopback driver. Generally this is already installed in your kernel. An image file will typically have a .img extension.
To use the loopback device, pass the loop option into the mount command. For example, to mount an image file called /root/boot.img onto the directory /mnt/image you would type the following (be sure to make a directory called /mnt/image!):
mount -o /root/boot.img /mnt/image
Advanced - NFS Mounts
There are times where you may want a directory on a remote machine to be available on your machine. In UNIX / GNU Linux, this is called NFS. NFS stands for Network File System, although often times people joke that it stands for No File Security. The authentication for NFS is very weak, and should only be used on internal network, or best yet avoided altogether.
There are two machines in an NFS setup. The client, and the server. The first system we'll address is the server. This machine needs to know what directory to make available (export) to the client(s). This is controlled in your /etc/exports file.
Let's say we want to export /home/music. The first thing we need to do is tell the server to share this directory. We also need to say who we want to share this directory with (i.e. what IP addresses). Let's say one of the machines is IP address 192.168.1.210. Our exports file would need the following line:
In this example we said to export /home/music only to the machine with IP address 192.168.1.210. The (rw) means allow that machine to both read and write to the directory.
Now you need to put these changes into effect by restarting NFS. This can be done by typing the following as root on the server:
You also need to set permissions in the file system for NFS to read and write to the directory. On modern Red Hat machines this can be done by typing:
chown nfsnobody:nfsnobody /home/music
Now we need to tell the client to access that directory to do that we'll become root on 192.168.1.210 and type the following (be sure to make a directory called /mnt/nfs):
mount 192.168.1.1:/home/music /mnt/nfs
In the example we typed the mount command, told it what IP address or server is, followed by a colon and the directory on the remote machine that we want to access, followed my the location we wish to mount it on in the clients directory structure.
Macintosh OS X Note: If you are sharing NFS from a Linux server to a Mac OS X machine, you will need to add the insecure port option to your exports file. Failture to do this will cause the mount to fail. An example export line for a Mac located at 192.168.1.15 would look like the following:
Slow Mounts In Linux: If mounts are taking forever on your Linux machine(s) file locking may be to blame. This can be fixed by passing the nolock option in on the client side. For example:
mount -o nolock 192.168.1.1:/home/music /mnt/nfs