Fdisk or parted

From Notes_Wiki

Home > CentOS > CentOS 6.x > Filesystem or partition tools > Fdisk or parted

Updated articles are available at CentOS 8.x 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

Updated articles are available at CentOS 8.x 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
print 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 exactly same partitions

Making exactly same partitions may be desired for use in software raid arrays.

If there are only primary partitions then the simplest way is to use something like:

dd if=/dev/sda of=partition.img bs=512 count=1
dd if=partition.img of=/dev/sdb

However if there are extended partitions 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.


Creating partitions and file-systems greater than 2.0TB in size

Normally if partitions are created using fdisk, it uses msdos partition table which does not allows size of a single partition to be greater than 2.0TB. To support partitions greater than 2.0TB gpt partition tables should be used.

Steps for creating a gpt partition table, creating a partition greater than 2.0TB and then installing a file-system such as ext4 or xfs on it are as follows:

  1. Use 'dd if=/dev/zero of=/dev/<device> bs=512 count=1' to remove all existing partition information from the given device. Note that this cannot be undone and all data on drive would be lost. Verify that the device name you specify is correct. If accidentally name of wrong device is typed then data on important drive may be lost.
  2. Configure disk to have gpt partition table using:
    parted /dev/<device>
    gpt
    quit
    Note that it is important to quit parted for 'gpt' command to take effect.
  3. Create a partition of desired size after looking at free space information
    print free
    mkpart primary xfs <start> <end>
    print
    quit
    Start and end can be specified as 1GB, 300GB etc. Ending on proper boundaries can be useful. gpt would warn in case the value chosen is not acceptable.
  4. Format created partition with ext4 or xfs. Again writing wrong device name would cause some other filesystem to get formatted, potentially causing data loss.
    mkfs.xfs /dev/<partition>
  5. Test the partition by mounting and checking free space
    mkdir /mnt/test
    mount /dev/<partition> /mnt/test
    df -h
    umount /mnt/test
    rmdir /mnt/test


Home > CentOS > CentOS 6.x > Filesystem or partition tools > Fdisk or parted