Mirroring an Existing LVM Volume

By | 2021-11-27

Mirroring an Existing LVM Volume is pretty straightforward. Assuming you have enough space for it on separate physical volumes, it is a single command. According to the documentation, you will find two varieties. One is just called mirror, the other raid1. The documentation recommends using raid1, so that is what I will cover.

There are a few reasons you may wish to use an LVM mirror. One is protecting data from drive failures. Another is better read performance, as your system can read different data from different devices.

Creating the mirror is simple. Assuming you have enough space on another PV for the LV in question, it is a single command.

# lvconvert --type raid1 -m 1 /dev/mapper/volume-name

The -m 1 part means the number of additional copies of the LV should be maintained. Using -m 1 means you will have TWO copies of your data. If you use -m 2, you will have THREE copies.

As usual with my guides, I will work through an example.

Example Scenario

I am going to convert a LV called zabbix on a VG called pgdata into a raid1 logical volume. First, I will add a disk to the volume group. Then I will covert the LV.

Here is the starting configuration:

# pvdisplay -s
  Device "/dev/sdb" has a capacity of 520.00 MiB
# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0 19.1G  0 part /
├─sda2            8:2    0    1K  0 part 
└─sda5            8:5    0  880M  0 part [SWAP]
sdb               8:16   0    1G  0 disk 
└─pgdata-zabbix 254:0    0  500M  0 lvm  /zabbix
sdc               8:32   0    1G  0 disk 
sdd               8:48   0    1G  0 disk 
sr0              11:0    1 1024M  0 rom  
# vgdisplay -s
  "pgdata" 1020.00 MiB [500.00 MiB used / 520.00 MiB free]
# lvs -o lv_name,vg_name,lv_size,lv_layout
  LV     VG     LSize   Layout    
  zabbix pgdata 500.00m linear   

As you can see, there is currently one PV, sdb.

Create the Mirror

If you don’t have a device with sufficient space, add one. As you can see, when I tried to do it without sufficient space, LVM outputs an error. In the example, I started with a single PV, so I add another:

# lvconvert --type raid1 -y -m 1 /dev/mapper/pgdata-zabbix
  Insufficient suitable allocatable extents for logical volume : 126 more required
# pvcreate /dev/sdc
  Physical volume "/dev/sdc" successfully created.
# vgextend pgdata /dev/sdc
  Volume group "pgdata" successfully extended

Now I modify the LV so it is a raid1:

# lvconvert --type raid1  -m 1 /dev/mapper/pgdata-zabbix 
Are you sure you want to convert linear LV pgdata/zabbix to raid1 with 2 images
enhancing resilience? [y/n]: y
  Logical volume pgdata/zabbix successfully converted.
# lvs -o lv_name,vg_name,lv_size,lv_layout
  LV     VG     LSize   Layout    
  zabbix pgdata 500.00m raid,raid1
# lsblk 
NAME                     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                        8:0    0   20G  0 disk 
├─sda1                     8:1    0 19.1G  0 part /
├─sda2                     8:2    0    1K  0 part 
└─sda5                     8:5    0  880M  0 part [SWAP]
sdb                        8:16   0    1G  0 disk 
├─pgdata-zabbix_rmeta_0  254:5    0    4M  0 lvm  
│ └─pgdata-zabbix        254:4    0  500M  0 lvm  /zabbix
└─pgdata-zabbix_rimage_0 254:6    0  500M  0 lvm  
  └─pgdata-zabbix        254:4    0  500M  0 lvm  /zabbix
sdc                        8:32   0    1G  0 disk 
├─pgdata-zabbix_rmeta_1  254:2    0    4M  0 lvm  
│ └─pgdata-zabbix        254:4    0  500M  0 lvm  /zabbix
└─pgdata-zabbix_rimage_1 254:3    0  500M  0 lvm  
  └─pgdata-zabbix        254:4    0  500M  0 lvm  /zabbix
sdd                        8:48   0    1G  0 disk 
sr0                       11:0    1 1024M  0 rom  

Notice how the zabbix LV is now showing on two different block devices.

Adding/Removing Mirrors

If you wish to add another device to create a three way mirror, just follow the same steps and add modify the copy count:

# pvcreate /dev/sdd
  Physical volume "/dev/sdd" successfully created.
# vgextend pgdata /dev/sdd
  Volume group "pgdata" successfully extended
# lvconvert --type raid1 -y -m 2 /dev/mapper/pgdata-zabbix
  Logical volume pgdata/zabbix successfully converted.
# lsblk 
NAME                     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                        8:0    0   20G  0 disk 
├─sda1                     8:1    0 19.1G  0 part /
├─sda2                     8:2    0    1K  0 part 
└─sda5                     8:5    0  880M  0 part [SWAP]
sdb                        8:16   0    1G  0 disk 
├─pgdata-zabbix_rmeta_0  254:1    0    4M  0 lvm  
│ └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
└─pgdata-zabbix_rimage_0 254:2    0  500M  0 lvm  
  └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
sdc                        8:32   0    1G  0 disk 
├─pgdata-zabbix_rmeta_1  254:3    0    4M  0 lvm  
│ └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
└─pgdata-zabbix_rimage_1 254:4    0  500M  0 lvm  
  └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
sdd                        8:48   0    1G  0 disk 
├─pgdata-zabbix_rmeta_2  254:5    0    4M  0 lvm  
│ └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
└─pgdata-zabbix_rimage_2 254:6    0  500M  0 lvm  
  └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
sr0                       11:0    1 1024M  0 rom  

You can remove mirrors too:

# lvconvert --type raid1 -y -m 1 /dev/mapper/pgdata-zabbix
  Logical volume pgdata/zabbix successfully converted.
# lsblk 
NAME                     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                        8:0    0   20G  0 disk 
├─sda1                     8:1    0 19.1G  0 part /
├─sda2                     8:2    0    1K  0 part 
└─sda5                     8:5    0  880M  0 part [SWAP]
sdb                        8:16   0    1G  0 disk 
├─pgdata-zabbix_rmeta_0  254:1    0    4M  0 lvm  
│ └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
└─pgdata-zabbix_rimage_0 254:2    0  500M  0 lvm  
  └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
sdc                        8:32   0    1G  0 disk 
├─pgdata-zabbix_rmeta_1  254:3    0    4M  0 lvm  
│ └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
└─pgdata-zabbix_rimage_1 254:4    0  500M  0 lvm  
  └─pgdata-zabbix        254:0    0  500M  0 lvm  /zabbix
sdd                        8:48   0    1G  0 disk 
sr0                       11:0    1 1024M  0 rom  

If you wish, you can even go back to a simple linear volume:

# lvconvert --type linear -y /dev/mapper/pgdata-zabbix
  Logical volume pgdata/zabbix successfully converted.
# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0 19.1G  0 part /
├─sda2            8:2    0    1K  0 part 
└─sda5            8:5    0  880M  0 part [SWAP]
sdb               8:16   0    1G  0 disk 
└─pgdata-zabbix 254:0    0  500M  0 lvm  /zabbix
sdc               8:32   0    1G  0 disk 
sdd               8:48   0    1G  0 disk 
sr0              11:0    1 1024M  0 rom  

References

See Also

Discuss