Linux中磁盘分区fdisk命令(简洁版)


 实例(环境为: CentOS Linux release 7.2.1511 (Core), 3.10.0-327.el7.x86_64)

选择要具体操作的第二块磁盘(linux下一切是文件形式对应):

[root@vathe ~]#fdisk /dev/sdb
[root@fp-web-130 ~]# lsblk 
NAME                                                                                        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                                                                                           8:0    0  100G  0 disk 
├─sda1                                                                                        8:1    0  500M  0 part /boot
└─sda2                                                                                        8:2    0 99.5G  0 part 
  ├─centos-root                                                                             253:0    0 95.5G  0 lvm  /
  └─centos-swap                                                                             253:1    0    4G  0 lvm  
sdb                                                                                           8:16   0  200G  0 disk 
sr0                                                                                          11:0    1 1024M  0 rom  
loop0                                                                                         7:0    0  100G  0 loop 
└─docker-253:0-268708167-pool                                                               253:2    0  100G  0 dm   
  ├─docker-253:0-268708167-edbba54df531d54ce62f43155c402a262fb51abfd1984c1ce3a765677fdd6256 253:3    0   10G  0 dm   /var/lib/docker/devicemapper/mnt/edbba54df531d54ce62f43155c402a262fb51abfd
loop1                                                                                         7:1    0    2G  0 loop 
└─docker-253:0-268708167-pool                                                               253:2    0  100G  0 dm   
  ├─docker-253:0-268708167-edbba54df531d54ce62f43155c402a262fb51abfd1984c1ce3a765677fdd6256 253:3    0   10G  0 dm   /var/lib/docker/devicemapper/mnt/edbba54df531d54ce62f43155c402a262fb51abfd

输入m可列出可以执行的命令:

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                  # 显示帮助菜单
   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   # 该表分区的系统id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit     # 保存分区并退出
   x   extra functionality (experts only)

查看分区表:

Command (m for help): p
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x48283ee0

   Device Boot      Start         End      Blocks   Id  System

新建一个主分区:

Command action                      #选择新增分区类型
   e   extended                     #扩展分区
   p   primary partition (1-4)      #主分区
Select (default p): p           // 键入p
Partition number (1-4): 1       // 键入1
First cylinder (1-1305, default 1): # 选择分区起点柱面 
Using default value 1       //选择默认1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +1G       #设置分区大小1G,+表示往起点后添加,-表示往起点前添加分区

查看主分区创建:

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes    # 磁盘总大小 10.7GB  
255 heads, 63 sectors/track, 1305 cylinders  # 255个磁头,每个磁道63个扇区,1305个圆柱面
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: 0x48283ee0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux

建立一个扩展分区

Command (m for help):n //输入n回车
  Partition type:
    p primary (0 primary, 0 extended, 4 free)
    e extended
  Select (default p): p #输入p表示创建主分区,回车
  Partition number (1-4): 1 #分区号为1,分区号是1到4,我们这里输入1,回车
  First sector (2048-419430399, default 2048): #直接回车默认从第一个柱面开始划分,默认分区起始位置是从2048开始
  Using default value 2048

  First sector (2048-419430399, default 2048): 2048
  Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): 69905066 //这里直接默认回车将
  全部都分配给当前创建的主分区,这里肯定不想,所以需要这里设置一个值,我用计算器419430399/6=69905066

  Partition 1 of type Linux and of size 33GiB is set

  Command (m for help): p //这时再次出现提示: 这里输入p,查看创建情况

  Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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 label type: dos
  Disk identifier: 0xbd1a05e2

  Device Boot Start End Blocks Id System
  /dev/sdb1 2048 69905066 34951509+ 83 Linux

  注: 每次操作完毕,就会回到 Command (m for help): 提示让你继续操作。

在扩展分区中创建一个逻辑分区

 

  Command (m for help):n
  Partition type:
         p primary (1 primary, 0 extended, 3 free)
    e extended
  Select (default p):e //输入e,表示要创建扩展分区 

  Partition number (2-4, default 2):2 //输入2回车,开始创建继续扩展分区

  First sector (69905067-419430399, default 69906432): //这里我直接回车

  Using default value 69906432: Last sector, +sectors or +size{K,M,G} (69906432-419430399, default 419430399): //这里也直接回车,表示把剩下的部分都划给扩展分区
  Using default value 419430399
  Partition 2 of type Extended and of size 166.7 GiB is set(这里翻译为分区2为扩展分区,大小为166.7G被设置)

查看分区列表


  Command (m for help): p   //出现提示,输入p查看当前创建情况


    Disk /dev/sdb: 214.7 GB, 214748364800 bytes, 419430400 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 label type: dos
    Disk identifier: 0xbd1a05e2


    Device    Boot Start    End       Blocks     Id System
    /dev/sdb1      2048     69905066  34951509+  83 Linux
    /dev/sdb2      69906432 419430399 174761984  5  Extended


保存退出

Command (m for help): w             # 保存退出
The partition table has been altered!

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

 分区成功,当然,要使用磁盘,还需要进行两部操作,才能使用磁盘——将磁盘挂载在具体目录(或文件),和对个分区进行格式化,最后结果如下。