【配置与安装】CentOS7 磁盘分区(主分区、扩展分区和逻辑分区)的创建、挂载与删除

创建磁盘分区

  • 查看磁盘分区情况:fdisk -l
[root@model ~]# fdisk -l                                                      //查看系统中所有磁盘的分区列表

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000da34b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      591871      294912   83  Linux
/dev/sda2          591872   189335551    94371840   83  Linux
/dev/sda3       189335552   209715199    10189824   8e  Linux LVM

Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 sectors             //sdb磁盘未分区,1099.5GB
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: 0x3238f946

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/mapper/centos-swap: 8334 MB, 8334082048 bytes, 16277504 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 65536 bytes / 65536 bytes
  • 创建主分区
[root@model ~]# 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.

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 (0 primary, 0 extended, 4 free)                  //主分区
   e   extended                                                 //扩展分区
Select (default p): p
Partition number (1-4, default 1): 1                            //选择分区号,范围为1~4(主分区和扩展分区加起来最多只能建立 4 个,而扩展分区最多只能建立 1 个),这里选择1
First sector (2048-2147483647, default 2048): 2048              //分区的起始扇面,默认从2048开始  
Last sector, +sectors or +size{K,M,G} (2048-2147483647, default 2147483647): +10G           //指定该磁盘分区的大小,默认占用整个磁盘;这里用 +size{K,M,G} 命令指定 10G
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): p                                         //打印当前磁盘的分区列表

Disk /dev/sdb: 1099.5 GB, 1099511627776 bytes, 2147483648 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: 0x3238f946

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux

Command (m for help): w                                          //将本次配置写入硬盘
The partition table has been altered!

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

[root@model ~]# lsblk                                            //lsblk 列表查看block分区
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 288M 0 part /boot
├─sda2 8:2 0 90G 0 part /
└─sda3 8:3 0 9.7G 0 part
├─centos-pool00_tmeta 253:0 0 8M 0 lvm
│ └─centos-pool00-tpool 253:2 0 7.8G 0 lvm
│ ├─centos-swap 253:3 0 7.8G 0 lvm [SWAP]
│ └─centos-pool00 253:4 0 7.8G 1 lvm
└─centos-pool00_tdata 253:1 0 7.8G 0 lvm
└─centos-pool00-tpool 253:2 0 7.8G 0 lvm
├─centos-swap 253:3 0 7.8G 0 lvm [SWAP]
└─centos-pool00 253:4 0 7.8G 1 lvm
sdb 8:16 0 1T 0 disk
└─sdb1 8:17 0 10G 0 part                                         //找到新建的分区/dev/sdb1
sr0 11:0 1 1024M 0 rom 

新建的分区还没有格式化和挂载,所以还不能使用。用上述方法依次创建2、3号主分区,并预留4号分区作为扩展分区。

  • 创建扩展分区

[root@model ~]# 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.

Command (m for help): n                                          //新建分区
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e                                            //选择扩展分区
Selected partition 4
First sector (62916608-2147483647, default 62916608):            //默认起始扇面
Using default value 62916608
Last sector, +sectors or +size{K,M,G} (62916608-2147483647, default 2147483647):       //默认将剩余全部空间分配给该扩展分区 
Using default value 2147483647
Partition 4 of type Extended and of size 994 GiB is set

Command (m for help): w                                          //将本次设置写入磁盘

The partition table has been altered!

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

这里把 /dev/sdb 硬盘的所有剩余空间都建立为扩展分区,也就是建立 3 个主分区,剩余空间都建立成扩展分区,再于扩展分区中建立逻辑分区。扩展分区是不能被格式化和直接使用的,所以还要在扩展分区内部再建立逻辑分区。 

  • 创建逻辑分区
[root@model ~]# 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.

Command (m for help): n                                           //新建分区
All primary partitions are in use
Adding logical partition 5                                        //所有主分区已分配完,默认新建逻辑分区,从分区号从 5 开始
First sector (62918656-2147483647, default 62918656):             //起始扇面
Using default value 62918656
Last sector, +sectors or +size{K,M,G} (62918656-2147483647, default 2147483647): +5G        //指定逻辑分区的大小为 5G
Partition 5 of type Linux and of size 5 GiB is setCommand (m for help): w                                           //保存设置
The partition table has been altered!

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

 所有分区过程中如果不保存并退出是不会生效的,所以建立错了也没有关系,使用 q 命令不保存退出即可。如果使用了 w 命令,就会保存退出。有时因为系统的分区表正忙,所以需要重新启动系统才能使新的分区表生效。

  • 格式化分区
[root@model ~]# mkfs.xfs /dev/sdb1                                //将分区/dev/sdb1的文件类型格式化为xfs
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

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

------------- 修改磁盘分区的文件类型 -----------------

[root@model ~]# blkid /dev/sdb6                                   //查看 /dev/sdb6 的 UUID 和 文件类型
/dev/sdb6: UUID="89f4f8e4-0592-4e91-9064-7388152fec32" TYPE="xfs"                             
[root@model ~]# mkfs.ext4 /dev/sdb6                               //将逻辑分区的文件类型从 xfs 修改为 ext4, 如果是从 ext4 修改为 xfs 需要在 命令后加 ‘-f’ 强制修改
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

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

[root@model ~]# blkid /dev/sdb6
/dev/sdb6: UUID="846373a4-35b0-4988-9574-a508da3906f2" TYPE="ext4" 

挂载磁盘分区 

[root@model ~]# mount /dev/sdb1 /usr/exdata1                       //将 /dev/sdb1 磁盘分区挂载到新建的挂载点 /usr/exdata1 上
[root@model ~]# df -T -h  | grep /dev/sdb1                         //查看 该分区是否已挂载
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/sdb1      ext4      9.8G   37M  9.2G   1% /usr/exdata1

[root@model ~]# vim /etc/fstab                                     //修改fstab配置文件,将该磁盘分区设置为开机自动挂载
 # Add command by Kay.sun in 2020/9/29 16:58                       //在配置文件中添加一行命令
 /dev/sdb1 /usr/exdata1 xfs defaults 1 2                           // /dev/sdb1 选择一个分区;xfs 是当前分区的格式; defaults = rw, suid, dev, exec, auto, nouser, and async; 1 需要 dump;0 不需要 dump; 2 开机时检查顺序,非 boot 文件系统为 1,其它文件系统都为 2
[root@model ~]# df -h | grep /dev/sdb1                             //重启后再次查看分区挂载情况,发现已经自动挂载~]# umount /usr/

删除磁盘分区

[root@model ~]# 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.

Command (m for help): d                                         //删除分区
Partition number (1-6, default 6): 6                            //选择待删除的分区号
Partition 6 is deleted

Command (m for help): w                                         //保存设置
The partition table has been altered!

Calling ioctl() to re-read partition table.
原文地址:https://www.cnblogs.com/SchrodingerDoggy/p/13746237.html