almost-online-resize-ext4.sh 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Eigentlich lässt sich ein EXT4 Dateisystem nicht online verkleinern.
  2. # Auf serverfault.com ist unter https://serverfault.com/a/888830/329677
  3. # jedoch ein eleganten Weg über die initramfs beschrieben:
  4. # resize using initramfs
  5. # --- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
  6. disk="/dev/sda1"
  7. disk-size="25G"
  8. ### Prepare /etc/initramfs-tools/hooks/resizefs
  9. touch /etc/initramfs-tools/hooks/resizefs
  10. chmod +x /etc/initramfs-tools/hooks/resizefs
  11. cat >/etc/initramfs-tools/hooks/resizefs<<'EOF'
  12. #!/bin/sh
  13. set -e
  14. PREREQS=""
  15. prereqs() { echo "$PREREQS"; }
  16. case $1 in
  17. prereqs)
  18. prereqs
  19. exit 0
  20. ;;
  21. esac
  22. . /usr/share/initramfs-tools/hook-functions
  23. copy_exec /sbin/e2fsck
  24. copy_exec /sbin/resize2fs
  25. exit 0
  26. EOF
  27. ### Prepare /etc/initramfs-tools/scripts/local-premount/resizefs
  28. touch /etc/initramfs-tools/scripts/local-premount/resizefs
  29. chmod +x /etc/initramfs-tools/scripts/local-premount/resizefs
  30. cat >/etc/initramfs-tools/scripts/local-premount/resizefs<<'EOF'
  31. #!/bin/sh
  32. set -e
  33. PREREQS=""
  34. prereqs() { echo "$PREREQS"; }
  35. case "$1" in
  36. prereqs)
  37. prereqs
  38. exit 0
  39. ;;
  40. esac
  41. /sbin/e2fsck -yf $disk
  42. /sbin/resize2fs $disk $disk-size
  43. /sbin/e2fsck -yf $disk
  44. EOF
  45. ### Prepare initramfs and reboot
  46. update-initramfs -u
  47. reboot
  48. ### Cleanup
  49. rm /etc/initramfs-tools/hooks/resizefs
  50. rm /etc/initramfs-tools/scripts/local-premount/resizefs
  51. update-initramfs -u
  52. # Change parition size
  53. # --- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
  54. tune2fs -l /dev/sda1 | grep "Block "
  55. # Block count: 6553600
  56. # Block size: 4096
  57. # This equals
  58. # 6553600 blocks * 4096 bytes / block = 26843545600 bytes
  59. # 26843545600 / (1024^3) = 25 GB
  60. fdisk -l /dev/sda
  61. # Sector size 512 bytes
  62. # Start 2048
  63. # Calculating last sector
  64. # 26843545600 bytes / 512 bytes/sector = 52428800 sectors
  65. # 52428800 sectors + 2048 to start from = 52430848
  66. fdisk /dev/sda
  67. # - Delete partition
  68. # - Create new starting with 2048 and end with 52430848