Merge a partition in LVM

General support questions
Post Reply
hack3rcon
Posts: 757
Joined: 2014/11/24 11:04:37

Merge a partition in LVM

Post by hack3rcon » 2023/02/05 11:55:32

Hello,
I want to add this 32GB free space to my "/":
HDD.png
HDD.png (68.52 KiB) Viewed 1699 times
My LVM info and HDD information are:

Code: Select all

# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0   280G  0 disk 
├─sda1            8:1    0   200M  0 part /boot/efi
├─sda2            8:2    0     1G  0 part /boot
├─sda3            8:3    0  38.8G  0 part 
│ ├─centos-root 253:0    0 246.8G  0 lvm  /
│ └─centos-swap 253:1    0     2G  0 lvm  [SWAP]
├─sda4            8:4    0   160G  0 part 
│ └─centos-root 253:0    0 246.8G  0 lvm  /
└─sda5            8:5    0    50G  0 part 
  └─centos-root 253:0    0 246.8G  0 lvm  /
sr0              11:0    1  1024M  0 rom  
loop0             7:0    0     4G  0 loop /var/tmp
#
# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 2.9G     0  2.9G   0% /dev
tmpfs                    2.9G     0  2.9G   0% /dev/shm
tmpfs                    2.9G   20M  2.9G   1% /run
tmpfs                    2.9G     0  2.9G   0% /sys/fs/cgroup
/dev/mapper/centos-root  243G  179G   54G  77% /
/dev/sda2                976M  284M  626M  32% /boot
/dev/sda1                200M   12M  189M   6% /boot/efi
/dev/loop0               3.9G  8.3M  3.7G   1% /tmp
tmpfs                    579M     0  579M   0% /run/user/0
And:

Code: Select all

# vgs
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   3   2   0 wz--n- 248.79g    0
#
# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- 246.79g                                                    
  swap centos -wi-ao----   2.00g    
#
# pvs
  PV         VG     Fmt  Attr PSize    PFree
  /dev/sda3  centos lvm2 a--    38.80g    0 
  /dev/sda4  centos lvm2 a--  <160.00g    0 
  /dev/sda5  centos lvm2 a--   <50.00g    0 
#
# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  7
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               248.79 GiB
  PE Size               4.00 MiB
  Total PE              63691
  Alloc PE / Size       63691 / 248.79 GiB
  Free  PE / Size       0 / 0   
  VG UUID               fr47JL-Wlkw-W1Uk-RjDl-xKTe-NnT2-nQ79IP
#
# lvdisplay -v
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                dFW3or-Tw3V-yLG2-Z9s6-yjVF-EfPt-VCL3tN
  LV Write Access        read/write
  LV Creation host, time X-X-X-X.static.MyHost.name, 2020-09-02 19:43:27 +0430
  LV Status              available
  # open                 1
  LV Size                246.79 GiB
  Current LE             63179
  Segments               4
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                FjaR6s-83n3-0YOm-p6lB-bfX6-S98i-nXZtzB
  LV Write Access        read/write
  LV Creation host, time X-X-X-X.static.MyHost.name, 2020-09-02 19:43:29 +0430
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
#
# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               centos
  PV Size               38.80 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              9933
  Free PE               0
  Allocated PE          9933
  PV UUID               mmwDs9-e5I6-vzzu-KkUJ-so75-PyBp-1upsYK
   
  --- Physical volume ---
  PV Name               /dev/sda4
  VG Name               centos
  PV Size               160.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              40959
  Free PE               0
  Allocated PE          40959
  PV UUID               XfUuPl-OsxC-NqfL-VnXN-Mrc6-oG3i-g2dfHR
   
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               centos
  PV Size               50.00 GiB / not usable 4.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12799
  Free PE               0
  Allocated PE          12799
  PV UUID               gh4uBI-huyn-fG3R-Y8UQ-wNC4-hEUg-Yexj39
I found following commands, but I don't know it is OK or not. Does this cause data loss?

Code: Select all

# lvextend -l +100%FREE /dev/centos/root
# resize2fs /dev/centos/root
Thank you.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Merge a partition in LVM

Post by jlehtone » 2023/02/05 13:43:24

First, the data on the screenshot seems to differ from the rest. Perhaps it is the program.

Second, it is always sensible to have a backup, even when you don't do any potentially dangerous operations.


You have a disk that has five partitions, but not all space is allocated to partitions.
1. Add partition (gdisk)
2. Initialize it as PV (pvcreate)
3. Add the PV to the VG
4. Expand the LV. The lvextend should have option -r that makes it call appropriate filesystem resize command.

hack3rcon
Posts: 757
Joined: 2014/11/24 11:04:37

Re: Merge a partition in LVM

Post by hack3rcon » 2023/02/05 19:01:19

Hello,
What does "You have a disk that has five partitions, but not all space is allocated to partitions." mean?
I created a partition from those 32GB with the "cfdisk" and it became to "sda2", but I can't see it in the output of the "lsblk" command. Why?
Can show me the full syntax of the commands from 1 to 4?

Thank you.

User avatar
TrevorH
Site Admin
Posts: 33202
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: Merge a partition in LVM

Post by TrevorH » 2023/02/05 19:44:00

The cfdisk supplied by CentOS 7 does not know about GPT labels on disk so I think you have just damaged your disk contents assuming that it had a GPT label on it before.
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Merge a partition in LVM

Post by jlehtone » 2023/02/05 20:09:20

That explains the screenshot.

The drive most likely has GPT (GUID Partition Table).
A GPT can have "protective MBR", a fake MBR partition table that has one entry to cover the sectors that are allocated in the GPT.
The cfdisk, which does not know about GPT, has assumed that the protective MBR is the real thing and modified it.

AFAIK, the actual GPT metadata is not on that location and cfdisk has merely corrupted the protective MBR.
UEFI does locate ESP (EFI System Partition) via GPT, so it should not care about the fake MBR.
Kernel ... hopefully looks for GPT first, so would not be distracted by the fake MBR either.

I don't know whether 'gdisk', which does support GPT, can actually clean up that mess when you add the 6th partition with it.

hack3rcon
Posts: 757
Joined: 2014/11/24 11:04:37

Re: Merge a partition in LVM

Post by hack3rcon » 2023/03/12 11:16:06

Hello,
Then, what are my options?


Thank you.

User avatar
jlehtone
Posts: 4523
Joined: 2007/12/11 08:17:33
Location: Finland

Re: Merge a partition in LVM

Post by jlehtone » 2023/03/13 12:28:43

Read man gdisk
There are options to show, backup and verify partition table.
Furthermore, the "recovery and transformation" menu has option to show the "protective MBR"
and "expert" menu has option to recreate the protective MBR.

Post Reply