相关知识点
LVM: Logical Volume Manager逻辑卷管理,它是一种磁盘的管理方式,是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区。
卷组volume group (VG)
卷组是LVM中最高抽象层,是由一个或多个物理卷所组成的存储器池。
物理卷physical volume (PV)
典型的物理卷是硬盘分区,但也可以是整个硬盘或已创建的Software RAID 卷。
逻辑卷logical volume (LV)
逻辑卷相当于非LVM系统中的分区,它在卷组上建立,是一个标准的块设备,可以在其上建立文件系统。
物理块physical extent (PE)
物理卷按大小相等的“块”为单位存储,块的大小与卷组中逻辑卷块的大小相同。
逻辑块logical extent (LE)
逻辑卷按“块”为单位存储,在一卷组中的所有逻辑卷的块大小是相同的。
作业环境
注:以下的操作是在VirtualBox4.1虚拟机环境下进行的。
系统版本:CentOS 5.8
硬盘大小(虚拟硬盘):20G
一、先查看系统空间使用情况。
根据关注点的不同,使用不同的使命查看系统空间的使用情况。
查看磁盘的分区信息,可以看到,系统除了引导分区(100M)外,其他所有的空间都在LVM里。
[root@ www.linuxidc.com ~]# fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 13 104391 83 Linux
/dev/sda2 14 2610 20860402+ 8e Linux LVM
查看节点的使用情况
[root@ www.linuxidc.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-lv_root
9.7G 926M 8.3G 10% /
/dev/sda1 99M 13M 82M 13% /boot
tmpfs 506M 0 506M 0% /dev/shm
查看逻辑卷组信息,可以看到,vg中有7.88G的剩余空间。
[root@ www.linuxidc.com ~]# vgscan
Reading all physical volumes. This may take a while…
Found volume group “VolGroup00” using metadata type lvm2
[root@ www.linuxidc.com ~]# vgdisplay
— Volume group —
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE / Size 448 / 12.00 GB
Free PE / Size 188 / 7.88 GB
VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH
查看逻辑卷信息,可以看到,根分区大小为10G,交换分区(swap)大小为2G。
[root@ www.linuxidc.com ~]# lvscan
ACTIVE ‘/dev/VolGroup00/lv_root’ [10.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_swap’ [2.00 GB] inherit
二、装机时,swap分区大小设置为2G,现在发现不够用,需要将其扩大到4G。扩大swap逻辑卷
步骤:首先取消激活swap空间,然后修改swap分区大小,接着将swap分区设置为交换区,最后激活swap空间。
[root@ www.linuxidc.com ~]# swapoff
usage: swapoff [-hV]
swapoff -a [-v]
swapoff [-v] special …
[root@ www.linuxidc.com ~]# lvresize -L 4G /dev/VolGroup00/lv_swap #指定大小增加到4G
Extending logical volume lv_swap to 4.00 GB
Logical volume lv_swap successfully resized
[root@ www.linuxidc.com ~]# mkswap -f /dev/VolGroup00/lv_swap
Setting up swapspace version 1, size = 4294963 kB
[root@ www.linuxidc.com ~]# swapon
usage: swapon [-hV]
swapon -a [-e] [-v]
swapon [-v] [-p priority] special|LABEL=volume_name …
swapon [-s]
(注:如果缩小swap逻辑卷,其操作步骤与上述步骤相同。)
操作完成后,查看当前的lv、vg信息,可以看到lv_swap现在已经是4G了,而逻辑卷的空闲大小由先前的7.88G,变为5.88G。
[root@ www.linuxidc.com ~]# lvscan
ACTIVE ‘/dev/VolGroup00/lv_root’ [10.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_swap’ [4.00 GB] inherit
[root@ www.linuxidc.com ~]# vgdisplay
— Volume group —
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 6
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE / Size 448 / 14.00 GB
Free PE / Size 188 / 5.88 GB
VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH
三、现在要将磁盘剩余空间利用起来。
在/dev/VolGroup00上新增一个逻辑分区,命名为lv_data,大小为逻辑卷组VolGroup00中剩余的空间(5.88G)。
新建逻辑卷lv_data
步骤:创建新分区;接着格式化新分区;然后挂载新分区(Linux上不能直接使用分区,使用分区时是通过目录存储的,故需将lv_data挂载到一个目录上)
(注:以下操作,本想一下子使用剩余的所有空间5.88G,但由于系统数据与显示数据(G级字节)上的出入,输入的数字5.88G过大,后改为5.86G)
[root@ www.linuxidc.com ~]# lvcreate -L 5.88G -n lv_data /dev/VolGroup00
Rounding up size to full physical extent 5.91 GB
Volume group “VolGroup00” has insufficient free space (188 extents): 189 required.
[root@ www.linuxidc.com ~]# lvcreate -L 5.86G -n lv_data /dev/VolGroup00
Rounding up size to full physical extent 5.88 GB
Logical volume “lv_data” created
[root@ www.linuxidc.com ~]# mkfs.ext3 /dev/VolGroup00/lv_data
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
770048 inodes, 1540096 blocks
77004 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1577058304
47 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
查看此时的vg信息,发现剩余空间刚好用完。
[root@ www.linuxidc.com ~]# vgdisplay
— Volume group —
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 7
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 3
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE / Size 636 / 19.88 GB
Free PE / Size 0 / 0
VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH
[root@ www.linuxidc.com ~]# mkdir /home/data
[root@ www.linuxidc.com ~]# mount /dev/VolGroup00/lv_data /home/data
[root@ www.linuxidc.com ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-lv_root
9.7G 926M 8.3G 10% /
/dev/sda1 99M 13M 82M 13% /boot
tmpfs 506M 0 506M 0% /dev/shm
/dev/mapper/VolGroup00-lv_data
5.8G 140M 5.4G 3% /home/data四、假设,为了方便管理,要在lv_data逻辑卷上挤出一部分空间(只留3G),用来新建一个逻辑卷lv_develop。那么,需要先缩小lv_data,然后再建lv_develop。出于数据安全的考虑,宜将逻辑卷从目录中卸载下来,再进行大小调整。
这里为了验证测试,在卸载之前,先在/home/data目录下创建一些文件。
[root@ www.linuxidc.com ~]# cd /home/data/
[root@ www.linuxidc.com data]# touch 001.txt 002.txt 003.txt
[root@ www.linuxidc.com data]# ls #卸载lv_data后,/home/data目录下有数据
001.txt 002.txt 003.txt lost+found
[root@ www.linuxidc.com data]# cd
[root@ www.linuxidc.com ~]# umount /home/data/
[root@ www.linuxidc.com ~]# ls /home/data/ #卸载lv_data后,/home/data目录为空
[root@ www.linuxidc.com ~]#
缩小逻辑卷lv_data
步骤:用e2fsck命令检查分区,再更改文件系统大小,最后缩小逻辑卷大小
[root@ www.linuxidc.com ~]# e2fsck -f /dev/VolGroup00/lv_data
e2fsck 1.39 (29-May-2006)
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/VolGroup00/lv_data: 14/770048 files (7.1% non-contiguous), 59982/1540096 blocks
[root@ www.linuxidc.com ~]# resize2fs -p /dev/VolGroup00/lv_data
resize2fs 1.39 (29-May-2006)
The filesystem is already 1540096 blocks long. Nothing to do!
[root@ www.linuxidc.com ~]# lvresize -L 3G /dev/VolGroup00/lv_data
WARNING: Reducing active logical volume to 3.00 GB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_data? [y/n]: y
Reducing logical volume lv_data to 3.00 GB
Logical volume lv_data successfully resized
查看此时的vg信息,发现已经腾出了2.88G的空间。
[root@ www.linuxidc.com ~]# vgdisplay
— Volume group —
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 8
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 3
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE / Size 544 / 17.00 GB
Free PE / Size 92 / 2.88 GB
VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH
此时的lv_data逻辑卷大小为3G。
[root@ www.linuxidc.com ~]# lvscan
ACTIVE ‘/dev/VolGroup00/lv_root’ [10.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_swap’ [4.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_data’ [3.00 GB] inherit
新建逻辑卷lv_develop
步骤:创建新分区;接着格式化新分区。
(注:吸收上面的教训,逻辑卷的大小不要一下子设为2.88G,而是要小一些,这里设为2.8G)
[root@ www.linuxidc.com ~]# lvcreate -L 2.8G -n lv_develop /dev/VolGroup00
Rounding up size to full physical extent 2.81 GB
Logical volume “lv_develop” created
[root@ www.linuxidc.com ~]# mkfs.ext3 /dev/VolGroup00/lv_develop
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
368736 inodes, 737280 blocks
36864 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=754974720
23 block groups
32768 blocks per group, 32768 fragments per group
16032 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
Lv_develop创建好了,大小了2.81G。
[root@ www.linuxidc.com ~]# lvscan
ACTIVE ‘/dev/VolGroup00/lv_root’ [10.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_swap’ [4.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_data’ [3.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_develop’ [2.81 GB] inherit查看此时的vg信息,发现还有64M的剩余空间。
[root@ www.linuxidc.com ~]# vgdisplay
— Volume group —
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 9
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 4
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE / Size 634 / 19.81 GB
Free PE / Size 2 / 64.00 MB
VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH
为了充分利用空间资源,将64M空闲空间加入到lv_develop中,
扩大逻辑卷lv_develop
步骤:先扩大逻辑分区大小,再更改文件系统以使其扩容
[root@ www.linuxidc.com ~]# lvresize -L +64M /dev/VolGroup00/lv_d #增加64M,注意前面有个加号
lv_data lv_develop
[root@ www.linuxidc.com ~]# lvresize -L +64M /dev/VolGroup00/lv_develop
Extending logical volume lv_develop to 2.88 GB
Logical volume lv_develop successfully resized
[root@ www.linuxidc.com ~]# resize2fs -p /dev/VolGroup00/lv_develop
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/VolGroup00/lv_develop to 753664 (4k) blocks.
The filesystem on /dev/VolGroup00/lv_develop is now 753664 blocks long.
[root@ www.linuxidc.com ~]# lvscan
ACTIVE ‘/dev/VolGroup00/lv_root’ [10.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_swap’ [4.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_data’ [3.00 GB] inherit
ACTIVE ‘/dev/VolGroup00/lv_develop’ [2.88 GB] inherit
查看此时的vg信息,发现剩余空间全部用光了,很好。
[root@ www.linuxidc.com ~]# vgdisplay
— Volume group —
VG Name VolGroup00
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 10
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 4
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.88 GB
PE Size 32.00 MB
Total PE 636
Alloc PE / Size 636 / 19.88 GB
Free PE / Size 0 / 0
VG UUID I6y3EN-nU8P-PWMp-gA32-rJ2W-E361-9zVTGH
将两个逻辑卷挂载到指定目录中。
[root@ www.linuxidc.com ~]# mkdir /home/develop
[root@ www.linuxidc.com ~]# mount /dev/VolGroup00/lv_data /home/data/
[root@ www.linuxidc.com ~]# mount /dev/VolGroup00/lv_develop /home/develop/
检查一下lv_data中数据
(注:lv_data缩小后的空间大小,一定要大于缩小之前已用空间的大小)
[root@ www.linuxidc.com ~]# ls /home/data/ #/home/data目录下的数据全都在
001.txt 002.txt 003.txt lost+found
[root@ www.linuxidc.com ~]# ls /home/develop/ #/home/develop目录为空是当然的
lost+found
至此,LVM相关操作完毕。
为了使系统在重启后自动挂载逻辑卷,需要挂载lv_data、lv_develop的信息写入到/etc/fstab中,/etc/fstab修改后的内容如下:
[root@ www.linuxidc.com ~]# cat /etc/fstab
/dev/VolGroup00/lv_root / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/lv_swap swap swap defaults 0 0
/dev/VolGroup00/lv_data /home/data ext3 defaults 0 0
/dev/VolGroup00/lv_develop /home/develop ext3 defaults 0 0
以上操作,一年前就已经学会了的,不过不常玩,便生疏了,如今重温,撰写此笔记。
实际工作中,会遇加硬盘的情况,要先创建分区(主分区、扩展分区)、格化、挂载,然后,才能使用以上命令进行LVM管理。
CentOS 5 LVM逻辑卷管理
转载请注明出处:服务器评测 » CentOS 5 LVM逻辑卷管理