-->
Previous | Table of Contents | Next |
The following sections show how to use fdisk. Here is an example of how to use fdisk to set up the partitions on a hard disk for use by Linux. Assume that you want to configure the first IDE drive in your system for Linux. Make sure that you have a backup of your data. All data on your hard disk is destroyed in the process. The name of the first IDE hard disk is /dev/hda, which is the default device for Linux.
Running fdisk
You run fdisk with this command
# fdisk
and fdisk responds with the following:
Using /dev/hda as default device! Command (m for help):
This tells you that fdisk is using disk /dev/hda as the device that youre working with. Because this is what you wanted, youre fine. You should always check to make sure that youre really on the disk that you think youre on. Linux then displays the fdisk command prompt.
Displaying the Current Partition Table
The first thing you want to do is display the current partition table. This is done with the p command:
Command (m for help): p Disk /dev/hda: 14 heads, 17 sectors, 1024 cylinders Units = cylinders of 238 * 512 bytes
Device Boot Begin Start End Blocks Id System
Command (m for help):
This listing shows that the current disk, /dev/hda, has a geometry of 14 heads, 17 sectors, and 1,024 cylinders. The display units are in cylinders of 238 * 512 (121,856) bytes each. Because there are 1,024 cylinders and each cylinder is 121,856 bytes, you can deduce that the disk can hold 1,0244×121,856 = 124,780,544 bytes, or about 120MB. You can also see that /dev/hda has no partitions.
Creating a New Partition
Assume that you want to create a 100MB Linux file partition for user home directories and a 20MB swap partition. Your next step is to use the n command to create a new partition:
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-1023): 1 Last cylinder or +size or +sizeM or +sizeK (1-1023): +100M
Using the n command to create a new partition displays another menu. You must choose whether you want to create an extended partition or a primary partition. You typically want to create a primary partition unless you have more than four partitions on a disk. fdisk then asks you for the partition number that you want to create. Because this is the first partition on the disk, you answer 1. Youre then prompted for the first cylinder for the partition. This determines where on the disk the data area starts. Again, because this will be the first partition on the disk, you can start the partition at cylinder 1.
The next line asks you how large you want the partition to be. You have several options as to how to answer this question. fdisk accepts either a number, which it interprets as the size in cylinders, or the size in bytes, kilobytes, or megabytes. The size in bytes is specified as +bytes, where bytes is the size of the partition. Similarly, +sizeK and +sizeM set the partition size to size kilobytes or size megabytes, respectively. You know that you want a 100MB partition, so the easiest answer to the prompt is +100M.
Rechecking the Partition Table
Now you should check the partition table again to see what fdisk has done:
Command (m for help): p Disk /dev/hda: 14 heads, 17 sectors, 1024 cylinders Units = cylinders of 238 * 512 bytes Device Boot Begin Start End Blocks Id System /dev/hda1 1 1 861 102400 81 Linux/MINIX Command (m for help):
The partition table shows that you have 1 partition, /dev/hda1, that goes from cylinder 1 to cylinder 861 and uses 102,400 blocks. Its listed as being type 81, Linux/MINIX.
Creating the Swap Partition
Now you need to create the 20MB swap partition by using the remaining disk space. This is just like creating the first partition:
Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (862-1023): 862 Last cylinder or +size or +sizeM or +sizeK (862-1023): 1023
TIP: Its usually better to go ahead and enter the size of the last partition in cylinders to make sure that you use all the disk space.
Here you specified partition number 2 for the second partition. When fdisk prompts for the first cylinder, notice that it gives a range of 862 to 1023. This is because the first partition takes up everything before cylinder 862. So enter 862 as the starting cylinder for the second partition. You want to use all the remaining space on the disk for the swap partition. You should have about 20MB left, but if you specify the size in megabytes, the internal fdisk calculations could leave you with a couple of unused cylinders. So you enter 1023 for the last cylinder on the size prompt.
NOTE: You might see an error similar to thisWarning: Linux cannot currently use the last xxx sectors of this partition.where xxx is some number. Such an error can be ignored. Its left over from the days when Linux couldnt access file systems larger than 64MB.
Making Sure the Sizes Are Correct
At this point, youve created both partitions that you wanted to create. You should take a look at the partition table one more time to check that the sizes are correct:
Command (m for help): p Disk /dev/hda: 14 heads, 17 sectors, 1024 cylinders Units = cylinders of 238 * 512 bytes Device Boot Begin Start End Blocks Id System /dev/hda1 1 1 861 102400 81 Linux/MINIX /dev/hda2 862 862 1023 19159 81 Linux/MINIX Command (m for help):
As you can see, /dev/hda1 uses cylinder 1 through cylinder 861 with a size of 102,400 blocks, which is approximately 100MB. Partition /dev/hda2 goes from cylinder 862 to cylinder 1023 with a size of 19,156 blocks, or almost 20MB.
Previous | Table of Contents | Next |