0

How do I move/merge LVM on sda3 to sda2? The problem is vg1-var and vg1-opt reside on both PVs, so a pvmove won't do the trick.

sda                          8:0    0   80G  0 disk
├─sda1                       8:1    0  300M  0 part /boot
├─sda2                       8:2    0   37G  0 part
│ ├─vg1-root         253:0    0    5G  0 lvm  /
│ ├─vg1-swap         253:1    0    4G  0 lvm  [SWAP]
│ ├─vg1-usr          253:2    0    8G  0 lvm  /usr
│ ├─vg1-home         253:4    0    2G  0 lvm  /home
│ ├─vg1-var          253:5    0  6.7G  0 lvm  /var
│ ├─vg1-tmp          253:6    0   10G  0 lvm  /tmp
│ └─vg1-opt          253:7    0    5G  0 lvm  /opt
└─sda3                       8:3    0 42.7G  0 part
  ├─vg1-var          253:5    0  6.7G  0 lvm  /var
  └─vg1-opt          253:7    0    5G  0 lvm  /opt
  PV         VG    Fmt  Attr PSize    PFree
  /dev/sda2  vg1   lvm2 a--    37.00g       0
  /dev/sda3  vg1   lvm2 a--   <42.70g   39.00g
3
  • It doesn't look like there's space for var and opt on /dev/sda2 – roaima Jul 25 '20 at 7:48
  • Is there a specific reason you want to merge these? It's certainly possible but LVM doesn't mind having two PVs either. – frostschutz Jul 25 '20 at 9:59
  • Mainly to follow the design we have set. Yes I understand there shouldn’t be an issue with this layout but well you know it would be nice to have it clean :) – Ykaly Jul 25 '20 at 10:21
2

The simplest method would be to attach a new drive temporarily and pvmove /dev/sda3 over. That would allow you to grow /dev/sda2 and move everything back.

If that's not possible, you'll have to shuffle the data within the drive you have. Due to alignment issues, adjacent PVs can usually not be merged directly, so you'll still be moving everything twice.

Your /dev/sda3 is 42.7G large and has 39G free, so ~3.7G used. So you should be able to shrink /dev/sda3 by 4G, in order to make a new /dev/sda4 size 4G at end of disk:

pvresize /dev/sda3 --setphysicalvolumesize 37G
parted /dev/sda -- resizepart 3 -4G
parted /dev/sda -- mkpart primary -4G -1

Move all data to the new partition:

vgextend vg1 /dev/sda4
pvmove /dev/sda3
vgreduce vg1 /dev/sda3

Delete the now free /dev/sda3 and grow /dev/sda2 accordingly:

parted /dev/sda -- rm 3
parted /dev/sda -- resizepart 2 -4G
pvresize /dev/sda2

Move everything from /dev/sda4 to the now large enough /dev/sda2:

pvmove /dev/sda4
vgreduce vg1 /dev/sda4

At this point, your VG only uses a single PV, /dev/sda2.

Delete the now free /dev/sda4 and grow /dev/sda2 again:

parted /dev/sda -- rm 4
parted /dev/sda -- resizepart 2 -1
pvresize /dev/sda2

Note these steps are very rough and might fail at some points, you'll have to adapt accordingly. The parted commands in particular should not be run blindly, always verify what's going on with parted /dev/sda print free, vgs, lvs, pvs, lsblk, ...

If there are no strong reasons to shuffle things around, I'd just leave as is. It's a lot of trouble for little benefit. Having multiple PVs also has advantages, such as additional metadata copies.

5
  • Just a dumb question. Does it need to be pvresize first before actually reducing the size of partition 3? Also the mkpart - is the ‘-4G’ a typo? And what does -1 do? – Ykaly Jul 26 '20 at 7:48
  • You need to pvresize shrink the PV before you shrink the partition. -4G means 4G from end of disk (so it will make room for a 4G partition). -1 means end of disk (so, it will resize partition to full size). This works in your case since your partition happens to be the last one that goes to end of disk; if it was a partition somewhere in the middle, you'd have to do the math yourself to shrink the partition. – frostschutz Jul 26 '20 at 8:32
  • So in mkpart, a - means the size of the partition to create? And in resizepart, it means the size to shrink? I apologize for so many question. I get what the plan is; I just wanted to understand more on the parted part cause I mainly use fdisk. – Ykaly Jul 26 '20 at 8:37
  • the negative value just means "from end of disk". for details please refer to parted manual. if you're not familiar with parted, feel free to use any other partitioning software. just make sure the partition is not smaller than what you resized the PV to, so if you pvresize to 37G, the partition has to be 37 GiB at minimum. if you make the partition too small, LVM will complain and there is a risk of data loss. – frostschutz Jul 26 '20 at 8:40
  • also when resizing partitions, the start sector must not change; parted resizepart only changes the partition size. if another partition software requires you to delete and re-create the partition, you must take extra care to make it start at the same sector. – frostschutz Jul 26 '20 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.