Windows-to-Linux roadmap: Part 6. Working with partitions and file

Partitions. File system types. Formatting partitions. Other file system tools .... Red Hat Linux, the cdrom device is set up to mount to the directory /mnt/cdrom. That.
114KB taille 1 téléchargements 156 vues
Windows-to-Linux roadmap: Part 6. Working with partitions and file systems

Search for:

within Use + - ( ) " "

IBM home IBM developerWorks

|

Products & services

All of dW Search help

|

Support & downloads

|

My account

> Linux

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems Contents:

Using disks and devices in Linux

No drive letters!

Level: Introductory

Mount up! The /etc/fstab file

Chris Walden (cmwalden-at-us.ibm.com) e-business Architect, IBM Developer Relations November 11, 2003

Adding file systems Partitions File system types Formatting partitions Other file system tools

IBM e-business architect Chris Walden is your guide through a nine-part developerWorks series on moving your operational skills from a Windows to a Linux environment. In this part, we explore Linux's hierarchical directory structure, and investigate mounting and devices. Working with files and storage devices in Linux is different from Windows. There are files and a hierarchical directory structure, but beyond that you will need to develop a different way of thinking.

The whole is the sum of its partitions Resources About the author Rate this article

Related content: Rest of the roadmap Advanced filesystem implementor's guide Put virtual filesystems to work Partition planning tips

Listing 1. Directory structure

Dual-booting Linux Partitioning in action

/ |-|-|-|-|-|-| | | | |-|-|-|-| | | | | | | | | | | | | | | |

Maximum swappage

bin boot dev etc mnt opt |-- IBM | |-- WebSphereStudio | `-- db2 |-- IBMHttpServer root sbin tmp usr |-- X11R6 | |-- bin | |-- include | |-- lib | |-- man | `-- share |-- bin |-- dict |-- doc |-- etc |-- include |-- lib |-- libexec |-- local | |-- OpenOffice | | |-- sbin

Getting to know GRUB Burning CDs on Linux Subscribe to the developerWorks newsletter developerWorks Toolbox subscription

Also in the Linux zone: Tutorials Tools and products Code and components Articles

No drive letters! There are no drive letters in Linux. This is actually quite useful. If you've worked on a Windows system in a complex networking environment on a robust machine with several devices, you may have found the alphabet lacking. In Linux, there is

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (1 of 8)11/19/2003 5:45:51 AM

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems

just one file structure. It starts with root (/) and all local file systems, all local devices, and all remote file systems are represented as subdirectories in this structure. When Linux first boots, it builds this file structure based on information in the /etc/fstab file. Where Windows assigns drive letters to hard drive partitions and other storage devices, Linux assigns them directories in the root file structure. The structure of the hierarchy is completely configurable and can be changed on the fly.

Mount up! The term for adding a device to the file system is mounting. Linux will automatically mount a / (root) file system. There may also be a separate /boot file system, containing the core kernel boot files. Linux will also mount some special file systems. The swap space is not shown as a part of the file system, but is handled by the kernel. However, other special file systems such as proc, are seen as a normal part of the file system, and its contents can be handled just like normal files. Other file systems, such as removable media or remote file systems, will need to be manually mounted. When mounting a file system, you will need to know the correct way to reference it from Linux, and have an empty directory to use as a mount point. For removable media, Linux will probably create mount points for you during installation. In Red Hat Linux, the cdrom device is set up to mount to the directory /mnt/cdrom. That means that when you put a CD into the CDROM device, you enter the command: mount /mnt/cdrom

The CD is added to the file system and the CDROM device is locked so that it cannot be accidentally ejected. To access the contents of the CD, simply use the directory /mnt/ cdrom. When you are finished using the CD, you can remove it from the file system with the command:

What is /proc? The /proc file system is an excellent example of the difference between thinking in Windows and thinking in Linux. /proc contains a virtual representation of various aspects of the running system. There is information about IRQ settings, memory usage, loaded device drivers, network status, and much, much more. There is even a file called /proc/kcore, which is a virtual representation of all of the used system memory. Each of these files can be parsed just like a normal text or binary file. Some files can be written to change the behavior of the running kernel, without rebooting. For example, to turn on IPforwarding for the first active ethernet device on the system, you can use a file command:

umount /mnt/cdrom echo 1 > /proc/sys/net/ipv4/conf/ eth0/forwarding

The /mnt/cdrom directory will empty and the CDROM device will be unlocked. You can now safely eject the CD. The same behavior would be used with other removable media, such as a floppy drive (/mnt/floppy).

The main benefit of such a system is that you can use simple scripting techniques to do very deep and powerful things to your running system.

Running mount with no arguments will show you the currently mounted file systems.

Why all of this locking behavior? Remember that Linux is not only multi-user, but also multi-session. That means that several users may be logged into the system, running processes and using resources all at the same time. This is not the same as logging in to use a file share in Windows. Each user can use the system just as though they were sitting at the console. Linux maintains stability by not arbitrarily releasing file systems that are currently in use, and by locking CDs so they cannot be ejected until the file system has been unmounted and no one is using it.

The /etc/fstab file The association between a device and its mount point is configured in the /etc/fstab file. It can be human-edited, or maintained with an administrative tool. Here is a sample of /etc/fstab/:

Understanding /etc/fstab /dev/hda5 /dev/hda2

/boot

ext3

defaults

11

ext3

exec,dev,duid,rw

12

/dev/hda6

swap

swap

defaults

00

/dev/scd0

/mnt/cdrom

auto

ro,noauto,exec

00

none

/dev/pts

devpts

id=5,mode=620

00

none

/proc

proc

defaults

00

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (2 of 8)11/19/2003 5:45:51 AM

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems

none

/dev/shm

tmpfs

defaults

00

Each line represents a file system to be mounted. The first column identifies the device to be mounted. The second column contains the mount point, the location for that device in the file system. The third column identifies the file system type. The fourth column has options for how that file system should be handled. The last column contains flags about that file system. The first number is either 1 or 0 and indicates whether the system should be copied with a dump (an option for system backups). The second number is 0, 1, or 2 and indicates the order in which the file system should be checked upon boot. 0 is not checked at all. 1 is checked first and should be used for the root (/) file system. Other file systems should be 2. In the fstab file listed above, the root file system is on the first IDE hard drive in the fifth partition, the first logical drive in an extended partition. The /boot file system, where the kernel startup files are located, is on the first IDE hard drive in the second primary partition. Swap space is located in the first IDE hard drive in the sixth partition, the second logical drive in an extended partition. Other file systems listed show their device as "none." We'll cover these shortly. For now let's concentrate on physical disks. The options in the fourth column will vary depending on the file system type. In the example above, / and /boot are mounted with the "default" options. That, is they are mounted automatically as read-write with asynchronous I/O. Only root can mount or unmount the device but users can execute binaries and use the "sticky bit" (covered later). The file system will be handled as a block character device. For the /mnt/cdrom, however, the options are different. It is not automatically mounted and will mount as a read-only file system. Users will be able to execute scripts and programs in that file system.

Adding file systems You can add file systems into /etc/fstab by adding new lines to the file. As a practical example, I have a RAID device that contains file resources for use by the department. This device will only contain data files and will be kept separate from the operating system, so it will be easy to move the device to another system in case of a hardware failure. The RAID has already been configured, and is recognized by Linux as /dev/sdc, the third SCSI device. A journaled ext3 file system has been created on the first partition, so we can access it as /dev/sdc1. I want to have this RAID automatically mount into the file system when the computer boots. I add the following line to the /etc/fstab:

Everything's a file In Linux, file systems are represented by a file-like name. All of the files in the /dev directory are special files called nodes that link to physical devices through the device driver. This allows you to do some interesting things. For example, to make an ISO image of a CD, you can use the cp (copy) command:

cp /dev/cdrecorder MyCD.iso

Rather than copying the file structure of the CD, a binary image is copied.

This file-centric approach also allows you to alias device names to something more meaningful. For example, there is usually an alias called /dev/cdrom that points to the physical CDROM device, often /dev/hdc. Once that alias is created, you can always refer to the device as /dev/cdrom, which is much easier to remember. This aliasing technique also allows you to standardize scripts across systems that might have different physical configurations.

/dev/sdc1 /data ext3 defaults 0 0

This will cause the RAID to be mounted at boot just like the / and /boot systems. Now I simply create the directory I specified as my mount point: mkdir /data

Once this empty directory is created, we can mount the file system into it: mount /data

The RAID is now associated to /data. If the system ever reboots, then /data will automatically be mounted.

Partitions Partitions in Linux work essentially the same as they do in Windows. The fdisk console command is used to create and manipulate partitions. When you execute fdisk, you must point it toward a device. To see the available devices, use the command fdisk -l. Listing 2. Using fdisk

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (3 of 8)11/19/2003 5:45:51 AM

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems

[root@cmw-t30 root]# fdisk -l Disk /dev/hda: 240 heads, 63 sectors, 7752 cylinders Units = cylinders of 15120 * 512 bytes Device Boot /dev/hda1 /dev/hda2 /dev/hda3 * /dev/hda4 /dev/hda5 /dev/hda6 /dev/hda7

Start 1 9 16 1404 1404 5566 5636

End 8 15 1403 7751 5565 5635 7751

Blocks 60448+ 52920 10493280 47990880 31464688+ 529168+ 15996928+

Id 8e 83 c f 83 82 b

System Linux LVM Linux Win95 FAT32 (LBA) Win95 Ext'd (LBA) Linux Linux swap Win95 FAT32

The above list was generated from a laptop, so it shows a rather unorthodox structure for a server. It shows one IDE hard drive with several partitions. If there were other devices, they would be listed as well. For example, a second IDE hard drive might be shown as /dev/hdb. Run fdisk again with a device, and you get a short prompt. Listing 3. fdisk on a device [root@cmw-t30 root]# fdisk /dev/hda The number of cylinders for this disk is set to 7752. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help):

Entering "m" will give you a menu of commands. You can display the current partition table with "p." You can create, delete, and modify the type for existing partitions. "l" will show you the full list of partition types available. Write your changes to the partition table with "w", and exit the program, or quit without saving with "q." Some changes will take place immediately. Some types may require a system reboot. Partition rules under Linux are the same as they are in Windows. You are allowed four primary partitions, any of which can be extended partitions.

File system types Linux can handle any file system type that the kernel knows about. A generous number of them come compiled by default, and new ones can be added. Here are some interesting ones:

● ● ● ● ●

ext2: The standard Linux file system ext3: The standard Linux file system with journaling added vfat: Microsoft's Fat32 file system jfs: IBM's journaled file system reiserfs: Another popular journaled file system

Journaling saves time and data Journaled file systems help protect data from unexpected shutdowns. If a volume is shut down without dismounting, there may be unfinished work and files left in an in-between state. With a typical file system, this requires a full check of the volume, which can take a long time for large volumes. A journaled file system keeps a transaction record of each write to the disk for a period of time, such as five seconds. When the volume is not cleanly unmounted, the file system simply rolls back to the last known good state. A volume that would take twenty minutes to come back up now comes up in seconds!

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (4 of 8)11/19/2003 5:45:51 AM

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems

Formatting partitions Once created, partitions are formatted with the correct version of the mkfs command. File systems will have their own version of the mkfs, such as the mkfs.ext2, or the mkfs.ext3. These helper scripts let you create a file system by simply pointing to the partition. Here are some examples: Listing 4. Using mkfs # Create an ext2 file system on the third # parition of the first IDE hard drive mkfs.ext2 /dev/hda3 # Create an ext3 file system on the first # partition of the 2nd SCSI hard drivemkfs.ext2 mkfs.ext3 /dev/sdb1 # Create a jfs file system in an extended # partition on the first IDE hard drive. mkfs.jfs /dev/hda5

There are various advanced parameters to affect how the partition is formatted, but for general purposes, the defaults are fine. Once the partition has been formatted, it can be mounted into the / file system. A file system must be unmounted to be reformatted.

Other file system tools Let's take a look at other useful tools.

Console tools There are several tools for looking at the condition of disks and file systems. df df stands for "disk free." It reports the amount of disk space used and available on mounted file systems. Useful switches:

Checking disk space df -h

Human readable; uses friendly k, M, and G indicators to show file size rather than listing them in bytes

df -l

Limits the listing to local file systems; by default, remote file systems are also listed

du du stands for "disk usage." It reports the amount of disk space used by the specified files and for each subdirectory (of directory arguments). Useful switches:

Checking disk usage du -a

Shows counts for all files, not just directories

du -h

Human readable; uses friendly k, M, and G indicators to show file size rather than listing them in bytes

du -c

Prints a grand total of all arguments after all arguments have been processed; can be used to find out the total disk usage of a given set of files or directories

du -s

Displays only a total for each argument

fsck This is the program used to check and repair file systems, equivalent to chkdsk in Windows. It will have different versions for different file system types, just like mkfs. fsck must be run on unmounted volumes, though it is rarely needed unless the file system was not cleanly unmounted. man fsck and info fsck provide details, as do several of the Resources included at the end of this article.

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (5 of 8)11/19/2003 5:45:51 AM

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems

Webmin Webmin has several tools to work with file systems and partitions. Figure 1. Webmin partition tool

Hardware, partitions on local disks Each disk and partition is shown with current usage information. Click on a file system to see details. Unmounted partitions can have their type edited and file systems formatted.

System, disk, and network filesystems Mount and unmount file systems listed in /etc/fstab. Common file system types have a wizard for creating entries. Unrecognized types can be mounted and unmounted from here, but must be hand-edited in /etc/fstab. Most server file systems can be handled well from here.

The whole is the sum of its partitions Though there are many similarities in how Windows and Linux handle partitions and file systems, moving from drive letters to a completely hierarchical tree will probably take some adjustment. As always, there are robust console tools to work with these functions and configuration files in the /etc directory. Browser-based front ends like Webmin offer some helpful tools.

Resources ●







Check out the other parts in the Windows-to-Linux roadmap series (developerWorks, November 2003). The Linux Partition HOWTO looks more closely at the mechanics of partitioning and gives more detail on the available tools. While Linux Administration Made Easy is an older reference, it is still useful, as the general procedures and techniques for Linux have remained consistent. The Multi Disk System Tuning HOWTO describes how best to use multiple disks and partitions for a Linux system.

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (6 of 8)11/19/2003 5:45:51 AM

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems





























"Installing and configuring SuSE Linux Enterprise Server (SLES) 8" shows how to install and configure SuSE Linux Enterprise Server (SLES) 8, including steps for graphical configuration using YaST. The Linux System Administrator's Guide is an introduction to system administration of a Linux system for novices. FIlesystems, quotas, and more are also covered in the developerWorks tutorial "LPI certification 101 exam prep, Part 4: Advanced administration". The IBM developerWorks series Advanced filesystem implementor's guide deals with advanced topics, but also introduces you to the different filesystem options that are available under Linux. Learn how to Put virtual filesystems to work in your code with this IBM developerWorks article. Formatting a new system? First read these: "Partition planning tips" and "Partitioning in action", both from IBM developerWorks. "Dual-booting Linux" from IBM developerWorks explains how you can easily have both Windows and Linux on a single machine. The IBM developerWorks article "Maximum swappage" can help you to improve the swap performance on your Linux server. The Linux Loader, or LILO, has been superseded! Read all about it in the developerWorks tutorial "Getting to know GRUB". "Burning CDs on Linux" is easy when you learn how in this IBM developerWorks guide. File permissions and security are addressed in Chapter 3 of the Introduction to Linux guide at the Linux Documentation Project. Another great resource for those transitioning from Windows to Linux is the Technical FAQ for Linux users. For getting started with IBM software on Linux, there's no better resource than the Speed-start your Linux app page. You'll find installation tips and links to resources for DB2, Lotus Domino, WebSphere Application Server, WebSphere Studio, and more. You can also sign up to receive a free Linux Software Evaluation Kit, containing trial software and training resources. Find more resources for Linux developers in the developerWorks Linux zone.

About the author Chris Walden is an e-business Architect for IBM Developer Relations Technical Consulting (also known as the dragonslayers) in Austin, Texas, providing education, enablement, and consulting to IBM Business Partners. He is the official Linux fanatic on his hallway and does his best to spread the good news to all who will hear it. In addition to his architect duties, he manages the area's all-Linux infrastructure servers, which include file, print, and other application services in a mixed-platform user environment. Chris has ten years of experience in the computer industry ranging from field support to Web application development and consulting. You can reach Chris at cmwalden-at-us.ibm.com.

What do you think of this document? Killer! (5)

Good stuff (4)

So-so; not bad (3)

Comments?

Submit feedback

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (7 of 8)11/19/2003 5:45:51 AM

Needs work (2)

Lame! (1)

Windows-to-Linux roadmap: Part 6. Working with partitions and file systems

IBM developerWorks

> Linux

About IBM

|

Privacy

|

Terms of use

|

Contact

http://www-106.ibm.com/developerworks/library/l-roadmap6/ (8 of 8)11/19/2003 5:45:51 AM