逻辑卷管理lvm

逻辑卷管理LVM

一 创建逻辑卷

1准备分区或硬盘

这里使用/dev/sdb、/dev/sdc两块硬盘和/dev/sda9、/dev/sda10两个分区,大小都为1G,磁盘有限,我也不想这么抠的。

添加分区/dev/sda9、/dev/sda10

[root@centos7 ~]# fdisk /dev/sda

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
All primary partitions are in use
Adding logical partition 9
First sector (31885312-41943039, default 31885312):
Using default value 31885312
Last sector, +sectors or +size{K,M,G} (31885312-41943039, default 41943039): +1G
Partition 9 of type Linux and of size 1 GiB is set

注意,要修改分区类型为Linux LVM

Command (m for help): t
Partition number (1-9, default 9): 8e
Partition number (1-9, default 9): 9
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help):

同样的方法创建/dev/sda10。

[root@centos7 ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda9 8:9 0 1G 0 part
└─sda10 8:10 0 1G 0 part
sdb 8:16 0 1G 0 disk
sdc 8:32 0 1G 0 disk

2 创建PV(物理卷)

命令格式为:pvcreate DEVICE,可以一次创建一个设备,也可以一次创建多个设备。

查看物理卷命令:pvdisplay(查看详细信息)、pvs(查看简易信息)

[root@centos7 ~]# pvcreate /dev/sda9

Physical volume "/dev/sda9" successfully created.
[root@centos7 ~]# pvcreate /dev/sda10 /dev/sdb /dev/sdc
Physical volume "/dev/sda10" successfully created.
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.

查看物理卷,看到已经成功创建。

[root@centos7 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda10 lvm2 --- 1.00g 1.00g
/dev/sda9 lvm2 --- 1.00g 1.00g
/dev/sdb lvm2 --- 1.00g 1.00g
/dev/sdc lvm2 --- 1.00g 1.00g

3 创建卷组(VG)

命令格式为:vgcreate -s PE VGNAME(vg名) DEVICE(设备名)

-s :指定物理扩展块大小 既就是PE的大小

查看命令:vgs、vgdisplay

[root@centos7 ~]# vgcreate -s 16M vg0 /dev/sda9 /dev/sda10 /dev/sdb /dev/sdc
Volume group "vg0" successfully created

查看一下

[root@centos7 ~]# vgs

VG #PV #LV #SN Attr VSize VFree
vg0 4 0 0 wz--n- 3.94g 3.94g

再来看下详细信息

[root@centos7 ~]# vgdisplay

--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 4
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 4
Act PV 4
VG Size 3.94 GiB
PE Size 16.00 MiB
Total PE 252
Alloc PE / Size 0 / 0
Free PE / Size 252 / 3.94 GiB
VG UUID qmoIMg-5wQR-GKCS-dpq9-HLrt-zVIi-JBC9ZX

一切都复合预期,大小约为4G,正好是两个分区和两个硬盘之和。

4创建逻辑卷(LV)

命令格式为:lvcreate -n LVNAME(指定LV名) -L SIZE(指定LV的大小) vg0(要使用的VG)

-l 指定PE 大小

查看命令(类似PV、VG):lvs、lvdisplay

[root@centos7 ~]# lvcreate -n lv0 -L 3G vg0

Logical volume "lv0" created.

创建成功,查看一下

[root@centos7 ~]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 3.00g

再看下详细信息

[root@centos7 ~]# lvdisplay

--- Logical volume ---
LV Path /dev/vg0/lv0
LV Name lv0
VG Name vg0
LV UUID 622i7m-uy6s-3nZI-8xSb-sxrR-cZzz-C9pwkd
LV Write Access read/write
LV Creation host, time centos7.3.loacl, 2017-04-25 16:25:13 +0800
LV Status available
# open 0
LV Size 3.00 GiB
Current LE 192
Segments 4
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0

可以确定已经成功创建。

现在就可以把/dev/vg0/lv0当做一块硬盘使用了。

现在看一下磁盘信息,已经发生了变化。

[root@centos7 ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 20G 0 disk
├─sda9 8:9 0 1G 0 part
│ └─vg0-lv0 253:0 0 3G 0 lvm
└─sda10 8:10 0 1G 0 part
└─vg0-lv0 253:0 0 3G 0 lvm
sdb 8:16 0 1G 0 disk
└─vg0-lv0 253:0 0 3G 0 lvm
sdc 8:32 0 1G 0 disk
└─vg0-lv0 253:0 0 3G 0 lvm

可以看到,所加入的分区和硬盘已经属于vg0-lv0了。

5 创建文件系统,格式化

mkfs.ext4 /dev/vg0/lv0

[root@centos7 ~]# mkfs.ext4 /dev/vg0/lv0

6 挂载使用

[root@centos7 ~]# mkdir /mnt/lv0

[root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0

 

二 扩展LV

 

扩展命令格式:

lvextend -L 100G /dev/vg0/lv0

lvextend -l +100G /dev/vg0/lv0

lvextend -l +100%FREE /dev/vg0/lv0

注意 –L 为扩展到多少 ,-l 为扩展(增加)多少,+100%FREE为扩展剩余vg的100%,可根据个人情况进行选择。

由于vg0还预留将近1G 空间,现在扩展一下lv0

1 先来查看原有LV

[root@centos7 ~]# vgs

VG #PV #LV #SN Attr VSize VFree
vg0 4 1 0 wz--n- 3.94g 960.00m

2 扩展LV

[root@centos7 ~]# lvextend -r -l +100%FREE /dev/vg0/lv0

-r为resizefs 一步搞定多方便

或者麻烦一点,一步一步来

[root@centos7 ~]# lvextend -l +100%FREE /dev/vg0/lv0

Size of logical volume vg0/lv0 changed from 3.00 GiB (192 extents) to 3.94 GiB (252 extents).
Logical volume vg0/lv0 successfully resized.

3 下来重定义文件系统大小

ext系列文件系统重定义:resize2fs /dev/vg0/lv0

xfs系列文件系统重定义:xfs_growfs /dev/vg0/lv0

[root@centos7 ~]# resize2fs /dev/vg0/lv0

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg0/lv0 is mounted on /mnt/lv0; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg0/lv0 is now 1032192 blocks long.

4 检查文件系统完成性(也可不做)

[root@centos7 ~]# fsck.ext4 /dev/vg0/lv0

e2fsck 1.42.9 (28-Dec-2013)
/dev/vg0/lv0: clean, 20/262144 files, 35918/1032192 blocks

5 确认逻辑卷大小

[root@centos7 ~]# df -h /dev/vg0/lv0

Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg0-lv0 3.9G 12M 3.6G 1% /mnt/lv0

很明显已经扩展成功。

6 挂载使用

[root@centos7 ~]# mount -o remount /dev/vg0/lv0

 

三 缩减LV

 

1 备份数据

虽然一般不会丢失数据,但是为了保险起见。还是先备份为好。

看下/mnt/lv0目录下现有的数据

[root@centos7 ~]# ls /mnt/lv0/

lost+found myfile2 myfile4 myfile6 myfile8

myfile1 myfile3 myfile5 myfile7 myfile9

[root@centos7 ~]# mkdir /backup

[root@centos7 ~]# cp -a /mnt/lv0/* /backup/

2 卸载/dev/vg0/lv0

[root@centos7 ~]# umount /mnt/lv0/

3 检查文件系统

e2fsck -f /dev/vg0/lv0 在重新定义文件系统大小之前必须检查磁盘完成性

[root@centos7 ~]# 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: 20/262144 files (0.0% non-contiguous), 35918/1032192 blocks

4下来重定义文件系统大小

resize2fs /dev/vg0/lv0 2G #2G位缩减后的LV 大小。

[root@centos7 ~]# resize2fs /dev/vg0/lv0 2G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg0/lv0 to 524288 (4k) blocks.
The filesystem on /dev/vg0/lv0 is now 524288 blocks long.

5 缩减LV

lvreduce -L 2G /dev/vg0/lv0 #缩减到2G

[root@centos7 ~]# lvreduce -L 2G /dev/vg0/lv0

WARNING: Reducing active logical volume to 2.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv0? [y/n]: y
Size of logical volume vg0/lv0 changed from 3.94 GiB (252 extents) to 2.00 GiB (128 extents).
Logical volume vg0/lv0 successfully resized.

6 挂载使用

mount /dev/vg0/lv0 /mnt/lv0

[root@centos7 ~]# mount /dev/vg0/lv0 /mnt/lv0/

查看磁盘挂载情况

[root@centos7 ~]# df -h /dev/vg0/lv0

Filesystem Size Used Avail Use% Mounted on

/dev/mapper/vg0-lv0 2.0G 9.0M 1.8G 1% /mnt/lv0

7 确认数据是否丢失

现在再来开心目录/mnt/lv0 的内容

[root@centos7 ~]# ls /mnt/lv0/

lost+found myfile2 myfile4 myfile6 myfile8
myfile1 myfile3 myfile5 myfile7 myfile9

 

很完美,数据并没有丢失!

posted @ 2017-06-08 16:16  Sunzz  阅读(330)  评论(0编辑  收藏  举报