实例:
使用lvm存储结构的主机需要扩容,现在我们已经将一个新的硬盘安装上去,将该新的硬盘的空间全部增加到主机上
20 ls /dev/sd* #查看新增加的硬盘名,我的为xvdb
21 ls /dev
22 fdisk -l
23 fdisk /dev/xvdb #对其进行操作(写下来大概是m,n,p,1,t,8e,w)
24 pvcreate /dev/xvdb1 #增加物理卷
25 vgdisplay #查看组,组名为cl
26 vgextend cl /dev/xvdb1 #扩展/dev/xvdb1到cl组
27 vgdisplay
28 lvdisplay #获取lv路径
29 lvresize -l +127999 /dev/cl/root #-l指定大小,因为前面已经vgextend,127999这个值可以从vgdisplay这里获取(Free PE)
30 df -h
31 df -Th #需要注意文件系统格式,如果是xfs格式,使用下一条命令,如果不是则是另一条resize2fs /dev/cl/root
32 xfs_growfs /dev/cl/root #xfs_growfs会自动扩展至最大可用值,这条命令只能用于文件系统是xfs的,文件系统可以通过df -Th查看,其他的文件系统就不是用这个命令了
33 df -h
可能会遇到一个问题,那就是在vgextend时报错,可能是因为磁盘太满了,建议清空部分文件,再次执行扩容
以下为详细过程及讲解
二次整理lvm:
首先,往系统中添加物理磁盘或虚拟磁盘,如果热插没有直接识别,可以尝试以下三条命令(或者是partprobe,不过这条命令我没试过,好像是fdisk之后才用的)
[root@localhost ~]# echo ‘- - -‘> /sys/class/scsi_host/host0/scan
[root@localhost ~]# echo ‘- - -‘> /sys/class/scsi_host/host1/scan
[root@localhost ~]# echo ‘- - -‘> /sys/class/scsi_host/host3/scan
首先是确认是否添加磁盘成功
ls /dev/xvd* (需要根据具体情况,我这个是xen虚拟平台的,如果是vm之类的虚拟软件,新设备名称可能是sd开头的),有显示则表示成功,或者有ll /dev下面的文件,修改时间为最新的即为新增磁盘设备
接下来就可以开始进行lvm操作了,直接上图看一下吧
先看一下,在原lvm基础上怎么扩展原空间吧
[root@localhost ~]# fdisk /dev/xvdb #进行fdisk操作,后面是我新增的磁盘 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0xde5438c0. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n #n是指新增分区 Command action e extended p primary partition (1-4) p #这里还需要按p Partition number (1-4): 1 #新增分区号 First cylinder (1-6788, default 1): #分区开始位置,回车默认就可以了 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-6788, default 6788): #选择分区大小,可以使用例如+50G然后回车确认,+50G的意思是该分区大小分50G Using default value 6788 Command (m for help): t #设置分区类型 Selected partition 1 #如果有多个分区,这里也有分别选中想操作的分区号 Hex code (type L to list codes): 8e #我选择的是LVM类型,可以使用L查看全部类型 Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w #保存这次修改并退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# vgextend VolGroup /dev/xvdb1 #将新增的分区加到vg,扩大vg容量,加入vg的好处是在vg中的lv可以动态扩展大小(注意xvdb1,也就是我们刚才选择的分区号,如果是选的2的话,则应是xvdb2) Physical volume "/dev/xvdb1" successfully created Volume group "VolGroup" successfully extended [root@localhost ~]# lvextend -L +52G /dev/VolGroup/lv_root #这里比较烦,我增加的是52G的磁盘,提示空间不够,那就相当于有损耗呗,空间稍微减少个1G就成功了,后面的lv_root可以从lvdisplay中查看 Insufficient free space: 13312 extents needed, but only 13311 available [root@localhost ~]# lvextend -L +51G /dev/VolGroup/lv_root Size of logical volume VolGroup/lv_root changed from 28.31 GiB (7247 extents) to 79.31 GiB (20303 extents). Logical volume lv_root successfully resized [root@localhost ~]# resize2fs /dev/VolGroup/lv_root #扩展之后,还需重置lv大小,resize2fs是针对etx4的,xfs是xfs_growfs,不知道为什么,这里不用格式化lv卷。执行之后df -Th应该就能显示增加成功了 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required old desc_blocks = 2, new_desc_blocks = 5 Performing an on-line resize of /dev/VolGroup/lv_root to 20790272 (4k) blocks. The filesystem on /dev/VolGroup/lv_root is now 20790272 blocks long.
接下来是新增独立的vg,重新划分,而不是在原来已经划分好的基础上
[root@localhost ~]# fdisk /dev/xvdc #fdisk 新增磁盘名(这里我将一块磁盘划分了4个分区,所以这里内容有点多重复的也多) Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9f0be0ba. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-67881, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-67881, default 67881): +24G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (3135-67881, default 3135): Using default value 3135 Last cylinder, +cylinders or +size{K,M,G} (3135-67881, default 67881): +125G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (19454-67881, default 19454): Using default value 19454 Last cylinder, +cylinders or +size{K,M,G} (19454-67881, default 67881): +64G Command (m for help): n Command action e extended p primary partition (1-4) p Selected partition 4 First cylinder (27810-67881, default 27810): Using default value 27810 Last cylinder, +cylinders or +size{K,M,G} (27810-67881, default 67881): Using default value 67881 Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 2 #每一个分区都需要分别选择类型 Hex code (type L to list codes): 8e Changed system type of partition 2 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 3 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): t Partition number (1-4): 4 Hex code (type L to list codes): 8e Changed system type of partition 4 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# mkdir /tmp #创建相应的挂载分区,嗯嗯,我这里是错误示范,这几个文件夹是系统自带的,我删掉之后就出现问题,所以自己另起个名字吧 [root@localhost ~]# mkdir /var [root@localhost ~]# mkdir /srv/Bigdata [root@localhost ~]# mkdir /opt [root@localhost ~]# vgcreate datavg /dev/xvdc xvdc xvdc1 xvdc2 xvdc3 xvdc4 [root@localhost ~]# vgcreate datavg /dev/xvdc1 /dev/xvdc2 /dev/xvdc3 /dev/xvdc4 #这里好像是在执行vgcreate时同时执行pvcreate了
#新增vg,只有在vg里面的lv才能动态扩展
Physical volume "/dev/xvdc1" successfully created
Physical volume "/dev/xvdc2" successfully created
Physical volume "/dev/xvdc3" successfully created
Physical volume "/dev/xvdc4" successfully created
Volume group "datavg" successfully created
[root@localhost ~]# lvcreate -n tmplv +24G datavg
Please specify either size or extents
Run `lvcreate --help' for more information.
[root@localhost ~]# lvcreate -n tmplv -L +23G datavg #增加一个lv,这个lv是用来挂载分区的
Logical volume "tmplv" created
[root@localhost ~]# mkfs.ext4 /dev/datavg/tmplv #挂载独立分区时就需要进行mkfs格式化,后面跟的是lv路径,可以使用lvdisplay查看
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1507328 inodes, 6029312 blocks
301465 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
184 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, 2654208,
4096000
Writing inode tables: done
Creating journal (32768 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.
[root@localhost ~]# mount /dev/datavg/tmplv /tmp #挂载时使用的是mount lv路径(也是lvdisplay查看) 挂载路径,注意,这条命令最好加入/etc/fstab里面,加入之后就可以开机自动挂载了
[root@localhost ~]# lvcreate -n varlv -L +124G datavg #接下来这些就是新增第二第三第四个分区了
Logical volume "varlv" created
[root@localhost ~]# mkfs.ext4 /dev/datavg/varlv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
8126464 inodes, 32505856 blocks
1625292 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
992 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, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mount /dev/datavg/varlv /var
[root@localhost ~]# lvcreate -n datalv -L +63G datavg
Logical volume "datalv" created
[root@localhost ~]# mkfs.ext4 /dev/datavg/datalv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
4128768 inodes, 16515072 blocks
825753 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
504 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, 2654208,
4096000, 7962624, 11239424
Writing inode tables: done
Creating journal (32768 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.
[root@localhost ~]# mount /dev/datavg/datalv /srv/Bigdata
[root@localhost ~]# lvcreate -n optlv -L +310G datavg
Volume group "datavg" has insufficient free space (79356 extents): 79360 required
.[root@localhost ~]# lvcreate -n optlv -L +309G datavg
Logical volume "optlv" created
[root@localhost ~]# mkfs.ext4 /dev/datavg/optlv
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
20250624 inodes, 81002496 blocks
4050124 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2472 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, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968
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.
[root@localhost ~]# mount /dev/datavg//opt
datalv optlv tmplv varlv
[root@localhost ~]# mount /dev/datavg/optlv /opt
e2fsck -f /dev/storage/vo 检查磁盘完整性
resize2fs /dev/storage/vo 重置硬盘容量(应该是相当于更新磁盘的意思吧),扩容的话是先扩容再检查最后重置并挂载,减容的话就是先检查磁盘完整性再重置大小并挂载
mount -a
df -h 查看磁盘容量
umount /bplvm 为后面缩小逻辑卷做准备
e2fsck -f /dev/storage/vo
resize2fs /dev/storage/vo 120M 将磁盘容量减少至120M
lvreduce -L 120M /dev/storage/vo 减少120M(如果执行完上面那条命令再执行这条,应该会减为0M了吧,没试过)
删除逻辑卷:
umount /bplvm
vim /etc/fstab 删除自己添加的那条挂载vo的命令的那行
lvremove /dev/storage/vo 删除LV逻辑卷设备
vgremove storage 删除VG卷组
pvremove /dev/sdb /dev/sdc 删除PV物理卷设备
注意删除的顺序是按照上面的删除顺序的,跟增加时正好相反,不能乱来