Home » , , » Managing File Systems in Linux

Managing File Systems in Linux

Written By Sajib Barua on Sunday, August 19, 2012 | 11:52 AM

A file system refers to the organization of files and directories. As a system administrator, you have to perform certain operations to manage file systems on various storage media. For example, you have to know how to mount — add a file system on a storage medium by attaching it to the overall Linux file system. You also have to back up important data and restore files from a backup. Other file-system operations include sharing files with the Network File System (NFS) and accessing MS-DOS files. This chapter shows you how to perform all file-system management tasks.
Exploring the Linux File System
The files and directories in your PC store information in an organized manner, just like paper filing systems. When you store information on paper, you typically put several pages in a folder and then store the folder in a file cabinet. If you have many folders, you probably have some sort of filing system. For example, you may label each folder’s tab and then arrange them alphabetically in the file cabinet. You might have several file cabinets, each with lots of drawers, which, in turn, contain folders full of pages.
Operating systems, such as Linux, organize information in your computer in a manner similar to your paper filing system. Linux uses a file system to organize all information in your computer. Of course, the storage medium isn’t a metal file cabinet and paper. Instead, Linux stores information on devices such as hard drives, USB drives, and DVD drives.
To draw an analogy between your computer’s file system and a paper filing system, think of a disk drive as the file cabinet. The drawers in the file cabinet correspond to the directories in the file system. The folders in each drawer are also directories — because a directory in a computer file system can contain other directories. You can think of files as the pages inside the folder — and that’s where the actual information is stored. Figure 3-1 illustrates the analogy between a file cabinet and the Linux file system.
It’s a bit of a stretch, but you can think of the Linux file system as similar to a filing cabinet
Figure 3-1: It’s a bit of a stretch, but you can think of the Linux file system as similar to a filing cabinet.
The Linux file system has a hierarchical structure — directories can contain other directories, which in turn contain individual files.
Everything in your Linux system is organized in files and directories. To access and use documents and programs on your system, you have to be familiar with the file system.
Understanding the file-system hierarchy
The Linux file system is organized like a tree, with a root directory from which all other directories branch out. When you write a complete pathname, the root directory is represented by a single slash (/). Then there’s a hierarchy of files and directories. Parts of the file system can be in different physical drives or different hard drive partitions.
Linux uses a standard directory hierarchy. Figure 3-2 shows some of the standard parts of the Linux file system. You can create new directories anywhere in this structure.
The Linux file system uses a standard directory hierarchy similar to this one
Figure 3-2: The Linux file system uses a standard directory hierarchy similar to this one.
Write the name of any file or directory by concatenating the names of directories that identify where that file or directory is and by using the forward slash (/) as a separator. For example, in Figure 3-2, the usr directory at the top level is written as /usr because the root directory (/) contains usr. On the other hand, the X11R6 directory is inside the usr directory, which is inside the root directory (/). Therefore, the X11R6 directory is uniquely identified by the name /usr/X11R6. This type of full name is a pathname because the name identifies the path you take from the root directory to reach a file. Thus, /usr/X11R6 is a pathname.
The Filesystem Hierarchy Standard (FHS) specifies the organization of files and directories in UNIX-like operating systems, such as Linux. FHS defines a standard set of directories and their intended use. The FHS, if faithfully adopted by all Linux distributions, should help improve the interoperability of applications, system administration tools, development tools, and scripts across all Linux distributions. FHS even helps the system documentation as well as books like this one because the same description of the file system applies to all Linux distributions. Version 2.3 of FHS was announced on January 29, 2004. FHS 2.3 is part of the Linux Standard Base version 3.x (LSB 3.0), which was released on July 1, 2005. The standard was updated with 3.1 on October 25, 2005, and 3.2 on January 28, 2008. LSB 3.x (see www.linux base.org) is a set of binary standards aimed at reducing variations among the Linux distributions and promoting portability of applications. As of this writing, the most current Base is 4.0.3 (April 2010). To find out more about FHS, check out the FHS home page at www.pathname.com/fhs.
Each of the standard directories in the Linux file system has a specific purpose. Table 3-1, Table 3-2, and Table 3-3 summarize these directories.
Table 3-1
Standard Directories in Linux File System
Directory
Used to Store
/bin
Executable files for user commands (for use by all users)
/boot
Files needed by the boot loader to load the Linux kernel
/dev
Device files
/etc
Host-specific system configuration files
/home
User home directories
/lib
Shared libraries and kernel modules
/media
Mount point for removable media
/mnt
Mount point for a temporarily mounted file system
/opt
Add-on application software packages
/root
Home directory for the root user
/sbin
Utilities for system administration
/srv
Data for services (such as Web and FTP) offered by this system
/tmp
Temporary files
 
Table 3-2
The /usr Directory Hierarchy
Directory
Secondary Directory Hierarchy
/usr/bin
Most user commands
/usr/include
Directory for include files files that are inserted into source code of applications by using various directives used in developing Linux applications
/usr/lib
Libraries used by software packages and for programming
/usr/libexec
Libraries for applications
/usr/local
Any local software
/usr/sbin
Nonessential system administrator utilities
/usr/share
Shared data that doesnt depend on the system architecture (whether the system is an Intel PC or a Sun SPARC workstation)
/usr/src
Source code

Table 3-3
The /var Directory Hierarchy
Directory
Variable Data
/var/cache
Cached data for applications
/var/lib
Information relating to the current state of applications
/var/lock
Lock files to ensure that a resource is used by one application only
/var/log
Log files organized into subdirectories
/var/mail
User mailbox files
/var/opt
Variable data for packages stored in the /opt directory
/var/run
Data describing the system since it was booted
/var/spool
Data thats waiting for some kind of processing
/var/tmp
Temporary files preserved between system reboots
/var/yp
Network Information Service (NIS) database files


Mounting a device on the file system
The storage devices that you use in Linux contain Linux file systems. Each device has its own local file system consisting of a hierarchy of directories. Before you can access the files on a device, you have to attach the device’s directory hierarchy to the tree that represents the overall Linux file system.
Mounting is the operation you perform to cause the file system on a physical storage device (a hard drive partition or a CD-ROM) to appear as part of the Linux file system. Figure 3-3 illustrates the concept of mounting.
Figure 3-3 shows each device with a name that begins with /dev. For example, /dev/cdrom is the first DVD/CD-ROM drive. Physical devices are mounted at specific mount points on the Linux file system. For example, the DVD/CD-ROM drive, /dev/cdrom, is mounted on /media/cdrom in the file system. After mounting the CD-ROM in this way, the Fedora directory on a CD-ROM or DVD-ROM appears as /media/cdrom/Fedora in the Linux file system.
You can use the mount command to manually mount a device on the Linux file system at a specified directory. That directory is the mount point. For example, to mount the DVD/CD-ROM drive at the /media/cdrom directory, type the following command (after logging in as root):
mount /dev/cdrom /media/cdrom
You have to mount a device on the Linux file system before accessing it
Figure 3-3: You have to mount a device on the Linux file system before accessing it.
The mount command reports an error if the DVD/CD-ROM device is mounted already or if no CD or DVD media is in the drive. Otherwise, the mount operation succeeds, and you can access the contents of the DVD or CD through the /media/cdrom directory.
You can use any directory as the mount point. If you mount a device on a nonempty directory, however, you can’t access the files in that directory until you unmount the device by using the umount command. Therefore, always use an empty directory as the mount point.
To unmount a device when you no longer need it, use the umount command. For example, for a DVD/CD-ROM device with the device name /dev/cdrom, type the following command to unmount the device:
umount /dev/cdrom
The umount command succeeds as long as no one is using the DVD/ CD-ROM. If you get an error when trying to unmount the DVD/CD-ROM, check to see if the current working directory is on the DVD or CD. If you’re currently working in one of the DVD/CD-ROM’s directories, that also qualifies as a use of the DVD/CD-ROM.
Examining the /etc/fstab file
The mount command has the following general format:
mount device-name mount-point
However, you can mount by specifying only the CD-ROM device name or the mount-point name, provided there’s an entry in the /etc/fstab file for the CD-ROM mount point. That entry specifies the CD-ROM device name and the file-system type. That’s why you can mount the CD-ROM with a shorter mount command.
For example, in Debian, you can mount the CD-ROM by typing one of the following commands:
mount /dev/cdrom
mount /media/cdrom
The /etc/fstab file is a configuration file — a text file containing information that the mount and umount commands use. Each line in the /etc/ fstab file provides information about a device and its mount point in the Linux file system. Essentially, the /etc/fstab file associates various mount points within the file system with specific devices, which enables the mount command to work from the command line with only the mount point or the device as argument.
Here’s a /etc/fstab file from a SUSE system. (The file has a similar format in other Linux distributions.)
/dev/hda11 / reiserfs acl,user_xattr 1 1
/dev/hda7 /boot ext3 acl,user_xattr 1 2
/dev/hda6 /data1 auto noauto,user 0 0
/dev/hda9 /data2 auto noauto,user 0 0
/dev/hda10 /data3 auto noauto,user 0 0
/dev/hda5 /data4 auto noauto,user 0 0
/dev/hda2 /windows/C ntfs ro,users,gid=users,umask=0002,nls=utf8 0 0
/dev/hda8 swap swap pri=42 0 0
devpts /dev/pts devpts mode=0620,gid=5 0 0
proc /proc proc defaults 0 0
usbfs /proc/bus/usb usbfs noauto 0 0
sysfs /sys sysfs noauto 0 0
/dev/cdrecorder /media/cdrecorder subfs fs=cdfss,ro,procuid,nosuid,nodev,exec,ioc
harset=utf8 0 0
The first field on each line shows a device name, such as a hard drive partition. The second field is the mount point, and the third field indicates the type of file system on the device. You can ignore the last three fields for now.
This /etc/fstab file shows that the /dev/hda8 device functions as a swap device for virtual memory, which is why both the mount point and the filesystem type are set to swap.
The Linux operating system uses the contents of the /etc/fstab file to mount various file systems automatically. During Linux startup, the init process executes a shell script that runs the mount -a command. That command reads the /etc/fstab file and mounts all listed file systems (except those with the noauto option). The third field on each line of /etc/ fstab specifies the type of file system on that device, and the fourth field shows a comma-separated list of options that the mount command uses when mounting that device on the file system. Typically, you find the defaults option in this field. The defaults option implies — among other things — that the device mounts at boot time, that only the root user can mount the device, and that the device mounts for both reading and writing. If the options include noauto, the device doesn’t mount automatically when the system boots.
In Fedora, you often find the managed option in the fourth field of /etc/ fstab entries. The managed option indicates that the line was added to the fstab file by the HAL (hardware abstraction layer) daemon, which runs the fstab-sync command to add entries in the /etc/fstab file for each removable drive that it detects. You typically find that the entries for DVD/ CD-ROM drive(s) (/dev/hdc in most systems) have the managed option in the fourth field.
Sharing Files with NFS
Sharing files through the NFS is simple and involves two basic steps:
  • On the NFS server, export one or more directories by listing them in the /etc/exports file and by running the /usr/sbin/exportfs command. In addition, you must run the NFS server.
  • On each client system, use the mount command to mount the directories the server has exported.
How you start the NFS server depends on the Linux distribution. If a GUI sysadmin tool is available, you can start the NFS server from the GUI tool. Otherwise, you can type a command in a terminal window to start the NFS server. For example, in Debian, you can type invoke-rc.d nfs-kernelserver start and invoke-rc.d nfs-common start to start the NFS server. In Fedora, type service nfs start. To start the NFS server in SUSE, you can use the YaST Control Center: From the main menu, choose System➪ YaST➪System➪System Services (Runlevel). In Xandros, you can start the NFS server from the Xandros Control Center (Main Menu➪Control Center) or by typing invoke-rc.d nfs-user-server start in a terminal window.
The only problem in using NFS is that each client system must support it. Most PCs don’t come with NFS — that means you have to buy NFS software separately if you want to share files by using NFS. If, however, all systems on your LAN run Linux (or other variants of UNIX with built-in NFS support), using NFS makes sense.
NFS has security vulnerabilities. Therefore, don’t set up NFS on systems directly connected to the Internet.
The upcoming section walks you through an NFS setup, using an example of two Linux PCs on a LAN.
Exporting a file system with NFS
To export a file system with NFS, start with the server system that exports — makes available to the client systems — the contents of a directory. On the server, you must run the NFS service and also designate one or more file systems to be exported to the client systems.
You have to add an appropriate entry to the /etc/exports file. For example, suppose you want to export the /home directory and you want to enable the hostname LNBP75 to mount this file system for read and write operations. (You can use a host’s IP address in place of the hostname.) You can do so by adding the following entry to the /etc/exports file:
/home LNBP75(rw)
If you use the IP address of a host, the entry might look like this:
/home 192.168.1.200(rw)
This specifies that 192.168.1.200 is the IP address of the host that’s allowed full access to the /home directory.
After adding the entry in the /etc/exports file, start the NFS server using a method appropriate for your Linux distribution. For example, in Fedora, log in as root and type the following command in a terminal window:
service nfs start
When the NFS service is up, the server side of NFS is ready. Now you can try to mount the exported file system from a client system and access the exported file system.
If you ever make any changes to the exported file systems listed in the /etc/exports file, remember to restart the NFS service. For example, in Fedora, type service nfs restart in a terminal window. In Xandros, type invoke-rc.d nfs-user-server restart.
Mounting an NFS file system
To access an exported NFS file system on a client system, you have to mount that file system on a mount point — which is, in practical terms, nothing more than a local directory. For example, suppose you want to access the /home/public directory exported from the server named LNBP200 at the local directory /mnt/lnbp200 on the client system. To do so, follow these steps:
  1. Log in as root and create the directory with the following command: mkdir /mnt/lnbp200
  2. Type the following command to perform the mount operation: mount lnbp200:/home/public /mnt/lnbp200If you know only the IP address of the server, replace the hostname (in this case, lnbp200) with the IP address.
  3. Change the directory to /mnt/lnbp200 with the command cd /mnt/lnbp200. Now you can view and access exported files from this directory.
To confirm that the NFS file system is indeed mounted, log in as root on the client system and type mount in a terminal window. You see a line similar to the following about the NFS file system:
lnbp200:/home/public on /mnt/lnbp200 type nfs (rw,addr=192.168.1.200)
Backing Up and Restoring Files
Backing up and restoring files is a crucial system administration task. If something happens to your system’s hard drive, you have to rely on the backups to recover important files. The following discussion presents some backup strategies, describes several backup media, and explains how to back up and restore files by using the tape archiver (tar) program that comes with Linux. Also, you find out how to perform incremental and automatic backups on tapes.
If you have a CD burner, you can back up files also by recording them on a CD-R. Consult Book II, Chapter 4, for information on what application you can use to burn a data CD.
Selecting a backup strategy and media
Your Linux system’s hard drive contains everything you need to keep the system running — as well as other files (such as documents and databases) that keep your business running. You have to back up these files so you can recover quickly and bring the system back to normal in case the hard drive crashes. Typically, you have to follow a strict regimen of regular backups because you can never tell when the hard drive may fail or the file system may get corrupted. To implement such a regimen, first decide which files you want to back up, how often, and what backup storage media to use. This process is what is meant by selecting a backup strategy and backup media.
Your choice of backup strategy and backup media depends on your assessment of the risk of business disruption due to hard drive failure. Depending on how you use your Linux system, a disk failure may or may not have much effect on you.
For example, if you use your Linux system as a learning tool (to find out more about Linux or programming), all you may need are backup copies of some system files required to configure Linux. In this case, your backup strategy can be to save important system configuration files on one or more floppies every time you change any system configuration.
On the other hand, if you use your Linux system as an office server that provides shared file storage for many users, the risk of business disruption due to disk failure is much higher. In this case, you have to back up all the files every week and back up any new or changed files every day. You can perform these backups in an automated manner (with the job-scheduling features described in Chapter 1 of this minibook). Also, you probably need a backup storage medium that can store many gigabytes of data. In other words, for high-risk situations, your backup strategy is more elaborate and requires additional equipment (such as a tape drive).
Your choice of backup media depends on the amount of data you have to back up. For a small amount of data (such as system configuration files), you can use USB flash drives as the backup media. If your PC has a Zip drive, you can use Zip disks as backup media; these are good for backing up a singleuser directory. To back up entire servers, use an external hard drive (which could be attached to the computer or the network) or a tape drive (typically a 4mm or 8mm tape drive that connects to a SCSI controller). Such tape drives can store several gigabytes of data per tape, and you can use them to back up an entire file system on a single tape.
When backing up files to these media, you have to refer to the backup device by name. Table 3-4 lists device names for some common backup devices.
Table 3-4
Device Names for Common Backup Devices
Backup Device
Linux Device Name
Floppy disk (where
they still exist)
/dev/fd0
IDE Zip drive
/dev/hdc4 or /dev/hdd4
SCSI Zip drive
/dev/sda (assuming its the first SCSI drive otherwise, the device name depends on the SCSI ID)
SCSI tape drive
/dev/st0 or /dev/nst0 (the n prefix means that the tape isnt rewound after files are copied to the tape)

Commercial backup utilities for Linux
The next section explains how to back up and restore files using the tape archiver (tar) program that comes with Linux. Although you can manage backups with tar, a number of commercial backup utilities come with graphical user interfaces and other features to simplify backups. Here are some well-known commercial backup utilities for Linux:
  • BRU: A backup and restore utility from the TOLIS Group, Inc. (www.tolisgroup.com)
  • LONE-TAR: Tape backup software package from Lone Star Software Corp. (www.cactus.com)
  • Arkeia: Backup and recovery software for heterogeneous networks from Arkeia (www.arkeia.com)
  • CA ARCserve Backup for Linux: Data-protection technology for Linux systems from Computer Associates (http://www.arcserve.com/us/products/product.aspx?id=5282)
Using the tape archiver — tar
You can use the tar command to archive files to a device, such as a floppy disk or tape. The tar program creates an archive file that can contain other directories and files and (optionally) compress the archive for efficient storage. The archive is then written to a specified device or another file. Many software packages are distributed in the form of a compressed tar file.
The command syntax of the tar program is as follows:
tar options destination source
Here, options are usually specified by a sequence of single letters, with each letter specifying what tar does. The destination is the device name of the backup device. And source is a list of file or directory names denoting the files to back up.
Backing up and restoring a single-volume archive
Suppose you want to back up the contents of the /etc/X11 directory on a floppy disk. Log in as root, place a disk in the floppy drive, and type the following command:
tar zcvf /dev/fd0 /etc/X11
The tar program displays a list of filenames as each file is copied to the compressed tar archive on the floppy disk. In this case, the options are zcvf, the destination is /dev/fd0 (the floppy disk), and the source is the /etc/X11 directory (which implies all its subdirectories and their contents). You can use a similar tar command to back up files to a tape — simply replace /dev/fd0 with the tape device — such as /dev/st0 for a SCSI tape drive.
Table 3-5 defines a few common tar options.
Table 3-5
Common tar Options
Option
Does the Following
c
Creates a new archive.
f
Specifies the name of the archive file or device on the next field in the command line.
M
Specifies a multivolume archive. (The next section describes multi-volume archives.)
t
Lists the contents of the archive.
v
Displays verbose messages.
x
Extracts files from the archive.
z
Compresses the tar archive using gzip.


To view the contents of the tar archive you create on the floppy disk, type the following command:
tar ztf /dev/fd0
You see a list of filenames (each begins with /etc/X11) indicating what’s in the backup. In this tar command, the t option lists the contents of the tar archive.
To extract the files from a tar backup, follow these steps while logged in as root:
  1. Change the directory to /tmp by typing this command:
    cd /tmp
    This step is where you can practice extracting the files from the tar backup. For a real backup, change the directory to an appropriate location (typically, you type cd /).
  2. Type the following command:
    tar zxvf /dev/fd0
    This tar command uses the x option to extract the files from the archive stored on /dev/fd0 (the floppy disk).
Now if you check the contents of the /tmp directory, you notice that the tar command creates an etc/X11 directory tree in /tmp and restores all the files from the tar archive into that directory. The tar command strips the leading / from the filenames in the archive and restores the files in the current directory. If you want to restore the /etc/X11 directory from the archive on the floppy, use this command:
tar zxvf /dev/fd0 -C /
The -C does a cd to the directory specified (in this case, the root directory of /) before doing the tar; the / at the end of the command denotes the directory where you want to restore the backup files.
You can use the tar command to create, view, and restore an archive. You can store the archive in a file or in any device you specify with a device name.
Backing up and restoring a multivolume archive
Sometimes the capacity of a single storage medium is less than the total storage space needed to store the archive. In this case, you can use the M option for a multivolume archive — meaning the archive can span multiple tapes or floppies (if you happen to be using an older machine that still has them). Note, however, that you can’t create a compressed, multivolume archive. That means you have to drop the z option. To see how multivolume archives work, log in as root, place one disk in the floppy drive, and type the following tar command:
tar cvfM /dev/fd0 /usr/share/doc/ghostscript*
Note: The M tells tar to create a multivolume archive. The tar command prompts you for a second floppy when the first one is filled. Take out the first floppy and insert another floppy when you see the following prompt:
Prepare volume #2 for ‘/dev/fd0’ and hit return:
When you press Enter, the tar program continues with the second floppy. In this example, you need only two floppies to store the archive; for larger archives, the tar program continues to prompt for floppies as needed.
To restore from this multivolume archive, type cd /tmp to change the directory to /tmp. (I use the /tmp directory for illustrative purposes, but you have to use a real directory when you restore files from archive.) Then type
tar xvfM /dev/fd0
The tar program prompts you to feed the floppies as necessary.
Use the du -s command to determine the amount of storage you need for archiving a directory. For example, type du -s /etc to see the total size of the /etc directory in kilobytes. Here’s a typical output of that command:
35724 /etc
The resulting output shows that the /etc directory requires at least 35,724K of storage space to back up.
Backing up on tapes
Although backing up on tapes is as simple as using the right device name in the tar command, you do have to know some nuances of the tape device to use it well. When you use tar to back up to the device named /dev/st0 (the first SCSI tape drive), the tape device automatically rewinds the tape after the tar program finishes copying the archive to the tape. The /dev/st0 device is called a rewinding tape device because it rewinds tapes by default.
If your tape can hold several gigabytes of data, you may want to write several tar archives — one after another — to the same tape (otherwise much of the tape may be left empty). If you plan to do so, your tape device can’t rewind the tape after the tar program finishes. To help you with scenarios like this one, several Linux tape devices are nonrewinding. The nonrewinding SCSI tape device is called /dev/nst0. Use this device name if you want to write one archive after another on a tape.
After each archive, the nonrewinding tape device writes an end-of-file (EOF) marker to separate one archive from the next. Use the mt command to control the tape — you can move from one marker to the next or rewind the tape. For example, after you finish writing several archives to a tape using the /dev/nst0 device name, you can force the tape to rewind with the following command:
mt -f /dev/nst0 rewind
After rewinding the tape, you can use the following command to extract files from the first archive to the current disk directory:
tar xvf /dev/nst0
After that, you must move past the EOF marker to the next archive. To do so, use the following mt command:
mt -f /dev/nst0 fsf 1
This positions the tape at the beginning of the next archive. Now use the tar xvf command again to read this archive.
If you save multiple archives on a tape, you have to keep track of the archives yourself. The order of the archives can be hard to remember, so you may be better off simply saving one archive per tape.
Performing incremental backups
Suppose you use tar to back up your system’s hard drive on a tape. Because such a full backup can take quite some time, you don’t want to repeat this task every night. (Besides, only a small number of files may have changed during the day.) To locate the files that need backing up, you can use the find command to list all files that have changed in the past 24 hours:
find / -mtime -1 -type f -print
This command prints a list of files that have changed within the last day. The -mtime -1 option means you want the files that were last modified less than one day ago. You can now combine this find command with the tar command to back up only those files that have changed within the last day:
tar cvf /dev/st0 `find / -mtime -1 -type f -print`
When you place a command between single back quotes, the shell executes that command and places the output at that point in the command line. The result is that the tar program saves only the changed files in the archive. This process gives you an incremental backup of only the files that have changed since the previous day.
Performing automated backups
previously we show how to use crontab to set up recurring jobs (called cron jobs). The Linux system performs these tasks at regular intervals. Backing up your system is a good use of the crontab facility. Suppose your backup strategy is as follows:
  • Every Sunday at 1:15 a.m., your system backs up the entire hard drive on the tape.
  • Monday through Saturday, your system performs an incremental backup at 3:10 a.m. by saving only those files that have changed during the past 24 hours.
To set up this automated backup schedule, log in as root and type the following lines in a file named backups (this example assumes that you are using a SCSI tape drive):
15 1 * * 0 tar zcvf /dev/st0 /
10 3 * * 1-6 tar zcvf /dev/st0 `find / -mtime –1 –type f –print`
Next, submit this job schedule by using the following crontab command:
crontab backups
Now you’re set for an automated backup. All you need to do is to place a new tape in the tape drive every day. Remember also to give each tape an appropriate label.
Accessing a DOS or Windows File System
If you’re using a legacy machine that you just don’t want to throw out and have Microsoft Windows 95, 98, or Me installed on your hard drive, you’ve probably already mounted the DOS or Windows partition under Linux. If not, you can easily mount DOS or Windows partitions in Linux. Mounting makes the DOS or Windows directory hierarchy appear as part of the Linux file system.
Mounting a DOS or Windows disk partition
To mount a DOS or Windows hard drive partition or floppy in Linux, use the mount command but include the option -t vfat to indicate the file-system type as DOS. For example, if your DOS partition happens to be the first partition on your IDE (Integrated Drive Electronics) drive and you want to mount it on /dosc, use the following mount command:
mount -t vfat /dev/hda1 /dosc
The -t vfat part of the mount command specifies that the device you mount — /dev/hda1 — has an MS-DOS file system. Figure 3-4 illustrates the effect of this mount command.
Here’s how you mount a DOS partition on the /doscdirectoryFigure 3-4: Here’s how you mount a DOS partition on the /doscdirectory.
Figure 3-4 shows how directories in your DOS partition map to the Linux file system. What was the C:\DOS directory under DOS becomes /dosc/dos under Linux. Similarly, C:\WINDOWS now is /dosc/windows. You probably can see the pattern. To convert a DOS filename to Linux (when you mount the DOS partition on /dosc), perform the following steps:
  1. Change the DOS names to lowercase.
  2. Change C:\ to /dosc/.
  3. Change all backslashes (\) to slashes (/).
Mounting those old DOS floppy disks
Just as you mount a DOS hard drive partition on the Linux file system, you can also mount a DOS floppy disk on a legacy machine. You must log in as root to mount a floppy, but you can set up your system so that any user can mount a DOS floppy disk. You also have to know the device name for the floppy drive. By default, Linux defines the following two generic floppy device names:
  • /dev/fd0 is the A drive (the first floppy drive)
  • /dev/fd1 is the B drive (the second floppy drive, if you have one)
You can use any empty directory in the file system as the mount point, but the Linux system comes with a directory, /media/floppy, specifically for mounting a floppy disk.
To mount a DOS floppy disk on the /media/floppy directory, put the floppy in the drive and type the following command:
mount -t vfat /dev/fd0 /media/floppy
After you mount the floppy, you can copy files to and from the floppy by using the Linux copy command (cp). To copy the file gnome1.pcx from the current directory to the floppy, type the following:
cp gnome1.pcx /media/floppy
Similarly, to see the contents of the floppy disk, type the following:
ls /media/floppy
If you want to remove the floppy disk from the drive, first unmount the floppy drive. Unmounting removes the association between the floppy disk’s file system and the mount point on the Linux file system. Use the umount command to unmount the floppy disk like this:
umount /dev/fd0
You can set up your Linux system so that any user can mount a DOS floppy. To enable any user to mount a DOS floppy in the A drive on the /a directory, for example, perform the following steps:
  1. Log in as root.
  2. Create the /a directory (the mount point) by typing the following command in a terminal window: mkdir /a
  3. Edit the /etc/fstab file in a text editor (such as vi or emacs) by inserting the following line, and then save the file and quit the editor:
    /dev/fd0 /a vfat noauto,user 0 0
    The first field in that line is the device name of the floppy drive (/dev/fd0); the second field is the mount directory (/a); the third field shows the type of file system (vfat). The user option (which appears next to noauto) enables all users to mount DOS floppy disks.
  4. Log out and then log back in as a normal user.
  5. To confirm that you can mount a DOS floppy as a normal user and not just as root, insert a DOS floppy in the A drive and type the following command:
    mount /a The mount operation succeeds, and you see a listing of the DOS floppy when you type the command ls /a.
  6. To unmount the DOS floppy, type umount /a.
Mounting an NTFS partition
Nowadays, most PCs come with Windows Vista or Windows 7 pre-installed on the hard drive. Both Windows Vista and Windows 7, as well as Windows XP, typically use the NT File System (NTFS). Linux supports read-only access to NTFS partitions, and many distributions come with the ntfs.ko kernel module, which is needed to access an NTFS partition.
If you’ve installed Linux on a Windows XP system and want to access files on the NTFS partition but your distribution doesn’t include the ntfs.ko module, you can build the kernel after enabling an NTFS module during the kernel configuration step.
After rebuilding and booting from the new kernel, log in as root and type the following command to create a mount point for the NTFS partition. (In this case, I’m creating a mount point in the /mnt directory.)
mkdir /mnt/xp
Now, you can mount the NTFS partition with the following command:
mount /dev/hda2 /mnt/xp -t ntfs -r -o umask=0222
If necessary, replace /dev/hda2 with the device name for the NTFS partition on your system. On most PCs that come with Windows XP pre-installed, the NTFS partition is the second one (/dev/hda2) — the first partition (/dev/hda1) is usually a hidden partition used to hold files used for Windows XP installation.
Using mtools
One way to access the MS-DOS file system is to first mount the DOS hard drive or floppy disk by using the mount command and then use regular Linux commands, such as ls and cp, to work with the mounted DOS file system. This approach of mounting a DOS file system is fine for hard drives.
Linux can mount the DOS partition automatically at startup, and you can access the DOS directories on the hard drive at any timeIf you want a quick directory listing of a DOS floppy disk, however, mounting can soon become tedious. First, you have to mount the floppy drive. Then you must use the ls command. Finally, you must use the umount command before ejecting the floppy out of the drive.
This situation is where the mtools package comes to the rescue. The mtools package implements most common DOS commands; the commands use the same names as in DOS except you add an m prefix to each command. Thus, the mdir command lists the directory listing and mcopy copies files. The best part of mtools is the fact that you don’t have to mount the floppy disk to use the mtools commands.
Because the mtools commands write to and read from the physical device (floppy disk), you must log in as root to perform these commands. If you want any user to access the mtools commands, you must alter the permission settings for the floppy drive devices. Use the following command to permit anyone to read from and write to the first floppy drive:
chmod o+rw /dev/fd0
Trying mtools
To try out mtools, follow these steps:
  1. Place an MS-DOS floppy disk in your system’s A drive.
  2. Type mdir. You see the directory of the floppy disk (in the standard DOS directory listing format).
Typically, you use the mtools utilities to access floppy disks. The default configuration file, /etc/mtools.conf, is set up to access the floppy drive as the A drive. Although you can edit that file to define C and D drives for your DOS hard drive partitions, you can access the hard drive partitions also by using the Linux mount command to mount them. Because you can mount the hard drive partitions automatically at startup, accessing them through the Linux commands is normally just as easy.
Understanding the /etc/mtools.conf file
The mtools package works with the default setup, but if you get any errors, check the /etc/mtools.conf file. That file contains the definitions of the drives (such as A, B, and C) that the mtools utilities see. Following are a few lines from a typical /etc/mtools.conf file:
drive a: file=”/dev/fd0” exclusive mformat_only
drive b: file=”/dev/fd1” exclusive mformat_only
# First SCSI hard disk partition
#drive c: file=”/dev/sda1”
# First IDE hard disk partition on a Windows 98 PC
drive c: file=”/dev/hda1”
# Internal IDE Zip drive
drive e: file=”/dev/hdd4” exclusive

The pound sign (#) indicates the start of a comment. Each line defines a drive letter, the associated Linux device name, and some keywords that indicate how to access the device. In this example, the first two lines define drives A and B. The third noncomment line defines drive C as the first partition on the first IDE drive (/dev/hda1). If you have other DOS drives (D, for example), you can add another line that defines drive D as the appropriate disk partition.
If your system’s A drive is a high-density, 3.5-inch drive, you don’t need to change anything in the default /etc/mtools.conf file to access the floppy drive. If you also want to access any DOS partition in the hard drive, uncomment and edit an appropriate line for the C drive.
You also can access Iomega Zip drives through mtools. Simply specify a drive letter and the appropriate device’s filename. For built-in IDE (ATAPI) Zip drives, try /dev/hdd4 as the device file and add the following line in the /etc/mtools.conf file:
drive e: file=”/dev/hdd4”
After you have made this change, you can use mtools commands to access the Zip drive (refer to it as the E drive). For example, to see the directory listing, place the Zip disk in the Zip drive and type:
mdir e:
Understanding the mtools commands
The mtools package is a collection of utilities. The discussion so far has included mdir — the mtools counterpart of the DIR command in DOS. The other mtools commands are fairly easy to use.
If you know MS-DOS commands, using the mtools commands is simple. Type the DOS command in lowercase letters and remember to add m in front of each command. Because the Linux commands and filenames are case-sensitive, you must use all lowercase letters when you type mtools commands.
Table 3-6 summarizes the commands available in mtools.
Table 3-6
The mtools Commands
mtools Utility
MS-DOS
Command
The mtools Utility Does the Following
(If Any)
mattrib
ATTRIB
Changes MS-DOS file-attribute flags
mbadblocks

Tests a floppy disk and marks the bad blocks in the file allocation table (FAT)
mcd
CD
Changes an MS-DOS directory
mcopy
COPY
Copies files between MS-DOS and Linux
mdel
DEL or ERASE
Deletes an MS-DOS file
mdeltree
DELTREE
Recursively deletes an MS-DOS directory
mdir
DIR
Displays an MS-DOS directory listing
mdu

Lists space that a directory and its contents occupy
mformat
FORMAT
Places an MS-DOS file system on a low-level-formatted floppy disk (use fdformat to low-level-format a floppy disk in Linux).
minfo

Gets information about an MS-DOS file system
mkmanifest

Makes a list of short name equivalents
mlabel
LABEL
Initializes an MS-DOS volume label
mmd
MD or MKDIR
Creates an MS-DOS directory
mmove

Moves or renames an MS-DOS file or subdirectory
mmount

Mounts an MS-DOS disk
mpartition

Creates an MS-DOS file system as a partition
mrd
RD or RMDIR
Deletes an MS-DOS directory
mren
REN or RENAME
Renames an existing MS-DOS file
mshowfat

Shows FAT entries for an MS-DOS file
mtoolstest

Tests and displays the current mtools configuration
mtype
TYPE
Displays the contents of an MS-DOS file
mwrite
COPY
Copies a Linux file to MS-DOS
mzip

Performs certain operations on SCSI Zip disks


You can use the mtools commands just as you use the corresponding DOS commands. The mdir command, for example, works the same as the DIR command in DOS. The same goes for all the other mtools commands shown in Table 3-6.
You can use wildcard characters (such as *) with mtools commands, but you must remember that the Linux shell is the first program to see your command. If you don’t want the shell to expand the wildcard character all over the place, use quotation marks around filenames that contain any wildcard characters. For example, to copy all *.txt files from the A drive to your current directory, use the following command:
mcopy “a:*.txt”.
If you omit the quotation marks, the shell tries to expand the string a:*.txt with filenames from the current Linux directory. It also tries to copy those files (if any) from the DOS floppy disk.
On the other hand, if you want to copy files from the Linux directory to the DOS floppy disk, you do want the shell to expand any wildcard characters. To copy all *.jpg files from the current Linux directory to the DOS floppy disk, for example, use mcopy like this:
mcopy *.jpg a:
With the mtools utilities, you can use the backslash character (\) as the directory separator, just as you do in DOS. However, when you type a filename that contains the backslash character, you must enclose the name in double quotation marks (“ ”). For example, here’s a command that copies a file from a subdirectory on the A drive to the current Linux directory:
mcopy “a:\test\sample.dat”.
next Installing and Updating Applications in Linux
Share this article :

0 comments:

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Linux - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger