fdisk

View disk usage and disk partitions

Supplementary instructions

fdisk command is used to observe the physical usage of the hard disk and also partition the hard disk. It uses a traditional question-and-answer interface instead of the cfdisk interactive operation interface similar to DOS fdisk, so it is more inconvenient to use, but its functionality is not compromised at all.

grammar

fdisk [options] <disk> change partition table
fdisk [options] -l [<disk>...] List partition table

Options


Options:
  -b, --sectors-size <size> Display sector count and size
  -B, --protect-boot don't erase bootbits when creating new tab
  -c, --compatibility[=<mode>] mode, "dos" or "nondos" (default)
  -L, --color[=<timing>] Color output (auto, always or never) enable color by default
  -l, --list show partitions and exit
  -x, --list-details Like --list but provide more details
  -n, --noauto-pt do not create the default partition table on an empty device
  -o, --output <list> output columns
  -t, --type <type> Only recognize the specified partition table type
  -u, --units[=<units>] Display units, "cylinders" cylinders or "sectors" sectors (default)
  -s, --getsz Display device size in 512-byte sectors [Deprecated]
       -b, --bytes print SIZE in bytes rather than in a human-readable format
       --lock[=<mode>] Use exclusive device lock (yes, no, or nonblock)
  -w, --wipe <mode> Wipe signature (auto, always or never)
  -W, --wipe-partitions <mode> Wipe signatures of new partitions (auto, always or never)

  -C, --cylinders <number> Specify the number of cylinders
  -H, --heads <number> Specify the number of heads
  -S, --sectors <number> Specify the number of sectors per track

  -h, --help show this help
  -V, --version show version

Parameters

Device file: Specify the hard disk device file to be partitioned or displayed.

Example

First select the disk you want to operate on:

[root@localhost ~]# fdisk /dev/sdb

Type m to list executable commands:

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
    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)

Enter p to list the current partition status of the disk:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
/dev/sdb1 1 1 8001 8e Linux LVM
/dev/sdb2 2 26 200812+ 83 Linux

Delete existing partitions by typing d and then selecting the partition:

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

Check the partition status and confirm that the partition has been deleted:

Command (m for help): print

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System

Command (m for help):

Enter n to create a new disk partition. First create two primary disk partitions:

Command (m for help): n
Command action
    eextended
    p primary partition (1-4)
p //Create primary partition
Partition number (1-4): 1 //Partition number
First cylinder (1-391, default 1): //Partition starting position
Using default value 1
last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100 //End position of partition, unit is sector

Command (m for help): n //Create another partition
Command action
    eextended
    p primary partition (1-4)
p
Partition number (1-4): 2 //The partition number is 2
First cylinder (101-391, default 101):
Using default value 101
Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M //End position of partition, unit is M

Confirm that the partition is created successfully:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
/dev/sdb1 1 100 803218+ 83 Linux
/dev/sdb2 101 125 200812+ 83 Linux

Create another logical partition:

Command (m for help): n
Command action
    eextended
    p primary partition (1-4)
e //Select extended partition
Partition number (1-4): 3
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
Using default value 391

Confirm that the extended partition is created successfully:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
/dev/sdb1 1 100 803218+ 83 Linux
/dev/sdb2 101 125 200812+ 83 Linux
/dev/sdb3 126 391 2136645 5 Extended

Create two logical partitions on the extended partition:

Command (m for help): n
Command action
    l logical (5 or over)
    p primary partition (1-4)
l //Select logical partition
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M

Command (m for help): n
Command action
    l logical (5 or over)
    p primary partition (1-4)
l
First cylinder (176-391, default 176):
Using default value 176
Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
Using default value 391

Confirm that the logical partition is created successfully:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

    Device Boot Start End Blocks Id System
/dev/sdb1 1 100 803218+ 83 Linux
/dev/sdb2 101 125 200812+ 83 Linux
/dev/sdb3 126 391 2136645 5 Extended
/dev/sdb5 126 175 401593+ 83 Linux
/dev/sdb6 176 391 1734988+ 83 Linux

Command (m for help):

From the above results, we can see that we have created 2 primary partitions (sdb1, sdb2), 1 extended partition (sdb3), and 2 logical partitions (sdb5, sdb6) on the hard disk sdb.

Note: The disk numbers of the primary partition and extended partition are 1-4, which means there are at most 4 primary partitions or extended partitions. The disk number starting from the logical partition is 5, so there is no sdb4 in this experiment.

Finally, save the partition operation:

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

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

After creating the partition, we still need to format the partition before we can use the disk in the system.

Create ext2 partition on sdb1:

[root@localhost ~]# mkfs.ext2 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
100576 inodes, 200804 blocks
10040 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=209715200
7 block groups
32768 blocks per group, 32768 fragments per group
14368 inodes per group
Superblock backups stored on blocks:
         32768, 98304, 163840

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

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

Create an ext3 partition on sdb6:

[root@localhost ~]# mkfs.ext3 /dev/sdb6
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
217280 inodes, 433747 blocks
21687 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=444596224
14 block groups
32768 blocks per group, 32768 fragments per group
15520 inodes per group
Superblock backups stored on blocks:
         32768, 98304, 163840, 229376, 294912

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

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#

Create two directories /oracle and /web, and mount the two newly created partitions to the system:

[root@localhost ~]# mkdir /oracle
[root@localhost ~]# mkdir /web
[root@localhost ~]# mount /dev/sdb1 /oracle
[root@localhost ~]# mount /dev/sdb6 /web

Check the partition mounting status:

[root@localhost ~]# df -h
File System Capacity Used Available % Used Mount Points
/dev/mapper/VolGroup00-LogVol00
                       6.7G 2.8G 3.6G 44% /
/dev/sda1 99M 12M 82M 13% /boot
tmpfs 125M 0 125M 0% /dev/shm
/dev/sdb1 773M 808K 733M 1% /oracle
/dev/sdb6 1.7G 35M 1.6G 3% /web

If you need to automatically mount every time you boot, you need to modify the /etc/fstab file and add two lines of configuration:

[root@localhost ~]# vim /etc/fstab

/dev/VolGroup00/LogVol00/ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/sdb1 /oracle ext2 defaults 0 0
/dev/sdb6 /web ext3 defaults 0 0