centos7硬盘分区及盘符挂载

常用命令说明

命令

说明

lsblk

查看目录结构

lvdisplay

说明:显示LVM逻辑卷空间大小、读写状态和快照信息等属性。如果省略”逻辑卷“参数,则lvdisplay命令显示所有的逻辑卷属性。否则,仅显示指定的逻辑卷属性

语法:lvdisplay [参数]

参数:逻辑卷(指定要显示属性的逻辑卷对应的设备文件,例如lvdisplay /dev/centos/home)。

实例:使用lvdisplay命令显示指定逻辑卷的属性。

[root@host-145-168-10-109 dev]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                Fsc1Mj-ALST-gI0Y-a49U-lXSZ-dtZb-2mImnL
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-07-28 15:10:21 +0800
  LV Status              available
  # open                 2
  LV Size                3.75 GiB
  Current LE             960
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/home
  LV Name                home
  VG Name                centos
  LV UUID                yEFmdw-PdUP-TsRX-kMp8-kpr0-M9gX-D7GHXZ
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-07-28 15:10:21 +0800
  LV Status              available
  # open                 1
  LV Size                245.24 GiB
  Current LE             62782
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                CQ2o2d-GfQ0-AhEE-rZSl-RWG0-4mO3-kHgl5X
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-07-28 15:10:22 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

vgdisplay

说明:显示LVM卷组的信息。如果不指定”卷组”参数,则分别显示所有卷组的属性。

语法:vgdisplay [选项] [参数]

-A

仅显示活动卷组的属性

-s

使用短格式输出的信息

参数:卷组名称(实例:使用单格式显示活动卷组“centos”的属性:vgdisplay -As centos

[root@host-145-168-10-109 dev]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <299.00 GiB
  PE Size               4.00 MiB
  Total PE              76543
  Alloc PE / Size       76542 / 298.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               UL86En-oFXU-p0rV-LGJ0-F7SZ-WeM9-hpc301

pvdisplay

说明:显示物理卷属性(物理卷名称、所属的卷组、物理卷大小、PE大小、总PE数、可用PE数、已分配的PE数和UUID)。

语法:pvdisplay [选项] [参数]

-s

使用短格式输出的信息

-m

显示PE到LE的映射

参数:物理卷(要显示的物理卷对应的设备文件名,例如:pvdisplay /dev/vda2

实例:使用pvdisplay命令显示指定的物理卷的基本信息。

[root@host-145-168-10-109 dev]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               <299.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              76543
  Free PE               1
  Allocated PE          76542
  PV UUID               oHpmL9-zta9-38rV-0zRx-dyVw-EFUR-MH9eRD

分区挂载步骤

  1. 使用fdisk -l查看分区详细信息
  2. 硬盘分区(fdisk /dev/vda)。新添加的硬盘默认是不使用的,需要先进行分区、格式化才能使用。
[root@host-145-168-10-88 ~]# fdisk /dev/vda
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.
# n: 添加一个新分区
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
# p: 创建主分区
Select (default p): p 
Using default response p
# 3: 分区号
Partition number (3,4, default 3): 3
# 起始扇区
First sector (629145600-1048575999, default 629145600): 629145600
Using default value 629145600
# 截止扇区
Last sector, +sectors or +size{K,M,G} (629145600-1048575999, default 1048575999): 1048575999
Using default value 1048575999
Partition 3 of type Linux and of size 200 GiB is set
# 保存创建的分区并退出操作
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
  1. 读取分区信息partprobe。使用fdisk工具只是将分区信息写到磁盘,如果需要mkfs磁盘分区则需重启系统,而使用partprobe则可以使kernel重新读取分区信息,从而避免重启系统。同时系统会创建分区对应的目录。
[root@host-145-168-10-88 ~]# ll /dev/vda*
brw-rw----. 1 root disk 252, 0 Feb 10 16:59 /dev/vda
brw-rw----. 1 root disk 252, 1 Feb 10 16:59 /dev/vda1
brw-rw----. 1 root disk 252, 2 Feb 10 16:59 /dev/vda2
[root@host-145-168-10-88 ~]# partprobe
[root@host-145-168-10-88 ~]# ll /dev/vda*
brw-rw----. 1 root disk 252, 0 Feb 10 17:00 /dev/vda
brw-rw----. 1 root disk 252, 1 Feb 10 17:00 /dev/vda1
brw-rw----. 1 root disk 252, 2 Feb 10 17:00 /dev/vda2
brw-rw----. 1 root disk 252, 3 Feb 10 17:00 /dev/vda3
  1. 创建物理卷pvcreate /dev/vda3
[root@host-145-168-10-88 ~]# pvcreate /dev/vda3
  Physical volume "/dev/vda3" successfully created.
  1. 将物理卷“/dev/vda3”加入到“centos”组中
[root@host-145-168-10-88 ~]# vgextend centos /dev/vda3
  Volume group "centos" successfully extended
  1. 动态在线扩展磁盘空间lvextend -l +100%FREE /dev/centos/home
[root@host-145-168-10-88 ~]# lvextend -l +100%FREE /dev/centos/home
  Size of logical volume centos/home changed from 245.24 GiB (62782 extents) to 445.24 GiB (113982 extents).
  Logical volume centos/home successfully resized.
  1. 同步文件系统
# 查看要同步的文件系统格式类型
[root@host-145-168-10-88 ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
devtmpfs                devtmpfs   16G     0   16G   0% /dev
tmpfs                   tmpfs      16G     0   16G   0% /dev/shm
tmpfs                   tmpfs      16G   12M   16G   1% /run
tmpfs                   tmpfs      16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root xfs        50G  4.1G   46G   9% /
/dev/mapper/centos-home xfs       246G   37M  246G   1% /home
/dev/vda1               xfs      1014M  201M  814M  20% /boot
tmpfs                   tmpfs     3.2G   12K  3.2G   1% /run/user/42
tmpfs                   tmpfs     3.2G     0  3.2G   0% /run/user/0

# xfs文件系统使用xfs_growfs,xfs_growfs /dev/centos/home
# ext4文件系统使用resize2fs,resize2fs /dev/centos/home
[root@host-145-168-10-88 ~]# xfs_growfs /dev/centos/home
meta-data=/dev/mapper/centos-home isize=512    agcount=4, agsize=16072192 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=64288768, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=31391, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 64288768 to 116717568
  1. 确认硬盘是否挂载成功
[root@host-145-168-10-88 ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                  16G     0   16G   0% /dev
tmpfs                     16G     0   16G   0% /dev/shm
tmpfs                     16G   12M   16G   1% /run
tmpfs                     16G     0   16G   0% /sys/fs/cgroup
/dev/mapper/centos-root   50G  4.1G   46G   9% /
/dev/mapper/centos-home  446G   37M  446G   1% /home
/dev/vda1               1014M  201M  814M  20% /boot
tmpfs                    3.2G   12K  3.2G   1% /run/user/42
tmpfs                    3.2G     0  3.2G   0% /run/user/0

整体步骤流程

[root@host-145-168-10-88 ~]# fdisk /dev/vda
# 选项 n p 回车、回车、回车 w
[root@host-145-168-10-88 ~]# partprobe
[root@host-145-168-10-88 ~]# pvcreate /dev/vda3
[root@host-145-168-10-88 ~]# vgextend centos /dev/vda3
[root@host-145-168-10-88 ~]# lvextend -l +100%FREE /dev/centos/home
[root@host-145-168-10-88 ~]# xfs_growfs /dev/centos/home