新秀篇 ##Linux中的lvm逻辑卷管理##

名词解析

PV(physical volume):物理卷载逻辑卷管理系统最底层,可为整个物理硬盘或实际物理硬盘上的分区
VG(volume group):卷组建立在物理卷上,一卷组中至少包括一个物理卷,卷组建立后可动态添加卷到卷组上,一个逻辑管理系统工程中可有多个卷组
PE(physical extent):物理扩展,物理区域是物理卷中可用于分配的最小储存单元,在建立卷组时指定大小,不能进行改变
LVM( Logical Volume Manager):逻辑卷管理,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现,目前最新版本为:稳定版1.0.5,开发版 1.1.0-rc2,以及LVM2开发版。Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。
LVM原理图:
这里写图片描述

建立LVM: 【在server虚拟机中】

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


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): +1G   ##给1G的大小空间
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (2099200-20971519, default 2099200): 
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (4196352-20971519, default 4196352): 
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +1G
Partition 3 of type Linux and of size 1 GiB is set

Command (m for help): t      ##修改类型
Partition number (1-3, default 3): 1
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-3, default 3): 2
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-3, default 3): 3
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# pvcreate  /dev/vdb1       ##创建物理卷/dev/vdb1
WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n] y
  Wiping xfs signature on /dev/vdb1.
  Physical volume "/dev/vdb1" successfully created
[root@localhost ~]# vgcreate vg0  /dev/vdb1   ##创建物理卷组vg0,将物理卷/dev/vdb1加入到vg0上
  Volume group "vg0" successfully created
[root@localhost ~]# lvcreate  -L  300M  -n lv0  vg0   ##在物理卷vg0上创建一个大小为500M的逻辑卷lv0【-L指定创建的lv大小,-l指定创建的lv的pe数量,-n是lv的名字】
  Logical volume "lv0" created
[root@localhost ~]# mkfs.xfs  /dev/vg0/lv0        ##格式化
meta-data=/dev/vg0/lv0           isize=256    agcount=4, agsize=19200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=76800, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mount  /dev/vg0/lv0   /mnt/       ##挂载【Linux下的文件系统需要被挂载之后才能使用】
然后新创建的lv就可以使用了
在监控中可以看到效果:watch -n 1 'pvs;vgs;lvs;df -h /mnt'

%%/dev/vg0/lvo\0==/dev/mapper/vg0-lv0 lv%%
[root@localhost ~]# ll /dev/vg0/lv0
lrwxrwxrwx. 1 root root 7 May  4 22:56 /dev/vg0/lv0 -> ../dm-0
[root@localhost ~]# ll  /dev/mapper/vg0-lv0 lv
ls: cannot access lv: No such file or directory
lrwxrwxrwx. 1 root root 7 May  4 22:56 /dev/mapper/vg0-lv0 -> ../dm-0
######扩容:
[root@localhost ~]# lvextend -L 500M   /dev/vg0/lv0
  Extending logical volume lv0 to 500.00 MiB
  Logical volume lv0 successfully resized
[root@localhost ~]# xfs_growfs  /dev/vg0/lv0    ##拓展文件系统
meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=4, agsize=19200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=76800, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 76800 to 128000
[root@localhost ~]# pvcreate /dev/vdb2
  Physical volume "/dev/vdb2" successfully created
[root@localhost ~]# vgextend vg0  /dev/vdb2    ##将新的物理卷vdb2添加到现有的物理卷组vg0上
  Volume group "vg0" successfully extended
[root@localhost ~]# lvextend -L  1500M  /dev/vg0/lv0 
  Extending logical volume lv0 to 1.46 GiB
  Logical volume lv0 successfully resized
[root@localhost ~]# xfs_growfs /dev/vg0/lv0
meta-data=/dev/mapper/vg0-lv0    isize=256    agcount=7, agsize=19200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=128000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 128000 to 384000

LVM逻辑卷扩容(逻辑卷格式为ext4):

[root@localhost ~]# umount  /mnt    ##卸载
[root@localhost ~]# mkfs.ext4  /dev/vg0/lv0    ##格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
96000 inodes, 384000 blocks
19200 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=394264576
12 block groups
32768 blocks per group, 32768 fragments per group
8000 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

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

[root@localhost ~]# mount /dev/vg0/lv0  /mnt/              ####挂载
[root@localhost ~]# mount             ##查看挂载
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=469344k,nr_inodes=117336,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,seclabel,size=484932k,nr_inodes=121233)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,seclabel,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,seclabel,size=484932k,nr_inodes=121233,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (rw,nosuid,nodev,noexec,seclabel,size=484932k,nr_inodes=121233,mode=755)
cgroup on /sys/fs/cgroup/systemd type cgroup (rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd)
pstore on /sys/fs/pstore type pstore (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset)
cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,nosuid,nodev,noexec,relatime,memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,nosuid,nodev,noexec,relatime,devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
cgroup on /sys/fs/cgroup/net_cls type cgroup (rw,nosuid,nodev,noexec,relatime,net_cls)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,nosuid,nodev,noexec,relatime,blkio)
cgroup on /sys/fs/cgroup/perf_event type cgroup (rw,nosuid,nodev,noexec,relatime,perf_event)
cgroup on /sys/fs/cgroup/hugetlb type cgroup (rw,nosuid,nodev,noexec,relatime,hugetlb)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/vda1 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
rpc_pipefs on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw,relatime)
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)
systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=29,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
mqueue on /dev/mqueue type mqueue (rw,relatime,seclabel)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime,seclabel)
sunrpc on /proc/fs/nfsd type nfsd (rw,relatime)
gvfsd-fuse on /run/user/0/gvfs type fuse.gvfsd-fuse (rw,nosuid,nodev,relatime,user_id=0,group_id=0)
fusectl on /sys/fs/fuse/connections type fusectl (rw,relatime)
/dev/mapper/vg0-lv0 on /mnt type ext4 (rw,relatime,seclabel,data=ordered)
[root@localhost ~]# lvextend  -L  1800M  /dev/vg0/lv0      ##将逻辑卷扩容到1800M
  Extending logical volume lv0 to 1.76 GiB
  Logical volume lv0 successfully resized
[root@localhost ~]# resize2fs /dev/vg0/lv0           
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 460800 blocks long.

LVM缩减操作:【xfs不支持】

[root@localhost ~]# pvmove /dev/vdb1  /dev/vdb2    ##将/dev/vdb1的东西移动到/dev/vdb2中
  /dev/vdb1: Moved: 0.8%
  /dev/vdb1: Moved: 58.4%
  /dev/vdb1: Moved: 100.0%                          
[root@localhost ~]# vgreduce vg0 /dev/vdb1              ##从vg0的物理卷中移除/vdb1     
  Removed "/dev/vdb1" from volume group "vg0"
[root@localhost ~]# pvremove /dev/vdb1                 ##移除//dev/vdb1
  Labels on physical volume "/dev/vdb1" successfully wiped

建立快照:

[root@localhost ~]# pvcreate  /dev/vdb1   ##建立vdb1【vdb2不够50M】
  Physical volume "/dev/vdb1" successfully created
[root@localhost mnt]# vgextend vg0 /dev/vdb1   ##放到物理卷组里
  Physical volume '/dev/vdb1' is already in volume group 'vg0'
  Unable to add physical volume '/dev/vdb1' to volume group 'vg0'.
[root@localhost ~]#touch  /mnt/file{1..5}   ##建立文件
[root@localhost ~]# lvcreate -L 50M -n  lv0backup -s /dev/vg0/lv0     ##建立快照
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "lv0backup" created
[root@localhost ~]# mount /dev/vg0/lv0backup  /mnt/     ##挂载

删除lvm设备:

[root@localhost ~]# umount /mnt/     ##卸载
[root@localhost ~]# lvremove /dev/vg0/lv0backup          ##移除快照
Do you really want to remove active logical volume lv0backup? [y/n]: y
  Logical volume "lv0backup" successfully removed
[root@localhost ~]# lvremove /dev/vg0/lv0     ##删除逻辑卷
Do you really want to remove active logical volume lv0? [y/n]: y
  Logical volume "lv0" successfully removed
[root@localhost ~]# vgremove vg0     ##删除物理卷组
  Volume group "vg0" successfully removed
[root@localhost ~]# pvremove /dev/vdb{1..2}     ##删除物理卷
  Labels on physical volume "/dev/vdb1" successfully wiped
  Labels on physical volume "/dev/vdb2" successfully wiped

猜你喜欢

转载自blog.csdn.net/china_zgd/article/details/80275563