ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


FreeBSD Basics

Mounting Other Filesystems

07/05/2000

Anyone who has ever worked in a networked environment running different operating systems using different filesystems knows the frustration of trying to get every computer to see the data on every other computer. Even on my multi-boot test computer, NT can't see the data on my FAT32 partition, Windows 98 can't see the data on my NTFS partition, DOS can't see data on either partition -- and these operating systems are all installed on the same hard drive.

Fortunately, I also have FreeBSD installed on this computer, and it has no problem accessing the data anywhere on that hard drive, thanks to the mount command.

The mount command is one of the most powerful commands available to root. It allows root to mount filesystems so users can access either data physically located on a device cabled to that computer or data physically located on other computers that understand the mount command.

The mount command itself is simple:

mount (-t filesystem) devicename mountpoint

Let's start with some common device names:

Device

Name

Floppy

/dev/fd0

CD-ROM

/dev/acd0c

SCSI drives

/dev/sd0s#

IDE drive, FreeBSD 2.x and 3.x

/dev/wd0s#

IDE drive, FreeBSD 4.x

/dev/ad0s#

Each device is numbered starting with 0. Storage devices will also have the sub partition unit, or "slice" number appended to them. The slice number (represented above as s#) consists of the letter 's' and a number.

For example, my test computer has one IDE drive that has been partitioned as follows:

200 MB FAT partition containing DOS
1.8 GB partition containing FreeBSD 4.0
500 MB NTFS partition containing NT Server 4.0
1.5 GB FAT32 partition containing Windows 98

If I boot to DOS, it only recognizes a partition named C:.

If I boot to NT, it sees a FAT partition called C: and an NTFS partition called D:.

If I boot to Windows 98, it sees a FAT partition called C: and a FAT32 partition called D:.

Because they don't recognize each other's filesystems, both Windows 98 and NT think they reside on a partition called D, even though they reside on different partitions of my hard drive.

FreeBSD's logic makes a bit more sense, as it sees my drive like this:

/dev/ad0s1 as FAT
/dev/ad0s2 as FreeBSD
/dev/ad0s3 as NTFS
/dev/ad0s4 as FAT32

since I've "sliced" my first IDE drive into 4 sections.

If you are multi-booting your FreeBSD computer, you can check out the device names for your partitions with /stand/sysinstall. As root, type /stand/sysinstall and choose Configure, then Fdisk, and then use your spacebar to select the drive

The "Name" column will list the device name; the "Desc" column will list the type of filesystem. If you used DOS fdisk to partition your hard drive, it will only show two entries: one for the primary partition and the other for the extended partition. An Intel BIOS may support up to 4 primary partitions, but DOS-based Fdisk utilities will only let you create one primary partition, and FreeBSD's Fdisk utility does not show logical partitions. I prefer to use Partition Magic, as it lets me create four primary partitions.

The mount command also requires you to specify a mountpoint. A mountpoint is simply an empty directory you've created as a reference point to access mounted data. The mounted data is not actually placed in this directory; instead, think of the mountpoint as a virtual shell where you can use your Unix commands to manipulate the mounted data. It is important that you keep your mountpoint directories empty; use other directories for storing files.

Mountpoints are usually created as subdirectories of /; to see them, type:

cd /
ls

Among the subdirectories listed, note that FreeBSD has already created 2 mountpoints for you: /cdrom and /mnt.

When you create your mountpoints, give them useful names. For example:

/floppy, /ntfs, /fat32, /fat.

Now for the filesystem: Notice that I put -t filesystem in brackets when I gave the syntax for the mount command. The filesystem switch is optional; FreeBSD assumes you want to mount the UFS (Unix File System) unless you specify otherwise. The most common filesystem types you'll probably specify are:

-t msdos

for FAT floppies, FAT16, and FAT32 partitions

-t cd9660

for data CDROMs

-t ntfs

for primary NTFS partitions

A few notes on floppies and Unix: If you are used to sticking a floppy into the floppy drive of a Windows computer and then ejecting it at will, it'll take some getting used to how Unix computers treat floppies.

Both hard drives and floppies contain filesystems that must be mounted for their data to be accessed. Only root can mount filesystems, so you must be root to mount a floppy. Also, you can't just eject a mounted floppy; you must tell Unix to unmount it first.

Keep in mind that hard drives are considered to be permanent storage devices, while floppies are temporary storage devices. You wouldn't dream of physically removing your hard drive and adding another one while your computer was booted into an operating system on that first hard drive. For the same reason, don't mount a floppy and then eject it without telling Unix to unmount it first.

It is possible to format a floppy with DOS from FreeBSD. To format a floppy, do NOT mount it first. Remember, you mount filesystems, and you don't have a filesystem until you format. As root, put a floppy in your floppy drive, then type:

fdformat /dev/rfd0
Format 1440 floppy '/dev/rfd0'? (y/n):y

When it is finished processing,

disklabel -w -r /dev/rfd0 fd1440
newfs_msdos -f 1440 fd0

You can now mount that floppy like this:

mount -t msdos /dev/fd0 /floppy
cd /floppy
ls

The ls command should confirm that there is nothing on the floppy. Let's copy something onto the floppy:

cp /etc/fstab /floppy
ls

You should now see a file called "fstab" on your floppy. Type:

more /floppy/fstab

You should be able to hear your floppy drive churn as you view the contents of fstab. If you try to unmount the floppy,

umount /floppy

you'll see:

umount: unmount of /floppy failed: Device busy

You can't unmount a filesystem if it is your present working directory. Let's try again:

cd /
umount /floppy

It is now safe to eject the floppy from the floppy drive.

Now let's try a CD-ROM. Put a data CD-ROM, not a music CD-ROM, into your CD-ROM bay and type:

mount /cdrom
ls

You should be able to see the contents of the CD. Why did the shortened mount command work? Remember that FreeBSD already created a mountpoint called /cdrom for you? Well, it also added an entry to a file that is read by the mount command if you don't specify a device name. Try this:

more /etc/fstab
# Device     Mountpoint	FStype  Options   Dump Pass#
/dev/ad0s2b  none		swap    sw        0	   0
/dev/ad0s2a  /          ufs     rw        1    1
/dev/ad0s2f  /usr       ufs     rw        2    2
/dev/ad0s2e  /var       ufs     rw        2    2
/dev/acd0c   /cdrom     cd9660  ro,noauto 0    0
proc         /proc      procfs  rw        0    0

Notice that there is an entry for /cdrom with its options set at "noauto." This tells FreeBSD not to mount your CD-ROM automatically when you reboot; however, it now shortens the mount command for when you do want to mount a CDROM. Let's unmount the CDROM and add an entry to the /etc/fstab file to shorten the mount command for floppies:

cd /
umount /cdrom
pico /etc/fstab

At the end of the file, add this line:

/dev/fd0    /floppy    msdos    rw,noauto    0    0

Make sure you tab over to keep your columns neat; also, make sure it all fits on one line. Doublecheck for typos before saving this file.

Now, insert a floppy into your floppy drive and try:

mount /floppy
ls
cd /
umount /floppy

I can also mount my C:\ drive while in FreeBSD; since its device name is /dev/ad0s1, I issue this command:

mount -t msdos /dev/ad0s1 /fat

I can then enter cd /fat and freely edit and delete files on the C:\ drive using my favorite Unix commands. I can also copy files back and forth between C:\ and FreeBSD.

If I want to get real fancy, I'll also mount my FAT32 partition:

mount -t msdos /dev/ad0s4 /fat32

and I can copy a file from C:\ to what Windows 98 calls the D:\ partition:

cp /fat/test.txt /fat32/test.txt

Saves a lot of rebooting if I just want to move some files around. If I want to save myself some typing when I wish to access these filesystems, I'll add the following lines to the end of /etc/fstab:

# Device    Mountpoint  FStype  Options  Dump  Pass#
/dev/ad0s1  /fat        msdos   rw       0     0
/dev/ad0s4  /fat32      msdos   rw       0     0

Because these file systems are located on my permanent storage device, I can have FreeBSD mount them at every boot; therefore, I haven't set the Options to "noauto".

If I've rebooted since adding these lines to /etc/fstab, these partitions will be mounted for me. I can simply use the commands cd /fat or cd /fat32 to access the data on these partitions.

Today's article focused on accessing the file systems of devices physically attached to your FreeBSD computer. Next week, we'll discuss how to access data located on Microsoft computers within your network.

Dru Lavigne is a network and systems administrator, IT instructor, author and international speaker. She has over a decade of experience administering and teaching Netware, Microsoft, Cisco, Checkpoint, SCO, Solaris, Linux, and BSD systems. A prolific author, she pens the popular FreeBSD Basics column for O'Reilly and is author of BSD Hacks and The Best of FreeBSD Basics.


Read more FreeBSD Basics columns.

Discuss this article in the Operating Systems Forum.

Return to the BSD DevCenter.

 

Copyright © 2009 O'Reilly Media, Inc.