Linux磁盘分区与使用小结

linux下新磁盘分区以后才可使用,在关于使用MBR还是GPT分区这块儿,要明白MBR的局限性,它最多只支持四个主分区,另外还需要搞清楚主分区,扩展分区,逻辑分区它们之间的联系。我个人还是比较喜欢GPT这种分区方法不仅限制少分区表还有冗余。工具的话主要用到了fdisk和parted前者实现MBR分区后者实现GPT分区。

fdisk

1.首先查看系统版本,这里使用了一台阿里云的虚拟主机
[root@iZ2zebi66737fiqkkhxzzeZ ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

2.查看磁盘信息,可以看到新添加的磁盘vdb
[root@iZ2zebi66737fiqkkhxzzeZ ~]# fdisk -l

Disk /dev/vda: 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: 0x000b2d99

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    41943039    20970496   83  Linux

Disk /dev/vdb: 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

3.针对vdb使用fdisk进行MBR方式的分区
[root@iZ2zebi66737fiqkkhxzzeZ ~]# fdisk /dev/vdb
这块有几个重要的Command action的含义需要搞清楚!
n   add a new partition
p   print the partition table
d   delete a partition
w   write table to disk and exit
t   change a partition's system id
最终这块磁盘分了三个主分区,一个扩展分区,在扩展分区的基础上创建了一个逻辑分区
Command (m for help): p

Disk /dev/vdb: 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: 0x919935ad

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     4196351     2097152   83  Linux
/dev/vdb2         4196352     8390655     2097152   83  Linux
/dev/vdb3         8390656    12584959     2097152   83  Linux
/dev/vdb4        12584960    16779263     2097152    5  Extended
/dev/vdb5        12587008    14684159     1048576   83  Linux

4.让内核读取新的分区表
[root@iZ2zebi66737fiqkkhxzzeZ ~]# partprobe /dev/vdb

5.查看下,至此fdisk分区完毕
[root@iZ2zebi66737fiqkkhxzzeZ ~]# fdisk -l

Disk /dev/vda: 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: 0x000b2d99

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    41943039    20970496   83  Linux

Disk /dev/vdb: 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: 0x919935ad

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     4196351     2097152   83  Linux
/dev/vdb2         4196352     8390655     2097152   83  Linux
/dev/vdb3         8390656    12584959     2097152   83  Linux
/dev/vdb4        12584960    16779263     2097152    5  Extended
/dev/vdb5        12587008    14684159     1048576   83  Linux

parted

1.针对新添加的磁盘vdc,使用parted进行GPT方式的磁盘分区,首先是修改分区表格式
[root@iZ2zebi66737fiqkkhxzzeZ ~]# parted /dev/vdc mklabel gpt

2.查看vdc磁盘分区表信息
[root@iZ2zebi66737fiqkkhxzzeZ ~]# parted /dev/vdc print
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

3.针对vdc创建一个从1MB开始到2GB位置的2GB主分区
[root@iZ2zebi66737fiqkkhxzzeZ ~]# parted /dev/vdc mkpart primary ext4 1 2G
Information: You may need to update /etc/fstab.

4.针对vdc创建一个从第2G开始到第4GB结束的2GB主分区
[root@iZ2zebi66737fiqkkhxzzeZ ~]# parted /dev/vdc mkpart primary ext4 2G 4G
Information: You may need to update /etc/fstab.

5.查看此时vdc磁盘的分区表信息
[root@iZ2zebi66737fiqkkhxzzeZ ~]# parted /dev/vdc print
Model: Virtio Block Device (virtblk)
Disk /dev/vdc: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  2000MB  1999MB               primary
 2      2000MB  4000MB  2001MB               primary

6.如果需要删除Number为2的分区的话可以这么操作
[root@iZ2zebi66737fiqkkhxzzeZ ~]# parted /dev/vdc rm 2

格式化分区&&挂载使用文件系统

1.针对parted分好的vdc1先格式化成linux默认的xfs文件格式
[root@iZ2zebi66737fiqkkhxzzeZ ~]# mkfs.xfs /dev/vdc1

2.我一般会修改此文件来进行文件系统的挂载使用
[root@iZ2zebi66737fiqkkhxzzeZ data_for_www]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Jul 11 02:52:01 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=1114fe9e-2309-4580-b183-d778e6d97397 /                       ext4    defaults        1 1
/dev/vdc1 /usr/local/src/data_for_www xfs defaults 0 0
可以看到将其挂载到了/usr/local/src/data_for_www这个新创建的目录下,xfs是文件系统的类型,defaults是挂载的属性,第一个0表示不使用dump备份,第二个0表示不需要进行磁盘检测

你可能感兴趣的:(Linux磁盘分区与使用小结)