Partitioning the Harddisk
|
URI: |
http://herbert.gandraxa.com/herbert/lfsphd.asp |
|
Link template: |
<a href="http://herbert.gandraxa.com/herbert/lfsphd.asp">Partitioning the Harddisk</a> |
|
Link symbols: |
|
Partitioning the Harddisk
|
URI: |
http://herbert.gandraxa.com/herbert/lfsphd.asp |
|
Link template: |
<a href="http://herbert.gandraxa.com/herbert/lfsphd.asp">Partitioning the Harddisk</a> |
|
Link symbols: |
|
Table of Contents
Home »
Articles »
Linux from Scratch »
Partitioning the Harddisk
This article is part of the series Linux from Scratch. — To work with a dedicated new harddisk, we first need to create Linux partitions on it.
2008-Jan-03
Every
harddisk can be split into several logical parts, which to the outside may appear like individual harddisks 1. Such a part is called a
partition 2. If you are accustomed to Microsoft Windows: in that OS the individual partitions are referred to with the letters of the Latin alphabet (C:, D: etc.), one letter per partition. Linux uses a different approach, as we will see later on.
After creation, a partition can be "formatted" so, that only dedicated operating systems can meaningfully work with it. This is accomplished by giving each partition a specific "partition type" (see
file system further below). Whereas it technically is possible for all operating systems to see all partitions, many prefer to completely ignore the partitions which were not dedicated to them. For example does MS Windows (with the exception of very few management programs) not list any partition with a Linux type, hence there is no letter C: or D: with which to access such partitions.
A typical Linux setup consists of at least 2 partitions for different purposes. One partition will be used as a Swap Partition, the other one(s) to store programs and data.
________
1 Physically such partitions still share the same hardware. This means, that in case of hardware failures all contained partitions may be affected.
2 Note, however, that it is also possible to create partitions spanning multiple harddisks. Also, it is not uncommon to have a single partition span the whole harddisk.
As lined out already in this series' previous article
Creating a Bootable LiveCD, wa assume that we have only one harddisk and that this harddisk is wholly dedicated to the new Linux system. Thus, we are going to partition the whole disk, which implies that all data on your harddisk will be lost for ever.
Before partitioning a disk we should know how many partitions we wish to create. So, the question is asked quickly: how many partitions do we need to run our Linux system? Unfortunately, the question is almost impossible to answer in general: too many users have too many different needs. However, there are some considerations which probably are valid for many users:
The system partition / is the container for the linux system itself. It is usually called the Root Partition. This is actually the only partition which is really required: Linux can be made run when having only this one, but it won't run in an optimal way.
One of the most important considerations is with respect to
paging. Paging is a mechanism which is invoked, when the OS requests more RAM than currently is available. There are different approaches to handle such situations: Windows uses a so-called Swap File, and Linux (although able to handle Swap Files as well) typically uses a dedicated harddisk area, which is formatted in a special way. Once created, this specially formatted area is useless for other data; it is just suitable as the swap area and nothing else. Because each partition allows one (and only one) formatting style, it can be deduced, that this swap area needs an own partition. Indeed, this is usually done, and that dedicated partition is called the Swap Partition.
It is possible now, that the whole remaining space on your harddisk is allocated to just one (possibly quite large) additional partition. Although this is sometimes done, it is not necessarily the most sensible approach.
The probably most important further separation is between "the system" (which keeps Linux running), and "data" (whose creation or manipulation is the reason for the system to exist) 1. The advantages are at least twofold when splitting data from the system: firstly, the system can easily be replaced by another system (e.g. another Linux distribution, even when it uses a different file system), without affecting the data at all. Secondly, the data can be backed up very easily without any "system overhead", simply by copying the whole data partition to another place.
Under Linux, the personal data of all users is usually stored in a directory called /home, and if following above reasoning, that directory should be contained within a dedicated partition.
A strong reason might call for yet another dedicated partition: certain server services (like mail and print spoolers) have the potential to be flooded, thereby filling the harddisk. Also, propagating errors can easily flood a partition. These situations result in your computer to appear hanging, and it will hang the longer, the larger the remaining space is. If you run such servers it might be a good idea to create a dedicated, not too large partition for the /var directory, onto which such services act.
There are a lot more reasons for even more partitions 2. However, as each partition is regarded as an individual harddisk, the more partitions you create, the more you run the risk, that you run out of disk space in one of the partitions. In the end, it is you, who needs to find the balance between cost and profit for your specific needs.
The harddisk being used for this article series has a capacity of 320 GB (298 GiB) 3. It will be partitioned as follows:
| Partition | Size | Comments |
| swap | 8 GiB | As a rule of thumb, the Swap Partition should be be between 1 and 2 times the size of the available RAM, with desktops tending towards the lower and servers towards the upper end. My machine, a designated future server, is currently equipped with 2 GiB of Memory, but I might double that in time to 4 GB. In anticipation of the doubling I dedicate 2×4 GB=8 GB to the Swap Partition. |
| / | 16 GiB | Although some sources4 recommend a size of about only 2-3 GiB for the Root Partition, I recommend a size of at least 5 and up to 10 GiB for normal distributions, depending on the number of programs which actually are going to be installed. Add to this number the data you are going to store (unless you use an own partition for that, which we do in our setting). For us, 10 GiB should do perfectly well, and because we have abundant space on our harddisk, and because I like factors of 8 (call it a genetic defect), I will allocate 16 GiB. |
| /var | 24 GiB | Here we store various variable files, like for example log files, e-mail files, temporary spool files for printing and news, etc. Assuming that we don't produce log files larger than 1 GiB per year and adding some additional space for mailing and printing, we should get away once again with 20 GiB. And due to the genetic defect I will allocate 24 GiB. |
| /home | 64 GiB | This is the place where the users' individual data goes into. It is very hard to estimate this partition's size. However, my old machine running under Windows has a harddisk of "only" 60 GiB and therefore is not really large by today's standards. However, I was not able so far to really fill it up to more than 50%, and that is including all programs. Given the server's task, I am pretty sure that about 60 GiB will be more than enough for several years. |
| unused | 186 GiB | This is the rest of the harddisk's capacity, which we are unlikely to ever use: a whopping 62% of the harddisk's capacity. However, it is easy to later on add more partitions, should additional space be required, and so we treat this as a still unpartitioned reserve for later needs. |
________
1 Note, however, that the term "data" is somewhat fuzzy. What is meant here is the user data, which is created by the user(s) of the future Linux system, such as documents, worksheets, notes etc. – but not data whose modification is required to run the system or manage the user data (which would be referred to as Meta Data).
2 For example is it possible in Linux to give just reading access to individual partitions: thus critical data can be better protected against manipulations by placing it into such a write-protected partition.
3 The "G" in 320 GB is referring to the ISO prefix giga-, meaning 109, and not the "IT-giga" of 230. 320×109 Bytes would amount to 320/1.0243=298 GiB.
4 For example the online book Linux from Scratch on its page about
Creating a New Partition.
Although we know that the harddisk's capacity is 320 GB (298 GiB), this does not help too much when employing the partitioning tool fdisk, because that program requires us to state partition sizes in cylinders.
Besides this requirement, knowledge of the concept cylinders is also important for another reason: we want our Swap Partition to be as fast as possible, but –as will be detailled later within this subsection– not all cylinders provide the same effective speed.
So, what are these cylinders, or more generally: how is a harddisk organized? In the early times of harddisks, the capacity of the disk could be calculated by multiplying the number of cylinders, heads, sectors and bytes per sector. These figures are still reported to the BIOS, but with
Zone Bit Recording (ZBR) they are not meant to be taken literally as in reflecting the mentioned physical parameters.
As a matter of fact, nowadays practically all harddisks1 use ZBR, due to which not all the cylinders on your harddisk have the same access speed. In general, the outer tracks are faster than the inner tracks 2. Since the numbering of the cylinders starts with the outer tracks, the implication is, that a harddisk's lower cylinder numbers provide faster access.
This implication is important, because we want to have the Swap Partition in the fastest available area 3. Since there is just one harddisk in our machine, the swap partition is placed in the lowest available cylinder numbers. (And this is also the reason, why the Swap Partition was listed as the first partition in above table.)
How large is such a cylinder then? At the prompt of your ISO image (see previous article
Creating a Bootable LiveCD) enter the command as shown in bold typing:
root [ ~ ]# fdisk -l
The parameter -l is the lower-case letter "L", not the digit "1". The command will list the characteristics of your harddisk (and, if available, already existing partitions). It will produce an output similar to the following:
disk /dev/sda: 320.0 GB, 320072933376 bytes 255 heads, 63 sectors/track, 38913 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes
plus some lines with actual partitioning data, which you can ignore for now.
There is no point in believing the given details: the disk most certainly does not have 255 heads, nor will the other parameters be accurate – the culprit is ZBR, for the reasons pointed out above. However, the data which is useful is the value of (in our case) 8,225,280 bytes per cylinder: we can use this value to create the desired partitions.
Note, however, that we can not allocate a partition arbitrary parts of the indicated cylinder size: this means, that we need to round up or down at boundaries of (in our case) 8,032½ KiB.
Another very useful information is provided by the above command: this is the name of the partitionable harddisk. If you have more than one harddisk in your machine, you will notice one such output per disk.
Examine the first part of the first line of above output: disk /dev/sda. The /dev part can be ignored for now: it names the directory into which all device information is stored under Linux. The first two letters of the second part, though, /sda in our example, states that the disk in question is a SCSI or a SATA disk. Would it be an IDE disk (alternative name: ATA, later renamed to PATA), then you would most likely read /hda instead 4.
We need the harddisk's name, including the /dev part, in order to successfully partition it. To have it handy, it might be a good idea to write it down on paper.
As we know now, we need to round our desired partition sizes to the unit size (in our case we have 38,913 units at 8,225,280 bytes each). It is appropriate to copy above table and doing the appropriate calculations in there:
| Partition | Size | Number of Units | Rounded up | Units |
| swap | 8 GiB | 8×10243/8225280=1044.3 | 1045 | 1 - 1045 |
| / | 16 GiB | 16×10243/8225280=2088.7 | 2089 | 1046 - 3134 |
| /var | 24 GiB | 24×10243/8225280=3133.0 | 3133 | 3135 - 6267 |
| /home | 64 GiB | 64×10243/8225280=8354.7 | 8355 | 6268 - 14622 |
| unused | 186 GiB | All the rest | 14623 - 38913 | |
________
1 To find harddisks without ZBR you need to find one of the early or mid 90ies (or earlier). However, due to their in general inadequate capacity and speed compared to today's requirements, it is unlikely that such harddisks still are in use.
2 For the reasons of this claim see Kristian Köhntop's article about the
speed within a harddisk.
3 It would be ideal if we had a dedicated harddisk just for the swap area, since in such a scenario we would avoid unneccessary movement of the harddisk's heads, and we would profit from possible parallel data transfers.
4 As you may have imagined, the "hd" in /hda stands for harddisk, and the "sd" in /sda for SCSI Disk or SATA Disk. The "a" indicates which disk is referred to. For SCSI/SATA disks this is just a lexicographically ascending letter, i.e. "a" stands for the first disk, "b" for the second one, etc. For IDE disks, though, "a" refers to the first master on the first IDE bus, "b" to the slave on the first IDE bus, and accordingly do "c" and "d" refer to the master and slave, resp., on the second IDE bus – this also means, that there may be two disks named /hda and /hdc, but none named /hdb, namely when there is a master at each of the two buses, but no slave at either bus.
Warning: If you do have more than one harddisk, make sure, that you are able to safely identify the correct one by its name (it was suggested earlier, that you write down its name). We are going to delete the content of the harddisk you indicate in the following step: choosing the wrong harddisk may leave you with irreparable loss of data.
Use the program fdisk again, but this time provide the name of the harddisk, which you are going to partition. You must give it the full name, i.e. including the /dev part. In my case, I need to enter:
root [ ~ ]# fdisk /dev/sda
This time you will receive a feedback about the total numbers of "cylinders" (in my case 38,913), followed by a warning. If you follow this article series, you can safely ignore this warning (and if you don't follow the series, it is assumed that you know what you do).
Notice, that the prompt has changed: you are not in the Linux shell anymore, but in the program fdisk.
Command (m for help): _
The command q (quit) will leave fdisk and bring you back to the Linux prompt, without saving anything. Use this, in case you mess up or are not sure about the consequences.
The command p (print) will give you the same results for the actual harddisk as you obtained before by using fdisk -l:
Command (m for help): p
Now examine the lower lines, the ones we ignored previously when having used fdisk -l. They will most likely look different for every user (because of the different harddisk makes and even more because of different harddisk histories); in my case, they read:
Device Boot Start End Blocks Id System /dev/sda1 * 1 6374 51199123+ 83 Linux /dev/sda2 6375 7011 5116702+ 82 Linux swap / Solaris /dev/sda3 7012 38913 256252815 83 Linux
There is one important thing to notice here: the partitions are numbered sequentially from 1 on, and this number is appended to the harddisk's name (which is sda in my case). So, sda1 means: the first partition (1) on the first (a) SCSI or SATA disk (sd). Accordingly, sda2 and sda3 signify the second and the third partition on that specific harddisk.
The start and end columns refer to the "cylinders". It can be seen, that the first partition currently has 6374 cylinders. Since 1 cylinder represents 8,225,280 Bytes, that partition has a size of about 48.8 GiB. Via a bit basic math it can be found out, that one "block" represents 1,024 Bytes (1 KiB); however, we won't need that information to set up the partitions we lined out.
The system column does hint it already: this particular harddisk has been used in a Linux environment already. That is correct, it was to test the recent Fedora distribution. However, we do not need the data on this disk anymore and are going to delete it. Note, that the system column is just a human-readable form of the column labelled Id. A list with the partition types in common use can be obtained by typing the command l (list) at the prompt of the fdisk program.
Partitions are removed with the command d (delete) at the prompt of the fdisk program.
Note, that if you have more than just one partition, fdisk will inquire about the partition number to delete, and you will need to enter the according number. No such question is posed when d is applied with the last remaining partition.
Command (m for help): d Partition number (1-4): 3 Command (m for help): d Partition number (1-4): 2 Command (m for help): d Selected partition 1 Command (m for help): _
The partitions were not removed yet. If you are unsure, you still may quit now with the command q (quit) and nothing has happened. If you choose to proceed, though, the alterations are applied to your harddisk. Therefore this is the point of no return: with the next command we are going to remove all the already existing partitions.
Final Warning: Executing the following step will remove the harddisk's partitions and all data on it. Are you absolutely sure, that it is the correct harddisk?
To save the modified partition table to the harddisk you use the command w (write table to disk and exit):
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. root [ ~ ]# _
Now the partitions of your harddisk are gone. Try the command fdisk -l once again and you will notice, that there are no more entries below the column headings.
Time to create the partitions as we defined them. To do so, you need to enter fdisk again for your particular harddisk:
root [ ~ ]# fdisk /dev/sda
As before, the warning can be ignored. The command to create a new partition is n (new):
Command (m for help): n Command action e extended p primary partition (1-4) _
Now at the prompt (at the unusual location at the start of the line) you are expected to enter either e or p, standing for "extended partition" or "primary partition", resp.
What is that? you might ask. Well, historically, OSes allowed a maximum of only 4 partitions on each harddisk. 4 were plenty, as the harddisks were quite small (several MiB only). These are the "primary partitions". Later on, when harddrives began to grow, the need for more partitions arose. To accomodate this need, and to retain backward compatibility, it was defined, that one of these partitions may be defined as an "extended partition". If you would answer with e, fdisk would ask you, which partition should become the extended partition. That "extended partition" itself will not be a real partition, capable to hold any data directly. Instead, it acts as a container, which can hold itself several more partitions, which are called "logical partitions". If working with an IDE (ATA/PATA) harddisk, you can have up to 60 logical partitions, if working with a SCSI or SATA harddisk, you can have 12 logical partitions (for a total of 63 or 15 partitions).
As per our table, we found that we want to create 4 partitions. Therefore, theoretically we would not need to use any logical partitions. However, a lot of space (the major part, actually) is not used yet, and there is good reason to assume, that sooner or later we will claim that space partially or wholly. This does not need to be a partition relating to our current project. For example could we install another OS on this same harddisk. Now, claiming even part of that space would mean to create an additional partition, with which we would exceed the 4-partitions-limit.
For above reasons it should have become apparent, that we indeed want to create an extended partition, containing logical partitions. Of course, the question arising naturally is: what partitions shall become the primary partitions and which ones shall become logical partitions?
Somewhat arbitrarily we decide, that the Swap Partition swap shall be a primary partition, as well as the Root Partition / (both granting a fastest possible harddisk speed by locating them in the outer cylinders with low unit numbers). The two remaining partitions /var and /home shall become logical partitions within the extended partition (with /home being the last logical partition in order to easily enlarge it, as obviously only the last partition can be enlarged without tedious problems).
This concept still enables us to use one additional primary partition (should a such be needed for whatever future reasons). However, this means that we need to provide twogaps with undefined areas: one for the remaining primary partition, and the other for all potentially possible logical partitions. Since we only have defined one such gap so far, we need to split it. This requires us to reflect about the size of one of the two "open" partitions. In our case, it is quite natural to reserve a relatively small size for the potential additional primary partition, say 8 GiB. This in turn reduces the total available size for all logical partitions by 8 GiB.
Amending the table as described, we have now:
| Partition | Size | Number of Units | Rounded up | Units | Partition Number |
| swap | 8 GiB | 8×10243/8225280=1044.3 | 1045 | 1 - 1045 | Primary 1 |
| / | 16 GiB | 16×10243/8225280=2088.7 | 2089 | 1046 - 3134 | Primary 2 |
| unused | 8 GiB | Additional primary partition | 1045 | 3135 - 4179 | Primary 3 (unused) |
| extended | 16 GiB | All the rest | 4180 - 38913 | Extended (Primary 4) | |
| /var | 24 GiB | 24×10243/8225280=3133.0 | 3133 | 4180 - 7312 | Logical 1 |
| /home | 64 GiB | 64×10243/8225280=8354.7 | 8355 | 7313 - 15667 | Logical 2 |
| unused | 266 GiB | All the rest | 15668 - 38913 | [Possible Logical 3-12] | |
With that knowledge, the first partition can be created:
Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-38913, default 1): 1 Last cylinder or +size or +sizeM or +sizeK (1-38913, default 38913): 1045 Command (m for help): _
As can be verified by using the command p (print), the just created partition is an ordinary Linux partition by default. This is not what we want here: we want it to be a Swap Partition.
To change the partitition type, we need to gve it another id. The list with ids is available with command l (list). In that list, we find an entry 82: Linux swap / Solaris, which is exactly what we want this partition to be. To change the id, we use command t (type):
Command (m for help): t Selected partition 1 Hex code (type l to list codes): 82 Changed system type of partition 1 to 82 (Linux swap / Solaris) Command (m for help): _
Creating the second primary partition is straightforward:
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (1046-38913, default 1): 1046 Last cylinder or +size or +sizeM or +sizeK (1046-38913, default 38913): 3134 Command (m for help): _
Because this partition will become the system partition /, it is here from which we will boot the Linux system. This must be told to fdisk with command a (toggle bootable flag).
Command (m for help): a Partition number (1-4): 2 Command (m for help): _
Partition 3 is our reserve primary partition, which we are not going to use at the moment. However, it is still a good idea to nevertheless create it: this avoids the nasty gaps in the sequence of cylinders.
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (3135-38913, default 1): 3135 Last cylinder or +size or +sizeM or +sizeK (3135-38913, default 38913): 4179 Command (m for help): _
Time to create our extended partition now, which automatically takes the slot of the only left primary partition:
Command (m for help): n Command action e extended p primary partition (1-4) e Selected partition 4 First cylinder (4180-38913, default 1): 4180 Last cylinder or +size or +sizeM or +sizeK (4180-38913, default 38913): 38913 Command (m for help): _
Within the extended partition, we create the first logical partition of 24 GiB for /var. Note, that fdisk does not any longer offer us a choice between e and p. e is not listed anymore, because only one extended partition is allowed. Instead, the new option l would be displayed, allowing us to create a logical partition. However, it is not really displayed, because also p is not allowed anymore (we already have used up the allowed 4 primary partitions), and so l is correctly assumed as the only remaining choice.
Command (m for help): n First cylinder (4180-38913, default 1): 4180 Last cylinder or +size or +sizeM or +sizeK (4180-38913, default 38913): 7312 Command (m for help): _
Eventually we also define the remaining logical partition:
Command (m for help): n First cylinder (7313-38913, default 1): 7313 Last cylinder or +size or +sizeM or +sizeK (7313-38913, default 38913): 15667 Command (m for help): _
Time to look at the partitions list with p:
Device Boot Start End Blocks Id System /dev/sda1 1 1045 8393931 82 Linux swap / Solaris /dev/sda2 * 1046 3134 16779892+ 83 Linux /dev/sda3 3135 4179 8393962+ 83 Linux /dev/sda4 4180 38913 279000855 5 Extended /dev/sda5 4180 7312 25165791 83 Linux /dev/sda6 7313 15667 67111506 83 Linux
Notice, that cylinders 15668 to 38913 (more than half of the whole harddisk) are still unused. Should one or more new partitions be required, we could proceed and create such easily with the by now familiar command n. The new partitions will then be named sda7, sda8 etc.
It is also possible to enlarge the last defined partition, i.e. in our case sda6. Using fdisk, it is not possible to make it smaller, though.
As before, we have the chance to quit now with command q, discarding all changes to the partition table. If the table looks fine, though, we write the new partition table to the harddisk with command w before quitting:
Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. root [ ~ ]# _
The harddisk has been partitioned as per our considerations outlined initially, and we are back to the Linux prompt. The next article within this series will care for the creation of the appropriate file systems within the just added partitions.