Hack 65. Partition using fdisk

by Ramesh

After you’ve installed brand new disks on your server, you have to use tools like fdisk to partition it accordingly.

Following are the 5 typical actions (commands) that you can execute inside fdisk.

  • n – New Partition creation
  • d – Delete an existing partition
  • p – Print Partition Table
  • w – Write the changes to the partition table. i.e save.
  • q – Quit the fdisk utility

Create a partition

In the following example, I created a /dev/sda1 primary partition.

  1. # fdisk /dev/sda
  2.  
  3. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel. Changes will remain in memory only,
  4. until you decide to write them. After that, of course, the previous content won't be recoverable.
  5.  
  6. The number of cylinders for this disk is set to 34893.
  7. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:
  8.  
  9. 1) software that runs at boot time (e.g., old versions of LILO)
  10. 2) booting and partitioning software from other OSs
  11. (e.g., DOS FDISK, OS/2 FDISK)
  12. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
  13.  
  14. Command (m for help): p
  15.  
  16. Disk /dev/sda: 287.0 GB, 287005343744 bytes
  17. 255 heads, 63 sectors/track, 34893 cylinders
  18. Units = cylinders of 16065 * 512 = 8225280 bytes
  19.  
  20. Device Boot Start End Blocks Id System
  21.  
  22. Command (m for help): n
  23. Command action
  24. e extended
  25. p primary partition (1-4)
  26. p
  27. Partition number (1-4): 1
  28. First cylinder (1-34893, default 1):
  29. Using default value 1
  30. Last cylinder or +size or +sizeM or +sizeK (1-34893, default 34893):
  31. Using default value 34893
  32.  
  33. Command (m for help): w
  34. The partition table has been altered!
  35.  
  36. Calling ioctl() to re-read partition table.
  37. Syncing disks.

Verify that the partition got created successfully

  1. # fdisk /dev/sda
  2.  
  3. The number of cylinders for this disk is set to 34893.
  4. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with:
  5. 1) software that runs at boot time (e.g., old versions of LILO)
  6. 2) booting and partitioning software from other OSs
  7. (e.g., DOS FDISK, OS/2 FDISK)
  8.  
  9. Command (m for help): p
  10.  
  11. Disk /dev/sda: 287.0 GB, 287005343744 bytes
  12. 255 heads, 63 sectors/track, 34893 cylinders
  13. Units = cylinders of 16065 * 512 = 8225280 bytes
  14. Device Boot Start End Blocks Id System
  15. /dev/sda1 1 34893 280277991 83 Linux
  16.  
  17. Command (m for help): q