LVM简介:
Lofical Volume Manger 逻辑卷管理。
LVM将一个或者多个硬盘分区在逻辑集上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,具有很高的灵活性。
创建和管理LVM
步骤:
1、创建分区 fdisk
2、创建逻辑卷 pv
3、将物理卷转化为卷组 vg
4、将卷组生成新的逻辑卷 lv
5、格式化 mkfs
6、挂载 mount
1、创建分区:
使用分区工具(如fdisk等),创建LVM分区,与其它分区创建类型相同,区别仅仅是LVM的分区类型为8e
[root@localhost ~]# fdisk /dev/sda
先创建一个扩展分区:(没有扩展分区无法创建逻辑分区)
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (3,4, default 3):
First sector (52447232-125829119, default 52447232):
Using default value 52447232
Last sector, +sectors or +size{K,M,G} (52447232-125829119, default 125829119): +10G
Partition 3 of type Extended and of size 10 GiB is set
创建了两个逻辑分区:
Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (52449280-73418751, default 52449280):
Using default value 52449280
Last sector, +sectors or +size{K,M,G} (52449280-73418751, default 73418751): +2G
Partition 5 of type Linux and of size 2 GiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (56645632-73418751, default 56645632):
Using default value 56645632
Last sector, +sectors or +size{K,M,G} (56645632-73418751, default 73418751): +2G
Partition 6 of type Linux and of size 2 GiB is set
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x0001e85f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 52447231 25174016 8e Linux LVM
/dev/sda3 52447232 73418751 10485760 5 Extended
/dev/sda5 52449280 56643583 2097152 83 Linux
/dev/sda6 56645632 60839935 2097152 83 Linux
更改分区类型: 8e为Linux LVM
Command (m for help): t
Partition number (1-3,5,6, default 6):
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,5,6, default 6): 5
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x0001e85f
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 52447231 25174016 8e Linux LVM
/dev/sda3 52447232 73418751 10485760 5 Extended
/dev/sda5 52449280 56643583 2097152 8e Linux LVM
/dev/sda6 56645632 60839935 2097152 8e Linux LVM
保存:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
同步磁盘:
[root@localhost ~]# partprobe
2、创建逻辑卷:
[root@localhost ~]# pvcreate /dev/sda5 /dev/sda6
Physical volume "/dev/sda5" successfully created
Physical volume "/dev/sda6" successfully created
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 24.00g 4.00m
/dev/sda5 lvm2 --- 2.00g 2.00g
/dev/sda6 lvm2 --- 2.00g 2.00g
3、将/dev/sda5 和/dev/sda6 转化为物理卷vgdata:
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- 24.00g 4.00m
[root@localhost ~]# vgcreate vgdata /dev/sda5 /dev/sda6
Volume group "vgdata" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- 24.00g 4.00m
vgdata 2 0 0 wz--n- 3.99g 3.99g
4、从物理卷上分割1G给新的逻辑卷lvdata:
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 20.00g
swap rhel -wi-ao---- 4.00g
[root@localhost ~]# lvcreate -L 1G -n lvdata vgdata
Logical volume "lvdata" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 20.00g
swap rhel -wi-ao---- 4.00g
lvdata vgdata -wi-a----- 1.00g
[root@localhost ~]#
5、使用(mkfs.ext4命令)在逻辑卷lvdata上创建ext4文件系统
[root@localhost ~]# mkfs.ext4 /dev/vgdata/lvdata
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
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
6、将创建好的文件系统/data1 挂载到/data1 目录上去(创建好后,会在/dev/mapper/生成一个软链接,名字为“卷组-逻辑卷”。
[root@localhost ~]# mkdir /data1
[root@localhost ~]# mount /dev/vgdata/lvdata /data1
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 20G 3.6G 17G 18% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 140K 1.9G 1% /dev/shm
tmpfs 1.9G 9.0M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 165M 850M 17% /boot
tmpfs 378M 28K 378M 1% /run/user/0
/dev/sr0 3.8G 3.8G 0 100% /mnt
/dev/mapper/vgdata-lvdata 976M 2.6M 907M 1% /data1
[root@localhost ~]# ll /dev/mapper/
lrwxrwxrwx. 1 root root 7 Aug 7 14:16 vgdata-lvdata -> ../dm-2
[root@localhost ~]# ll /dev/vgdata/lvdata
lrwxrwxrwx. 1 root root 7 Aug 7 14:16 /dev/vgdata/lvdata -> ../dm-2
便于以后服务器启动重新挂载,需要将创建好的文件系统挂载信息添加到/etc/fstab
里面,UUID可以通过 blkid
查询
[root@localhost ~]# blkid
/dev/mapper/vgdata-lvdata: UUID="644eb577-ab63-44f8-8319-700ba1c46ea3" TYPE="ext4"
[root@localhost ~]# vim /etc/fstab
UID=644eb577-ab63-44f8-8319-700ba1c46ea3 /data1 ext4 defaults 0 0
如何扩展LVM
给逻辑卷增加空间不会影响以前的使用,所以无需卸载文件系统。
增加500M空间:
[root@localhost ~]# lvextend -L +500M /dev/vgdata/lvdata
Size of logical volume vgdata/lvdata changed from 1.00 GiB (256 extents) to 1.49 GiB (381 extents).
Logical volume lvdata successfully resized.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 20.00g
swap rhel -wi-ao---- 4.00g
lvdata vgdata -wi-ao---- 1.49g
使用resize2fs来同步文件系统:
[root@localhost ~]# resize2fs /dev/vgdata/lvdata
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vgdata/lvdata is mounted on /data1; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vgdata/lvdata is now 390144 blocks long.
为扩展前:
/dev/mapper/vgdata-lvdata 976M 2.6M 907M 1% /data1
扩展后:
/dev/mapper/vgdata-lvdata 1.5G 3.0M 1.4G 1% /data1
如何扩大卷组
创建新的逻辑卷:
/dev/sda7 60841984 65036287 2097152 8e Linux LVM
[root@localhost ~]# partprobe
将逻辑卷添加到卷组中:
[root@localhost ~]# vgextend vgdata /dev/sda7
Physical volume "/dev/sda7" successfully created
Volume group "vgdata" successfully extended
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 24.00g 4.00m
/dev/sda5 vgdata lvm2 a-- 2.00g 520.00m
/dev/sda6 vgdata lvm2 a-- 2.00g 2.00g
/dev/sda7 vgdata lvm2 a-- 2.00g 2.00g
释放逻辑卷
当硬盘空间不够的情况下,减少逻辑卷空间,释放给其它逻辑卷使用。
步骤:
1、先卸载逻辑卷lvdata
2、通过e2fsck命令检测逻辑卷上的空余空间
3、使用resize2fs将文件系统减少到 1.3G
4、在使用lvreduce 命令将逻辑卷减少到 1.3G
注意:文件系统大小和逻辑卷大小一定要保持一致,如果逻辑卷大于文件系统,由于部分区域未格式化成文件系统,就会造成空间浪费。
如果逻辑卷小于文件系统,数据会出现问题。
刚才不小心将vgdata-lvdata删除了,重新创建了vgdata-lvdata1,大小为1G
卸载:
[root@localhost ~]# unmount /data1/
查看空间使用情况:
[root@localhost ~]# e2fsck -f /dev/mapper/vgdata-lvdata1
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/mapper/vgdata-lvdata1: 11/65536 files (0.0% non-contiguous), 12955/262144 blocks
减少到700M:
[root@localhost ~]# lvreduce -L 700M /dev/vgdata/lvdata1
WARNING: Reducing active logical volume to 700.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvdata1? [y/n]: y
Size of logical volume vgdata/lvdata1 changed from 1.00 GiB (256 extents) to 700.00 MiB (175 extents).
Logical volume lvdata1 successfully resized.
挂载使用:
[root@localhost ~]# mount /dev/vgdata/lvdata1 /data1
[root@localhost ~]# df -h
/dev/mapper/vgdata-lvdata1 673M 1.4M 623M 1% /data1
数据转移
如果某一块磁盘发生了故障,如何将数据快速的转移到相同的卷组其它空间去
步骤:
1、通过pvmove命令转移空间数据
2、通过vgreduce命令将即将坏掉的磁盘或者分区从vgdata中移除出去
3、通过pvremove命令将坏的磁盘或者分区从系统中删除掉
将dev/sda5中的数据转移到/dev/sda6中:
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 24.00g 4.00m
/dev/sda5 vgdata lvm2 a-- 2.00g 1.31g
/dev/sda6 vgdata lvm2 a-- 2.00g 2.00g
/dev/sda7 vgdata lvm2 a-- 2.00g 2.00g
[root@localhost ~]# pvmove /dev/sda5 /dev/sda6
/dev/sda5: Moved: 1.1%
/dev/sda5: Moved: 100.0%
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 24.00g 4.00m
/dev/sda5 vgdata lvm2 a-- 2.00g 2.00g
/dev/sda6 vgdata lvm2 a-- 2.00g 1.31g
/dev/sda7 vgdata lvm2 a-- 2.00g 2.00g
将/dev/sda5从卷组中移除:
[root@localhost ~]# vgreduce vgdata /dev/sda5
Removed "/dev/sda5" from volume group "vgdata"
[root@localhost ~]# pvremove /dev/sda5
Labels on physical volume "/dev/sda5" successfully wiped
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 24.00g 4.00m
/dev/sda6 vgdata lvm2 a-- 2.00g 1.31g
/dev/sda7 vgdata lvm2 a-- 2.00g 2.00g
删除整个逻辑卷
1、通过umount命令卸载lvdata1
2、修改/etc/fstab
里面的逻辑卷挂载信息,否则系统可能会启动不起来
3、通过lvremove删除逻辑卷lvdata1
4、通过vgremove删除卷组vgdata
5、通过pvremove 将物理卷转化成普通分区
卸载:
[root@localhost ~]# umount /dev/mapper/vgdata-lvdata1
删除刚才配置的这一行:
[root@localhost ~]# vim /etc/fstab
UUID=644eb577-ab63-44f8-8319-700ba1c46ea3 /data1 ext4 defaults 0 0
删除:
[root@localhost ~]# lvremove /dev/vgdata/lvdata1
Do you really want to remove active logical volume lvdata1? [y/n]: y
Logical volume "lvdata1" successfully removed
[root@localhost ~]# vgremove vgdata
Volume group "vgdata" successfully removed
[root@localhost ~]# pvremove /dev/sda6 /dev/sda7
Labels on physical volume "/dev/sda6" successfully wiped
Labels on physical volume "/dev/sda7" successfully wiped