基于lvm的系统盘在线迁移

发布时间 2024-01-12 14:30:51作者: shn7798

早期系统装在SSD盘上,现由于磁盘IO性能问题需要将SSD盘用作于业务。
而SSD通常容量不是很大,所以需要将操作系统挪走,系统引导迁移可以参考Linux换系统盘后引导修复

本次迁移内容为:
将/dev/sylink/data分区从sda迁移到sdb

[root@master193 ~]# pvscan
  PV /dev/sda3   VG sylink          lvm2 [<99.50 GiB / 0    free]
  PV /dev/sdb3   VG sylink          lvm2 [<100.00 GiB / <100.00 GiB free]

[root@master193 ~]# lvscan
  ACTIVE            '/dev/sylink/data' [<19.50 GiB] inherit
  ACTIVE            '/dev/sylink/root' [80.00 GiB] inherit

迁移引导分区

引导分区不能使用lvm,所以这里需要手动迁移

查看原始分区信息

[root@master193 boot]# fdisk -l /dev/sda
Model: VMware Virtual disk (scsi)
Disk /dev/sda: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  标志
 1      1049kB  3146kB  2097kB  primary
 2      3146kB  540MB   537MB   primary  ext4         启动
 3      540MB   107GB   107GB   primary               lvm

创建相同分区表
分区可以适当调大,但是不能小

[root@master193 boot]# parted /dev/sdb
(parted) mkpart efi fat32 0% 200M                                        
(parted) mkpart boot ext4 200M 700M
(parted) mkpart data ext4 700M 100%
(parted) p

Number  Start   End     Size    File system  Name  标志
 1      1049kB  200MB   199MB                efi
 2      200MB   700MB   500MB                boot
 3      700MB   201GB   200GB                data

[root@master193 boot]# yum install dosfstool
[root@master193 boot]# mkfs -t vfat /dev/sdb1
[root@master193 boot]# mkfs -t ext4 /dev/sdb2

加入lvm

[root@master193 boot]# pvcreate /dev/sdb3
[root@master193 boot]# vgextend sylink /dev/sdb3

克隆数据

[root@master193 mnt]# mkdir /mnt/efi /mnt/boot
[root@master193 mnt]# mount /dev/sdc1 /mnt/efi
[root@master193 mnt]# mount /dev/sdc2 /mnt/boot

[root@master193 mnt]# cp -a /boot/efi/* /mnt/efi/
[root@master193 mnt]# cp -a /boot/* /mnt/boot/

迁移pv

将lvm设置成raid1模式,
根据磁盘性能和数据量,该操作可能需要几时分钟到几小时

  [root@master193 ~]# lvconvert --type mirror -m 1 /dev/sylink/root  /dev/sdb3
  Logical volume sylink/root being converted.
  sylink/root: Converted: 0.12%
  sylink/root: Converted: 1.58%
  sylink/root: Converted: 4.43%
  sylink/root: Converted: 12.61%
  sylink/root: Converted: 21.03%
  sylink/root: Converted: 26.09%
  sylink/root: Converted: 31.90%
  sylink/root: Converted: 39.99%
  sylink/root: Converted: 47.80%
  sylink/root: Converted: 51.71%
  sylink/root: Converted: 53.77%
  sylink/root: Converted: 56.97%
  sylink/root: Converted: 65.02%
  sylink/root: Converted: 73.40%
  sylink/root: Converted: 76.80%
  sylink/root: Converted: 84.40%
  sylink/root: Converted: 92.87%
  sylink/root: Converted: 100.00%

执行成功后查看验证,可以看到Mirrored volumes=2,说明已经成功组成raid1

[root@master193 ~]# lvdisplay /dev/sylink/root
  --- Logical volume ---
  LV Path                /dev/sylink/root
  LV Name                root
  VG Name                sylink
  LV UUID                eTwRSu-WDbm-KjDq-jwLR-s1Gc-b8ks-lHFB36
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-03-31 18:49:40 +0800
  LV Status              available
  # open                 1
  LV Size                80.00 GiB
  Current LE             20480
  Mirrored volumes       2
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

移除旧pv

将lv设置成普通模式,最后一个参数是需要移除的pv

[root@master193 ~]# lvconvert --type mirror -m 0 /dev/sylink/root  /dev/sda3
  Logical volume sylink/root converted.

验证,可以看到pv已经空出来了

[root@master193 ~]# lvdisplay /dev/sylink/root
  --- Logical volume ---
  LV Path                /dev/sylink/root
  LV Name                root
  VG Name                sylink
  LV UUID                eTwRSu-WDbm-KjDq-jwLR-s1Gc-b8ks-lHFB36
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-03-31 18:49:40 +0800
  LV Status              available
  # open                 1
  LV Size                80.00 GiB
  Current LE             20480
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0


[root@master193 ~]# pvscan
  PV /dev/sda3   VG sylink          lvm2 [<99.50 GiB / 80.00 GiB free]
  PV /dev/sdb3   VG sylink          lvm2 [<100.00 GiB / <20.00 GiB free]
  Total: 2 [199.49 GiB] / in use: 2 [199.49 GiB] / in no VG: 0 [0   ]