LVM原理及配置
2.修改/etc/fstab里面逻辑卷的挂载信息,否则系统有可能启动不起来。
一、LVM简介
LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由Heinz Mauelshagen在Linux 2.4内核上实现。LVM将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将其它的硬盘的分区加入其中,这样可以实现磁盘空间的动态管理,相对于普通的磁盘分区有很大的灵活性。
与传统的磁盘与分区相比,LVM为计算机提供了更高层次的磁盘存储。它使系统管理员可以更方便的为应用与用户分配存储空间。在LVM管理下的存储卷可以按需要随时改变大小与移除(可能需对文件系统工具进行升级)。LVM也允许按用户组对存储卷进行管理,允许管理员用更直观的名称(如"sales'、 'development')代替物理磁盘名(如'sda'、'sdb')来标识存储卷
二、LVM基本术语
物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。
物理卷(physical volume,PV):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
卷组(Volume Group,VG):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
逻辑卷(logical volume,LV):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
物理块(physical extent,PE):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
逻辑块(logical extent,LE):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
如下图所示PV、VG、LV三者关系:
三、安装LVM
首先确定是否安装了LVM工具
安装lvm
[[email protected] ~]# yum -y install lvm2
四、创建和管理LVM
4.1、LVM逻辑卷的创建
1.创建分区
首先从磁盘中创建三个分区,为接下来做LVM做准备,大小都为5G,然后同步磁盘
[[email protected] ~]# fdisk /dev/sdb
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-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G
Partition 1 of type Linux and of size 5 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
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): 2
First sector (10487808-41943039, default 10487808):
Using default value 10487808
Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
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 (20973568-41943039, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +5G
Partition 3 of type Linux and of size 5 GiB is set
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): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
2.创建物理卷
[[email protected] ~]# pvcreate /dev/sdb{1,2,3}
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
Physical volume "/dev/sdb3" successfully created.
[[email protected] ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 5.00g 5.00g
/dev/sdb2 lvm2 --- 5.00g 5.00g
/dev/sdb3 lvm2 --- 5.00g 5.00g
3.创建卷组
[[email protected] ~]# vgcreate vg /dev/sdb{1,2,3}
Volume group "vg" successfully created
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg 3 0 0 wz--n- <14.99g <14.99g
4.创建逻辑卷
[[email protected] ~]# lvcreate -L 8G -n lv vg
Logical volume "lv" created.
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv vg -wi-a----- 8.00g
5.格式化逻辑卷为ext4格式
[[email protected] ~]# mkfs.ext4 /dev/vg/lv
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
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 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, 1605632
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
6.设置开机自动挂载
先使用blkid获取此逻辑卷的UUID,然后在/etc/fstab中写入以下内容,即可实现开机自动挂载
[[email protected] ~]# blkid | grep /dev/mapper/vg-lv
/dev/mapper/vg-lv: UUID="0dfa016c-4289-454e-b783-c35dd5f0823b" TYPE="ext4"
[[email protected] ~]# echo "UUID="0dfa016c-4289-454e-b783-c35dd5f0823b" /opt ext4 defaults 0 0" >> /etc/fstab
[[email protected] ~]# mount -a
[[email protected] ~]# df -h | grep /dev/mapper/vg-lv
/dev/mapper/vg-lv 7.8G 36M 7.3G 1% /opt
4.2、逻辑卷的扩展
给逻辑卷增加空间并不会影响以前空间的使用,所以无需卸载文件系统,直接通过命令lvextend –L +2G /dev/vg/lv或者lvextend –L 12G /dev/vg/lvd 给lv增加2G空间(lvdata1目前是10G空间)设置完成之后,记得使用resize2fs命令来同步文件系统。
[[email protected] ~]# lvextend -L +2G /dev/vg/lv
Size of logical volume vg/lv changed from 10.00 GiB (2560 extents) to 12.00 GiB (3072 extents).
Logical volume vg/lv successfully resized.
[[email protected] ~]# resize2fs /dev/vg/lv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vg/lv is mounted on /opt; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 2
The filesystem on /dev/vg/lv is now 3145728 blocks long.
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- <17.00g
swap rhel -wi-ao---- 2.00g
lv vg -wi-ao---- 12.00g
4.3、卷组的扩展
重新从第二块硬盘上创建一个分区sdb4,并将创建好的分区加入到已经存在的卷组vg中。通过pvs命令查看是否成功。
[[email protected] ~]# pvcreate /dev/sdb4
Physical volume "/dev/sdb4" successfully created.
[[email protected] ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- <19.00g 0
/dev/sdb1 vg lvm2 a-- <5.00g 0
/dev/sdb2 vg lvm2 a-- <5.00g 1.99g
/dev/sdb3 vg lvm2 a-- <5.00g <5.00g
/dev/sdb4 lvm2 --- 2.00g 2.00g
[[email protected] ~]# vgextend vg /dev/sdb4
Volume group "vg" successfully extended
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <19.00g 0
vg 4 1 0 wz--n- 16.98g 8.98g
4.4、逻辑卷的缩小
1.检查逻辑卷的使用情况
缩小文件系统的大小或存在于逻辑卷中的任何文件都是非常重要的,否则可能丢失数据
[[email protected] ~]# df -h | grep /dev/mapper/vg-lv
/dev/mapper/vg-lv 12G 40M 12G 1% /opt
2.卸载逻辑卷
[[email protected] ~]# umount /dev/vg/lv
3.通过e2fsck命令检查逻辑卷上空余的空间
[[email protected] ~]# e2fsck -f /dev/vg/lv
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/vg/lv: 11/786432 files (0.0% non-contiguous), 92368/3145728 blocks
4.缩小文件系统大小
[[email protected] ~]# resize2fs /dev/vg/lv 3G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/vg/lv to 786432 (4k) blocks.
The filesystem on /dev/vg/lv is now 786432 blocks long.
5.缩小逻辑卷的大小
[[email protected] ~]# lvreduce -L 3G /dev/vg/lv
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg/lv? [y/n]: y
Size of logical volume vg/lv changed from 12.00 GiB (3072 extents) to 3.00 GiB (768 extents).
Logical volume vg/lv successfully resized.
[[email protected] ~]# mount /dev/vg/lv /opt/
[[email protected] ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- <17.00g
swap rhel -wi-ao---- 2.00g
lv vg -wi-ao---- 3.00g
[[email protected] ~]# df -h | grep /dev/mapper/vg-lv
/dev/mapper/vg-lv 2.9G 24M 2.7G 1% /opt
4.5、整个逻辑卷的删除
1.先通过umount命令卸载掉逻辑卷
[[email protected] ~]# umount /dev/vg/lv
2.修改/etc/fstab里面逻辑卷的挂载信息,否则系统有可能启动不起来。
3.通过lvremove 删除逻辑卷lvdata1
[[email protected] ~]# lvremove /dev/vg/lv
Do you really want to remove active logical volume vg/lv? [y/n]: y
Logical volume "lv" successfully removed
4.通过vgremove 删除卷组vgdata
[[email protected] ~]# vgremove vg
Volume group "vg" successfully removed
5.通过pvremove 将物理卷转化成普通分区
[[email protected] ~]# pvremove /dev/sdb{1,2,3}
Labels on physical volume "/dev/sdb1" successfully wiped.
Labels on physical volume "/dev/sdb2" successfully wiped.
Labels on physical volume "/dev/sdb3" successfully wiped.
6.修改分区的ID表示为普通分区
[[email protected] ~]# fdisk /dev/sdb
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): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x829dbe14
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 20973567 5242880 8e Linux LVM
/dev/sdb3 20973568 31459327 5242880 8e Linux LVM
Command (m for help): t
Partition number (1-3, default 3): 1
Hex code (type L to list all codes): 83
Changed type of partition 'Linux LVM' to 'Linux'
Command (m for help): t
Partition number (1-3, default 3): 2
Hex code (type L to list all codes): 83
Changed type of partition 'Linux LVM' to 'Linux'
Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 83
Changed type of partition 'Linux LVM' to 'Linux'
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
五、swap分区的扩展
SWAP称之为交换分区,相当于Windows系统下的虚拟内存。作用就是当我们系统中的内存不够用的时候,可以通过SWAP来增加内存。安装Linux系统的时候,必须的两个分区一个是根,一个是SWAP。如果安装系统的时候,SWAP分区分小了的话,需要增加大小,那么接下来的内容就是这个:SWAP分区的创建/扩展。
5.1、利用磁盘的剩余空间
前提就是磁盘还有剩余空间没有划分,或者是已经划分的分区没有使用。这个时候才能使用此方法。
1.首先分区,修改ID号
[[email protected] ~]# fdisk /dev/sdb
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): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x829dbe14
Device Boot Start End Blocks Id System
/dev/sdb1 2048 10487807 5242880 8e Linux LVM
/dev/sdb2 10487808 20973567 5242880 8e Linux LVM
/dev/sdb3 20973568 31459327 5242880 8e Linux LVM
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
First sector (31459328-41943039, default 31459328):
Using default value 31459328
Last sector, +sectors or +size{K,M,G} (31459328-41943039, default 41943039): +2G
Partition 4 of type Linux and of size 2 GiB is set
Command (m for help): t
Partition number (1-4, default 4): 4
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
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.
2.同步磁盘,让内核识别到新创建的分区
[[email protected] ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
3.格式化swap分区
[[email protected] ~]# mkswap /dev/sdb4
Setting up swapspace version 1, size = 2097148 KiB
no label, UUID=316c0550-b933-4c1c-bfba-0c6fbf05cdac
4.**swap分区
[[email protected] ~]# swapon /dev/sdb4
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
/dev/sdb4 partition 2097148 0 -2
5.写入/etc/fstab,开启自动挂载
[[email protected] ~]# echo "UUID=316c0550-b933-4c1c-bfba-0c6fbf05cdac swap swap defaults 0 0" >> /etc/fstab
[[email protected] ~]# mount -a
5.2、利用分区的剩余空间
1.查看哪个分区比较大
2.创建一个1G的文件,名字为根目录下的swapfile
[[email protected] ~]# dd if=/dev/zero of=/swapfile bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB) copied, 5.72724 s, 187 MB/s
[[email protected] ~]# ll -h /swapfile
-rw-r--r--. 1 root root 1.0G Feb 25 17:45 /swapfile
3.格式化创建的swapfile文件
[[email protected] ~]# mkswap /swapfile
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=86df4c74-eaae-46d8-a562-92c04005a429
4.**swap
[[email protected] ~]# swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
[[email protected] ~]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 2097148 0 -1
/swapfile file 1048572 0 -2
5.写入/etc/fstab,实现开机自动挂载
[[email protected] ~]# echo "/swapfile swap swap defaults 0 0" >> /etc/fstab
[[email protected] ~]# mount -a
注:数字越大,优先级越高
[[email protected] ~]# swapon
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 2G 0B -1
/swapfile file 1024M 0B -2
/dev/sdb4 partition 2G 0B -3