Linux hard disk partitioning
Created September 14, 1999
Last modified September 15, 1999
Written by Allen Francom (aef@prismnet.com)
Purpose
This document is intended to explain partitions, why they are used, and how they are used.
The goal is to provide enough information so that the reader wanting to discover how and when to mount what, where; will be able to make up their own mind.
The premise is, nobody can properly scratch you because you're the only one that knows where it itches.
- Same thing with your own partitions...
Hopefully,
The beginner will learn a lot, really well, really fast
The novice will gain a proper context and reassurance prior to proceeding with a partitioning endeavor
Guru's who are not yet familiar with Linux should be able to get everything they need out of this
Real Guru's will occasionally review this and let me know what I don't already know, or thought I knew, and then I'll include it here in future releases.
Required
You must know what a Directory Tree is or this document will not work for you until you ask someome, or become enlightened otherwise.
You should have at least rudimentary skills at running simple things from the Linux command-line.
Which implies you should have some level of experience with computers, say the DOS command-line.
( don't say "What's DOS ?" - Don't )
You should know how to read "man" pages.
You should know how to edit text files
Check 'em for bees... Know that joke ?
Hints:
- cd /home - example of changing directories
- man cd - example of reading the man page on the cd command
- vi mytextfile - example of editing a text file
- ":q!" - example of how to get out of the stupid vi editor
- man vi - example of learning how to use the vi editor
- "q" - example of how to get out of the "man" program and back to the command prompt
It would maybe be a good idea to have a RUNNING Linux machine you can look at while you read this.
It would be great if you could be using an HTML browser running on a Linux machine while you read this. A pretty one, like Netscape.
Introduction
Like a partition in an office, a hard disk partition is a chunk of space allocated for a particular purpose... A particular person or entity is going to use that space, and in that space are going to be things.
Usually, these things on a hard disk are files and the entity that uses them is the operating system's File System. Users then run programs that talk to the operating system, which runs the file system, which reads and writes data inside of particular hard drive partitions.
Another way to say this, perhaps, is that a hard disk partition is a logical separation of storage capacity on a given physical device. - cute
Hard disk drives may have many partitions, or just one big one in order to be of any use. You are not likely to see situations where that is not true.
Each hard disk drive, therefore, is expected to have a Partition Table that identifies the offsets, sizes, and types of partitions present at the moment.
You will see later on that the partition table is of primary importance to operating systems, as it allows them to know about where everything is expected to be, enough for them to get started at least. This is because an operating system can always expect to find the partition table in exactly the same place on any hard disk drive.
The partition table is one of the only things about a hard disk drive that an OS can properly read from the drive, if it is present, as an operating system begins to boot up. The partition table must be read FIRST, before the Operating System will be able to know enough to do anything else with the given drive.
Partitions containing File Systems are Formatted according to different standards, depending on the Operating System that created them. Formatting is a separate step.
Some Operating Systems, like Linux, know how to read and write to partitions in a wide variety of native formats. You can imagine that the Partition Type stored in the Partition Table comes in very handy. Almost always, commands that format partitions first check the partition's type, and abort if it doesn't match what is expected.
Also, when you define a partition its definition is stored on the given hard disk drive's partition table, and therefore the partition table is also important to you as you will need to be able to read from it and write to it in order to make changes and partition your drives the way you want.
Partitioning is the art of designing and implementing partitions on your hard drives, primarily for the purpose of constructing your directory tree in a manner that will be most efficient for your use of it, and for the Operating System's overall performance.
Running Linux requires at least two hard disk partitions:
1.The Swap Partition
The swap partition is basically a place for the computer to pretend like it has more RAM than it actually does.
A good rule of thumb is to allocate a "Swap Partition" that is at least twice as big as your physical RAM. If you have 64MB RAM, then make at least a 128MB Swap Partition.
Commands useful for setting-up and fixing swap space:
fdisk
mkswap
swapon
Note: The setup and initial configuration of swap space is usually handled by Linux install programs. These commands are for you to use only if you need to work on swap space manually for some reason. Quite probably, you'll never need to use "mkswap" and "swapon".
2.The Root Partition
"/" is the root of the directory tree on Unix machines.
Do not confuse this "root" with the Super User account of the same name. One is the root of your directory tree, and the other root is the Linux User Account that get's to do darn well whatever it pleases.
To make matters worse, there is, on my machine, a "/root" directory as well. This is the HOME directory of the root user account, and not the root of the directory tree. Got it ?
There is NO DRIVE IDENTIFIER on Unix systems.
Partitions on almost any number of drives drives can be mounted starting at some point in the directory tree under /.
Commands useful for mapping your directory tree, and/or portions thereof, to hard disk partitions:
mount
umount
Note: You will most likely use these commands day-to-day in order to mount floppy disks, CD-ROMs, etc., These same two commands mount hard disk partitions to specific points within your directory tree.
*You do not normally see the swap partition in the directory tree, as it is ordinarily directly accessible only by the kernel of the operating system. The swap partition is not mounted and unmounted with these commands.
Psychology-fu
_ _ _ _ is the name given to the top of the directory tree, the user account that has the most permissions, and, in my case, that user account's HOME directory.
Hard disk drives have to have one or more _ _ _ _ _ _ _ _ _s in order to be usable by an Operating System like Linux or Windows.
Linux requires at least two partitions, one for _ _ _ _ and at least one for mapping into the directory tree.
Every hard disk drive is expected by most operating systems to have a Partition _ _ _ _ _, which identifies all of the drive's partition t_ _ _ s, offsets, and s_ _ _s.
Synonymous with actions taken with Floppy Diskettes and CD-ROM drives, you can allocate hard disk drive partitions to specific locations within your directory tree by using the commands _ _ _ _ _ and un_ _ _ _ _.
Thus, if you think about it, you or the Linux kernel must _ _ _ _ _ at least one _ _ _ _ _ _ _ _ _ in order for the directory tree to even exist. And that is very important, because all of the programs, library files, data files, EVERYTHING needed to run Linux has to be mounted in the directory tree before anything will work.
Thinking a little bit more, Linux does not have drive identifiers like "C:\" which you find in DOS and Windows because all of your hard disks' _ _ _ _ _ _ _ _ _ _ are _ _ _ _ _ed into one big overall directory tree starting at a particular directory or Mount Point.
Installing Linux requires a hard disk partition to be mounted at the "/" location in your directory tree. I can't think of a way around it. Therefore when you are setting up Linux initially, you will obviously have to somewhere specify and/or create the r_ _ _ partition; that is, the partition assigned to the root of the directory tree.
Linux install programs remember what you did in the install/configuration phase and they create configuration files in the /etc directory which tell Linux what partitions to mount and where they go in the directory tree - for Linux to refer to when it is booting and getting ready to let you use it initially.
Another interesting thing about _ _ _ _ _ _ _ _ _s and the directory tree is that you can _ _ _ _ _ and un_ _ _ _ _ them at any time they are not actively being used.
That statement is loaded ! So let me repeat it in more and different words... You can change what partition, and even what physical hard disk drive is corresponding to what part of your directory tree right now ! - if you happen to be running Linux at the moment. - Try that on Windblows...
Linux and Unixes/en, Unixen, in general are rather more interesting than DOS or Windows in this regard, and it gets much much better, in other documents ;)
The kinds of things you can do with all this flexibility is amazing. This is why partitioning your hard drives for Linux is YOUR problem. There's just so much creativity in what you might want to do and why...
Let's do one more really quick... Linux' _ _ _ _ partition is never to be found in the directory tree. To see it you must use fdisk or some other partition software, and although you are unlikely to ever do this, to enable and disable the _ _ _ _ partition, you'll use the _ _ _ _ _ _ and, as you might be able to guess, the _ _ _ _ _ _ _ commands.
Determining what partitions you want
Simplest Case
If you like to keep things simple, you'll have two partitions, one for Swap Space, and one big huge one for everything else. If you do this, the "Mount Point" for your big huge partition is "/" the root of the directory tree. Period. Every file and directory lives on that big partition. The whole entire directory tree refers only to stuff in that one partition. Enough said.
Regarding the directory tree
What's in it ? And where can you mount partitions ?
On my RedHat 6.0 system, I have the following out-of-the-box top-level directory structure, each of these are directories with more subdirectories:
/
The root or top of your directory tree
Click here to see your own directory root
/bin
Programs needed to run your system, commands like cd, ls, etc., programs that most users would be likely to use day to day.
Click here to see your own /bin directory
/sbin
Programs that are more serious, generally, than those in /bin, things like commands to format your hard drive partitions, swapon, etc.,
In one fell-swoop you can change the permissions on all the files in this directory so that only special users can run this dangerous programs.
Click here to see your own /sbin directory
/lib
HINT: Don't ever delete stuff in here or change permissions unless you really know what you're doing.
Files in the /lib directory are "chunks" of programs that are shared and needed by other programs in order to run.
This is a LIBRARY of functions that programs can use.
Many of the library files in this directory wrapper the actual interface to the operating system that all the programs on the system use... say, to Open a File.
Click here to see your own /lib directory
/etc
Configuration files, scripts, etc., - pardon the pun, go under this directory.
Relative to partitions, you'll want to peek at fstab, mtab... Heck, check out *tab, anything ending in tab.
Often, *tab files have man pages, so you can type "man <something>tab" and read about what that file is doing here.
Another excellent thing to know is that the system's knowledge of users and passwords is stored in the /etc directory in a file called passwd.
Take a peek at that, definitely, also the file called group or groups.
Also, anything relative to configuring networking, the scripts that run when you startup and shutdown your system, and so on, all the good config files go in /etc.
Look in the rc.d directory under /etc. There, you'll find all the startup scripts your system runs when it is booting up, configuring networking, mounting partitions, etc.,
/dev
This is my favorite directory.
It is perhaps best to say that /dev is a directory but the files in it are not really files... Anyway, these are your DEVICES, printer, scanner, serial ports, modems, hard disk drives !
Your hard disk drive is probably known as "/dev/hda" and your partitions are known as "/dev/hda1" "/dev/hda2" etc., That's for IDE drives.
SCSI drives are known as "/dev/sda", like that.
Somewhere there's a doc that talks about all the files in /dev...
Here is a couple of examples that are insightful as to how Linux itself uses this directory...
You can copy a file to /dev/lp0 and it will print instead of making a file /dev/lp0.
You can "setserial /dev/ttyS2 irq 5" and this will set your COM3 to IRQ 5...
You can "mknod" to create a device file in this directory if it doesn't already exist by default. This assumes the Kernel knows what the device is even if there isn't already a stub for it in this directory...
When you rebuild the Linux kernel you will enable and disable various types of devices, this may require you to use mknod before the devices actually work.
/root
When you create a user in Linux usually their HOME DIRECTORY is created under /home. This directory is the HOME directory of the root user, the user than can do anything.
/usr
Under this directory go things, files, programs, that are either created by users for use by other users, or for things useful to users.
When you see RTFM in an email message, it means that you should go to /usr/doc and READ THE FREAKING MANUAL, which is to be found here in the form of lots of little documents, subdirectories, HTML files, etc.,
ALSO, if you feel ready for it and want to rebuild your Linux kernel, the source code you will need to compile is almost always to be found under /usr/src/linux. Change directories with cd to /usr/src and then do "ls -al linux".
You will see usually that this is a symbolic link to some big huge long directory name that indicates the kernel source version. If you don't see any of this stuff it means you have not installed the kernel source and you will not be able to play with it until you do.
/home
Here is the first good example candidate for a partition !
User's home directories... Who knows how much crap users are going to create.
If all you have is one big partition for everything and it fills up, your system will crash.
If the part of your directory tree that fills up is itself a partition, then that part of your directory tree fills up and crashes and not your whole machine.
/boot
After you build a kernel, it's image is placed here.
Also, when you install Linux a backup copy of the original boot record goes here, say, if you had first installed Windows 98.
You can put it back the original Master Boot Record with the lilo command. - see "man lilo" for details... If you want your Windows to come back to life, for example, after having seemingly ruined it.
/mnt
That looks like short for MOUNT !
What do you bet there are subdirectories under /mnt that are completely empty ?
These are complimentary default Mount Points for things like your floppy disk, CD ROM drive, etc.,
An example of how this works: Go to the /mnt directory, "cd /mnt", then type "ls".
Put in a CD ROM, then type "mount cdrom".
You can now "cd /mnt/cdrom" and use "ls" to see the contents of your CD ROM.
If you then type cd /mnt, and umount cdrom you will note that the directory /mnt/cdorm is now empty.
Click here to see your own /mnt directory
/opt
The recent trend in standardizing Unix directory tree structures suggests that third party applications go in /opt, things like Apache, Netscape Server, Oracle, etc.,
Although on Redhat, you'll likely find Apache located under "/etc/httpd"
/proc
This is truly a directory that's not a directory.
This is a pseudo file system.
All files you find here are stubs for accessing the kernel's data as it runs.
If you view the contents of a file in here you'll see live data come out of the kernel, and not the static contents of a file.
Don't play with /proc, don't make a partition for it... Unless you're a guru, just ignore this.
/var
Variable Stuff goes in /var, log files, MAIL MESSAGES, your Email Inbox, temporary holdings of incoming and outgoing mail...
This part of the directory tree usually ends up growing and growing... and will eventually fill up if you don't do something.
This is another excellent part of the directory tree to place on its own partition.
/tmp
This place is for programs' temporary files.
Poorly behaved or crashing programs will leave turds here, and /tmp will fill up eventually just like /var.
Summary
Computers can have many hard disk drives
Each hard drive can have one or more logical partitions
Linux will need at least TWO partitions, one for swap space and one for the file system
Unix file systems are made up of a Directory Tree
Each directory, starting with the root directory, can be a Mount Point for a partition. In other words, /home/allen can represent a whole partition, and /var/logs can be on another partition, etc., etc., and these partitions can be on any of the hard drives in the system. Which is why on Unix you don't see drive identifiers like on DOS and Windows "C:\".
Some parts of the Linux directory tree are known to be good things to stick on their own partitions, including /var, /tmp, /home...
Hard drives have names that are represented in the /dev directory. IDE Drives look like /dev/hda, /dev/hdb; and SCSI drives look like /dev/sda, and /dev/sdb.
Partitions on disk drives are named with the device and then the partition's number, thus: /dev/hda1, /dev/hda2, etc., on IDE drives, and /dev/sda1, /dev/sda2, etc., etc., on SCSI drives.
Mapping a Partition into the Directory Tree requires that you know the name of the partition, like "/dev/sda5", and the Directory Path under which the partition is to be mounted, like "/home/allen/lots_of_stuff"
No particular part of the directory tree needs to be on any specific partition. The sky is the limit.
Partitions, must be big enough to allow for all of the files they must contain initially, plus room for growth where appropriate. For example, if and when you rebuild the Linux kernel from source, there had better be enough room for the compiler to run and generate output on whatever partition "/usr/src" is mounted. Not only that, there had better be room in "/boot", if it is mounted on a separate partition, for the new kernel image !
Mounting and un-mounting a floppy disk or CD-ROM is synonymous with mounting and un-mounting a hard disk partition - except in the former cases they are "Removable Media", whereas, hard disk drives normally don't come out without a lot of effort.
Aha! And I have Windows !
If you want to have more than one operating system installed on the same set of hard disk drives in one computer, you'll need to remember that hard disk partitions have a TYPE.
This TYPE is an integer value that identifies the category of file system expected to be Formatted onto that partition.
For example, you might have three partitions, one for Windows with a TYPE DOS-32, and two for Linux, TYPED Linux-Swap and Linux-Native.
The way that YOU will be identifying the TYPES will be by the integer values if you are using fdisk, or by the Named Types like I've listed here if you're using Disk Druid - of course Disk Druid is just being friendly and will actually use the associated integer value, I believe ranging from 0-255.
Fdisk is friendly in a different way, using it's help for partition types, you can see a list of partition types and their associated integer values, and you'll have to remember that and type it in as you're creating a partition.
ANY PARTITION MUST BE FORMATTED BEFORE AN OPERATING SYSTEM CAN USE IT. In Windows you do this with the format command, and in Linux you will use the mke2fs command.
When you install Linux, you will have had to specify a partition for Linux Swap, and at least one partition Linux-Native.
At a later point in the install, Linux will look for partitions stamped with the TYPE it is expecting and then ask you to format them with mke2fs.
Windows will also look for Partitions stamped with the TYPE it is expecting, AND, it will limit itself to playing only with partitions of its TYPE during the install.
If you partition all your drives properly before installing either Linux or Windows, then the installations of both of these will be a breeze.
Hmn...
We just learned that another good reason for partitions is so that different operating systems can exist on the hard disk drives of one computer at the same time. - not necessarily that they can all run at the same time. If you like that idea, find out about vmware.
Fixing drives that appear to be broken
A comment about tools... The fdisk and disk druid that come with Linux are BETTER than ones you're likely to find elsewhere. As a matter of fact, you will probably not be able to effectively set up Linux and Windows... Or Linux and anything else, without Linux' neato tools.
WARNING: Because of this you may actually need to use these Linux tools to wipe-out Linux partitions. I've noticed repeatedly Windows 95/98... do not want to touch a partition that is not of their TYPE, however, I think NT's fdisk is significantly more likely to work in such cases.
The fix... Whatever situation you might find yourself in where Windows or whatever will not recognize a hard drive that you got from some other system, or the same system, you can always BEGIN A LINUX INSTALLATION, and run it up to the point where you get to run the partitioning tools like fdisk or Disk Druid and use that tool to zap whatever partitions are there and make new ones of just the right type for whatever you are doing.
Often, this means that right after you run the tool and save changes, you must reboot. This is a good time to take out Linux from the Floppy and/or CD ROM, and put in whatever you're wanting to install.
Actual output from fdisk
[root@localhost /]# fdisk Using /dev/hda as default device! Command (m for help): p Disk /dev/hda: 255 heads, 63 sectors, 627 cylinders Units = cylinders of 16065 * 512 bytes Device Boot Start End Blocks Id System /dev/hda1 * 1 255 2048256 b Win95 FAT32 /dev/hda2 256 627 2988090 5 Extended /dev/hda5 256 587 2666758+ 83 Linux /dev/hda6 588 620 265041 82 Linux swap*I have Windows 98 and RedHat Linux 6.0I have only 1 5GB IDE Hard Disk Drive. By looking at the output of fdisk you can tell that Linux knows my hard drive as /dev/hda - right ?
Looking at this output tells me is that I have 4 partitions, two for Windows 98 and two for Linux.
The Windows 98 partition is the one that is bootable.
HOWEVER, by default my machine boots Linux.
That is because I used The Linux Loader "lilo" and it created the Master Boot Record on this hard drive.
Lilo is a program that sets up your computer to boot.
It is very capable, but ordinarily, I expect you'll find it will setup to boot Linux - until you get good at it.
Interestingly, as a result of my RedHat install's execution of Lilo, and my having first installed Windows98, my /boot directory contains the original boot image that Windows98 installed on /dev/hda1, and I can put it back anytime using the lilo command.
You'll need to read up on it, try "man lilo"
Note: In order to get fdisk to point to the particular hard disk drive you want to be working with, if different from the default one, you need to tell it to do so, either on the command-line or from the menu you get when entering "m"
A note on booting...
Here's a neat know-how... If ever I wanted to boot off of a Linux bootable floppy disk, a prompt will come up.
After some time, Linux will boot from the floppy.
HOWEVER, if I type "boot vmlinuz root=/dev/hda5" before Lilo kicks-in automatically, Linux will boot THE WAY I TELL IT TO, in this case, assigning /dev/hda5 to the root "/" of my directory tree.
See ? We used the name "root" to mean the root of our directory tree !
Still with me ?
The only thing that will come from the floppy in this example will be the image of the Linux kernel that will run.
Linux people have a tendency to know more about their hardware than Windows people... I know that /dev/hda5 is where I originally assigned the root of my directory tree. HINT-HINT
You can play tricks and have multiple partitions with the appropriate contents for a directory root...
At boot time you can pick one. This is neato for testing out various configurations of your Linux. - OR recovering if you accidently deleted necessary things from the root you were working in
To be tricky like that, you'll need to remember what I said about /etc containing all the good configuration files, as it would be no good whatsoever to mount a new root directory, containing an /etc directory, which then tries, or doesn't try, to mount the right partitions... That is, the config files in one place may no longer be appropriate if you change things around.
It is very good to know how to be tricky like that. A simple example of why: You HAD two IDE drives, Windows on the first one, Linux on the second. You get rid of your Windows drive, and now have just the Linux drive (which just changed names from /dev/hdb to /dev/hda).
See that ? Try and boot now, and by the way, FAIL - and not just because you forgot about IDE's master/slave relationship.
The RIGHT WAY to do that is to first edit the fstab-like files in /etc to say /dev/hda before you yank the Windows drive and reboot.
Oops, you can also do all this with the boot prompt that comes from your Master Boot Record, that is, not using a floppy - if it is working... I mentioned the floppy thing assuming you broke the Master Boot Record ;)
You have made an emergency boot floppy, right ? You should look that up too.
A note on performance
One of the final considerations when partitioning is PERFORMANCE.
For example, it might be a good idea to put database index files on one physical hard disk drive, and the database data files on another, and the database log files on yet another... all on separate hard disk drives.
This has the effect of maximizing the efficiencies of mechanical bottlenecks... Linux can simultaneously read indexes, write data to data tables, and append to logs, without waiting for any one hard disk to finish doing something before it begins something else on another.
Processing speed far exceeds hard drive speed.
A note on backups
I'd have to lookup the command, since I never use that sort of thing anymore, but you can backup and restore an image of a partition.
This is not a file by file backup, but a binary image of the whole partition.
It is a good idea if you like to do things this way NOT to make your partitions bigger than your backup media... i.e., You'll probably want an image backup of a partition to fit on a single tape or zip disk, right ? - Think about it
Ta dah ! There
Now you're a guru... Almost, anyway, all you need to do now is try and try again until you have the experience to go with all this ;)
Errors in this document
Evidently, you can run SWAP as a FILE the way Windows does and you don't have to have a SWAP partition... I don't know anybody who does that -FYI But also I didn't know that, so I guess I'd better RTFM!
Your mission, should you choose to accept it...
I intend to add some sections regarding how to measure how big a partition should be before you make it, basically this means running "df -k" and "du -ks" and taking a look at something first...
I'm curious what effect this document has on someone who has either never set up Linux before, or who really isn't very good at it yet.
SO SEND ME YOUR COMMENTS AND EXPERIENCES IF YOU ARE GOOD AT EXPRESSING YOURSELF