fdisk:磁盘分区工具

xiaohai 2021-07-31 21:21:01 3285人围观 标签: Linux 
简介fdisk命令是Liunx下的磁盘的分区工具。受mbr分区表的限制,fdisk只能给小于2TB的磁盘划分分区。如果使用fdisk对大于2TB的磁盘分区,虽然可以,但是仅仅只会识别2TB的空间,所以当磁盘容量超过2TB,就要使用parted分区工具进行分区。
说明

  fdisk命令是Liunx下的磁盘的分区工具。受mbr分区表的限制,fdisk只能给小于2TB的磁盘划分分区。如果使用fdisk对大于2TB的磁盘分区,虽然可以,但是仅仅只会识别2TB的空间,所以当磁盘容量超过2TB,就要使用parted分区工具进行分区。

格式

  fdisk [option] [device]

常用命令
  • -l:显示所有磁盘分区的信息
示例

1、显示磁盘分区列表信息

[root@localhost ~]# fdisk -l #查看当前系统所有磁盘的分区信息

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors #磁盘/dev/sha的大小
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: 0x000a3468

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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 /dev/mapper/centos-root: 18.8 GB, 18798870528 bytes, 36716544 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

说明:

  • Device:分区,这里有三个分区
  • Boot:启动分区,用*表示是启动分区
  • Start:表示开始的柱面
  • End:表示结束的柱面
  • Blocks:block的块数量
  • Id:分区类型ID
  • System:分区类型

2、在虚拟机上模拟磁盘分区
准备工作,在对应的虚拟机上,新增一块磁盘,要求先关闭虚拟机,操作步骤,选择需要扩展磁盘的虚拟机—>硬盘—>添加—>选择硬盘—>下一步—>默认下一步—>默认下一步—>输入1—>下一步—>完成—>保存,然后启动虚拟机。

查看刚刚新添加的硬盘

[root@localhost ~]# fdisk -l #显示分区信息

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000a3468

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 sectors #这里是刚刚添加的硬盘名为sdb,是第二块硬盘
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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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 /dev/mapper/centos-root: 18.8 GB, 18798870528 bytes, 36716544 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

可以指定查看特定分区的信息

[root@localhost ~]# fdisk -l /dev/sdb#显示分区信息
Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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

交互式分区操作

[root@localhost ~]# fdisk /dev/sdb #不加参数,直接跟设备名就可以进行分区
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x50a972d4.

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
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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)

Command (m for help): n #创建一个分区,需要输入n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p #创建一个主分区
Partition number (1-4, default 1): 1 #设置分区的编号为1,默认也为1 
First sector (2048-2097151, default 2048):  #设置起始柱面,直接回车即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +100M #设置结束柱面或分区大小,因为需要划分指定的大小的分区,所以常用+100M这种方法,如果分区使用的fdisk -cu /dev/sdb,这里就会使用扇区为单位来进行分区
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): p #打印分过的分区表

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x50a972d4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux

Command (m for help): n #再建一个新的分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e #创建一个扩展分区
Partition number (2-4, default 2): 2 #分区编号为2
First sector (206848-2097151, default 206848):  #回车即可
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-2097151, default 2097151): #回车即可
Using default value 2097151
Partition 2 of type Extended and of size 923 MiB is set

Command (m for help): p #打印分区信息

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x50a972d4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended

Command (m for help): n #再建一个分区
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): p #创建一个主分区
Partition number (3,4, default 3): 
No free sectors available #不能再创建主分区,没有磁盘空间了

Command (m for help): n #在新建一个分区
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l #新建一个逻辑分区,逻辑分区是在扩展分区的基础上建的
Adding logical partition 5
First sector (208896-2097151, default 208896): #回车即可
Using default value 208896
Last sector, +sectors or +size{K,M,G} (208896-2097151, default 2097151): +400M #设置分区大小为400M
Partition 5 of type Linux and of size 400 MiB is set

Command (m for help): p #打印分区信息

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x50a972d4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended
/dev/sdb5          208896     1028095      409600   83  Linux

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
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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)

Command (m for help): n #在新建一个分区
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l #在新建一个逻辑分区
Adding logical partition 6
First sector (1030144-2097151, default 1030144): 
Using default value 1030144
Last sector, +sectors or +size{K,M,G} (1030144-2097151, default 2097151): 
Using default value 2097151
Partition 6 of type Linux and of size 521 MiB is set

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x50a972d4

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      206847      102400   83  Linux
/dev/sdb2          206848     2097151      945152    5  Extended
/dev/sdb5          208896     1028095      409600   83  Linux
/dev/sdb6         1030144     2097151      533504   83  Linux

Command (m for help): w #将操作写入分区表生效并退出程序
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# ll /dev/sd* #查看分区后的设备的状态
brw-rw----. 1 root disk 8,  0 Jul  2 03:08 /dev/sda
brw-rw----. 1 root disk 8,  1 Jul  2 03:08 /dev/sda1
brw-rw----. 1 root disk 8,  2 Jul  2 03:08 /dev/sda2
brw-rw----. 1 root disk 8, 16 Jul  2 03:17 /dev/sdb
brw-rw----. 1 root disk 8, 17 Jul  2 03:17 /dev/sdb1
brw-rw----. 1 root disk 8, 18 Jul  2 03:17 /dev/sdb2
brw-rw----. 1 root disk 8, 21 Jul  2 03:17 /dev/sdb5
brw-rw----. 1 root disk 8, 22 Jul  2 03:17 /dev/sdb6
[root@localhost ~]# partprobe /dev/sdb #执行该命令通知内核分区表已更改,此步是不重启让分区表生效的命令

格式化磁盘

[root@localhost ~]# mkfs.ext4 /dev/sdb1  #只有格式化后的磁盘才能挂载到系统中使用
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33685504
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@localhost ~]# tune2fs -c -1 /dev/sdb1  #执行这个命令可以避免磁盘挂载自动检查磁盘
tune2fs 1.42.9 (28-Dec-2013)
Setting maximal mount count to -1

挂载磁盘分区

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G   11G  7.5G  58% /
devtmpfs                 481M     0  481M   0% /dev
tmpfs                    490M     0  490M   0% /dev/shm
tmpfs                    490M  6.7M  484M   2% /run
tmpfs                    490M     0  490M   0% /sys/fs/cgroup
/dev/sda1                497M  115M  383M  24% /boot
[root@localhost ~]# mount /dev/sdb1 /mnt #磁盘分区只有挂载了才能使用
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G   11G  7.5G  58% /
devtmpfs                 481M     0  481M   0% /dev
tmpfs                    490M     0  490M   0% /dev/shm
tmpfs                    490M  6.7M  484M   2% /run
tmpfs                    490M     0  490M   0% /sys/fs/cgroup
/dev/sda1                497M  115M  383M  24% /boot
/dev/sdb1                 93M  1.6M   85M   2% /mnt

开机启动挂载磁盘

#方法1,在/etc/rc.local最后一行加上
mount /dev/sdb1 /mnt

#方法2、在/etc/fstab最后一行加上
/dev/sdb1 /mnt ext4 defaults 0 0

非交互式分区

这里不详细说明,大致的操作就是将以上的输入步骤写入到一个文本文件,然后读文本文件执行