Fdisk or parted
fdisk or parted
fdisk and/or parted can be used to manipulate partition tables. They can help in at least following:
- Intializing new raw disk with proper partition tables (fdisk)
- Creating new partitions (fdisk, parted)
- Show current partition table in various units (fdisk -> cylinders, sectors, parted -> sectors, mb)
- Delete partitions (fdisk, parted)
- Change filesystem type (fdisk)
- Make partition bootable (fdisk)
fdisk
listing hard-disks
We can use
fdisk -l
command to list all hard-disks on system and their partition information. This also lists raid devices like /dev/md0, /dev/md1 etc. as hard-disks. This will also list modern pen drives.
Editing partition table
We can edit partition table using
fdisk /dev/sda
command. Here '/dev/sda' should be replaced with appropriate disk. fdisk provides menu based on user choice and also supports 'm' for help. We can see help and then choose appropriate option to achieve desired output. Advanced options like change of unit are available in advanced menu.
Seeing exact partition size
We can use
fdisk -s /dev/sda<n>
to see exact size of partition /dev/sda<n> in terms of sectors.
parted
We can use parted when we want size of partitions in MB of existing device and then we want to create partition with exactly same size on other device. This is normally required when using software raid to create raid partitions of exactly same size.
parted gives an interactive shell and we can use help command to see various commands supported by parted. Some important ones are listed below:
Command | Description |
select /dev/sda | Can be used to select hard-disk on which we want to work |
unit {kb or mb or gb} | Can be used to specify unit using which partition information should be printed |
Prints partition information of currently selected device | |
mkpart {primary or secondary} <start> <end> | Can be used to create partition of exact size. Units are by default in MB but we can use other units also. |
Making exact partitions
If we need to make exact partitions for use in software raid then we can use fdisk -s option to see size of partition and then while creating partition we can specify size in sectors using +<sectors> option. This may sometimes require entering <sector> as double of what is given in fdisk -s output.
Sometimes fdisk does not allows creating partition by specifying sectors. In that case instead of fdisk we can try to use parted. With parted first select source device with 'select /dev/sda'. Then set unit to mb using 'unit mb'. Then use 'print' to print partitions. Then select the new device which should be partitioned same as source using 'select /dev/sdb'. Use 'print' to verify that it is not raw and some partition table exists. If hard-disk is raw then open it using 'fdisk /dev/sdb' and use 'w','x' to write and exit. This would write emtpy partition table on hard-disk. Now in parted we can create partitions using 'mkpart primary <start> <end>' where start and end are in MB which we know from print of source device. After creating partitions open hard-disk using fdisk to set filesystem ID, bootable flags, etc.
If fdisk -l output lists sectors which end with + such as 25646436+ then it is possible that both sectors and megabyte based partitioning mentioned above does not work. In that case try to use fdisk to create partitions based on cylinders.
At least one among fdisk and parted should be able to help with creating partitions with desired properties.