磁盘分区格式化

磁盘分区

 ➜  ~ fdisk /dev/sdc
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 0x5afcbee2.

Command (m for help): p # 打印磁盘/dev/sdc分区信息

Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 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: 0x5afcbee2

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n # 创建第一个分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): # 第一个分区默认选择主分区
Using default response p
Partition number (1-4, default 1): # 第一个分区默认编号为1
First sector (2048-62914559, default 2048): 
Using default value 2048 # 第一个分区的起始扇区编号默认为2048
Last sector, +sectors or +size{K,M,G} (2048-62914559, default 62914559): +4G # 设置第一个分区的大小为4G(如果默认,该分区默认会使用整个磁盘)
Partition 1 of type Linux and of size 4 GiB is set

Command (m for help): n # 创建第二个分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): # 第二个分区默认选择主分区
Using default response p
Partition number (2-4, default 2): # 第二个分区默认编号为2
First sector (8390656-62914559, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-62914559, default 62914559): +6G # 设置第二个分区的大小为6G
Partition 2 of type Linux and of size 6 GiB is set

Command (m for help): n # 创建第三个分区
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): # 第三个分区默认选择主分区
Using default response p
Partition number (3,4, default 3): # 第三个分区默认编号为3
First sector (20973568-62914559, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559): +8G # 设置第三个分区的大小为8G
Partition 3 of type Linux and of size 8 GiB is set

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 (37750784-62914559, default 37750784):
Using default value 37750784
Last sector, +sectors or +size{K,M,G} (37750784-62914559, default 62914559):
Using default value 62914559
Partition 4 of type Extended and of size 12 GiB is set
# 后面的分区都是逻辑分区
Command (m for help): n # 创建第五个分区
All primary partitions are in use
Adding logical partition 5
First sector (37752832-62914559, default 37752832):
Using default value 37752832
Last sector, +sectors or +size{K,M,G} (37752832-62914559, default 62914559): +8G
Partition 5 of type Linux and of size 8 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (54532096-62914559, default 54532096):
Using default value 54532096
Last sector, +sectors or +size{K,M,G} (54532096-62914559, default 62914559):
Using default value 62914559
Partition 6 of type Linux and of size 4 GiB is set

Command (m for help): p

Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 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: 0x5afcbee2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     8390655     4194304   83  Linux
/dev/sdc2         8390656    20973567     6291456   83  Linux
/dev/sdc3        20973568    37750783     8388608   83  Linux
/dev/sdc4        37750784    62914559    12581888    5  Extended
/dev/sdc5        37752832    54530047     8388608   83  Linux
/dev/sdc6        54532096    62914559     4191232   83  Linux

Command (m for help): t
Partition number (1-6, default 6): 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): t
Partition number (1-6, default 6): 5
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): t
Partition number (1-6, default 6):
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 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: 0x5afcbee2

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     8390655     4194304   82  Linux swap / Solaris
/dev/sdc2         8390656    20973567     6291456   83  Linux
/dev/sdc3        20973568    37750783     8388608   83  Linux
/dev/sdc4        37750784    62914559    12581888    5  Extended
/dev/sdc5        37752832    54530047     8388608   8e  Linux LVM
/dev/sdc6        54532096    62914559     4191232   8e  Linux LVM

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

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

最终在磁盘/dev/sdc上创建了六个分区,可用分区有五个sdc1、sdc2、sdc3、sdc5、sdc6,sdc4作为一个扩展分区,sda4的Start和End之间的扇区被sdc5、sdc6使用。

 ➜  ~ fdisk -l | grep "/dev/sdc"
Disk /dev/sdc: 32.2 GB, 32212254720 bytes, 62914560 sectors
/dev/sdc1            2048     8390655     4194304   82  Linux swap / Solaris
/dev/sdc2         8390656    20973567     6291456   83  Linux
/dev/sdc3        20973568    37750783     8388608   83  Linux
/dev/sdc4        37750784    62914559    12581888    5  Extended
/dev/sdc5        37752832    54530047     8388608   8e  Linux LVM
/dev/sdc6        54532096    62914559     4191232   8e  Linux LVM

格式化分区

  • sdc1: 分区类型为Linux swap / Solaris,将其格式化为交换分区,mkswap /dev/sdc1
  • sdc2: 分区类型为Linux,将其格式化为常用的ext4文件系统,mkfs.ext4 /dev/sdc2
  • sdc3: 分区类型为Linux,将其格式化为常用的xfs文件系统,mkfs.xfs /dev/sdc3
  • sdc5、sdc6: 分区类型为Linux LVM,可以将其创建为PV,pvcreate /dev/sdc5 pvcreate /dev/sdc6

挂载

接着上一步,现在将格式化的分区挂载到文件

创建对应目录作为挂载点

mkdir /my-xfs-fs /my-ext4-fs

使用mount命令进行挂载

mount /dev/sdc2 /my-ext4-fs
mount /dev/sdc3 /my-xfs-fs

使用命令df -hT可以看到/dev/sdc2 /dev/sdc3已分别挂载到了目录/my-ext4-fs /my-xfs-fs

 ➜  ~ df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  898M     0  898M   0% /dev
tmpfs                   tmpfs     910M     0  910M   0% /dev/shm
tmpfs                   tmpfs     910M   13M  897M   2% /run
tmpfs                   tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        57G  3.5G   54G   7% /
/dev/sda1               xfs      1014M  195M  820M  20% /boot
tmpfs                   tmpfs     182M     0  182M   0% /run/user/1000
/dev/sdc2               ext4      5.8G  507M  5.0G  10% /my-ext4-fs
/dev/sdc3               xfs       8.0G   33M  8.0G   1% /my-xfs-fs

直接在终端通过mount无法实现系统启动自动挂载,所以需要将挂载配置写入到配置文件/etc/fstab,这样才能在启动系统的时候自动挂载。

首先备份配置文件/etc/fstab

cp /etc/fstab /etc/fstab.bak

将挂载信息写入到配置文件中

echo '/dev/sdc2 /my-ext4-fs ext4 defaults 0 0
/dev/sdc3 /my-xfs-fs xfs defaults 0 0' >> /etc/fstab

建议在/etc/fstab中使用全局唯一标识符UUID来引用新分区,可以使用blkid命令获得新分区的UUID

echo "`blkid /dev/sdc2 | awk '{print $2}' | sed 's|"||g'` /my-ext4-fs ext4 defaults 0 0
`blkid /dev/sdc3 | awk '{print $2}' | sed 's|"||g'` /my-xfs-fs xfs defaults 0 0" >> /etc/fstab

LVM管理分区

创建PV

 ➜  ~ pvcreate /dev/sdc5
  Physical volume "/dev/sdc5" successfully created.

查看创建好的PV

 ➜  ~ pvdisplay /dev/sdc5
  --- Physical volume ---
  PV Name               /dev/sdc5
  VG Name               myvg
  PV Size               8.00 GiB / not usable 4.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              2047
  Free PE               2047
  Allocated PE          0
  PV UUID               gHzz11-nLHQ-r91b-x147-83h9-5A3L-GgOoN7

创建VG

 ➜  ~ vgcreate myvg /dev/sdc5
  Volume group "myvg" successfully created

查看创建好的VG

 ➜  ~ vgdisplay myvg
  --- Volume group ---
  VG Name               myvg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <8.00 GiB
  PE Size               4.00 MiB
  Total PE              2047
  Alloc PE / Size       0 / 0
  Free  PE / Size       2047 / <8.00 GiB
  VG UUID               l7UyP6-0teT-C1Jh-DfW9-228B-kDQP-f5yN4k

创建LV

分配所有VG空间到LV

lvcreate -n mylv -l 100%VG myvg

分配指定的VG空间到LV

lvcreate -n mylv -L 6G myvg
 ➜  ~ mkfs.xfs /dev/mapper/myvg-mylv

创建一个目录,作为挂载点

 ➜  ~ mkdir /mylv-mount-point

将挂载配置写入到配置文件中,实现系统启动自动挂载

cp /etc/fstab /etc/fstab.bak
echo `blkid /dev/mapper/myvg-mylv | awk '{print $2}' | sed 's|"||g'` /mylv-mount-point xfs defaults 0 0 >> /etc/fstab

LV扩容

在LV空间快要用完或者已经用完的时候,可以对LV进行扩容。

我们对上一步创建的LV mylv进行扩容

首先查看当前文件系统使用情况,可以看到/dev/mapper/myvg-mylv大小为8G,接下来我们用/dev/sdc6这块逻辑分区对LV mylv进行扩容。

 ➜  ~ df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  898M     0  898M   0% /dev
tmpfs                   tmpfs     910M     0  910M   0% /dev/shm
tmpfs                   tmpfs     910M  9.6M  901M   2% /run
tmpfs                   tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        57G  3.5G   54G   7% /
/dev/sda1               xfs      1014M  195M  820M  20% /boot
tmpfs                   tmpfs     182M     0  182M   0% /run/user/1000
/dev/sdc2               ext4      5.8G  507M  5.0G  10% /my-ext4-fs
/dev/sdc3               xfs       8.0G   33M  8.0G   1% /my-xfs-fs
/dev/mapper/myvg-mylv   xfs       8.0G   33M  8.0G   1% /mylv-mount-point
  1. 创建PV
 ➜  ~ pvcreate /dev/sdc6
  Physical volume "/dev/sdc6" successfully created.
  1. 将新建的PV /dev/sdc6添加到VG myvg
 ➜  ~ vgextend myvg /dev/sdc6
  Volume group "myvg" successfully extended
  1. 将VG myvg的可用空间都添加到LV /dev/myvg/mylv
 ➜  ~ lvextend -l +100%FREE /dev/myvg/mylv
  Size of logical volume myvg/mylv changed from <8.00 GiB (2047 extents) to 11.99 GiB (3070 extents).
  Logical volume myvg/mylv successfully resized.
  1. 调整文件系统大小

 ➜  ~ xfs_growfs /dev/myvg/mylv
meta-data=/dev/mapper/myvg-mylv  isize=512    agcount=4, agsize=524032 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2096128, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2096128 to 3143680

➜  ~ resize2fs /dev/myvg/mylv

  1. 再次查看文件系统使用情况,可以看到/dev/mapper/myvg-mylv大小已经从8G扩展到12G
 ➜  ~ df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs  898M     0  898M   0% /dev
tmpfs                   tmpfs     910M     0  910M   0% /dev/shm
tmpfs                   tmpfs     910M   21M  889M   3% /run
tmpfs                   tmpfs     910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        57G  3.5G   54G   7% /
/dev/sda1               xfs      1014M  195M  820M  20% /boot
tmpfs                   tmpfs     182M     0  182M   0% /run/user/1000
/dev/sdc2               ext4      5.8G  507M  5.0G  10% /my-ext4-fs
/dev/sdc3               xfs       8.0G   33M  8.0G   1% /my-xfs-fs
/dev/mapper/myvg-mylv   xfs        12G   33M   12G   1% /mylv-mount-point
最后修改:2022 年 05 月 01 日
如果觉得我的文章对你有用,请随意赞赏