lvm磁盘挂载方法

系统环境:centos 7.x ubuntu

一.fdisk -l 查看分区信息,找到要添加的硬盘盘符 如:/dev/vdb

   #fdisk -l
   Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 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

二.划分物理分区

(一)分区没超过2T的,使用fdisk

(1)fdisk /dev/vdb

#fdisk /dev/vdb
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 0xd069b076.

Command (m for help): 

(2) n(新建)、p-1、其他的默认

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
First sector (2048-104857599, default 2048):   #输入开始分区位置,回车默认位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):  #输入结束分区位置,回车默认全部分区
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set

(3)将磁盘格式修改为lvm格式, t(改格式)、8e

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

(4)p 查看创建分区信息,w 保存

Command (m for help): p

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 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: 0xd069b076

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048   104857599    52427776   8e  Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.
# partprobe                 ##更新分区表

(二)分区超过2T的,用parted

(1)parted /dev/vdb

#parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.

(2)mklabel gpt #设置格式为gpt

(parted) p
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start  End  Size  Type  File system  Flags
(parted) mklabel gpt
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you want to
continue?
Yes/No? yes                                                               
(parted) p                                                                
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start  End  Size  File system  Name  Flags

(3)mkpart #新建分区

(parted) mkpart
Partition name?  []? vdb1            #分区名称                                     
File system type?  [ext2]? ext4      #文件系统类型                                     
Start? 1                             #起始位置,1为1M,一般预留1M空间                                     
End? 100%                            #结束位置,100% 为使用全部空间                                    
(parted) p                           #显示分区内容                                    
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name  Flags
 1      1049kB  53.7GB  53.7GB               vdb1

(parted) q                             #退出                                  
Information: You may need to update /etc/fstab.

三.将创建的vdb1分区,创建pv 物理卷

#pvcreate /dev/vdb1
  Physical volume "/dev/vdb1" successfully created.
#pvdisplay
  --- Physical volume ---
  PV Name               /dev/vda2
  VG Name               centos
  PV Size               <99.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              25343
  Free PE               1
  Allocated PE          25342
  PV UUID               2I5XOD-VzbD-fCZU-gdTm-fLPD-pnpa-oUjYjg

  "/dev/vdb1" is a new physical volume of "<50.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               <50.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               u1CAKQ-Tae9-0XwY-Y9Vs-Ub9G-rlL9-r6VXIz

四.创建vg卷组,创建lv逻辑卷

(一)扩展已有分区

(1)vgdisplay #查看要扩展的vg(卷组)的名称 例如:centos

#vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               4
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <99.00 GiB
  PE Size               4.00 MiB
  Total PE              25343
  Alloc PE / Size       25342 / 98.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               QHEpYY-Z6c1-dAkv-9Vl5-uWs7-Ydpa-OQNNvF

(2)vgextend centos /dev/vdb1 #把vdb1的pv空间扩展给centos的vg

#vgextend centos /dev/vdb1
  Volume group "centos" successfully extended
#vgdisplay                        #查看增加后的卷组
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                4
  Open LV               4
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               148.99 GiB
  PE Size               4.00 MiB
  Total PE              38142
  Alloc PE / Size       25342 / 98.99 GiB
  Free  PE / Size       12800 / 50.00 GiB
  VG UUID               QHEpYY-Z6c1-dAkv-9Vl5-uWs7-Ydpa-OQNNvF

(3)df -hT #查看要增加空间的名称,例要增加根目录 / 的空间

#df -hT
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  5.8G     0  5.8G   0% /dev
tmpfs                      tmpfs     5.8G     0  5.8G   0% /dev/shm
tmpfs                      tmpfs     5.8G   27M  5.8G   1% /run
tmpfs                      tmpfs     5.8G     0  5.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root    xfs        50G  8.6G   42G  18% /
/dev/vda1                  xfs      1014M  209M  806M  21% /boot
/dev/mapper/centos-home    xfs        31G   33M   31G   1% /home
/dev/mapper/centos-var_log xfs        12G   65M   11G   1% /var/log
tmpfs                      tmpfs     1.6G     0  1.6G   0% /run/user/0

(4)lvextend -l +100%FREE /dev/mapper/centos-root #将空闲的空间,都增 加给lv(逻辑卷),即增加给根目录 /

#lvextend -l +100%FREE /dev/mapper/centos-root
  Size of logical volume centos/root changed from 50.00 GiB (12800 extents) to 100.00 GiB (25600 extents).
  Logical volume centos/root successfully resized.

(5)xfs_growfs /dev/mapper/centos-root #重新刷新识别空间(注:xfs_growfs命令,针对的是xfs文件系统,resize2fs命令 针对的是ext2、ext3、ext4文件系统)

#xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3276800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=13107200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6400, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13107200 to 26214400

(6)df -hT #查看确认空间增长

#df -hT
Filesystem                 Type      Size  Used Avail Use% Mounted on
devtmpfs                   devtmpfs  5.8G     0  5.8G   0% /dev
tmpfs                      tmpfs     5.8G     0  5.8G   0% /dev/shm
tmpfs                      tmpfs     5.8G   27M  5.8G   1% /run
tmpfs                      tmpfs     5.8G     0  5.8G   0% /sys/fs/cgroup
/dev/mapper/centos-root    xfs       100G  8.6G   92G   9% /
/dev/vda1                  xfs      1014M  209M  806M  21% /boot
/dev/mapper/centos-home    xfs        31G   33M   31G   1% /home
/dev/mapper/centos-var_log xfs        12G   66M   11G   1% /var/log
tmpfs                      tmpfs     1.6G     0  1.6G   0% /run/user/0

(二)挂载新的分区

(1)vgcreate myvg /dev/vdb1 #创建新的vg 卷组

(2)lvcreate -l +100%FREE myvg -n mylv #-L 100G 是只分100G给lv

(3)mkfs.ext4 /dev/myvg/mylv #格式化lv

(4)分别用pvdisplay、vgdisplay、lvdisplay查看添加的pv、vg、lv

(5)mkdir /data #新建要挂载的文件夹

(6)vi /etc/fstab ,在末尾添加 /dev/myvg/mylv /data ext4 defaults 0 0

(7)mount -a #挂载成功

(8)df -hT #查看挂载的分区和空间

五.xfs文件系统空间减小方法(如果分区有数据,不能减小)

(一)umount /home #卸载分区

(二)减少home分区空间

# lvreduce -L -100G /dev/mapper/centos-home
  WARNING: Reducing active logical volume to 618.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce centos/home? [y/n]: y
  Size of logical volume centos/home changed from 718.00 GiB (183808 extents) to 618.00 GiB (158208 extents).
  Logical volume centos/home successfully resized.

(三)xfs文件系统不能直接执行分区减小的调整

# xfs_growfs /dev/mapper/centos-home
xfs_growfs: /dev/mapper/centos-home is not a mounted XFS filesystem
# mount /dev/mapper/centos-home /home/
mount: /dev/mapper/centos-home:不能读超级块

(四)重新格式化这个分区,格式化后才能再次挂载到home下

# mkfs.xfs /dev/mapper/centos-home -f
meta-data=/dev/mapper/centos-home isize=512    agcount=4, agsize=41156608 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=164626432, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=80384, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
# mount /dev/mapper/centos-home /home/

(五)df -hT查看,空间已经减小100G了,但/home里的文件已经被 格式化,丢失了。

# df -hT         
文件系统                 类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs       205G  2.2G  203G    2% /
devtmpfs                devtmpfs   32G     0   32G    0% /dev
tmpfs                   tmpfs      32G     0   32G    0% /dev/shm
tmpfs                   tmpfs      32G   49M   32G    1% /run
tmpfs                   tmpfs      32G     0   32G    0% /sys/fs/cgroup
/dev/sda1               xfs       197M  139M   59M   71% /boot
tmpfs                   tmpfs     6.3G     0  6.3G    0% /run/user/0
/dev/mapper/centos-home xfs       618G   73M  578G    1% /home
# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  9
  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               930.80 GiB
  PE Size               4.00 MiB
  Total PE              238285
  Alloc PE / Size       211456 / 826.00 GiB
  Free  PE / Size       26829 / 104.80 GiB
  VG UUID               a5hiAh-LB8M-9lRv-Ps1a-z35L-J4fk-sP3KrF

注:格式化时,也可以格式化成ext4格式的,增加减少空间就比较方便,不会存在要格式化分区的情况

# mkfs.ext4 /dev/mapper/centos-home           #格式化成ext4格式
#lvextend -L 120G /dev/mapper/centos-home     #增大至120G
lvreduce -L -80G /dev/mapper/centos-home      #减小80G
resize2fs /dev/mapper/centos-home             #执行调整