Shrinking and extending the fs to extend the RAID with a new disk

Technically, it should be simple, add the device to the raid and grow:

mdadm --add /dev/md127 /dev/sdc1
mdadm --grow --raid-devices=4 /dev/md127

extend the pv and lv (including the file system, ext4). This could with the filesystem mounted.

pvresize /dev/md127
lvresize --resizefs -l +100%FREE /dev/mapper/vgdata-lvdata

But, this doesn’t work if the new device is slightly smaller than the previous, as this steps requires the new device to be at least as big as all the previous. Here the issue was that the new WD GREEN ssd was 3 MiB smaller than the old model.

Get to work

We need to execute the following steps: * shrink lv and fs * shrink pv * shrink raid * add device to raid * grow raid * grow pv * grow lv/fs

The raid is /dev/md127, part of the PV /dev/mapper/vgdata-lvdata. The new device is /dev/sdc. One GPT partition /dev/sdc1 is covering the whole device.

Shrinking

Shrinking lv and the filesystem (ext4) in this case is one step. The size here is large enough for all files on the fs, but small enough for the new disk.

lvresize --resizefs --size  1921470000K /dev/mapper/vgdata-lvdata

The next step works with mounted file system. First we need to ensure that the free space is in the end. Here it is, because there is only one lv.

pvs -v --segments /dev/md127
  PV         VG     Fmt  Attr PSize  PFree   Start  SSize  LV     Start Type   PE Ranges    
  /dev/md127 vgdata lvm2 a--  <1.81t <17.54g      0 469109 lvdata     0 linear /dev/md127:0-469108
  /dev/md127 vgdata lvm2 a--  <1.81t <17.54g 469109   4490            0 free

Now we shrink the PV.

pvresize --setphysicalvolumesize 1820G /dev/md127

And finally use device size here, not the total size of the RAID. Should be at most the size of the new drive. We grow everything later.

mdadm --grow /dev/md127 --size=976528896 --backup-file=/root/mdadm.backup

Growing

Add the new device and grow the RAID5 from 3 to 4 devices.

mdadm --add /dev/md127 /dev/sdc1
mdadm --grow --raid-devices=4 /dev/md127

Now we can increase the size to the maximum allowed. This is limited by the smallest device.

mdadm --grow /dev/md127 --size=max
pvresize /dev/md127

And finally increase the LV to use all the free capacity.

lvresize --resizefs -l +100%FREE /dev/mapper/vgdata-lvdata

And we are done.

pvs -v --segments  /dev/md127
  PV         VG     Fmt  Attr PSize  PFree Start SSize  LV     Start Type   PE Ranges
  /dev/md127 vgdata lvm2 a--  <2.73t    0      0 715305 lvdata     0 linear /dev/md127:0-715304