Last active
July 4, 2022 23:26
-
-
Save tuklusan/3a502a8b19b5ee2fcca494f4056bdae2 to your computer and use it in GitHub Desktop.
A shell script to defragment Linux ext4 partitions and fill empty space with zeroes for compact backup of virtual machine. See https://supratim-sanyal.blogspot.com/2016/12/zero-out-free-disk-space-on-virtual.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -x | |
# * ---------------------------------------------------------------------------- | |
# * "THE BEER-WARE LICENSE" (Revision 42): | |
# * Supratim Sanyal wrote this file. As long as you retain this notice you | |
# * can do whatever you want with this stuff. If we meet some day, and you think | |
# * this stuff is worth it, you can buy me a beer in return. | |
# * https://www.emailmeform.com/builder/form/65c8aanX6uJ8RVa | |
# * ---------------------------------------------------------------------------- | |
echo THIS MUST BE RUN IN SINGLE-USER MODE AND WILL TAKE A LONG TIME maybe a day | |
echo DID YOU DO telinit 1 BEFORE RUNNING THIS? | |
echo Press ^C to abort or any other key to continue ... | |
read -n 1 | |
sync;sync;sync | |
# Defrag the data partitions | |
for i in 1 2 3 4 5 | |
do | |
e4defrag /dev/sda1 | |
sync;sync;sync | |
sleep 3 | |
e4defrag /dev/sdc1 | |
sync;sync;sync | |
sleep 3 | |
done | |
# zerofree swap partitons | |
swapoff /dev/sda2 | |
dd if=/dev/zero of=/dev/sda2 bs=4096 | |
sync;sync;sync | |
mkswap /dev/sda2 | |
swapon /dev/sda2 | |
sleep 3 | |
swapoff /dev/sdb1 | |
dd if=/dev/zero of=/dev/sdb1 bs=4096 | |
sync;sync;sync | |
mkswap /dev/sdb1 | |
swapon /dev/sdb1 | |
sleep 3 | |
# zerofree the data partitions | |
umount /dev/sdc1 | |
zerofree /dev/sdc1 | |
sync;sync;sync | |
sleep 3 | |
mount -o remount,ro /dev/sda1 | |
zerofree /dev/sda1 | |
sync;sync;sync | |
mount -o remount,rw /dev/sda1 | |
sleep 3 | |
echo NOW SHUTDOWN, then | |
echo vboxmanage modifyhd --compact the vdi virtual disks and take a backup. | |
sync;sync;sync | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment