Skip to content

Instantly share code, notes, and snippets.

@skyhisi
Last active February 9, 2024 09:43
Show Gist options
  • Save skyhisi/2a4810b7aaa4064677c277f09f600bbd to your computer and use it in GitHub Desktop.
Save skyhisi/2a4810b7aaa4064677c277f09f600bbd to your computer and use it in GitHub Desktop.
Compact ext3/4 filesystems in a VM
#!/bin/bash
# Compact ext3/4 filesystems in a VM
# Tools should all be available within a gparted live VM
# Enable discard support in VirtualBox with a command like:
# vboxmanage storageattach "GParted Live" --storagectl=VirtIO --port=0 --discard=on --nonrotational=on
# Or disable the fstrim and use
# vbox-img.exe compact --filename DISK.vdi
set -eux
for DEV in $(lsblk -o name,fstype --raw --paths | grep 'ext[34]' | cut '-d ' -f1)
do
e2fsck -f -D -p "$DEV"
resize2fs -M -p "$DEV"
resize2fs -p "$DEV"
zerofree -v "$DEV"
done
for DEV in $(lsblk -o name,fstype --raw --paths | grep 'ext[34]' | grep -v 0B | cut '-d ' -f1)
do
mount "$DEV" /mnt
fstrim -v /mnt
umount /mnt
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment