Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- VM_SYS_CAPACITY=$(df -h / | tail -n 1 | awk '{print $2}')
- echo "[Hit CTRL+C right NOW! if you previously did NOT read that WARNING]"
- sleep 2
- echo
- echo
- echo
- echo
- echo "
- This script is a helper when shrinking or recovering unused free guest VM disk space.
- Note: Hypervisors like Proxmox allows to discard unused free space, QNAP VM clone functions work very similar and in fact
- is equal to: 'qemu-img convert -p -O qcow2 -o compat=1.1,lazy_refcounts ...'
- Requirements:
- 1) Script will perform quick disk defragmentaion to move data from end parts of the disk volume.
- Recommended: Before doing this operation its recommended to manually stop as much services as possible.
- once defragmentation is done....
- 2) free space on the guest will be filled with 0 bytes:
- That means:
- - On the host you have at least: ${VM_SYS_CAPACITY} free space on the datastore - OTHERWISE HIT CTRL+C
- - Critical services like MySQL/MariaDB has to be stopped for safety reasons
- (to prevent crash & corrupt DB disk image will run out of free space)
- ---- [Hit CTRL+C to ABORT]: 10 seconds left ...."
- sleep 10
- stop_services(){
- echo Stop critical services....
- service monitorix stop 2>/dev/null 1>/dev/null
- service grafana-server stop 2>/dev/null 1>/dev/null
- service memcached stop 2>/dev/null 1>/dev/null
- service redis-server stop 2>/dev/null 1>/dev/null
- systemctl stop kibana 2>/dev/null 1>/dev/null
- systemctl stop elasticsearch 2>/dev/null 1>/dev/null
- service mysql stop 2>/dev/null 1>/dev/null
- service apache2 stop 2>/dev/null 1>/dev/null
- }
- start_services(){
- echo Start services once again....
- service mysql start 2>/dev/null 1>/dev/null
- systemctl start elasticsearch 2>/dev/null 1>/dev/null
- systemctl start kibana 2>/dev/null 1>/dev/null
- service apache2 start 2>/dev/null 1>/dev/null
- service monitorix start 2>/dev/null 1>/dev/null
- service grafana-server start 2>/dev/null 1>/dev/null
- service memcached start 2>/dev/null 1>/dev/null
- service redis-server start 2>/dev/null 1>/dev/null
- }
- drop_cache(){
- echo Drop and flush cache...
- sync
- # drop cache
- sysctl -w vm.drop_caches=3
- # drop memory cache
- echo 3 > /proc/sys/vm/drop_caches
- echo 1 > /proc/sys/vm/compact_memory
- # sync once again
- sync
- # make sure to sync once again
- sync
- # and again for last time
- sync
- }
- stop_services
- drop_cache
- # install dependencies
- apt update 2>/dev/null 1>/dev/null
- apt install fstrim e4defrag 2>/dev/null 1>/dev/null
- # do defrag
- echo Defragmenting data:
- e4defrag /
- drop_cache
- # fill up hdd with zeros
- dd if=/dev/zero of=/zero bs=64k
- drop_cache
- # cleanup
- rm -f /zero
- drop_cache
- echo Lets trim all devices
- fstrim -a -v 2>/dev/null 1>/dev/null
- echo Lets trim all devices last time...
- fstrim -a -v 2>/dev/null 1>/dev/null
- echo Done
- echo Please shutdown VM and clone to new VM.
- echo [Hit CTRL+C to abort shut-down procedure]
- sleep 10
- shutdown -P 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement