LVM的基本操作

系统版本:

[root@centos71d1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core) 
[root@centos71d1 ~]# uname -r
3.10.0-229.el7.x86_64

 
以下演示LVM的基本操作:
1. PV的创建、查看、移除
2. VG的创建、查看、扩展、缩减
3. LV的创建、查看、扩展、缩减、快照、移除

 
在VMware虚拟机中添加2块10GB的硬盘

[root@centos71d1 ~]# fdisk -l | grep -i "disk /dev/sd"
Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 sectors
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
#除了sda,sdb和sdc为新添加的硬盘

 

PV的创建、查看、移除


对/dev/sdb分区和格式化,并设置分区为“Linux LVM”格式

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

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xc98da444.

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-20971519, default 2048): 
#起始扇区,按默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
#结束扇区,这里只分一个区,因此按默认,指定最后一个扇区
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

Command (m for help): t
#更改分区类型
Selected partition 1
Hex code (type L to list all codes): 8e
#8e为Linux LVM的代码
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p
#显示分区表,可以看到已经创建好的/dev/sdb1分区

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0xc98da444

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20971519    10484736   8e  Linux LVM

Command (m for help): w
#保存退出
The partition table has been altered!

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

[root@centos71d1 ~]# partprobe
#刷新分区表
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  \
/dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  \
/dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  \
/dev/sr0 has been opened read-only.

PV的创建

[root@centos71d1 ~]# pvcreate /dev/sdb1
#创建PV /dev/sdb1
  Physical volume "/dev/sdb1" successfully created

PV的查看

[root@centos71d1 ~]# pvs
#查看简要的PV信息
  PV         VG     Fmt  Attr PSize  PFree 
  /dev/sda2  centos lvm2 a--  19.51g 40.00m
  /dev/sdb1         lvm2 ---  10.00g 10.00g

[root@centos71d1 ~]# pvdisplay /dev/sdb1
#查看PV /dev/sdb1的详细信息
  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               4XdGpd-80lY-cDtZ-vPQn-mWPZ-c469-7r3At5

PV的移除

[root@centos71d1 ~]# pvremove /dev/sdb1
#移除PV /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped

 

VG的创建、查看、扩展、缩减


VG的创建

[root@centos71d1 ~]# pvcreate /dev/sdb1
#把前面移除的PV /dev/sdb1创建回来
  Physical volume "/dev/sdb1" successfully created

[root@centos71d1 ~]# vgcreate -s 16M myvg /dev/sdb1
#使用PV /dev/sdb1创建VG myvg,指定PE大小为16MB
  Volume group "myvg" successfully created

VG的查看

[root@centos71d1 ~]# vgs
#查看简要的VG信息
  VG     #PV #LV #SN Attr   VSize  VFree 
  centos   1   2   0 wz--n- 19.51g 40.00m
  myvg     1   0   0 wz--n-  9.98g  9.98g

[root@centos71d1 ~]# vgdisplay myvg
#查看VG 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               9.98 GiB
  PE Size               16.00 MiB
  Total PE              639
  Alloc PE / Size       0 / 0   
  Free  PE / Size       639 / 9.98 GiB
  VG UUID               WoszmO-0t4v-EYWr-Rk0Y-UgKt-f1rt-w8I1l9

VG的扩展

和前面一样,在硬盘/dev/sdc上,分一个区,格式化为Linux LVM格式

[root@centos71d1 ~]# 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 0x28ebc5c0.

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): 
First sector (2048-20971519, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): 
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set

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'

Command (m for help): p

Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 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: 0x28ebc5c0

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    20971519    10484736   8e  Linux LVM

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

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

[root@centos71d1 ~]# partprobe 
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  \
/dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  \
/dev/sr0 has been opened read-only.
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  \
/dev/sr0 has been opened read-only.
[root@centos71d1 ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created

[root@centos71d1 ~]# vgextend myvg /dev/sdc1
#添加PV /dev/sdc1,扩展VG myvg
  Volume group "myvg" successfully extended

[root@centos71d1 ~]# vgdisplay myvg 
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.97 GiB
  PE Size               16.00 MiB
  Total PE              1278
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1278 / 19.97 GiB
  VG UUID               WoszmO-0t4v-EYWr-Rk0Y-UgKt-f1rt-w8I1l9
#可以看到VG的空间增加到近20GB

VG的缩减

[root@centos71d1 ~]# pvmove /dev/sdb1
  No data to move for myvg
#先移动PV /dev/sdb1中的数据到VG myvg中的其它PV;因为现在没有数据,所以出现上面的提示

[root@centos71d1 ~]# vgreduce myvg /dev/sdb1
  Removed "/dev/sdb1" from volume group "myvg"
#将PV /dev/sdb1从VG myvg中移除

[root@centos71d1 ~]# vgdisplay myvg 
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  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               9.98 GiB
  PE Size               16.00 MiB
  Total PE              639
  Alloc PE / Size       0 / 0   
  Free  PE / Size       639 / 9.98 GiB
  VG UUID               WoszmO-0t4v-EYWr-Rk0Y-UgKt-f1rt-w8I1l9
#可以看到VG myvg的空间又变成了9.98GiB   

[root@centos71d1 ~]# vgextend myvg /dev/sdb1
  Volume group "myvg" successfully extended
#再次将/dev/sdb1加回myvg

 

LV的创建、查看、扩展、缩减、快照、移除


LV的创建

[root@centos71d1 ~]# lvcreate -L 12G -n mylv myvg 
  Logical volume "mylv" created.
#在VG myvg中创建LV mylv,大小为12GB

[root@centos71d1 ~]# mke2fs -t ext4 -L MYLV -b 1024 /dev/myvg/mylv 
#格式化LV mylv的文件系统为ext4格式,指定label为MYLV,块大小为1024 bytes
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=MYLV
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
786432 inodes, 12582912 blocks
629145 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=46137344
1536 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks: 
    8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553, 
    1024001, 1990657, 2809857, 5120001, 5971969

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

[root@centos71d1 ~]# mkdir /backup
[root@centos71d1 ~]# mount /dev/myvg/mylv /backup/
#将LV mylv挂载至/backup目录

[root@centos71d1 ~]# mount | tail -1
#mount显示最后一行,可以看到已经成功挂载,/dev/myvg/mylv实际指向/dev/mapper/myvg-mylv
/dev/mapper/myvg-mylv on /backup type ext4 (rw,relatime,data=ordered)

[root@centos71d1 ~]# cp /etc/grub2.cfg /backup/
[root@centos71d1 ~]# ls /backup/
grub2.cfg  lost+found

LV的查看

[root@centos71d1 ~]# lvdisplay /dev/myvg/mylv 
#查看LV mylv的详细信息
  --- Logical volume ---
  LV Path                /dev/myvg/mylv
  LV Name                mylv
  VG Name                myvg
  LV UUID                GgtQzX-3jfe-KVMX-tvOe-Sp35-GpWG-PJCE5w
  LV Write Access        read/write
  LV Creation host, time centos71d1, 2018-07-05 13:49:13 +0800
  LV Status              available
  # open                 1
  LV Size                12.00 GiB
  Current LE             768
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

LV的扩展

[root@centos71d1 ~]# vgdisplay myvg 
  --- Volume group ---
  VG Name               myvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.97 GiB
  PE Size               16.00 MiB
  Total PE              1278
  Alloc PE / Size       768 / 12.00 GiB
  Free  PE / Size       510 / 7.97 GiB
  VG UUID               WoszmO-0t4v-EYWr-Rk0Y-UgKt-f1rt-w8I1l9
#VG myvg中还有近8GB的空间  

[root@centos71d1 ~]# lvextend -L 16G /dev/myvg/mylv
#扩展LV mylv的空间到16GB
  Size of logical volume myvg/mylv changed from 12.00 GiB (768 extents) to \
    16.00 GiB (1024 extents).
  Logical volume mylv successfully resized

[root@centos71d1 ~]# resize2fs /dev/myvg/mylv 
#调整文件系统大小,没有指定大小,表示使用所有可用空间
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv is mounted on /backup; on-line resizing required
old_desc_blocks = 96, new_desc_blocks = 128
The filesystem on /dev/myvg/mylv is now 16777216 blocks long.

[root@centos71d1 ~]# df -lh
#使用df命令查看文件系统信息
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   18G  948M   17G   6% /
devtmpfs                 481M     0  481M   0% /dev
tmpfs                    490M     0  490M   0% /dev/shm
tmpfs                    490M  6.6M  484M   2% /run
tmpfs                    490M     0  490M   0% /sys/fs/cgroup
/dev/sda1                497M  120M  378M  24% /boot
/dev/mapper/myvg-mylv     16G  3.3M   15G   1% /backup

LV的缩减

#LV的缩减,可能会损毁数据,因此在该操作之前,要确保已经做好数据备份;
#并且要先查看分区已使用的大小,确认要缩减多少空间,缩减到的大小不能小于原文件系统已使用的空间大小

[root@centos71d1 ~]# umount /backup/
#先卸载

[root@centos71d1 ~]# e2fsck -f /dev/myvg/mylv
#强制检查分区错误
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
MYLV: 12/1048576 files (0.0% non-contiguous), 304322/16777216 blocks

[root@centos71d1 ~]# resize2fs /dev/myvg/mylv 12G
#先缩减文件系统到12GB
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv to 12582912 (1k) blocks.
The filesystem on /dev/myvg/mylv is now 12582912 blocks long.

[root@centos71d1 ~]# lvreduce -L 12G /dev/myvg/mylv
#再调整LV mylv到12GB
  WARNING: Reducing active logical volume to 12.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv? [y/n]: y
  Size of logical volume myvg/mylv changed from 16.00 GiB (1024 extents) to \
    12.00 GiB (768 extents).
  Logical volume mylv successfully resized

[root@centos71d1 ~]# mount /dev/myvg/mylv /backup/
#重新挂载
[root@centos71d1 ~]# ls /backup/
grub2.cfg  lost+found
#运气不错,数据没有丢失

LV的快照

#对某个LV创建快照,快照的功能类似于硬链接,因此创建的快照会与原LV处于同一个VG中

[root@centos71d1 ~]# cp /etc/issue /backup/
[root@centos71d1 ~]# ls /backup/
grub2.cfg  issue  lost+found

[root@centos71d1 ~]# lvcreate -s -L 2G -p r -n mylv-snap /dev/myvg/mylv
  Logical volume "mylv-snap" created.
#创建LV mylv的快照mylv-snap,大小为2GB,权限为只读

[root@centos71d1 ~]# mount /dev/myvg/mylv-snap /mnt/
mount: /dev/mapper/myvg-mylv--snap is write-protected, mounting read-only
#将快照卷挂载至/mnt目录

[root@centos71d1 ~]# vim /backup/issue 
[root@centos71d1 ~]# cat /backup/issue
CentOS release 6.7 (Final)
Kernel \r on an \m

new line
#添加一行“new line”

[root@centos71d1 ~]# cat /mnt/issue 
\S
Kernel \r on an \m
#快照卷中对应的文件没有“new line”

LV的移除

[root@centos71d1 ~]# cp -a /mnt/grub2.cfg /tmp
[root@centos71d1 ~]# cp -a /mnt/issue /tmp
#备份快照卷中的数据

[root@centos71d1 ~]# umount /mnt/

[root@centos71d1 ~]# lvremove /dev/myvg/mylv-snap 
Do you really want to remove active logical volume mylv-snap? [y/n]: y
  Logical volume "mylv-snap" successfully removed
#删除快照卷

 
 

发表评论

邮箱地址不会被公开。