LVM(logical volume management)就是解决磁盘空间不足的问题,必须满足的是其它磁盘分区要有剩余的空间我们才能创建逻辑卷管理。
在逻辑卷管理器中我们会常用到以下:
Pv代表的是物理卷可一块硬盘也可以是一个分区
Vg 代表的是逻辑卷组
Lv 代表的是逻辑卷
我们就以下面新加的硬盘为例来介绍LVM
[root@mxl ~]# fdisk -l
Disk /dev/sda: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 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 144 1052257+ 82 Linux swap / Solaris
/dev/sda3 145 1958 14570955 83 Linux
Disk /dev/sdb: 8589 MB, 8589934592 bytes 新加硬盘1
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 125 1004031 83 Linux
/dev/sdb2 126 250 1004062+ 83 Linux
/dev/sdb3 251 1044 6377805 5 Extended
/dev/sdb5 251 375 1004031 83 Linux
/dev/sdb6 376 500 1004031 83 Linux
/dev/sdb7 501 750 2008093+ 83 Linux
/dev/sdb8 751 1000 2008093+ 83 Linux
Disk /dev/sdc: 8589 MB, 8589934592 bytes 新加硬盘2
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdc1 1 250 2008093+ 83 Linux
/dev/sdc2 251 1044 6377805 5 Extended
/dev/sdc5 251 375 1004031 83 Linux
/dev/sdc6 376 625 2008093+ 83 Linux
You have new mail in /var/spool/mail/root
现在我们要做的是把这二个新加的硬盘创建逻辑卷
创建逻辑卷的方法:
1、 创建分区(fdisk /dev/sdx或hdx)
2、 创建物理卷(pvcreate /dev/sdx or hdx ………………..)
3、 创建卷组(vgcreate mxl dev/sdx or hdx…………………)
4、 激活卷组(vgchange –a y mxl)这里的mxl就是创建的名字自己可以随便起,如果创建的是为存放mysql的就起这个名字,方便以后的记忆
5、 创建逻辑卷(lvcreate)
通过以上的方法创建LVM、创建后如果查看我们创建的是否成功,
下面的命令就可以轻松实现:
1、 查看物理卷的命令pvscan pvdisplay
2、 查看创建的卷组 vgscan vgdisplay
3、 查看创建的逻辑卷 lvscan lvdisplay
下面就来详细介绍操作步骤
创建分区就不在介绍
1、
[root@mxl ~]# pvcreate /dev/sdb1 dev/sdb2 /dev/sdb5 /dev/sdb6 dev/sdb7 /dev/sdb8 /dev/sdc1 /dev/sdc5 /dev/sdc6
Physical volume "/dev/sdb1" successfully created
Device dev/sdb2 not found (or ignored by filtering).
Physical volume "/dev/sdb5" successfully created
Physical volume "/dev/sdb6" successfully created
Device dev/sdb7 not found (or ignored by filtering).
Physical volume "/dev/sdb8" successfully created
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdc5" successfully created
Physical volume "/dev/sdc6" successfully created
注意看这里是不是有错误,呵呵linux系统中命令格式要求是很严格的,所在大家在操作当中一定要细心。下面是正确的
[root@mxl ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb5 /dev/sdb6 /dev/sdb7 /dev/sdb8 /dev/sdc1 /dev/sdc5 /dev/sdc6
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
Physical volume "/dev/sdb5" successfully created
Physical volume "/dev/sdb6" successfully created
Physical volume "/dev/sdb7" successfully created
Physical volume "/dev/sdb8" successfully created
Physical volume "/dev/sdc1" successfully created
Physical volume "/dev/sdc5" successfully created
Physical volume "/dev/sdc6" successfully created
运用上面的方法我们查看创建是否成功
[root@mxl ~]# pvscan
PV /dev/sdb1 lvm2 [980.50 MB]
PV /dev/sdb2 lvm2 [980.53 MB]
PV /dev/sdb5 lvm2 [980.50 MB]
PV /dev/sdb6 lvm2 [980.50 MB]
PV /dev/sdb7 lvm2 [1.92 GB]
PV /dev/sdb8 lvm2 [1.92 GB]
PV /dev/sdc1 lvm2 [1.92 GB]
PV /dev/sdc5 lvm2 [980.50 MB]
PV /dev/sdc6 lvm2 [1.92 GB]
Total: 9 [12.45 GB] / in use: 0 [0 ] / in no VG: 9 [12.45 GB]
2、创建逻辑卷组
[root@mxl ~]# vgcreate mysql /dev/sdb1 /dev/sdb2 /dev/sdb5 /dev/sdb6 /dev/sdb7 /dev/sdb8 /dev/sdc1 /dev/sdc5 /dev/sdc6
Volume group "mysql" successfully created
[root@mxl ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "mysql" using metadata type lvm2
4、 激活卷组
[root@mxl ~]# vgchange -a y mysql
0 logical volume(s) in volume group "mysql" now active
Vgchange –a y 后加上自己创建的名字,记住和上面的对应
5、 创建逻辑卷
[root@mxl ~]# lvcreate -L 1500 -n mysql-data mysql
Logical volume "mysql-data" created
[root@mxl ~]# lvscan
ACTIVE '/dev/mysql/mysql-data' [1.46 GB] inherit
如果希望创建一个使用全部卷组的逻辑卷,则需要知道该卷组的PE数,然后创建时指定
[root@mxl ~]# vgdisplay mysql |grep "Total PE"
Total PE 3185
[root@mxl ~]# lvcreate -l 3185 mysql -n mysql-data
Logical volume "mysql-data" already exists in volume group "mysql"
6、创建文件系统
[root@mxl ~]# mkfs -t ext3 /dev/mysql/mysql-data
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
192000 inodes, 384000 blocks
19200 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=394264576
12 block groups
32768 blocks per group, 32768 fragments per group
16000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
7、挂载
[root@mxl ~]# mkdir /mnt/mysql-data
[root@mxl ~]# mount /dev/mysql/mysql-data /mnt/mysql-data
[root@mxl ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 14G 3.6G 9.2G 29% /
/dev/sda1 99M 12M 83M 12% /boot
tmpfs 128M 0 128M 0% /dev/shm
/dev/mapper/mysql-mysql--data
1.5G 35M 1.4G 3% /mnt/mysql-data
如果想每次开机系统都自动加载我们必需在/etc/fstab下加入相应的行即可
扩展卷空间
扩展卷空间可以方面我们调整逻辑卷的大小,命令是lvextend
步骤:
1、为逻辑卷mysql增加500M的空间
[root@mxl ~]# lvextend -L+500M /dev/mysql/mysql-data
Extending logical volume mysql-data to 1.95 GB
Logical volume mysql-data successfully resized
[root@mxl ~]# lvdisplay
--- Logical volume ---
LV Name /dev/mysql/mysql-data
VG Name mysql
LV UUID ypysQp-DgMF-cp1J-YQk2-XPoi-qbrt-cxMOtz
LV Write Access read/write
LV Status available
# open 1
LV Size 1.95 GB
Current LE 500
Segments 2
Allocation inherit
Read ahead sectors 0
Block device 253:0
2、卸载文件系统当前的挂载点
[root@mxl ~]# umount /dev/mysql/mysql-data
[root@mxl ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 14G 3.6G 9.2G 29% /
/dev/sda1 99M 12M 83M 12% /boot
tmpfs 128M 0 128M 0% /dev/shm
3、对于 etc2 etc3 文件系统我们可以使用e2fsprogs软件的resize2fs
[root@mxl ~]# resize2fs /dev/mysql/mysql-data
resize2fs 1.39 (29-May-2006)
Please run 'e2fsck -f /dev/mysql/mysql-data' first.
提示我们加参数对文件系统进行检查
[root@mxl ~]# e2fsck -f /dev/mysql/mysql-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/mysql/mysql-data: 11/256000 files (9.1% non-contiguous), 16810/512000 blocks
4、新加一个分区加入到vg组中
[root@mxl ~]# pvcreate /dev/sdc7 创建物理卷
[root@mxl ~]# vgextend mysql /dev/sdc7 将物理卷加入到组中
[root@mxl ~]# pvscan
PV /dev/sdb1 VG mysql lvm2 [980.00 MB / 980.00 MB free]
PV /dev/sdb2 VG mysql lvm2 [980.00 MB / 980.00 MB free]
PV /dev/sdb5 VG mysql lvm2 [980.00 MB / 980.00 MB free]
PV /dev/sdb6 VG mysql lvm2 [980.00 MB / 980.00 MB free]
PV /dev/sdb7 VG mysql lvm2 [1.91 GB / 0 free]
PV /dev/sdb8 VG mysql lvm2 [1.91 GB / 1.88 GB free]
PV /dev/sdc1 VG mysql lvm2 [1.91 GB / 1.91 GB free]
PV /dev/sdc5 VG mysql lvm2 [980.00 MB / 980.00 MB free]
PV /dev/sdc6 VG mysql lvm2 [1.91 GB / 1.91 GB free]
PV /dev/sdc7 VG mysql lvm2 [3.21 GB / 3.21 GB free]
Total: 10 [15.65 GB] / in use: 10 [15.65 GB] / in no VG: 0 [0 ]
我们在来创建几个逻辑卷以加深印象
[root@mxl ~]# lvcreate -L 2048 -n web-data mysql
Logical volume "web-data" created
[root@mxl ~]# lvscan
ACTIVE '/dev/mysql/mysql-data' [1.95 GB] inherit
ACTIVE '/dev/mysql/web-data' [2.00 GB] inherit
[root@mxl ~]# lvcreate -L 1000 -n file-data mysql
Logical volume "file-data" created
[root@mxl ~]# lvcreate -L 1000 -n caiwu-data mysql
Logical volume "caiwu-data" created
[root@mxl ~]# lvscan
ACTIVE '/dev/mysql/mysql-data' [1.95 GB] inherit
ACTIVE '/dev/mysql/web-data' [2.00 GB] inherit
ACTIVE '/dev/mysql/file-data' [1000.00 MB] inherit
ACTIVE '/dev/mysql/caiwu-data' [1000.00 MB] inherit
删除卷及卷组
删除卷和卷组以前首先要删除挂载点用umount命令,然后在删除逻辑卷、卷组、物理卷等
1、 删除物理卷命令 pvremove
2、 删除卷组命令 vgremove
3、 删除逻辑卷命令 lvremove
1、[root@mxl ~]# lvremove /dev/mysql/caiwu-data
Do you really want to remove active logical volume "caiwu-data"? [y/n]: y
Logical volume "caiwu-data" successfully removed
[root@mxl ~]# lvremove /dev/mysql/file-data
Do you really want to remove active logical volume "file-data"? [y/n]: y
Logical volume "file-data" successfully removed
2、[root@mxl ~]# vgremove /dev/mysql
Volume group "mysql" successfully removed
[root@mxl ~]# vgdisplay
3、[root@mxl ~]# pvscan
PV /dev/sdb1 lvm2 [980.50 MB]
PV /dev/sdb2 lvm2 [980.53 MB]
PV /dev/sdb5 lvm2 [980.50 MB]
PV /dev/sdb6 lvm2 [980.50 MB]
PV /dev/sdb7 lvm2 [1.92 GB]
PV /dev/sdb8 lvm2 [1.92 GB]
PV /dev/sdc1 lvm2 [1.92 GB]
PV /dev/sdc5 lvm2 [980.50 MB]
PV /dev/sdc6 lvm2 [1.92 GB]
PV /dev/sdc7 lvm2 [3.21 GB]
我们把/dev/sdb1物理卷删除
[root@mxl ~]# pvremove /dev/sdb1
Labels on physical volume "/dev/sdb1" successfully wiped
[root@mxl ~]# pvscan 再次查看/dev/sdb1已成功删除
PV /dev/sdb2 lvm2 [980.53 MB]
PV /dev/sdb5 lvm2 [980.50 MB]
PV /dev/sdb6 lvm2 [980.50 MB]
PV /dev/sdb7 lvm2 [1.92 GB]
PV /dev/sdb8 lvm2 [1.92 GB]
PV /dev/sdc1 lvm2 [1.92 GB]
PV /dev/sdc5 lvm2 [980.50 MB]
PV /dev/sdc6 lvm2 [1.92 GB]
PV /dev/sdc7 lvm2 [3.21 GB]
Total: 9 [14.70 GB] / in use: 0 [0 ] / in no VG: 9 [14.70 GB]
删除的方法是由下向上的顺序,先删除逻辑卷再删除卷组最后删除物理卷
小知识:如果对较多的硬盘进行lvm规化时我们要注意的是设计好要规化的内容和、合理分配硬盘的空间,可以把多个硬盘规化在一个卷组内也可以规化到不同的卷组内,要注意的是如果把硬盘的空间全部分配给一个卷组了,那就不能在创建第二卷组了。不知道大家有没有做过这样的试验,对于逻辑卷的化分就相对上手了,我们可以按需求来化分逻辑卷的空间,对于不同的部门起不同的文件名。
LVM中的命令 |
pvcreate
创建物理卷 |
pvscan
查看物理卷 |
pvdisplay
详细查看物理卷 |
vgcreate
创建卷组 |
vgscan
查看卷组 |
Vgdisplay
详细查看卷组 |
Lvcreate
创建逻辑卷 |
Lvscan
查看逻辑卷 |
Lvdisplay
详细查看逻辑卷 |
Vgchange
激活卷组 |
Lvextend
增加逻辑卷容量 |
Lvremove
删除逻辑卷 |
Vgremove
删除卷组 |
|
Pvremove
删除物理卷 |