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 themountcommand, 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 themount 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 tomountdifferent 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 themountcommand:
I used the type (-t) switch to indicate that this floppy was formatted from an msdos-based system. I could have used the mount_msdosfscommand 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.
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 wholemountbusiness. 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
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, meaningmountassumed 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)
If you get that one, quickly reach down and push in the floppy before anyone 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 thatmount command.
How about this one:
mount: /floppy: No such file or directory
Looks like I forgot to make that mount point first. Amkdir /floppyshould 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 firstnand is instead spelledumount:
# 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:
# df Command
as will the mount command with no arguments:
This system currently has a floppy /dev/fd0 mounted on /floppy, meaning youll need to issue theumountcommand before ejecting the floppy.
Several other filesystems are also mounted, yet I only used themountcommand 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 fromdf:
# 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 usualmount 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 thevfs.usermountvariable:
# sysctl vfs.usermount=1
By changing the default0to a1, 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
If you'd like any user to have the right to mount a floppy, change the permissions so everyone has read and write access:
# chmod 666 /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
Oh boy. Looks like were back to square one, doesn't it? Remember, that entry in /etc/fstab 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 themountcommand, Im truly too lazy to have to type outmount -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
Now you simply need to typemfwhenever you want to mount a floppy andufwhen it's time to unmount the floppy. Or perhaps you'll prefer to create a keyboard shortcut [Hack #4].
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 invocations 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 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
First, notice that we don't use themount command. You can'tmounta filesystem before you have a filesystem! (You do have to have the floppy in the drive, though.) Take a look at the three steps:
If I see the following error message when I try tomountthe floppy, I'll realize that I forgot that third step:
Because mymfmount floppy alias uses themsdos 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 calledff(for format floppy):
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 theread pathnameline. 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:
Not too bad. I can now manipulate floppies with my own custommf,uf, andffcommands.
blog comments powered by Disqus