Advanced Concepts on Dealing with Files and Filesystems in BSD (
Page 1 of 6 )
In this conclusion to a two-part article, we continue our discussion of BSD commands and filesystems. It is excerpted from chapter two of the book BSD Hacks, written by Dru Lavigne (O'Reilly, 2005; ISBN: 0596006799). Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
HACK#18: DOS Floppy Manipulation
Bring simplicity back to using floppies.
If youre like many Unix users, you originally came from a Windows background. Remember your initial shock the first time you tried to use a floppy on a Unix system? Didn't Windows seem so much simpler? Forever gone seemed the days when you could simply insert a floppy, copy some files over, and remove the disk from the drive. Instead, you were expected to plunge into the intricacies of the
mount
command, only to discover that you didn't even have the right to use the floppy drive in the first place!
There are several ways to make using floppies much, much easier on your FreeBSD system. Let's start by taking stock of the default mechanisms for managing floppies.
Mounting a Floppy
Suppose I have formatted a floppy on a Windows system, copied some files over, and now want to transfer those files to my FreeBSD system. In reality, that floppy is a storage media. Since it is storing files, it needs a filesystem in order to keep track of the locations of those files. Because that floppy was formatted on a Windows system, it uses a filesystem called FAT12.
In Unix, a filesystem can't be accessed until it has been mounted. This means you have to use the
mount
command before you can access the contents of that floppy. While this may seem strange at first, it actually gives Unix more flexibility. An administrator can mount and unmount filesystems as they are needed. Note that I used the word administrator. Regular users don't have this ability, by default. Well change that shortly.
Unix also has the additional flexibility of being able to
mount
different filesystems. In Windows, a floppy will always contain the FAT12 filesystem. BSD understands floppies formatted with either FAT12 or UFS, the Unix File System. As you might expect from the name, the UFS filesystem is assumed unless you specify otherwise.
For now, become the superuser and let's pick apart the default invocation of the
mount
command:
% su
Password:
# mount -t msdos /dev/fd0 /mnt
#
I used the type (-t) switch to indicate that this floppy was formatted from an msdos-based system. I could have used the mount_msdosfs
command instead:
# mount_msdosfs /dev/fd0 /mnt
Both commands take two arguments. The first indicates the device to be mounted. /dev/fd0 represents the first (0) floppy drive (fd) device (
/dev
).
The second argument represents the mount point. A mount point is simply an empty directory that acts as a pointer to the mounted filesystem. Your FreeBSD system comes with a default mount point called /mnt. If you prefer, create a different mount point with a more useful name. Just remember to keep that directory empty so it will be available as a mount point, because any files in your mount point will become hidden and inaccessible when you mount a device over it.
This can be a feature in itself if you have a filesystem that should always be mounted. Place a README file in /mnt/important_directory containing: “If you can see this file, contact the administrator at this number....”
In this example, I'll create a mount point called /floppy, which Ill use in the rest of the examples in this hack:
# mkdir /floppy
Common Error Messages
This is a good place to explain some common error messages. Trust me, I experienced them all before I became proficient at this whole
mount
business. At the time, I wished for a listing of error messages so I could figure out what I had done wrong and how to fix it.
Let's take a look at the output of this command:
# mount /dev/fd0 /mnt
mount: /dev/fd0 on /mnt: incorrect super block
Remember my first mount command? I know it worked, as I just received my prompt back. I know this command didnt work, because mount instead wrote me a message explaining why it did not do what I asked.
That error message isn't actually as bad as it sounds. I forgot to include the type switch, meaning
mount
assumed I was using UFS. Since this is a FAT12 floppy, it simply didn't understand the filesystem.
This error message also looks particularly nasty:
fd0: hard error cmd=read fsbn 0 of 0-3 (No status
)
msdosfs: /dev/fd0: Input/output error
If you get that one, quickly reach down and push in the floppy before any
one else notices. You forgot to insert it into the bay.
Here's another error message:
msdosfs: /dev/fd0: Operation not permitted
Oops. Looks like I didn't become the superuser before trying that
moun
t
command
.
How about this one:
mount: /floppy: No such file or directory
Looks like I forgot to make that mount point first. A
mkdir /floppy
should fix that one.
The one error message you do not want to see is a system panic followed by a reboot. It took me a while to break myself of the habit of just ejecting a floppy once I had copied over the files I wanted. That's something you just don't do in Unix land.
You must first warn your operating system that you have finished using a filesystem before you physically remove it from the computer. Otherwise, when it goes out looking for a file, it will panic when it realizes that it has just disappeared off of the edge of the universe! (Well, the computer's universe anyway.) Put yourself in your operating system's shoes for a minute. The user entrusted something important to your care. You blinked for just a split second and it was gone, nowhere to be found. You'd panic too!
Managing the Floppy
How do you warn your operating system that the universe has shrunk? You unmount the floppy before you eject it from the floppy bay. Note that the actual command used is missing the first
n
and is instead spelled
umount
:
# umount /floppy
Also, the only argument is the name of your mount point. In this example, it's /floppy.
How can you tell if a floppy is mounted? The disk free command will tell you:
|
Filesystem |
1K-blocks |
Used |
Avail |
Capacity |
Mounted on |
|
/dev/ad0s1a |
257838 |
69838 |
167374 |
29% |
/ |
|
devfs |
1 |
1 |
0 |
100% |
/dev |
|
/dev/ad0s1e |
257838 |
616 |
236596 |
0% |
/tmp |
|
/dev/ad0s1f |
13360662 |
2882504 |
9409306 |
23% |
/usr |
|
/dev/ad0sld |
257838 |
28368 |
208844 |
12% |
/var |
|
/dev/fe0 |
1424 |
1 |
1423 |
0% |
/floppy |
# df Command
as will the mount command with no arguments:
# mount
/dev/ad0s1a on / (ufs, local)
devfs on /dev (devfs, local)
/dev/ad0s1e on /tmp (ufs, local, soft-updates)
/dev/ad0s1f on /usr (ufs, local, soft-updates)
/dev/ad0s1d on /var (ufs, local, soft-updates)
/dev/fd0 on /floppy (msdosfs, local)
This system currently has a floppy /dev/fd
0 mounted on /floppy, meaning youll need to issue the
umount
command before ejecting the floppy.
Several other filesystems are also mounted, yet I only used the
mount
command on my floppy drive. When did they get mounted and how? The answer is in /etc/fstab, which controls which filesystems to mount at boot time. Here's my /etc/fstab; it's pretty similar to the earlier output from
df
:
|
# Device |
Mountpoint |
FStype |
Options |
Dump |
Pass# |
|
/dev/ad0s1b |
none |
swap |
sw |
0 |
0 |
|
/dev/ad0s1a |
/ |
ufs |
rw |
1 |
1 |
|
/dev/ad0s1e |
/tmp |
ufs |
rw |
2 |
2 |
|
/dev/ad0s1f |
/usr |
ufs |
rw |
2 |
2 |
|
/dev/ad0s1d |
/var |
ufs |
rw |
2 |
2 |
|
/dev/acd0 |
/cdrom |
cd9660 |
ro,noauto |
0 |
0 |
|
proc |
/proc |
procfs |
rw |
0 |
0 |
|
linproc |
/compat/linux/proc |
linprocfs |
rw |
0 |
0 |
# more /etc/fstab command
Each mountable filesystem has its own line in this file. Each has its own unique mount point and its filesystem type listed. See how the /cdrom mount point has the options ro, noauto instead of rw? The noauto tells your system not to mount your CD-ROM at bootup. That is a good thing--if there's no CD in the bay at boot time, the kernel will either give an error message or pause for a few seconds, looking for that filesystem.
However, you can mount a data CD-ROM at any time by simply typing:
# mount /cdrom
That command was shorter than the usual
mount
command for one reason: there was an entry for /cdrom in /etc/fstab. That means you can shorten the command to mount a floppy by creating a similar entry for /floppy. Simply add this line to /etc/fstab:
/dev/fd0 /floppy msdos rw,noauto 0 0
Test your change by inserting a floppy and issuing this command:
# mount /floppy
If you receive an error, check /etc/fstab for a typo and try again.
Allowing Regular Users to Mount Floppies
Now that the superuser can quickly mount floppies, let's give regular users this ability. First, we have to change the default setting of the
vfs.usermount
variable:
# sysctl vfs.usermount=1
vfs.usermount: 0 -> 1
By changing the default
0
to a
1
, we've just enabled users to mount virtual filesystems. However, don't worry about your users running amok with this new freedom--the devices themselves are still owned by root. Check out the permissions on the floppy device:
# ls -l /dev/fd0
crw-r----- 1 root operator 9, 0 Nov 28 08:31 /dev/fd0
If you'd like any user to have the right to mount a floppy, change the per
missions so everyone has read and write access:
# chmod 666 /dev/fd0
Now, if you don't want every user to have this right, you could create a group, add the desired users to that group, and assign that group permissions to /dev/fd0.
You're almost there. The only kicker is that the user has to own the mount point. The best place to put a user's mount point is in his home directory. So, logged in as your usual user account:
% mkdir ~/floppy
Now, do you think the mount command will recognize that new mount point?
% mount ~/floppy
mount: /home/dru/floppy: unknown special file or file system
Oh boy. Looks like were back to square one, doesn't it? Remember, that entry in /etc/fsta
b only refers to root's mount point, so I can't use that shortcut to refer to my own mount point. While it's great to have the ability to use the
mount
command, Im truly too lazy to have to type out
mount -t msdos /dev/fd0 ~/floppy
, let alone remember it.
Thank goodness for aliases. Try adding these lines to the alias section of your ~.cshrc file:
alias mf mount -t msdos /dev/fd0 ~/floppy
alias uf umount ~/floppy
Now you simply need to type
mf
whenever you want to mount a floppy and
uf
when it's time to unmount the floppy. Or perhaps you'll prefer to create a keyboard shortcut [Hack #4].
Formatting Floppies
Now that you can mount and unmount floppies with the best of them, it's time to learn how to format them. Again, let's start with the default invoca
tions required to format a floppy, then move on to some ways to simplify the process.
When you format a floppy on a Windows or DOS system, several events occur:
- The floppy is low-level formatted, marking the tracks and sectors onto the disk.
- A filesystem is installed onto the floppy, along with two copies of its FAT table.
- You are given the opportunity to give the floppy a volume label.
The same process also has to occur when you format a floppy on a FreeBSD system. On a 5.x system, the order goes like this:
% fdformat -f 1440 /dev/fd0
Format 1440K floppy `/dev/fd0'? (y/n): y
Processing ------------------------------
% bsdlabel -w /dev/fd0 fd1440
% newfs_msdos /dev/fd0
/dev/fd0: 2840 sectors in 355 FAT12 clusters (4096 bytes/cluster)
bps=512 spc=8 res=1 nft=2 rde=512 sec=2880 mid=0xf0 spf=2 spt=18 hds=2 hid=0
First, notice that we don't use the
mount
command. You can't
mount
a filesys
tem before you have a filesystem! (You do have to have the floppy in the drive, though.) Take a look at the three steps:
-
fdformat
does the low-level format.
-
bsdlabel
creates the volume label.
- newfs_msdos
installs the FAT12 filesystem.
If I see the following error message when I try to
mount
the floppy, I'll realize that I forgot that third step:
% mf
msdosfs: /dev/fd0: Invalid argument
Because my
mf
mount floppy alias uses the
msdos
filesystem, it will complain if the floppy isn't formatted with FAT12.
Automating the Format Process
Any three-step process is just begging to be put into a shell script. I like to keep these scripts under ~/bin. If you don't have this directory yet, create it. Then create a script called
ff
(for format floppy):
% cd
% mkdir bin
% cd bin
% vi ff
#!/bin/sh
#this script formats a floppy with FAT12
#that floppy can also be used on a Windows system
# first, remind the user to insert the floppy
echo "Please insert the floppy and press enter"
read pathname
# then, proceed with the three format steps
fdformat -f 1440 /dev/fd0
bsdlabel -w /dev/fd0 fd1440
newfs_msdos /dev/fd0
echo "Format complete."
Note that this script is basically those three commands, with comments thrown in so I remember what the script does. The only new part is the
read pathname
line. I added it to force the user to press Enter before the script proceeds.
Remember to make the script executable:
% chmod +x ff
I'll then return to my home directory and see how it works. Since I use the C shell, I'll use the rehash command to make the shell aware that there is a new executable in my path:
rehash command to make the shell aware that there is a new executable in my path:
Ill then return to my home directory and see how it works. Since I use the C shell, Ill use the rehash command to make the shell aware that there is a new executable in my path:
% cd
% rehash
% ff
Please insert the floppy and press enter
Format 1440K floppy `/dev/fd0'? (y/n): y
Processing ------------------------------ /dev/fd0: 2840 sectors in 355 FAT12 clusters (4096 bytes/cluster)
bps=512 spc=8 res=1 nft=2 rde=512 sec=2880 mid=0xf0 spf=2 spt=18 hds=2 hid=0
Format complete.
Not too bad. I can now manipulate floppies with my own custom
mf
,
uf
, and
ff
commands.
See Also
-
man fstab
- man fdformat
- man bsdlabel
- man newfs
-
The Creating and Using Floppies section of the FreeBSD Handbook (http:/
/ www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/floppies.html)
- The Mounting and Unmounting File Systems section of the FreeBSD Handbook (http://www.freebsd.org/doc/en_US. ISO8859-1/books/handbook/mount-unmount.html)