GNU Linux (Debian) - how to resize lvm2 luks encrypted root partition - how to enlarge SWAP space
“to swap, or not to swap, that is the question”: definately swap Shakespeare
“to encrypt, or not to encrypt” (if it contains valuable data, encrypt)
“to lvm, or not to lvm” (well….)
because: one fine day, the user decides to open 1000x programs + serveral vms at the same time, why not right?
the system runs out of RAM AND SWAP space (it almost freezes, to death).
but behold!
instead of simply pulling the plug.
the admin decides to wait and work on device B, while device A is struggling to keep alive.
after aprox 15min… the kvm vm crashes (probably because it used the most RAM) while the other programs survive.
For some reason, the otherwise excellent Debian installer, per default decides to reserve only 1GByte of SWAP on an 1000GB SSD.
In past times, the rule of thumb was “at least half of RAM size”, would even go as far and say: make swap = RAM size, why not? SSD space and NVMe space are getting cheaper and faster, nothing worse then running out of RAM & SWAP space (system enters undefined behavior, not only GNU Linux, OSX too)
let’s change that to something like 30GB of swap.
per default the user (probably) has chosen to use lvm2 (logical volume management) + luks2 encryption.
gparted is a pretty nice gui based program, that can do resize of ext4 partitions nicely, but it can’t help with logical volume management.
<span style="color: #ff0000;"># HAVE A FULL BACKUP OF THE HARDDISK IN QUESTION!</span>
# <a href="https://dwaves.de/2021/04/11/ideallinux2021-updated-optimized-bootale-gnu-debian-10-9-64bit-non-free-based-usb-stick-the-live-system-that-keeps-changes-stored/">boot dwaves' live usb stick, or another live system</a>
# (it is Debian 10 based)
# IdealLinux stick, already has those requirements installed
<span style="color: #00ffff;">su - root
apt update
apt install lvm2 cryptsetup</span>
# first thing: find the partition in question
<span style="color: #00ffff;">lsblk -fs</span>
# might help too
<span style="color: #00ffff;">alias harddisks='lsblk -o '\''NAME,MAJ:MIN,RM,SIZE,RO,FSTYPE,MOUNTPOINT,UUID'\'''</span>
# run the alias
<span style="color: #00ffff;">harddisks</span>
# look at the disk sizes...
# which one is (probably) the partition in question?
# found it? good, let's continue...
# decrypt the partition
<span style="color: #00ffff;">cryptsetup open /dev/sda5 encrypted</span>
*** enter decryption password ***
# make all volume groups active
<span style="color: #00ffff;">vgchange -ay</span>
2 logical volume(s) in volume group "IdealLinux2021-vg" now active
2 logical volume(s) in volume group "toplap-vg" now active <- that's the one
<span style="color: #00ffff;">vgdisplay</span>; # list all volume groups
--- Volume group ---
VG Name toplap-vg <-
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size <931.02 GiB
PE Size 4.00 MiB
Total PE 238340
Alloc PE / Size 238340 / <931.02 GiB
Free PE / Size 0 / 0
<span style="color: #00ffff;">lvdisplay</span>; # list all logical volumes
--- Logical volume ---
LV Path /dev/toplap-vg/root <- the one to shrink down to 900GB
LV Name root
VG Name toplap-vg
LV Write Access read/write
LV Creation host, time toplap, 2022-05-17 03:48:56 -0400
LV Status available
# open 0
LV Size 930.06 GiB
Current LE 238096
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:3
--- Logical volume ---
LV Path /dev/toplap-vg/swap_1 <- the swap that is too small, will increase to 30GB
LV Name swap_1
VG Name toplap-vg
LV Write Access read/write
LV Creation host, time toplap, 2022-05-17 03:48:56 -0400
LV Status available
# open 0
LV Size 976.00 MiB
Current LE 244
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:4
# run a pre-resize filesystem check
<span style="color: #00ffff;">fsck -y -v -f /dev/toplap-vg/root</span>
# resize the root filesystem from 930GB to 900GB
<span style="color: #00ffff;">resize2fs /dev/toplap-vg/root 900G</span>
resize2fs 1.44.5 (15-Dec-2018)
Resizing the filesystem on /dev/toplap-vg/root to 235929600 (4k) blocks.
The filesystem on /dev/toplap-vg/root is now 235929600 (4k) blocks long.
<span style="color: #00ffff;">lvreduce -L 900G /dev/toplap-vg/root</span>
WARNING: Reducing active logical volume to 900.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce toplap-vg/root? [y/n]: <span style="color: #00ffff;">y</span>
Size of logical volume toplap-vg/root changed from 930.06 GiB (238096 extents) to 900.00 GiB (230400 extents).
Logical volume toplap-vg/root successfully resized.
# post-resize filesystem check
<span style="color: #00ffff;">fsck -y -v -f /dev/toplap-vg/root</span>
<span style="color: #00ffff;">
lvextend -l +100%FREE /dev/toplap-vg/swap_1</span>
Size of logical volume toplap-vg/swap_1 changed from 976.00 MiB (244 extents) to <31.02 GiB (7940 extents).
Logical volume toplap-vg/swap_1 successfully resized.
<span style="color: #00ffff;">mkswap /dev/toplap-vg/swap_1</span>
mkswap: /dev/toplap-vg/swap_1: warning: wiping old swap signature.
Setting up swapspace version 1, size = 31 GiB (33302769664 bytes)
<span style="color: #00ffff;">sync; sync; sync;</span>
# reboot system
<span style="color: #00ffff;">shutdown -r now</span>;
it worked?
possibly related Links:
also checkout:
https://dwaves.de/2017/05/29/lnux-lvm-lvm2-logical-volumen-management-concept-man-pages-dynamic-resizing-partitions-snapshots-how-to-upgrade-lvm2-encrypted-to-larger-harddisk/
cudos:
https://www.casesup.com/category/knowledgebase/howtos/how-to-shrink-an-lvm-volume-safely-on-linux
#linux #gnu #gnulinux #opensource #administration #sysops #gnu-linux #debian #storage #ram #swap
Originally posted at: https://dwaves.de/2022/05/19/gnu-linux-debian-how-to-resize-lvm2-luks-encrypted-root-partition-how-to-enlarge-swap-space/