逻辑卷(Logical Volume)是由逻辑磁盘形成的虚拟盘,也可称为磁盘分区。
逻辑卷的基本操作可以分为四类:
1.创建逻辑卷
2.扩展逻辑卷
3.缩减逻辑卷
4.迁移逻辑卷
缩减逻辑卷
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 953M 0 part /boot
├─sda2 8:2 0 46.6G 0 part /
├─sda3 8:3 0 9.3G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1.9G 0 part [SWAP]
sdb 8:16 0 200G 0 disk
├─sdb1 8:17 0 15G 0 part
│ └─vg0-lv0 253:0 0 10G 0 lvm /app/lv3
├─sdb2 8:18 0 10G 0 part
│ └─vg0-lv2 253:2 0 5G 0 lvm /app/lv1
└─sdb3 8:19 0 20G 0 part
└─vg0-lv1 253:1 0 15G 0 lvm /app/lv2
sr0 11:0 1 7.7G 0 rom /run/media/root/CentOS 7 x86_64
1.取消要缩减逻辑卷的挂载
[root@centos7 ~]#umount /app/lv1
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 953M 0 part /boot
├─sda2 8:2 0 46.6G 0 part /
├─sda3 8:3 0 9.3G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1.9G 0 part [SWAP]
sdb 8:16 0 200G 0 disk
├─sdb1 8:17 0 15G 0 part
│ └─vg0-lv0 253:0 0 10G 0 lvm /app/lv1
├─sdb2 8:18 0 10G 0 part
│ └─vg0-lv2 253:2 0 5G 0 lvm (取消挂载)
└─sdb3 8:19 0 20G 0 part
└─vg0-lv1 253:1 0 15G 0 lvm /app/lv2
sr0 11:0 1 7.7G 0 rom /run/media/root/CentOS 7 x86_64
2.检查文件系统
[root@centos7 ~]#fsck -f /dev/vg0/lv2(检查文件系统)
fsck from util-linux 2.23.2
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/mapper/vg0-lv2: 11/327680 files (0.0% non-contiguous), 58462/1310720 blocks
3.检查文件系统
[root@centos7 ~]#resize2fs /dev/vg0/lv2 (这里这部可以省略)
resize2fs 1.42.9 (28-Dec-2013)
The filesystem is already 1310720 blocks long. Nothing to do!
缩减逻辑卷到2G
[root@centos7 ~]#lvreduce -L 2G /dev/vg0/lv2(把lv2大小缩减到2G)
WARNING: Reducing active logical volume to 2.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/lv2? [y/n]: y
Size of logical volume vg0/lv2 changed from 5.00 GiB (1280 extents) to 2.00 GiB (512 extents).
Logical volume vg0/lv2 successfully resized.
[root@centos7 ~]#lsblk (查看缩减后的逻辑卷大小)
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 953M 0 part /boot
├─sda2 8:2 0 46.6G 0 part /
├─sda3 8:3 0 9.3G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1.9G 0 part [SWAP]
sdb 8:16 0 200G 0 disk
├─sdb1 8:17 0 15G 0 part
│ └─vg0-lv0 253:0 0 10G 0 lvm /app/lv1
├─sdb2 8:18 0 10G 0 part
│ └─vg0-lv2 253:2 0 2G 0 lvm (缩减后逻辑卷大小为2G)
[root@centos7 ~]#mount /dev/vg0/lv2 /app/lv3(重新挂载)
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 953M 0 part /boot
├─sda2 8:2 0 46.6G 0 part /
├─sda3 8:3 0 9.3G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1.9G 0 part [SWAP]
sdb 8:16 0 200G 0 disk
├─sdb1 8:17 0 15G 0 part
│ └─vg0-lv0 253:0 0 10G 0 lvm /app/lv1
├─sdb2 8:18 0 10G 0 part
│ └─vg0-lv2 253:2 0 2G 0 lvm /app/lv3(挂载成功)
└─sdb3 8:19 0 20G 0 part
└─vg0-lv1 253:1 0 2G 0 lvm /app/lv2
sr0 11:0 1 7.7G 0 rom /run/media/root/CentOS 7 x86_64
迁移逻辑卷
为了能够体现逻辑卷的迁移,我们会拷贝一些文件过去
[root@centos7 ~]#cp -r /etc/sysconfig/* /mnt/lv0
[root@centos7 /mnt/lv0]#ls(这些为拷贝过来的文件)
atd firewalld ksm network-scripts readonly-root selinux
authconfig grub libvirtd nfs rpcbind smartmontools
cbq init libvirt-guests ntpdate rpc-rquotad sshd
console ip6tables-config lost+found pluto rsyncd sysstat
cpupower iptables-config man-db qemu-ga rsyslog sysstat.ioconf
crond irqbalance modules radvd run-parts virtlockd
ebtables-config kdump netconsole raid-check samba virtlogd
fcoe kernel network rdisc saslau
1.首先取消挂载关系
[root@centos7 ~]#umount /dev/vg0/lv0
[root@centos7 ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 953M 0 part /boot
├─sda2 8:2 0 46.6G 0 part /
├─sda3 8:3 0 9.3G 0 part /app
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 1.9G 0 part [SWAP]
sdb 8:16 0 20G 0 disk
└─vg0-lv0 253:0 0 10G 0 lvm (取消挂载关系)
sr0 11:0 1 7.7G 0 rom
2.为了不跟目标系统中的逻辑卷名有冲突,我们把逻辑卷名,逻辑卷组名改掉
[root@centos7 ~]#vgrename vg0 newvg0
Volume group "vg0" successfully renamed to "newvg0"
[root@centos7 ~]#vgs 组名更改成功
VG #PV #LV #SN Attr VSize VFree
newvg0 1 1 0 wz--n- 20.00g 10.00g
[root@centos7 ~]#lvrename /dev/newvg0/lv0 newlv0(更改逻辑卷名)
Renamed "lv0" to "newlv0" in volume group "newvg0"
[root@centos7 ~]#lvs(逻辑卷名更改成功)
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
newlv0 newvg0 -wi-a----- 10.00g
3.把逻辑卷组改为非活动状态并导出之后关机拆除硬盘,插入新的主机中
因为我们是模拟拆除,所以只需要把对应的centos7硬盘文件复制到centos6中即可,然后在centos6中添加硬盘,选择已有的硬盘(复制过来的硬盘)即可
[root@centos7 ~]#vgchange -an newvg0(改为非活动状态)
0 logical volume(s) in volume group "newvg0" now active
[root@centos7 ~]#vgexport newvg0(导出卷组)
Volume group "newvg0" successfully exported
[root@centos7 ~]#pvdisplay
Physical volume "/dev/sdb" of volume group "newvg0" is exported
--- Physical volume ---
PV Name /dev/sdb
VG Name newvg0 (exported)(导出成功)
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 2559
Allocated PE 2560
PV UUID eopt26-rQiz-0N8F-Z1Zf-GtcS-gReg-dQaLSq
4.扫描逻辑卷并导入
[root@centos6 ~]#pvscan(扫描)
PV /dev/sdc is in exported VG newvg0 [20.00 GiB / 10.00 GiB free]
PV /dev/sdb1 VG vg0 lvm2 [10.00 GiB / 2.00 GiB free]
PV /dev/sdb2 VG vg0 lvm2 [5.01 GiB / 8.00 MiB free]
Total: 3 [35.00 GiB] / in use: 3 [35.00 GiB] / in no VG: 0 [0 ]
[root@centos6 ~]#vgimport newvg0(导入)
Volume group "newvg0" successfully imported
5.查看逻辑卷状态并启用
[root@centos6 ~]#pvdisplay
--- Physical volume ---
PV Name /dev/sdc
VG Name newvg0
PV Size 20.00 GiB / not usable 4.00 MiB(可以看出为非活动状态)
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 2559
Allocated PE 2560
PV UUID eopt26-rQiz-0N8F-Z1Zf-GtcS-gReg-dQaLSq
[root@centos6 ~]#vgchange -ay newvg0(激活状态)
1 logical volume(s) in volume group "newvg0" now active
[root@centos6 ~]#lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
newlv0 newvg0 -wi-a----- 10.00g (激活成功)
lv0 vg0 -wi-a----- 8.00g
lv1 vg0 -wi-a----- 5.00g
6.创建新的文件夹,进行挂载,验证文件是否存在
[root@centos6 ~]#mkdir /mnt/newlv0 (创建文件夹)
[root@centos6 ~]#mount /dev/newvg0/newlv0 /mnt/newlv0
[root@centos6 ~]#df查看挂载情况
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 94716764 4452828 85445864 5% /
tmpfs 953640 72 953568 1% /dev/shm
/dev/sda1 194241 40463 143538 22% /boot
/dev/mapper/newvg0-newlv0
10190100 37312 9628500 1% /mnt/newlv0
/dev/mapper/vg0-lv0 8125880 18420 7688032 1% /mnt/lv0
/dev/mapper/vg0-lv1 5029504 10232 4757128 1% /mnt/lv1
[root@centos6 /mnt]#cd newlv0/
[root@centos6 /mnt/newlv0]#ls查看拷贝的文件是否还存在
atd crond init kernel man-db nfs raid-check rsyncd selinux virtlockd
authconfig ebtables-config ip6tables-config ksm modules ntpdate rdisc rsyslog smartmontools virtlogd
cbq fcoe iptables-config libvirtd netconsole pluto readonly-root run-parts sshd wpa_supplicant
console firewalld irqbalance libvirt-guests network qemu-ga rpcbind samba sysstat
cpupower grub kdump lost+found network-scripts radvd rpc-rquotad saslauthd sysstat.ioconf