justlinux.com
Tue, 09-Feb-2010 15:03:08 GMT
internet.com
Forum: Registered Users: 73634, Online: 264
nhfs Here you can view your subscribed threads, work with private messages and edit your profile and preferences Registration is free! Calendar Find other members Frequently Asked Questions Search Home Home

Help File Library: Using fdisk


Written By: Danny "Strike" DiPaolo

In spite of its mostly spartan interface, fdisk is actually a very powerful, easy-to-use tool. To run fdisk, su to root and run "fdisk <device>", or perhaps (if /sbin is not in your path) "/sbin/fdisk <device>" - where <device> is something like /dev/hda or /dev/sda, the device we will be partitioning. For a good primer on partitions and filesystems, go see the Help File on them by 7DS.

For my personal example, I've got Linux on my second IDE hard drive, so I'm going to be using /dev/hdb for my example. Remember to change it where appropriate.

So, once you are in fdisk, you should see a prompt like this:


[root@localhost /]# /sbin/fdisk /dev/hdb

The number of cylinders for this disk is set to 2491.
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., LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): 

You may or may not get that warning, depending upon your hard drive geometry. Most disks larger than 8GB will get this warning, if not all.

But now what do I do? Well, about the only clue I have here is that "m for help" in the prompt, so let's try that:


Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): 

Excellent! Now we have a list of commands whenever we need them. Okay, now lets explore my hard drive for a second and see what fdisk has to say about it. So let's use "p" and print the partition table:


Command (m for help): p

Disk /dev/hdb: 255 heads, 63 sectors, 2491 cylinders
Units = cylinders of 16065 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdb1   *         1       195   1566306   a5  BSD/386
/dev/hdb2           196       212    136552+  82  Linux swap
/dev/hdb3           213      2491  18306067+   5  Extended
/dev/hdb5           213       474   2104514+  83  Linux
/dev/hdb6           475       506    257039+  83  Linux
/dev/hdb7           507       532    208844+  83  Linux
/dev/hdb8           795       925   1052257   83  Linux
/dev/hdb9           926      1056   1052257   83  Linux
/dev/hdb10         1057      1088    257008+  83  Linux
/dev/hdb11         1089      1350   2104483+  83  Linux
/dev/hdb12          533       794   2104483+  83  Linux

Command (m for help): 

Let's take a look at each of these columns one by one.

Device - this specifies which device fdisk is looking at as well as which partition. The partition number is the last (and only) number in this section. Partitions 1-4 are "primary partitions", and any partition above 5 is a "logical partition". I'm not going to go into them much more because that would make this Help File much longer than it's already going to be.

Boot - If this column has an asterisk (like my /dev/hdb1 does), that means this partition is flagged as bootable. This means that if this is the primary hard drive (for IDE, /dev/hda is), then this is the partition that the OS or boot-loader is expected to be found on.

Start - This is the starting cylinder of the partition. A cylinder isn't a fixed size for all hard drives, but you can figure out how big each one is on your particular hard drive just by looking at the info at the top. Where it says "Units = cylinders of 16065 * 512 bytes", that tells you how big a cylinder is. So, mine are actually about 8MB per cylinder for this hard drive.

End - This is the ending cylinder of the partition. This can be important when loading up a system because of a (now old) limitation in LILO that won't allow /boot to be beyond the 1024th cylinder. Other than that, it's not too important to us right now.

Blocks - the number of blocks in this partition (duh). A block's size depends upon how you set up the filesystem. For the most part though, they will be 1KB blocks. For example, my first partition has 1566306KB of space, or about 1.5GB - that sounds about right :)

Id - a sort of identification number for partition types. Each type of partition has a different number. You can see all the different types of partitions that fdisk recognizes by simply entering in "l" at the menu prompt (that was option "list known partition types").

System - this is actually just an English version of the ID column. It simply takes the entry from the ID column and compares it to the table of partition types that it knows (as you can see with the "l" option), and prints out that type.

Creating Partitions

I'm going to do two examples that are hopefully pretty illustrative and will help you figure out the magic behind fdisk.

Creating a swap partition
I'm not sure if you can run a Linux system without a swap partition or not, but I wouldn't ever want to. So, this is one of the more important partitions. It has no special restrictions on it, really. Actually, for older (2.0) kernels some of them can only handle swap partitions that are not any larger than 128MB. But, by now those kernels are rarely in use anymore and ought to be upgraded anyway.

In this example, I'm going to create a swap partition of 256MB at the end of my drive. So, once I'm in fdisk, I want to choose the "n" option for "add a new partition". This is what I get:


Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)

I sort of explained the difference between these two earlier. Since I don't have any more room for another primary partition (partitions 1-3 contain all of my cylinders), I have to make it a logical partition, so I choose "l".


l
First cylinder (1351-2491, default 1351): 

Unless you have a good reason for leaving space between your partitions (and they do exist), it's almost always best to choose the default, which is the first available cylinder. You can actually type it in or just hit Enter.


Using default value 1351
Last cylinder or +size or +sizeM or +sizeK (1351-2491,
default 2491): 

Now here's the best feature of fdisk. You don't have to calculate the cylinders you want a partition to take up, you can just give it a size in MB or KB and it will get it as close as possible for you. Since I want this to be a 256MB swap partition I enter "+256M" (without quotes):


Last cylinder or +size or +sizeM or +sizeK (1351-2491,
default 2491): +256M

Command (m for help): 

And now I'm back at a command prompt. Let's print out the partition table again (p) and see if that change took place:


Command (m for help): p

Disk /dev/hdb: 255 heads, 63 sectors, 2491 cylinders
Units = cylinders of 16065 * 512 bytes

   Device Boot    Start       End    Blocks   Id  System
/dev/hdb1   *         1       195   1566306   a5  BSD/386
/dev/hdb2           196       212    136552+  82  Linux swap
/dev/hdb3           213      2491  18306067+   5  Extended
/dev/hdb5           213       474   2104514+  83  Linux
/dev/hdb6           475       506    257039+  83  Linux
/dev/hdb7           507       532    208844+  83  Linux
/dev/hdb8           795       925   1052257   83  Linux
/dev/hdb9           926      1056   1052257   83  Linux
/dev/hdb10         1057      1088    257008+  83  Linux
/dev/hdb11         1089      1350   2104483+  83  Linux
/dev/hdb12          533       794   2104483+  83  Linux
/dev/hdb13         1351      1383    265041   83  Linux

Command (m for help): 

Well, I now have a 259MB partition at the end. But, there's a problem. It's a "Linux" partition (ID = 83) and not a "Linux swap" partition (ID = 82). After a quick look at the list of commands (m), I see the "t" option - "change a partition's system id". This sounds right, so let's give it a shot.


Command (m for help): t
Partition number (1-13): 

Of course, we just created partition #13, so I choose that one:

Partition number (1-13): 13        
Hex code (type L to list codes): 

Now I look up and see that the hex code (ID) for "Linux swap" is 82, but I just want to make sure, so I hit "L", and surely enough, you can see that 82 is indeed "Linux swap", so we enter in 82:

Hex code (type L to list codes): 82
Changed system type of partition 13 to 82 (Linux swap)

Excellent! Now let's print the partition table again just to make sure that the changes took effect. I get this line for our partition:

/dev/hdb13         1351      1383    265041   82  Linux swap

Wonderful. We have created a Linux swap partition. Now in order for it to actually be written onto the hard drive we must write the partition table to the disk. That is option "w" - "write table to disk and exit". When I call it I get this:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.

So, now we have a new partition on our disk for swap. All that's left now is to actually set up the swap "filesystem" on it so that the system can read and write to it correctly. I'm not going to cover that in this particular Help File though I plan on writing another one about it.

Creating an ext2 partition
These days, Linux is using the ext2 filesystem as its default, so that's what we're going to learn how to make next. I'm going to go over this very quickly because it's pretty much exactly the same as the swap partition creation, minus that system ID switch.

In this example, I'm going to create a 1GB partition. Again, first step is "n".


Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)

Then, for reasons explained earlier, I have to choose "l" for logical.


l
First cylinder (1384-2491, default 1384): 

Use the default (hit Enter).


Using default value 1384
Last cylinder or +size or +sizeM or +sizeK (1384-2491,
default 2491): 

We want a gigabyte, so we want 1024MB, so we enter in "+1024M":


Last cylinder or +size or +sizeM or +sizeK (1384-2491,
default 2491): +1024M

Command (m for help):

List the partitions to see if we got it right, I get this for the last line:


/dev/hdb14         1384      1514   1052226   83  Linux

Once again, all we have to do now is write the table to disk, and then create a filesystem on that partition and we are ready to go. So, we hit "w" and we are done with creating partitions.

Deleting Partitions
This is so incredibly easy, I'm not even going to DO an example. It's option "d" in fdisk. When you hit that, it will ask for a partition number. Simply give it that number and the partition is deleted! However, as a technicality (and as a way to insure against people accidentally deleting partitions), this change doesn't take effect unless you write the partition table to disk again. So, once your partition is deleted, simply hit "w" and then it's gone for good.

IMPORTANT NOTE - You can never have gaps in the partition numbering scheme once you get past 5. If you delete a logical partition that isn't the last one, it WILL mess up all the partitions that came after it because it will shift them all down. In my example, if I were to delete partition 13, partition 14 would then become partition 13. This is very dangerous on a running system! If you delete partitions that come before partitions that are normally mounted on startup you MUST edit /etc/fstab to fix the changes.

That's it for fdisk! You can now create the two necessary types of partitions to build a Linux system the way you want it.

Send me feedback - Danny "Strike" DiPaolo


internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
internet.commerce
Be a Commerce Partner












The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers