The Joy of Mounting
The topic of mounting is a broad one. An individual need only look at the mount(8) man pages, one of the largest tomes accessible via man rivaling even ps. To truly get to the heart of mounting we first need the various filesystems at our disposal. The most common file types you’ll want to mount will probably be one of the following (though there’s many more!):
Ext2
Ext3
JFS
XFS
Reiserfs
NTFS
(V)FAT
NFS
SMB
ISO9660
You’ll probably also uses some of the following in your fstab file which we’ll talk about more later:
swap
proc
tmpfs
What makes mounting… involved, is not the process of mounting but the sheer volume of options each one of these filesystems accept. You’ll quickly find the -o option for mount can take more forms than Sydney Bristow in the show “Alias”.
The basic mount request will look something like this:
mount -t <filesystem> -o <options> /dev/<device> /<mount point>
This is not the shortest version of course, however the most basic form requires we set up our fstab file and that’s getting ahead of ourselves. Let’s take this command apart in the following example:
mount -t ext2 -o acl,usrquota /dev/hda3 /home
This mounts the ext2 file system which happens to be the third partition on the master drive on the primary IDE controller enabling POSIX acls (Access Control Lists) and user level quotas as the home directory. Pretty simple eh? As a side note in order to enable acls your kernel must be complied to support them.
Lets talk about some trickier filesytems now. NTFS is a good place to start. Starting with windows NT, Microsoft has been using the NTFS filesystem as their default. NTFS offers great security features for windows users, but makes it very hard for us poor Linux users. Starting with the 2.4 kernel rudimentary NTFS support has been available, however, even in the latest kernel release (2.6.16.17) that support is limited to read-only. With kernel level support that means we can look at an NTFS partition and even copy files over to our native filesystems to work on. We just can’t copy those changed files back to the drive, or in any way modify the filesystem.
Luckily for us there are ways to break that read only barrier. One of them is called NTFS Fuse which is still limited to only 10 writes, but what’s really interesting is something called Captive NTFS. This uses Microsoft’s own drivers for full access to the partition, you can copy, write, override, etc to your heart’s content. To mount using these drivers you need to use the type of “captive-ntfs”. (i.e., mount -t captive-ntfs …)
There’s a live distro out there called The Trinity Resource Kit which is really great imo and I’ve used it a number of times to save windows computers. The distro is cmd line only and a relatively small d/l but is full of power. It comes w/ a script to d/l the captive ntfs drivers as well as update the clamAV database and then create a new ISO for you to burn off giving you the ability to fully access a windows computer even with out an Internet connection.
Since I originally wrote this tutorial kernel support for NTFS writes is still not supported. However, most distributions now offer the ntfs-3g drivers which support read/write to NTFS with great reliability. Check your distro’s documentation for further information on ntfs-3g support and options.
Let’s shift out discussion from native filesystems for a moment. If you’re in a heterogeneous network with windows and Linux computers you’ll undoubtedly be running Samba on your Linux computer to access the windows shares. If you’re not then you really need to get w/ the program. ;) You want to make that windows share look like just another folder on your Linux box just like windows can map a drive don’t you? The question is how do you do that.
There’s actually “two” ways to go about it. I say two because technically it’s only one and the other is just a wrapper for it. Technically the command you’re working with is smbmount. Of course, almost nobody uses that. The way to use smbmount through mount is to use the “smbfs” file system. Let’s imagine I have a windows computer called “game”, and I’ve shared out a folder with the share name of “files”. Let’s further assume it’s locked out via acls so only my username “tulkas” has access. How can I access it from my Linux box? Simple:
mount -t smbfs -o username=tulkas //game/files /mnt/files
We already discussed the smbfs part and if you can’t figure out what that option does… well let’s not go there. That leaves the last two parts. In order to mount any samba share you need to use the UNC name of the server which if you’re not familiar with is simply //<computer’s name>/<share> and finally the where you’re mounting to, namely, /mnt/files. Remember that this folder needs to exist *before* you mount. (mkdir -p /mnt/files) It should be noted that /mnt/ is the default location where most people mount there extraneous filesystems, especially things like CD-ROM, DVD, floppy, etc. However, KDE likes to use /media/<same folder name> for some reason so you might want to make a symlink (or just use /media).
NFS mounting follows a similar method for mounting except yo use nfs for the type and the server is referenced via: server:/<export> NFS also has some extra options that a lot of people tend to take advantage of. A typical NFS mount will look something like:
mount -t nfs -o hard,wsize=8192,rsize8192 server:/export/data /mnt/data
The hard option tells Linux to wait as long as it takes for the server to come back up if it happens to fail preventing data loss in the event the server goes down for some reason. the wsize and rsize stand for write and read buffer, the default is 1024bytes (1MB) this is a bit of a hold over from older days using 10base5 etc. On today’s 100baseTX(+) networks you can notice a substantial increase of performance upping those values to 8192bytes (8MB).
Loops, if you’re a programmer this is not a new concept. However, it’s nothing like a programming loop. One of the most interesting options mount has is loop. If you you have an ISO of a CD for example you can mount it just like any CD, the key is the loop option. Let’s see how:
mount -t iso9660 -o loop /home/tulkas/my.iso /mnt/pictures
Now you’re all set to go! If you’ve ever wondered how to make an ISO in Linux, it’s a very simple process and all distros come w/ the tool you need. It’s called “dd” Let’s say my CD-ROM is /dev/hdc and you want to make an ISO of it. Here’s what you need to do: dd if=/dev/hdc of=my.iso Too simple. If you want to make a make a back up of one of your games that may have copy protection you might want to add the conv=noerror option so the final command would look like: dd conv=noerror if=/dev/hdc of=my.iso if stands for input file, and of is output file by the way.
I should tell you that while using the -t <filesystem> is the “correct” method, it’s almost never used. Mount can almost always figure out what the filesystem is that you’re trying to mount, and if it can’t it just says “hey, wtf are you trying to mount” (or something like that anyway) and then you can just add the flag to get it working. So, for example to mount that NFS export you can just type:
mount -o hard,wsize=8192,rsize8192 server:/export/data /mnt/data
Now that we have the basics under out belt, let’s figure out how to have the system take care of all the dirty work for us. The key is a file called /etc/fstab and here’s mine:
/dev/hda1 /boot ext2 noauto,noatime 1 2
/dev/hda3 / reiserfs noatime 0 1
/dev/hda2 none swap sw 0 0
/dev/dvd /mnt/dvd iso9660 noauto,ro,user 0 0
/dev/dvdrecorder /mnt/dvdrecorder iso9660 noauto,ro,user 0 0
/dev/fd0 /mnt/floppy auto noauto 0 0
proc /proc proc defaults 0 0
shm /dev/shm tmpfs nodev,nosuid,noexec 0 0
The first column is the device, second is mount point, third is filesystem, fourth is options. Lastly you’ll notice two numbers in the final column(s). The first number is used by the dump command to figure out how often it should be backed up, and the second tells fsck which order the drives should be checked at boot up.
Once you have your fstab file set up mounting becomes even easier, for example if I want to mount my dvd drive all I have to type is:
mount /mnt/dvd
That’s it! mount will look through fstab and figure the rest out for me and let me browse at ease, you’ll notice for my dvd I have the “user” option. Normally only root can use mount, but for devices which have the “user” option specified any user can mount them. Most often you’ll want your removable storage to have this option.
Another benefit to having your fstab set up is that mount has a flag “-a” which lets you mount all the filesystems listed in your fstab (of a particular filesystem, say iso9660, if you prefer).
One thing that is a little deceptive of the fstab is the inclusion of the swap space. Swap is *not* mounted. In order to activate a swap space you need to use “swapon <swap space>”. I specifically used the words “swap space” because, contrary to most novice’s belief, swap does not have to be a partition. It most often is, in the my case I’m using /dev/hda2 with the fdisk 82 and allocated with makeswap. However I could just as easily use touch a file and use dd to fill it to the size I want with the /dev/zero device and then use that for extra swap space.
You’ll also notice /proc and /dev/shm don’t have device files they’re just “proc” and “shm”. Those are special, and you don’t need to worry about them, at least not in the scope of this text.
Now that we have all these fun filesystems mounted for use it often comes up that we want to unmount them. The command do do this is very cleverly named “unmount”. If you no longer need your dvd mounted for example you can type “umount /mnt/dvd” and you’re all set. If you want to be spiffy and the envy of all your friends you can also try “eject dvd” which will unmount the device and then open the tray for you. If you’ve got a filesystem is giving you some trouble say one of those pesky users is still on it you can force a dismount by adding the “-f” flag: umount -f /mnt/stuff
Another way to make mounting even easier is to use the automounter. The automounter is just what it says. Once you have it set up you don’t even have to worry about typing out that pesky mount command. You just brows to the mount point and the automounter will mount it for you, it couldn’t get any easier.
Automounter is based on what’s called “maps” The main map is called /etc/auto.master which lists the other maps like:
/home auto.home
/stuff auto.stuff
We will assume that our friendly local admin has a windows server called “Server1” hosting all our /home dirs in the shared folder “homes” with the subdirs indicating all the users. For example, my home would be located at //Server1/homes/tulkas and my other Valar have theirs similarity named. The /etc/auto.home file would look something like this:
tulkas //Server1/homes/tulkas
manwe //Server1/homes/manwe
orome //Server1/homes/orome
ulmo //Server1/homes/ulmo
Now we have that all set up the only thing left is to.. oh yeah… get the automounter itself. Install it with your favorite method, apt-get, rpm, emerge, tarball… whatever floats your boat. Then make sure the rc files get set for run level 3, 5 and 6. To get it start w/o having to reboot your sever (something you should only have to do after a kernel update) just type: /etc/init.d/autofs start and then you’re set to go. Now if I type “cd /home” automounter will automatically mount my windows hosted home dir for me.
Well, that’s it for today’s lecture. You should now have a pretty firm grasp as to what mount is, what it does and what it doesn’t do. You should also know where to go to get more information (say it w/ me everyone: “man”!) which you’ll probably have to do to figure out some of the fun options out there for all the file systems. So, go forth and start mounting!