linux-LVM 逻辑卷

 

LVM     逻辑卷管理

 

为什么使用LVM?  

  我们在安装操作系统时候如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到当前某个分区需要的容量,还要预见该分区以后可能需要的容量的最大值。因为如果估 计不准确,当遇到某个分区不够用时管理员可能甚至要备份整个系统、清除硬盘、重新对硬盘分区,然后恢复数据到新分区。这样我们生产环境中如果磁盘中存储有重要数据时,我们没有办法动态得取增长我们磁盘容量。

  当我们无法预估我们我们我们得生产环境对disk 资源使用情况,比如说我们一块硬盘分区后挂载后提供给用户使用, 用户需要disk存储得空间很大,后期有反复添加disk 得操作,那么我们可以使用lvm 将这些磁盘作为逻辑卷来使用,所谓得lvm 就是将我们多块硬盘或者硬盘分区整合起来,对存储资源得集中调度。

 

什么是LVM ?

  LVM是Linux环境中对磁盘分区进行管理的一种机制,是建立在硬盘和分区之上、文件系统之下的一个逻辑层,可提高磁盘分区管理的灵活性。

  LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。

  

LVM  基本术语

PV(Physical Volume):物理空间的意思,其实就是指一个分区(如/dev/sdb1 )或者是一个盘(如/dev/sdb)
VG(Volume Group):相当于一个Pool 存储池,由多个PV组成的pool  
LV(Logical Volume):用来建立一个文件系统的空间,这个空间来源于VG,大小随意,可以扩展,我们使用得就是LV 

 

 

 

实现过程:

 

1.  准备硬盘格式化分区

linux-LVM 逻辑卷

 

 

分区

root@testyk:~# fdisk /dev/sdb                      # 分区/dev/sdb 盘
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x51321694.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won‘t be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

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                     # 主分区有四个,默认从1开始
First sector (2048-20971519, default 2048): 10000000           # 选择分区大小
Last sector, +sectors or +size{K,M,G} (10000000-20971519, default 20971519):
Using default value 20971519

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

Calling ioctl() to re-read partition table.

 

root@testyk:~# fdisk /dev/sdb

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

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

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

 

 分出两个分区

linux-LVM 逻辑卷

 

 

2. 创建物理卷

 

root@testyk:~# pvcreate /dev/sdb1 /dev/sdb2
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created

 

查看创建出得物理卷

root@testyk:~# pvdisplay
--- Physical volume ---
PV Name /dev/sda5
VG Name testyk-vg
PV Size 49.76 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 12738
Free PE 8
Allocated PE 12730
PV UUID ACKWc1-aUrg-3gBH-a8vJ-DF9C-G07U-OHHl7n

"/dev/sdb1" is a new physical volume of "5.23 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 5.23 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 0NmONH-VyzK-DbHR-4G14-18x5-QOLs-Z2txcd

"/dev/sdb2" is a new physical volume of "4.77 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 4.77 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID oH2A3j-UbLd-0LJb-xoEC-uBAH-RJID-Y5KZLs

 

 

3. 创建物理卷组

创建 物理卷组命名为 test-VG 

root@testyk:~# vgcreate test-VG /dev/sdb1 /dev/sdb2
Volume group "test-VG" successfully created

 

查询物理卷组

root@testyk:~# vgdisplay
--- Volume group ---
VG Name test-VG
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 0 / 0
Free PE / Size 2559 / 10.00 GiB
VG UUID e2dkt4-3ZAf-Q5Lk-ifhe-2Qck-h3Nz-Sxp21B

--- Volume group ---
VG Name testyk-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 49.76 GiB
PE Size 4.00 MiB
Total PE 12738
Alloc PE / Size 12730 / 49.73 GiB
Free PE / Size 8 / 32.00 MiB
VG UUID c3tzX1-wxuo-ztDj-Jmtt-0M3W-DzkP-7Mpfpl

 

 

4. 生产LV 创建逻辑卷

创建逻辑卷 -L 指定大小  -n 指定逻辑卷名   test-VG 物理卷组

root@testyk:~# lvcreate -L 6G -n test-lv test-VG
Logical volume "test-lv" created

 

查看创建得逻辑卷
root@testyk:~# lvdisplay
--- Logical volume ---
LV Path /dev/test-VG/test-lv
LV Name test-lv
VG Name test-VG
LV UUID HnNx3e-xYyj-M1qY-6RCz-yyVg-wOoV-4Q3uqK
LV Write Access read/write
LV Creation host, time testyk, 2021-01-28 02:23:02 +0800
LV Status available
# open 0
LV Size 6.00 GiB
Current LE 1536
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:2

--- Logical volume ---
LV Path /dev/testyk-vg/root
LV Name root
VG Name testyk-vg
LV UUID UkmqWb-X61e-k6as-CIFx-0H4Z-y2fz-Nhuu7L
LV Write Access read/write
LV Creation host, time testyk, 2020-08-27 20:42:46 +0800
LV Status available
# open 1
LV Size 48.66 GiB
Current LE 12457
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

--- Logical volume ---
LV Path /dev/testyk-vg/swap_1
LV Name swap_1
VG Name testyk-vg
LV UUID v3foBN-65jX-nrmi-rLWB-JM4G-ITkF-J1NINk
LV Write Access read/write
LV Creation host, time testyk, 2020-08-27 20:42:46 +0800
LV Status available
# open 0
LV Size 1.07 GiB
Current LE 273
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:1

 

5. 对逻辑卷得使用

linux-LVM 逻辑卷

 

 

根据需求将逻辑卷(LV)格式化成所需要文件格式得

root@testyk:~# mkfs.ext4 /dev/test-VG/test-lv
mke2fs 1.42.9 (4-Feb-2014)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
393216 inodes, 1572864 blocks
78643 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1610612736
48 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks):

done
Writing superblocks and filesystem accounting information: done

 

 创建挂载点,挂载逻辑卷

root@testyk:~# mkdir test-expand
root@testyk:~# mount /dev/test-VG/test-lv test-expand/
root@testyk:~# df -TH
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.6G 4.1k 1.6G 1% /dev
tmpfs tmpfs 314M 717k 313M 1% /run
/dev/dm-0 ext4 52G 20G 29G 41% /
none tmpfs 4.1k 0 4.1k 0% /sys/fs/cgroup
none tmpfs 5.3M 0 5.3M 0% /run/lock
none tmpfs 1.6G 0 1.6G 0% /run/shm
none tmpfs 105M 0 105M 0% /run/user
/dev/sda1 ext2 247M 55M 180M 24% /boot
/dev/mapper/test--VG-test--lv ext4 6.3G 13M 5.9G 1% /root/test-expand

 

 

 

扩容场景:

 当我们存储空间不够用,我们添加了磁盘怎么扩容后添加得磁盘到LVM 中? 

 

1.  将扩容得磁盘格式化分区  (怎么分区可以参照上述中分区来操作)

linux-LVM 逻辑卷

 

 

 

2. 将磁盘添加到我们得物理卷中

root@testyk:~# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created 

检查我们后添加得物理卷

root@testyk:~# pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name test-VG
PV Size 5.23 GiB / not usable 1.19 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 1339
Free PE 0
Allocated PE 1339
PV UUID 0NmONH-VyzK-DbHR-4G14-18x5-QOLs-Z2txcd

--- Physical volume ---
PV Name /dev/sdb2
VG Name test-VG
PV Size 4.77 GiB / not usable 1.81 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 1220
Free PE 1023
Allocated PE 197
PV UUID oH2A3j-UbLd-0LJb-xoEC-uBAH-RJID-Y5KZLs

--- Physical volume ---
PV Name /dev/sda5
VG Name testyk-vg
PV Size 49.76 GiB / not usable 2.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 12738
Free PE 8
Allocated PE 12730
PV UUID ACKWc1-aUrg-3gBH-a8vJ-DF9C-G07U-OHHl7n

"/dev/sdc1" is a new physical volume of "2.62 GiB"
--- NEW Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 2.62 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID Noak11-dvyq-PYvb-ZkEh-rOUT-Pcgl-LEHgDT

 

3. 将我们得物理卷添加到我们物理卷组中

这里得test-VG 卷组是我们上面实验创建得物理卷组

root@testyk:~# vgextend test-VG /dev/sdc1
Volume group "test-VG" successfully extended

 

查看我们物理卷组

root@testyk:~# vgdisplay
--- Volume group ---
VG Name test-VG
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 3
Act PV 3
VG Size 12.61 GiB            #这里得VG 明显扩大了2.6 G 可以上上面VG 查询得对比
PE Size 4.00 MiB
Total PE 3228
Alloc PE / Size 1536 / 6.00 GiB
Free PE / Size 1692 / 6.61 GiB
VG UUID e2dkt4-3ZAf-Q5Lk-ifhe-2Qck-h3Nz-Sxp21B

--- Volume group ---
VG Name testyk-vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 49.76 GiB
PE Size 4.00 MiB
Total PE 12738
Alloc PE / Size 12730 / 49.73 GiB
Free PE / Size 8 / 32.00 MiB
VG UUID c3tzX1-wxuo-ztDj-Jmtt-0M3W-DzkP-7Mpfpl

 

 

4. 增加逻辑卷空间 

当前我们得lv 卷得空间

root@testyk:~# df -TH
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.6G 4.1k 1.6G 1% /dev
tmpfs tmpfs 314M 721k 313M 1% /run
/dev/dm-0 ext4 52G 20G 29G 41% /
none tmpfs 4.1k 0 4.1k 0% /sys/fs/cgroup
none tmpfs 5.3M 0 5.3M 0% /run/lock
none tmpfs 1.6G 0 1.6G 0% /run/shm
none tmpfs 105M 0 105M 0% /run/user
/dev/sda1 ext2 247M 55M 180M 24% /boot
/dev/mapper/test--VG-test--lv ext4 6.3G 13M 5.9G 1% /root/test-expand

 

将我逻辑卷扩容空间 到12G   -L 12G 这个12G 是扩容后LV 卷得大小

root@testyk:~# lvextend -L 12G /dev/test-VG/test-lv
Extending logical volume test-lv to 12.00 GiB
Logical volume test-lv successfully resized

 

扩容后需要更新逻辑卷信息,否正我们挂载目录上查看到得空间不会更新成我扩容之后得

root@testyk:~# resize2fs /dev/test-VG/test-lv
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/test-VG/test-lv is mounted on /root/test-expand; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/test-VG/test-lv is now 3145728 blocks long.

 

检查

root@testyk:~# df -TH
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.6G 4.1k 1.6G 1% /dev
tmpfs tmpfs 314M 721k 313M 1% /run
/dev/dm-0 ext4 52G 20G 29G 41% /
none tmpfs 4.1k 0 4.1k 0% /sys/fs/cgroup
none tmpfs 5.3M 0 5.3M 0% /run/lock
none tmpfs 1.6G 0 1.6G 0% /run/shm
none tmpfs 105M 0 105M 0% /run/user
/dev/sda1 ext2 247M 55M 180M 24% /boot
/dev/mapper/test--VG-test--lv ext4 13G 16M 12G 1% /root/test-expand

 

 

 

 

 

缩容

当我们的空间释放出来后我们的disk 也可以将空间释放出来。 但是不建议使用因为在释放空间缩容时可能造成一定的数据损坏(这个我没有在生产环境验证过,只是听同事过)。

 

1. 在我们缩容逻辑卷挂载的目录上创建一个3 G 大小的文件

root@testyk:~/test-expand# dd if=/dev/zero of=test bs=3M count=1000
1000+0 records in
1000+0 records out
3145728000 bytes (3.1 GB) copied, 2.40726 s, 1.3 GB/s

验证MD5值 

root@testyk:~/test-expand# md5sum test
4322d422199eaccfbd36feb3da955b16 test

 

2. 将逻辑卷解挂载

root@testyk:~# umount /root/test-expand

 

3. 扫描逻辑卷上的空余空间

root@testyk:~# e2fsck -f /dev/test-VG/test-lv
e2fsck 1.42.9 (4-Feb-2014)
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/test-VG/test-lv: 12/393216 files (0.0% non-contiguous), 828527/1572864 blocks

 

4. 设备文件减少到4G 大小

root@testyk:~# resize2fs /dev/test-VG/test-lv 4G
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/test-VG/test-lv to 1048576 (4k) blocks.
The filesystem on /dev/test-VG/test-lv is now 1048576 blocks long.

 

5. 将逻辑卷减少到4G 
root@testyk:~# lvreduce -L 4G /dev/test-VG/test-lv
WARNING: Reducing active logical volume to 4.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce test-lv? [y/n]: y
Reducing logical volume test-lv to 4.00 GiB
Logical volume test-lv successfully resized
root@testyk:~#

 

6. 挂载逻辑卷
root@testyk:~# mount /dev/test-VG/test-lv /root/test-expand/
root@testyk:~# cd test-expand/

检查MD5 值
root@testyk:~/test-expand# md5sum test
4322d422199eaccfbd36feb3da955b16 test

检查逻辑卷大小

root@testyk:~/test-expand# df -TH
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.6G 4.1k 1.6G 1% /dev
tmpfs tmpfs 314M 721k 313M 1% /run
/dev/dm-0 ext4 52G 20G 29G 41% /
none tmpfs 4.1k 0 4.1k 0% /sys/fs/cgroup
none tmpfs 5.3M 0 5.3M 0% /run/lock
none tmpfs 1.6G 0 1.6G 0% /run/shm
none tmpfs 105M 0 105M 0% /run/user
/dev/sda1 ext2 247M 55M 180M 24% /boot
/dev/mapper/test--VG-test--lv ext4 4.1G 3.2G 736M 82% /root/test-expand

 

linux-LVM 逻辑卷

上一篇:Linux更换镜像源


下一篇:linux 查看操作系统