“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.

# HAVE A FULL BACKUP OF THE HARDDISK IN QUESTION!

# boot dwaves' live usb stick, or another live system
# (it is Debian 10 based)

# IdealLinux stick, already has those requirements installed
su - root
apt update
apt install lvm2 cryptsetup

# first thing: find the partition in question

lsblk -fs

# might help too
alias harddisks='lsblk -o '\''NAME,MAJ:MIN,RM,SIZE,RO,FSTYPE,MOUNTPOINT,UUID'\'''

# run the alias
harddisks

# look at the disk sizes...
# which one is (probably) the partition in question?

# found it? good, let's continue...

# decrypt the partition
cryptsetup open /dev/sda5 encrypted

*** enter decryption password ***

# make all volume groups active
vgchange -ay
  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

vgdisplay; # 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   

lvdisplay; # 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
fsck -y -v -f /dev/toplap-vg/root

# resize the root filesystem from 930GB to 900GB
resize2fs /dev/toplap-vg/root 900G

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.

lvreduce -L 900G /dev/toplap-vg/root 

  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]: y
  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
fsck -y -v -f /dev/toplap-vg/root

lvextend -l +100%FREE /dev/toplap-vg/swap_1
  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.

mkswap /dev/toplap-vg/swap_1
mkswap: /dev/toplap-vg/swap_1: warning: wiping old swap signature.
Setting up swapspace version 1, size = 31 GiB (33302769664 bytes)

sync; sync; sync;

# reboot system

shutdown -r now;

it worked?

free -m
               total        used        free      shared  buff/cache   available
Mem:            7647         838        5338         162        1470        6382
Swap:          31759           0       31759

possibly related Links:

also checkout:

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

admin