Advertisement
silek65

shrink_to_export.sh

Mar 16th, 2024
791
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.91 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. VM_SYS_CAPACITY=$(df -h / | tail -n 1 | awk '{print $2}')
  4.  
  5. echo "[Hit CTRL+C right NOW! if you previously did NOT read that WARNING]"
  6. sleep 2
  7. echo
  8. echo
  9. echo
  10. echo
  11. echo "
  12. This script is a helper when shrinking or recovering unused free guest VM disk space.
  13.  
  14. Note: Hypervisors like Proxmox allows to discard unused free space, QNAP VM clone functions work very similar and in fact
  15.      is equal to: 'qemu-img convert -p -O qcow2 -o compat=1.1,lazy_refcounts ...'
  16.  
  17. Requirements:
  18. 1) Script will perform quick disk defragmentaion to move data from end parts of the disk volume.
  19.   Recommended: Before doing this operation its recommended to manually stop as much services as possible.
  20.  
  21. once defragmentation is done....
  22. 2) free space on the guest will be filled with 0 bytes:
  23.   That means:
  24.   - On the host you have at least: ${VM_SYS_CAPACITY} free space on the datastore - OTHERWISE HIT CTRL+C
  25.   - Critical services like MySQL/MariaDB has to be stopped for safety reasons
  26.     (to prevent crash & corrupt DB disk image will run out of free space)
  27.  
  28. ---- [Hit CTRL+C to ABORT]: 10 seconds left ...."
  29.  
  30. sleep 10
  31.  
  32. stop_services(){
  33.     echo Stop critical services....
  34.     service monitorix stop 2>/dev/null 1>/dev/null
  35.     service grafana-server stop 2>/dev/null 1>/dev/null
  36.     service memcached stop 2>/dev/null 1>/dev/null
  37.     service redis-server stop 2>/dev/null 1>/dev/null
  38.     systemctl stop kibana 2>/dev/null 1>/dev/null
  39.     systemctl stop elasticsearch 2>/dev/null 1>/dev/null
  40.     service mysql stop 2>/dev/null 1>/dev/null
  41.     service apache2 stop 2>/dev/null 1>/dev/null
  42. }
  43.  
  44. start_services(){
  45.     echo Start services once again....
  46.     service mysql start 2>/dev/null 1>/dev/null
  47.     systemctl start elasticsearch 2>/dev/null 1>/dev/null
  48.     systemctl start kibana 2>/dev/null 1>/dev/null
  49.     service apache2 start 2>/dev/null 1>/dev/null
  50.     service monitorix start 2>/dev/null 1>/dev/null
  51.     service grafana-server start 2>/dev/null 1>/dev/null
  52.     service memcached start 2>/dev/null 1>/dev/null
  53.     service redis-server start 2>/dev/null 1>/dev/null
  54. }
  55.  
  56. drop_cache(){
  57.     echo Drop and flush cache...
  58.  
  59.     sync
  60.     # drop cache
  61.     sysctl -w vm.drop_caches=3
  62.  
  63.     # drop memory cache
  64.     echo 3 > /proc/sys/vm/drop_caches
  65.     echo 1 > /proc/sys/vm/compact_memory
  66.     # sync once again
  67.     sync
  68.     # make sure to sync once again
  69.     sync
  70.     # and again for last time
  71.     sync
  72. }
  73.  
  74. stop_services
  75.  
  76. drop_cache
  77.  
  78. # install dependencies
  79. apt update 2>/dev/null 1>/dev/null
  80. apt install fstrim e4defrag 2>/dev/null 1>/dev/null
  81.  
  82. # do defrag
  83. echo Defragmenting data:
  84. e4defrag /
  85.  
  86. drop_cache
  87.  
  88. # fill up hdd with zeros
  89. dd if=/dev/zero of=/zero bs=64k
  90. drop_cache
  91. # cleanup
  92. rm -f /zero
  93.  
  94. drop_cache
  95.  
  96. echo Lets trim all devices
  97. fstrim -a -v 2>/dev/null 1>/dev/null
  98.  
  99.  
  100. echo Lets trim all devices last time...
  101. fstrim -a -v 2>/dev/null 1>/dev/null
  102.  
  103. echo Done
  104. echo Please shutdown VM and clone to new VM.
  105.  
  106. echo [Hit CTRL+C to abort shut-down procedure]
  107. sleep 10
  108. shutdown -P 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement