Skip to content

Instantly share code, notes, and snippets.

@shadowbq
Last active March 15, 2023 18:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadowbq/71b934607743c9f96c16c2b6331e6761 to your computer and use it in GitHub Desktop.
Save shadowbq/71b934607743c9f96c16c2b6331e6761 to your computer and use it in GitHub Desktop.
Enlarge LVM on LUKS encrypted VG

Enlarge LVM on LUKS

Example #1 - Move Data from small physical drive to another bigger physical drive

Enlarging a LVM-on-LUKS logical partition, for instance after migrating to a larger hard disk, is done in the opposite way - from the outermost to the innermost partition:

primary partition(LUKS device{volume group[(logical partition1)(logical partition2-->)]}) Preparation Create a new partition on the new hard disk of wanted size, f.i. by using GNU Parted, and clone the old partition sdX1, containing your LUKS container, into the new partition sdY1:

# dd if=/dev/sdX1 of=/dev/sdY1 bs=4M

Extending the physical segments of the cryptdevice Now, open the cryptdevice CryptDisk on the new hard disk:

# cryptsetup open /dev/sdY1 CryptDisk

Take a look at your current physical volume. In this example, we have a cryptdevice CryptDisk containing a volume group CryptVolumeGroup of two partitions root and home:

# pvdisplay -m
 --- Physical volume ---
 PV Name               /dev/mapper/CryptDisk
 VG Name               CryptVolumeGroup
 PV Size               <118.75 GiB / not usable 3.00 MiB
 Allocatable           yes (but full)
 PE Size               4.00 MiB
 Total PE              30399
 Free PE               0
 Allocated PE          30399
 PV UUID               hu0iA9-i8fv-2SC1-C6ys-LQCz-sptQ-RSOUE5

 --- Physical Segments ---
 Physical extent 0 to 6399:
   Logical volume      /dev/CryptVolumeGroup/root
   Logical extents     0 to 6399
 Physical extent 6400 to 30398:
   Logical volume      /dev/CryptVolumeGroup/home
   Logical extents     0 to 23998

By taking the total physical extents (PE) times the PE's size, we get the total size of the physical volume (PV), in this case 118.75 GiB. Although pvdisplay does not show the free extents, we can enlarge the PV to use all the available remaining space of the partition:

# pvresize /dev/mapper/CryptDisk

Now we get:

# pvdisplay -m
...
 --- Physical Segments ---
 Physical extent 0 to 6399:
   Logical volume      /dev/CryptVolumeGroup/root
   Logical extents     0 to 6399
 Physical extent 6400 to 30398:
   Logical volume      /dev/CryptVolumeGroup/home
   Logical extents     0 to 23998
 Physical extent 30399 to 60922:
   FREE

Note the free extents at the end of the PV. Calculate the size difference by taking the free physical extends times PE size - in that case (60922-30399)*4 MiB = 119.2 GiB.

Resizing the logical volume Now we are going to resize the second logical volume (LV), in this case containing the /home partition, by the size of the free physical extents minus some safety space:

# lvresize -L +119G /dev/CryptVolumeGroup/home

Note the new size of the second logical volume. Calculate its total size by taking the total logical extends time the PE size - in that case 53438 * 4 MiB = 208.7 GiB:

# pvdisplay -m
...
 --- Physical Segments ---
 Physical extent 0 to 6399:
   Logical volume      /dev/CryptVolumeGroup/root
   Logical extents     0 to 6399
 Physical extent 6400 to 59838:
   Logical volume      /dev/CryptVolumeGroup/home
   Logical extents     0 to 53438
 Physical extent 59839 to 60922:
   FREE

Resizing the encrypted volume Now we are going to resize the encrypted volume itself. By taking in account the total size of the logical volume minus some safety space:

# resize2fs -p /dev/CryptVolumeGroup/Home 208G

Execute e2fsck, if asked, then rerun the resize2fs.

Example 2 - VMWare Growth Using Resizing an off VM.

  • Turn off the VM.
  • Delete all the snapshots. (Required by VMWare)
  • In Settings resize the Drive from 70GB to 120GB (Adding 50GB)
  • Download an Xubuntu LiveCD.iso (22.04LTS)
  • Boot Startup from CD with LiveCD.iso Attached

Note the Drive is now 119GB

root@xubuntu:~# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0    7:0    0   2.5G  1 loop /rofs
loop1    7:1    0     4K  1 loop /snap/bare/5
loop2    7:2    0  63.3M  1 loop /snap/core20/1822
loop3    7:3    0 240.6M  1 loop /snap/firefox/2356
loop4    7:4    0  49.8M  1 loop /snap/snapd/18357
loop5    7:5    0 346.3M  1 loop /snap/gnome-3-38-2004/119
loop6    7:6    0  91.7M  1 loop /snap/gtk-common-themes/1535
sda      8:0    0   120G  0 disk
├─sda1   8:1    0     1M  0 part
├─sda2   8:2    0     1G  0 part
└─sda3   8:3    0   119G  0 part
sr0     11:0    1   2.9G  0 rom  /cdrom

Open LUKS (using the password) and mount to /dev/mapper/cryptdisk

root@xubuntu:~# cryptsetup luksOpen /dev/sda3 cryptdisk
Enter passphrase for /dev/sda3: *********
root@xubuntu:~# pvdisplay -m
  --- Physical volume ---
  PV Name               /dev/mapper/cryptdisk
  VG Name               ubuntu-vg
  PV Size               77.98 GiB / not usable 0
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              19963
  Free PE               0
  Allocated PE          19963
  PV UUID               043Nca-YPTd-2Eqj-fWgg-RdHq-cdIj-uRHo3q

  --- Physical Segments ---
  Physical extent 0 to 19962:
    Logical volume	/dev/ubuntu-vg/ubuntu-lv
    Logical extents	0 to 19962

Resize the PV Group CryptDisk and Validate it - New FREE (19963 to 30458)

root@xubuntu:~# pvresize /dev/mapper/cryptdisk
  Physical volume "/dev/mapper/cryptdisk" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
root@xubuntu:~# pvdisplay -m
  --- Physical volume ---
  PV Name               /dev/mapper/cryptdisk
  VG Name               ubuntu-vg
  PV Size               118.98 GiB / not usable 1.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              30459
  Free PE               10496
  Allocated PE          19963
  PV UUID               043Nca-YPTd-2Eqj-fWgg-RdHq-cdIj-uRHo3q

  --- Physical Segments ---
  Physical extent 0 to 19962:
    Logical volume	/dev/ubuntu-vg/ubuntu-lv
    Logical extents	0 to 19962
  Physical extent 19963 to 30458:
    FREE

Resize the Logical Volume (LV) (with difference of size +37G) (short of the +50G)

root@xubuntu:~# lvresize -L +37G /dev/ubuntu-vg/ubuntu-lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from 77.98 GiB (19963 extents) to 106.98 GiB (27387 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

Examine the new PV Group Physical Extent Used (0 to 29434) vs FREE (29435 to 30458)

root@xubuntu:~# pvdisplay -m
  --- Physical volume ---
  PV Name               /dev/mapper/cryptdisk
  VG Name               ubuntu-vg
  PV Size               118.98 GiB / not usable 1.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              30459
  Free PE               1024
  Allocated PE          29435
  PV UUID               043Nca-YPTd-2Eqj-fWgg-RdHq-cdIj-uRHo3q

  --- Physical Segments ---
  Physical extent 0 to 29434:
    Logical volume	/dev/ubuntu-vg/ubuntu-lv
    Logical extents	0 to 29434
  Physical extent 29435 to 30458:
    FREE

Execute resize2fs (but mandatory e2fsck first)

root@xubuntu:~# resize2fs -p /dev/ubuntu-vg/ubuntu-lv 114G
resize2fs 1.46.5 (30-Dec-2021)
Please run 'e2fsck -f /dev/ubuntu-vg/ubuntu-lv' first.
root@xubuntu:~# e2fsck -f /dev/ubuntu-vg/ubuntu-lv
e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/ubuntu-vg/ubuntu-lv: 415036/5111808 files (0.2% non-contiguous), 19048850/20442112 blocks

Execute resize2fs

root@xubuntu:~# resize2fs -p /dev/ubuntu-vg/ubuntu-lv 114G
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/ubuntu-vg/ubuntu-lv to 29884416 (4k) blocks.
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 29884416 (4k) blocks long.

Execute reboot

After reboot, and the drive is unlocked. Show the drive sizes df -h

# df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              439M  1.7M  438M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  112G   71G   36G  67% /
tmpfs                              2.2G     0  2.2G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          974M  250M  657M  28% /boot
tmpfs                              439M  4.0K  439M   1% /run/user/1000

Reference:

@shadowbq
Copy link
Author

BTW all the GUIs have an issue doing this correctly.

apt install partitionmanager - KDE partition manager is close, but doesnt resize it without failing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment