0

I know you can add space onto the end of a filesystem, but what about into the middle? For example, if the FS is on a logical volume which spans multiple drives.

[logical-volume]
  |    |    |
[sda][sdb][sdc]

Like if previously unused space becomes available on sdb drive?

P.S. I guess this would be handled by LVM itself rather than the filesystem, if it's even possible.

1 Answer 1

0

Found it.

Short version: use pvmove. Long version:

If the situation as might be returned by lvdisplay --maps is as follows:

--- Logical volume ---
  LV Path                /dev/main/data
  Current LE             300
  Segments               3
  (redacted)

  --- Segments ---
  Logical extent 0 to 99:
    Physical volume /dev/sda1
    Physical extents    0 to 99

  Logical extent 100 to 199:
    Physical volume /dev/sdb1
    Physical extents    0 to 99

  Logical extent 200 to 299:
    Physical volume /dev/sdc1
    Physical extents    0 to 99

Then as you can see, a logical volume is spread across 3 disks, and occupies 100 extents on each. 'Extents' are LVM's version of sectors; they are the smallest unit it is generally concerned with.

But if we look at the physical volumes underneath:

pvdisplay

  --- Physical volume ---
  PV Name           /dev/sda1
  Total PE          100
  Free PE           0
  Allocated PE      100

  --- Physical volume ---
  PV Name           /dev/sdb1
  Total PE          300
  Free PE           200
  Allocated PE      100

  --- Physical volume ---
  PV Name           /dev/sdc1
  Total PE          100
  Free PE           0
  Allocated PE      100

... we can see that the second disk's partition (/dev/sdb1) has quite a lot of extra space.

Now, we could just add that directly to the logical volume, and while this would probably suffice for most situations, it would would leave the volume in an unnecessarily fragmented state. The end of the logical volume would be on the middle disk. This is where the pvmove command comes in.

pvmove allows us to arbitrarily rearrange the configuration of a logical volume, without the filesystem contained within being aware of it.

Warning: pvmove may cause data loss. Ideally, backup the filesystem before proceeding.

Make sure the logical volume is unmounted before proceeding.

basic syntax: pvmove SourcePhysicalVolume:PE-PE DestinationPhysicalVolume:PE-PE

First command: Reattach the extents from sdc1 onto sdb1, thus making everything contiguous:

pvmove /dev/sdc1:0-99 /dev/sdb1:100-199

Afterwards lvdisplay --maps gives:

--- Logical volume ---
  LV Path                /dev/main/data
  Current LE             300
  Segments               2
  (redacted)

  --- Segments ---
  Logical extent 0 to 99:
    Physical volume /dev/sda1
    Physical extents    0 to 99

  Logical extent 100 to 299:
    Physical volume /dev/sdb1
    Physical extents    0 to 199

Now, it's simply a matter of extending the logical volume through the remaining space on /dev/sdb1 and the now entirely empty /dev/sdc1. This is much simpler. The next command tells lvm to expand our logical volume across all the remaining free extents on those partitions.

lvextend -l +100%free /dev/main/data /dev/sdb1 /dev/sdc1

Afterwards the logical volume would be like so:

--- Logical volume ---
  LV Path                /dev/main/data
  Current LE             500
  Segments               2
  (redacted)

  --- Segments ---
  Logical extent 0 to 99:
    Physical volume /dev/sda1
    Physical extents    0 to 99

  Logical extent 100 to 399:
    Physical volume /dev/sdb1
    Physical extents    0 to 299

  Logical extent 400 to 499:
    Physical volume /dev/sdc1
    Physical extents    0 to 99

Which is beautiful, and as seamless as possible.

Now we can extend the filesystem into the new space.

resize2fs /dev/main/data

Conclusion: The pvmove command is extremely powerful and allows one to take advantage of all the abstraction lvm offers. It allows you to arbitrarily rearrange the physical space on which a logical volume sits.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .