Linux 基础 之 lvm 逻辑卷管理

linux系统里,文件系统需要被挂载后才可以被使用

lvm没有分区限制,分一个物理分区,建立一个物理卷,然后这个物理卷建立一个卷组,我们就可以在这个卷组里面去分无数个逻辑卷,而且逻辑卷可以随意扩大缩小,不会对物理分 区有影响,逻辑卷的功能也和物理分区差不多,一样可以格式化成随意的文件系统,挂载到随意的目录。

lvm的主要功能
从硬盘驱动器中创建物理卷(physical volumes-PV)。
从物理卷中创建卷组(volume groups-VG)。
从卷组中创建逻辑卷(logical volumes-LV),并分派逻辑卷挂载点
其中只有逻辑卷才可以写数据

就像金箍棒,想大就大,想小就小。
使硬盘无限分区,扩容。

物理分区、pv、vg、pE、lvm、/mnt ;
pv 处理后的物理分区,为物理卷,处于lvm最底层,可以是物理硬盘或者分区 ;
vg 物理卷组,建立在pv之上,可以含有一个到多个pv ;
pE 是vg的最小单位 。

[root@localhost ~]# fdisk /dev/vdb                 ##创建分区
Welcome to fdisk (util-linux 2.23.2).

Command (m for help): p

Disk /dev/vdb: 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: 0x1a008f30

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     2050047     1024000   83  Linux
/dev/vdb2         2050048     4098047     1024000   83  Linux
/dev/vdb3         4098048     6146047     1024000   83  Linux

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): p

Disk /dev/vdb: 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: 0x1a008f30

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     2050047     1024000   8e  Linux LVM
/dev/vdb2         2050048     4098047     1024000   8e  Linux LVM
/dev/vdb3         4098048     6146047     1024000   8e  Linux LVM

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe

记得最后wq退出保存后得同步分区partprobe

重启一个sell插入一个监控命令:
[root@localhost ~]# watch -n 1 'pvs;vgs;lvs;df -h /mnt'
在开始操作,就可以看得到了
[root@localhost ~]# pvcreate /dev/vdb1                  ##创建物理卷
  Physical volume "/dev/vdb1" successfully created
[root@localhost ~]# vgcreate vg0 /dev/vdb1              ##创建物理卷组
  Volume group "vg0" successfully created
[root@localhost ~]# lvcreate -L 300M -n lv0 vg0         ##创建逻辑卷
  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/            ##挂载
[root@localhost ~]# ll /dev/vg0/lv0                     
lrwxrwxrwx. 1 root root 7 May  4 22:53 /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:53 /dev/mapper/vg0-lv0 -> ../dm-0

右边的是监控命令,左边的是执行命令

一、逻辑卷的扩容
(目的使扩大系统的储存容量)

xfs系统:
情况一、vg足够拉伸
[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

情况二、vg不够拉伸,得先扩大设备,在扩大系统。
[root@localhost ~]# pvcreate /dev/vdb2
  Physical volume "/dev/vdb2" successfully created
[root@localhost ~]# vgextend vg0 /dev/vdb2               ##添加物理卷到物理卷组
  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

efx4系统:
[root@localhost ~]# umount /mnt/                     ##先卸载掉之前的xfs系统的/mnt
[root@localhost ~]# mkfs.ext4 /dev/vg0/lv0           ##格式化
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
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 ~]# df                                    
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/vda1            10473900 3809252   6664648  37% /
devtmpfs               469344       0    469344   0% /dev
tmpfs                  484932     140    484792   1% /dev/shm
tmpfs                  484932   12840    472092   3% /run
tmpfs                  484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-lv0   1479088    4512   1381392   1% /mnt
[root@localhost ~]# mount
/dev/mapper/vg0-lv0 on /mnt type ext4 (rw,relatime,seclabel,data=ordered)

这里写图片描述
权限为rw

[root@localhost ~]# lvextend -L 1800M /dev/vg0/lv0           ##扩展逻辑卷大小  
  Extending logical volume lv0 to 1.76 GiB
  Logical volume lv0 successfully resized
[root@localhost ~]# resize2fs /dev/vg0/lv0   
                  ##更新逻辑卷信息  

这里写图片描述

二、逻辑卷的缩容
(目的使缩小系统的储存容量)
注意:xfs系统没有缩容。

ext4系统:
[root@localhost ~]# umount /mnt/                       ##卸载
[root@localhost ~]# e2fsck  -f /dev/vg0/lv0            ##扫描逻辑卷上的空间
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
/dev/vg0/lv0: 11/120000 files (9.1% non-contiguous), 16862/460800 blocks
[root@localhost ~]# resize2fs  /dev/vg0/lv0  1000M                            ##缩系统到1000M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 256000 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 256000 blocks long.

这里写图片描述

[root@localhost ~]# mount /dev/vg0/lv0 /mnt/                                   ##挂载
[root@localhost ~]# lvreduce -L 1000M /dev/vg0/lv0                             ##缩设备
  WARNING: Reducing active and open logical volume to 1000.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 1000.00 MiB
  Logical volume lv0 successfully resized

这里写图片描述

缩减vg:
(迁移到闲置设备)

[root@localhost ~]# pvmove /dev/vdb1 /dev/vdb2        ##将vdb1内的数据移到vdb2中
  /dev/vdb1: Moved: 88.0%
  /dev/vdb1: Moved: 100.0%
[root@localhost ~]# vgreduce vg0 /dev/vdb1            ##再将vdb1从vg0中取出来
  Removed "/dev/vdb1" from volume group "vg0"
[root@localhost ~]# pvremove /dev/vdb1                ##删除vdb1
  Labels on physical volume "/dev/vdb1" successfully wiped

这里写图片描述

报错:

[root@localhost ~]# pvmove /dev/vdb1 /dev/vdb2
  Insufficient free space: 249 extents needed, but only 248 available
  Unable to allocate mirror extents for pvmove0.
  Failed to convert pvmove LV to mirrored

vdb1 缩的不够小,vdb2容不下。所以说,得再重新缩小vdb1.

[root@localhost ~]# umount /mnt/                       ##卸载
[root@localhost ~]# lvreduce -L 500M /dev/vg0/lv0
  WARNING: Reducing active and open logical volume to 500.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv0? [y/n]: y
  Reducing logical volume lv0 to 500.00 MiB
  Logical volume lv0 successfully resized
[root@localhost ~]# mount /dev/vg0/lv0 /mnt/                                   ##挂载
[root@localhost ~]# pvmove /dev/vdb1 /dev/vdb2        ##将vdb1内的数据移到vdb2中
  /dev/vdb1: Moved: 88.0%
  /dev/vdb1: Moved: 100.0%
[root@localhost ~]# vgreduce vg0 /dev/vdb1            ##再将vdb1从vg0中取出来
  Removed "/dev/vdb1" from volume group "vg0"
[root@localhost ~]# pvremove /dev/vdb1                ##删除vdb1
  Labels on physical volume "/dev/vdb1" successfully wiped

这里写图片描述

之后再继续对vg缩小,就可以了。

快照

[root@localhost ~]# touch /mnt/file{1..5}
[root@localhost ~]# lvcreate -L 50M -n lv0backup -s /dev/vg0/lv0  ##建立一个50M的快照
[root@localhost ~]# mount /dev/vg0/lv0backup /mnt  
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# rm -fr *   ##删除所有文件
[root@localhost mnt]# cd
[root@localhost ~]# umount /mnt
[root@localhost ~]# lvremove /dev/vg0/lv0backup 
[root@localhost ~]# lvcreate -L 50M -n lv0backup -s /dev/vg0/lv0
[root@localhost ~]# mount /dev/vg0/lv0backup /mnt   ##挂载快照
[root@localhost ~]# ls /mnt  ##又可以看到之前建立的文件
file1  file2  file3  file4  file5  lost+found

这里写图片描述

当快照的大小大于所在文件的大小时,则需要再重新建一个vdb1。
[root@localhost ~]# pvcreate /dev/vdb1 ##创建物理卷
Physical volume “/dev/vdb1” successfully created
[root@localhost ~]# vgcreate vg0 /dev/vdb1 ##创建物理卷组
Volume group “vg0” successfully created

删除设备:

[root@localhost ~]# umount /mnt/                                       ##卸载/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/vdb2                                 ##删除物理卷
  Labels on physical volume "/dev/vdb2" successfully wiped

这里写图片描述
这里写图片描述

报错:


[root@localhost ~]# lvremove  /dev/vg0/lv0
  Logical volume vg0/lv0 contains a filesystem in use.
[root@localhost ~]# df
Filesystem          1K-blocks    Used Available Use% Mounted on
/dev/vda1            10473900 3809236   6664664  37% /
devtmpfs               469344       0    469344   0% /dev
tmpfs                  484932     140    484792   1% /dev/shm
tmpfs                  484932   12816    472116   3% /run
tmpfs                  484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/vg0-lv0    471176    2272    434328   1% /mnt
[root@localhost ~]# umount /mnt/
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3809260   6664640  37% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932     140    484792   1% /dev/shm
tmpfs             484932   12816    472116   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup

重新卸载/mnt,再继续操作。

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值