# https://linuxconfig.org/linux-lvm-logical-volume-manager
# apt-get install lvm2 # sudo dnf install lvm2
# fdisk -l
# lsblk # lsblk -fe7 # lsblk -o NAME,FSTYPE,LABEL,MOUNTPOINT,SIZE,MODEL
# blkid
# parted -l # parted /dev/sde u s p
# df -hT # df -TH
# du -sh /home # du -h /var | sort -hr | head -n10
# Check FREE SPACE # sfdisk --list-free /dev/vda # hwinfo --short --block # cat /proc/partitions #
# Check SPEED # sudo hdparm -t --direct /dev/nvme0n1p1
# pvs, vgs, lvs, pvdisplay, vgdisplay, lvdisplay
----------------------------------------------------------
# WIPE DESTROY DISK CONTENTS
# shred -n 5 -vz /dev/sdb
# scrub -p dod /dev/sdb
umount /dev/vgiso/iso && umount /dev/vgvm/vm && umount /dev/vgbackups/backups
dd if=/dev/zero of=/dev/sdb  bs=512  count=1
----------------------------------------------------------
# PARTITION ON CMD LINE
# cfdisk /dev/sdb
fdisk /dev/sdb # then m, p, g, n, w, q
Device          Start        End    Sectors  Size Type
/dev/sdb1   419432448  838862847  419430400  200G Linux filesystem
/dev/sdb2   838862848 1048578047  209715200  100G Linux filesystem
/dev/sdb3  1048578048 3907028991 2858450944  1.3T Linux filesystem
mkfs.ext4 -L VM /dev/sdb1
mkfs.ext4 -L ISO /dev/sdb2
mkfs.ext4 -L BACKUPS /dev/sdb3
----------------------------------------------------------
# CREATE LVM VOLUME(s)
pvcreate /dev/sdb1
vgcreate vgvm /dev/sdb1
lvcreate vgvm -l 100%FREE -n vm
pvcreate /dev/sdb2
vgcreate vgiso /dev/sdb2
lvcreate vgiso -l 100%FREE -n iso
pvcreate /dev/sdb3
vgcreate vgbackups /dev/sdb3
lvcreate vgbackups -l 100%FREE -n backups
# lvcreate vgextra-vm -L 500G -n vg-vm vgextra
# lvcreate vgextra-backups -L 900G -n vg-backups vgextra
lvdisplay
mkfs.ext4 -m 0 /dev/vgvm/vm
mkfs.ext4 -m 0 /dev/vgiso/iso
mkfs.ext4 -m 0 /dev/vgbackups/backups
mkdir /iso && mkdir /vm && mkdir /backups
vi /etc/fstab
/dev/vgiso/iso    /iso    ext4    defaults        0       0
/dev/vgvm/vm     /vm     ext4    defaults        0       0
/dev/vgbackups/backups        /backups    ext4    defaults        0       0
systemctl daemon-reload
mount -a
----------------------------------------------------------
# REMOVE LVM VOLUME # df -hT # vi /etc/fstab
umount /dev/vgiso/iso && umount /dev/vgvm/vm && umount /dev/vgbackups/backups
lvchange -an /dev/vgiso/iso && lvchange -an /dev/vgvm/vm && lvchange -an /dev/vgbackups/backups
lvremove /dev/vgiso/iso && lvremove /dev/vgvm/vm && lvremove /dev/vgbackups/backups
lvscan
vgremove vgiso && vgremove vgvm && vgremove vgbackups
pvremove /dev/sdd1 --force --force && pvremove /dev/sdd2 --force --force && pvremove /dev/sdd3 --force --force
----------------------------------------------------------
# RESIZE LVM AS ROOT: https://www.golinuxcloud.com/lvm-shrink-logical-volume/
df -hT /home
lsblk -f
umount /home
fsck -f /dev/mapper/pg--vg-home
resize2fs /dev/mapper/pg--vg-home 5G
lvreduce -r -L 5G /dev/mapper/pg--vg-home
mount /dev/mapper/pg--vg-home /home

# Extend the logical volume zeus-vg/var by 4G
lvextend -L+4G /dev/mapper/zeus--vg-var
# resize the ext4 filesystem in logical volume zeus-vg/var to the new volume size
resize2fs /dev/mapper/zeus--vg-var
# lvextend --resizefs -l +100%FREE /dev/mapper/vg_debian-lv_root
----------------------------------------------------------
# SHRINK REDUCE LVM GROUP AS ROOT:https://unix.stackexchange.com/questions/67702/how-to-reduce-volume-group-size-in-lvm
# https://www.rootusers.com/lvm-resize-how-to-decrease-an-lvm-partition/
sudo lvresize --verbose --resizefs -L -150G /dev/ubuntu/root
sudo pvresize --setphysicalvolumesize {any size here} /dev/sda5

# The last command, pvresize, may yield the error
# /dev/sda5: cannot resize to xxxxx extents as later ones are allocated.
# You have to rearrange the unallocated space at the end of the LVM. That means after root and swap_1 partition. You can see the current arrangement of space with this command

pvs -v --segments /dev/sda5
pvs -v --segments /dev/nvme0n1p3

pvs will show output like this

/dev/sda5 ubuntu lvm2 a-- 698.04g 150g 0 xxx+1 root 0 linear /dev/sda:0-xxx
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx+1 iii 0 free
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g yyyy jjj swap 0 linear /dev/sda5:yyyy-end

Now use pvmove to remove external fragmentation:

sudo pvmove --alloc anywhere /dev/sda5:yyyy-end

pvs -v --segments /dev/sda5

should show the new order of volumes:

/dev/sda5 ubuntu lvm2 a-- 698.04g 150g 0 xxx+1 root 0 linear /dev/sda:0-xxx
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g xxx+1 iii swap 0 linear /dev/sda5:xxx+1-yyyy
/dev/sda5 ubuntu lvm2 a-- 698.04g 150g yyyy+1 end 0 free
pvs -v --segments /dev/nvme0n1p3

After that, use GParted and resize the LVM to the maximum used area. The rest will be in unallocated space.
----------------------------------------------------------
#RESIZE ext4 - # unmount /home because an ext4 filesystem cannot be reduced while mounted
umount /home
fsck -f /dev/mapper/zeus--vg-home
resize2fs /dev/mapper/zeus--vg-home 50G
# reduce the logical volume zeus-vg/home to 50G
lvreduce -L50G /dev/mapper/zeus--vg-home
# remount /home
mount /home

# MOUNT OTHER DRIVES - GET UUID by blkid & Edit /etc/fstab
UUID=24e22f8f-5218-4c1c-bb44-970c0a7723fa       /2TBSEAGATE     ext4    defaults        0       2
UUID=6e291f1f-9640-420a-8fdc-6468459bcf8d       /1TBNVME        ext4    defaults        0       2

# cd/media
# mkdir 2TBSEAGATE
# mkdir 1TBNVME
# cd /etc
# mkdir backup
# cp /etc/fstab /etc/backup/fstab
# vi /etc/fstab
# /dev/sda1       /media/2TBSEAGATE       ext4    defaults        0       2
# /dev/nvme1n1p1  /media/1TBNVME          ext4    defaults        0       2
----------------------------------------------------------
# CREATE NEW SNAPSHOT
lvcreate /dev/mapper/<SOURCE VOLUMEGROUP NAME> -L 1G -s -n snapshot_name
lvs
# Mount a snapshot
mount /dev/mapper/vg_extra-web_snapshot_20240624 /mnt/extra/snapshot
# Restore a snapshot, Umount the original volume:
umount /path/to/mounted/logical-volume
lvconvert --merge /dev/mapper/snapshot_name
# Deactivate/reactivate to fresh it:
lvchange -an /dev/mapper/vg_extra-lv_web
lvchange -ay /dev/mapper/vg_extra-lv_web
