Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

USEFUL LINUX FDISK COMMAND WITH EXAMPLES – A LINUX DISK PARTITION TOOL

USEFUL LINUX FDISK COMMAND WITH EXAMPLES - A LINUX DISK PARTITION TOOL

USEFUL LINUX FDISK Command WITH EXAMPLES – A LINUX DISK Partition TOOL

Introduction

In this section we are going to learn How to use Linux Fdisk command. Linux fdisk command is also known as Fixed Disk is a very powerful command is used for disk partition. fdisk was first introduced by IBM on year 1983. Let’s have a look at some very useful and important Linux fdisk command with examples.

Linux fdisk command with examples :

To check manual or help page of linux fdisk command tool use fdisk command with argument -h.

[[email protected] ~]# fdisk -h  # Checking help page of fdisk

Usage:
 fdisk [options]     change partition table
 fdisk [options] -l  list partition table(s)
 fdisk -s       give partition size(s) in blocks

Options:
 -b                  sector size (512, 1024, 2048 or 4096)
 -c                        switch off DOS-compatible mode
 -h                        print help
 -u                  give sizes in sectors instead of cylinders
 -v                        print version
 -C                specify the number of cylinders
 -H                specify the number of heads
 -S                specify the number of sectors per track

To check Version of installed fdisk command package version we can use fdisk command with argument -v.

[[email protected] ~]# fdisk -v   # Checking fdisk package Verison
fdisk (util-linux-ng 2.17.2)

fdisk command with option -s will return you the size of the partition in Blocks.

[[email protected] ~]# fdisk -s /dev/sda1  # Check the Partition Size in Blocks
307200

To list the disk Partition Table use the fdisk command with argument -l. By running this command you will able to get below Disk Informations :

  • Disk Cylinder Details
  • Sector & Track details
  • Type of Partition (eg: Swap Partition, LVM Partition, RAID Partition…etc.)
  • Block Details
[[email protected] ~]# fdisk -l  # List Partition Table

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00088ea1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2350    18566144   83  Linux
/dev/sda3            2350        2611     2097152   82  Linux swap / Solaris

To List details of a Particular Partition using fdisk command refer the below command.

[[email protected] ~]# fdisk -l /dev/sda1  # Checking Details of a Particular Partition

Disk /dev/sda1: 314 MB, 314572800 bytes
255 heads, 63 sectors/track, 38 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Also Read :

  • BEST LINUX GREP COMMAND EXAMPLES
  • COMPLETE UNIX COMMANDS AND BASIC LINUX COMMANDS WITH EXAMPLES FOR BEGINNERS
  • BEST ZIP COMMAND WITH EXAMPLES IN LINUX

As we know that fdisk command is used for disk partition. We have to look at so many most useful commands that we have during disk partition creation. So here I am creating a partition.

[[email protected] ~]# fdisk /dev/sdb  # To create a Partition
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x720b712d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): # Here we have so many options to Learn for that enter "m" for Help

So refer the below command where I listed the Partition creation options (Highlighted in Yellow color).

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x720b712d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m   # Enter "m" for Options
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)

Now let’s create a Partition so that we can learn and understand all above disk partition options.

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa83d36ba.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n ---> Enter "n" to create a New Partition

# Here we have two options i.e. Primary and Extended, So enter "p" if you want to create a Primary Partition & enter "e"
if you want to create extended partition.

Command action
   e   extended
   p   primary partition (1-4)
p   ----> Here I am creating Primary Partition
Partition number (1-4): 1   # Enter Number for Primary Partition. We can create only 4 Primary Partition/Disk
First cylinder (1-391, default 1): # Press ENTER to allow the system to take cylinder automatically
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-391, default 391): +2G   # Enter Space for the Partition

Command (m for help): w   # Enter "w" to Save the Partition Table
The partition table has been altered!

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

After creating the Partition format the partition using below command.

[[email protected] ~]# mkfs.ext3 /dev/sdb1   # Formatting the Partition
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

# Now create a Directory and Mount the Partition

[[email protected] ~]# mkdir /data
[[email protected] ~]# mount /dev/sdb1 /data/   # Mounting the Partition
[[email protected] ~]# df -h /dev/sdb1   # Confirming the Mounted Device
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       2.0G   68M  1.9G   4% /data

Option “p” allows us to Print the Partition Table.

[[email protected] ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p   # Enter the "p" to print the Partition Table

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa83d36ba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux

Option “t” allows us to Change the partition ID. For example if you want configure LVM or Software RAID then for that first you have to change the partition ID like Partition ID for LVM is 8e and Partition ID for RAID is fd. To list more Partition ID’s for different need’s press “L”.

[[email protected] ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): t    # To change the Partition ID
Selected partition 1
Hex code (type L to list codes): L   # To List the available Partition ID's
0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16

Option “q” is for quit the Partition creation without saving anything.

[[email protected] ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): q  ---> To quit the Partition Creation

Option “d” is to delete the Partition.

[[email protected] ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): d   # Enter "d" to delete the Partition
Selected partition 1

Command (m for help): w   # Enter "w" to Save the Partition Table
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Option “a” is for enable or disable bootable flag in partition. You have to enable bootflag for a partition if you have installed the GRUB in root Partition instead of MBR. Otherwise the system will not boot. But if you installed the GRUB on MBR then there is no need of Bootflag.

[[email protected] ~]# fdisk /dev/sdb

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): a   # To enable or disable Bootable Flag on Partition
Partition number (1-4): 1

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

# After Enable the Bootflag on Partition you will notice a * on the Partition (Highlighted in Red Color).

[[email protected] ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa83d36ba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1         262     2104483+  83  Linux

For more advance options like change the Cylinder number, change the number of head, fix partition order..etc…you can use the option “x”. This option is for Experts. All options are listed below.

[[email protected] ~]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): x   # For advance disk related Operations

Expert command (m for help): m   # Enter "m" to List Advance disk Options
Command action
b move beginning of data in a partition
c change number of cylinders
d print the raw data in the partition table
e list extended partitions
f fix partition order
g create an IRIX (SGI) partition table
h change number of heads
i change the disk identifier
m print this menu
p print the partition table
q quit without saving changes
r return to main menu
s change number of sectors/track
v verify the partition table
w write table to disk and exit

To Print the size of the partition in Sectors instead of Cylinder we can use fdisk command with option -u.

[[email protected] ~]# fdisk -u -l /dev/sda1   # To print the Partition ID in Sectors instead of Cylinder

Disk /dev/sda1: 314 MB, 314572800 bytes
255 heads, 63 sectors/track, 38 cylinders, total 614400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

For more fdisk command related commands and options you can use below command.

[[email protected] ~]# man fdisk  # For more fdisk command related options

We tried to include as possible linux fdisk command, If you something missed out then please comment on comment box below, So that we can include that in the article.

If you found this article useful then Like Us, Share Us, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.

The post USEFUL LINUX FDISK COMMAND WITH EXAMPLES – A LINUX DISK PARTITION TOOL appeared first on Elinuxbook: Linux Tutorials, Guides, Howtos, Tips and Tricks.



This post first appeared on Elinuxbook: Linux Tutorials, Guides, Howtos, Tips And Tricks, please read the originial post: here

Share the post

USEFUL LINUX FDISK COMMAND WITH EXAMPLES – A LINUX DISK PARTITION TOOL

×

Subscribe to Elinuxbook: Linux Tutorials, Guides, Howtos, Tips And Tricks

Get updates delivered right to your inbox!

Thank you for your subscription

×