Home arrow Site Administration arrow Advanced Concepts on Dealing with Files and Filesystems in BSD

Advanced Concepts on Dealing with Files and Filesystems in BSD

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.

TABLE OF CONTENTS:
  1. Advanced Concepts on Dealing with Files and Filesystems in BSD
  2. HACK#19: Access Windows Shares Without a Server
  3. HACK#20: Deal with Disk Hogs
  4. HACK#21: Manage Temporary Files and Swap Space
  5. HACK#22: Recreate a Directory Structure Using mtree
  6. HACK#23: Ghosting Systems
By: O'Reilly Media
Rating: starstarstarstarstar / 8
January 04, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

  % 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_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.

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 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
 
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, 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)
  msdosfs: /dev/fd0: Input/output error

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:

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/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:

# 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 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
 
vfs.usermount: 0 -> 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
  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 permissions 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/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
  alias uf  umount ~/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].

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 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:

  1. The floppy is low-level formatted, marking the tracks and sectors onto the disk.
  2. A filesystem is installed onto the floppy, along with two copies of its FAT table.
  3. 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 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:

  1. fdformat does the low-level format.
  2. bsdlabel creates the volume label.
  3. newfs_msdosinstalls the FAT12 filesystem.

If I see the following error message when I try tomountthe floppy, I'll realize that I forgot that third step:

  % mf
 
msdosfs: /dev/fd0: Invalid argument

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):

  % 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 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:

  % 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 custommf,uf, andffcommands.

See Also

  1. man fstab
  2. man fdformat
  3. man bsdlabel
  4. man newfs
  5. The Creating and Using Floppies section of the FreeBSD Handbook (http:// www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/floppies.html)
  6. 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)



 
 
>>> More Site Administration Articles          >>> More By O'Reilly Media
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

SITE ADMINISTRATION ARTICLES

- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: