LVM笔记
[toc]
环境准备
- 准备磁盘分区:
[root@centos6-node3 ~]# fdisk /dev/sdg Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (131-261, default 131): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (131-261, default 261): +500M Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 8e Changed system type of partition 3 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
按照上述的操作方法创建3个500M的分区,然后如下检查
[root@centos6-node3 ~]# fdisk -l Device Boot Start End Blocks Id System /dev/sdg1 1 65 522081 8e Linux LVM /dev/sdg2 66 130 522112+ 8e Linux LVM /dev/sdg3 131 195 522112+ 8e Linux LVM
第一阶段:准备PV(物理卷)
- 先创建和检查pv
[root@centos6-node3 ~]# pvcreate /dev/sdg1 [root@centos6-node3 ~]# pvcreate /dev/sdg2 [root@centos6-node3 ~]# pvcreate /dev/sdg3 [root@centos6-node3 ~]# pvdisplay /dev/sdg1 #检查pv情况 --- NEW Physical volume --- PV Name /dev/sdg1 VG Name PV Size 509.84 MiB Allocatable NO PE Size 0 Total PE 0 Free PE 0 Allocated PE 0 PV UUID tmkRdY-h9LX-V0h0-sTXI-lFZW-Cref-LcoCXE [root@centos6-node3 ~]# pvremove /dev/sdg3 #移除pv
准备卷组(VG)
- 创建VG
[root@centos6-node3 ~]# vgcreate volume-group1 /dev/sdg1 /dev/sdg2 [root@centos6-node3 ~]# vgdisplay #查看VG信息 --- Volume group --- VG Name volume-group1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 1016.00 MiB PE Size 4.00 MiB Total PE 254 Alloc PE / Size 0 / 0 Free PE / Size 254 / 1016.00 MiB VG UUID NZfaUa-lchX-qQLk-WdtO-clN8-msE2-b3q6YP
- 删除VG
[root@centos6-node3 ~]# vgremove volume-group1
准备逻辑卷(LV)
- 创建LV
root@centos6-node3 ~]# lvcreate -L 100M -n lv1 volume-group1 #从VG画出100M的空间作为lv1 [root@centos6-node3 ~]# lvdisplay #查看lv /dev/md126: read failed after 0 of 4096 at 0: Input/output error --- Logical volume --- LV Path /dev/volume-group1/lv1 LV Name lv1 VG Name volume-group1 LV UUID Aq4zSR-Rc3A-l4lv-7aoS-qADe-4Q1I-vhGUe1 LV Write Access read/write LV Creation host, time centos6-node3, 2017-12-12 23:08:44 +0800 LV Status available # open 0 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
最后一步,将LV格式化并挂载
- 格式化&初始化:
[root@centos6-node3 ~]# mkfs.ext4 /dev/volume-group1/lv1 #格式化 [root@centos6-node3 ~]# mkdir /mnt/lvm-mount [root@centos6-node3 ~]# mount /dev/volume-group1/lv1 /mnt/lvm-mount #挂载 [root@centos6-node3 ~]# dd if=/dev/zero of=/mnt/lvm-mount/test1 bs=1M count=89 #数据写入
- 删除LV
[root@centos6-node3 ~]# umount /mnt/lvm-mount/ #要先卸载 [root@centos6-node3 lvm-mount]# lvremove /dev/volume-group1/lv1 #删除LV(高危操作)
- 拓展LVM卷
[root@centos6-node3 lvm-mount]# umount /mnt/lvm-mount/ #扩容之前一定要卸载 [root@centos6-node3 lvm-mount]# lvresize -L 200M /dev/volume-group1/lv1 #扩容200M [root@centos6-node3 lvm-mount]# e2fsck -f /dev/volume-group1/lv1 #检查磁盘错误 [root@centos6-node3 ~]# resize2fs /dev/volume-group1/lv1 #更新lv [root@centos6-node3 ~]# lvdisplay #检查lv1 /dev/md126: read failed after 0 of 4096 at 0: Input/output error --- Logical volume --- LV Path /dev/volume-group1/lv1 LV Name lv1 VG Name volume-group1 LV UUID emCezv-2nBN-0vgu-y54a-R93P-KCBG-SSF7ZI LV Write Access read/write LV Creation host, time centos6-node3, 2017-12-12 23:21:15 +0800 LV Status available # open 0 LV Size 200.00 MiB Current LE 50 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 [root@centos6-node3 ~]# mount /dev/volume-group1/lv1 /mnt/lvm-mount/ #挂盘即可
- LVM缩容
- 卸载卷
[root@centos6-node3 ~]# df #回家目录卸载,不然会报错 Filesystem 1K-blocks Used Available Use% Mounted on /dev/mapper/vg_centos6node3-lv_root 38645208 1022088 35653356 3% / tmpfs 502056 0 502056 0% /dev/shm /dev/sda1 487652 52916 409136 12% /boot /dev/mapper/volume--group1-lv1 194466 92687 91539 51% /mnt/lvm-mount [root@centos6-node3 ~]# umount /dev/mapper/volume--group1-lv1 [root@centos6-node3 ~]# e2fsck -f /dev/volume-group1/lv1 #检查磁盘错误
-
缩容
[root@centos6-node3 ~]# resize2fs /dev/volume-group1/lv1 100M #缩小文件系统 [root@centos6-node3 ~]# lvresize -L 100M /dev/volume-group1/lv1 #减小逻辑卷的大小 [root@centos6-node3 ~]# lvdisplay #检查是否缩小了100M /dev/md126: read failed after 0 of 4096 at 0: Input/output error --- Logical volume --- LV Path /dev/volume-group1/lv1 LV Name lv1 VG Name volume-group1 LV UUID emCezv-2nBN-0vgu-y54a-R93P-KCBG-SSF7ZI LV Write Access read/write LV Creation host, time centos6-node3, 2017-12-12 23:21:15 +0800 LV Status available # open 0 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2
其他问题
拓展卷组
让我们假设我们的卷组'volume-group1'已经满了,需要扩大。手上的硬盘(sdg)已经没有其他空闲分区,我们添加了另外一个硬盘(sdc)。我们将看到如何把sdc的分区添加到卷组以扩展。
- 检查卷组状态:
[root@centos6-node3 ~]# vgdisplay volume-group1 /dev/md126: read failed after 0 of 4096 at 0: Input/output error --- Volume group --- VG Name volume-group1 System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 6 VG Access read/write VG Status resizable MAX LV 0 Cur LV 1 Open LV 0 Max PV 0 Cur PV 2 Act PV 2 VG Size 1016.00 MiB PE Size 4.00 MiB Total PE 254 Alloc PE / Size 25 / 100.00 MiB Free PE / Size 229 / 916.00 MiB VG UUID QgBiHO-391u-jrra-QfiG-BKhF-GCBR-EuhEY0
- 初始化一个分区
[root@centos6-node3 ~]# fdisk /dev/sdf Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-261, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): Using default value 261 Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdf: 2147 MB, 2147483648 bytes 255 heads, 63 sectors/track, 261 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x27eee55a Device Boot Start End Blocks Id System /dev/sdf1 1 261 2096451 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.
- 创建pv
[root@centos6-node3 ~]# pvcreate /dev/sdf1
- 拓展vg
[root@centos6-node3 ~]# vgextend volume-group1 /dev/sdg3 [root@centos6-node3 ~]# vgdisplay volume-group1 #检查是否扩容