resize-rootfs-if-needed.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #!/bin/sh
  2. # Possibly resize root device (partition, filesystem), if underlying device was
  3. # enlarged.
  4. set -e
  5. # if underlying root device is read-only, don't do anything
  6. if [ "$(blockdev --getro /dev/xvda)" -eq "1" ]; then
  7. echo "xvda is read-only, not resizing" >&2
  8. exit 0
  9. fi
  10. sysfs_xvda="/sys/class/block/xvda"
  11. # if root filesystem is already using (almost) the whole disk
  12. # 203M for BIOS and /boot data
  13. boot_data_size=$((203 * 2 * 1024))
  14. # rootfs size is calculated on-the-fly. `df` doesn't work because it doesn't
  15. # include fs overhead, and calculating a static size for overhead doesn't work
  16. # because that can change dynamically over the filesystem's lifetime.
  17. # See QubesOS/qubes-core-agent-linux#146 and QubesOS/qubes-core-agent-linux#152
  18. # for more details
  19. ext4_block_count=$(dumpe2fs /dev/mapper/dmroot | grep '^Block count:' | sed -E 's/Block count:[[:space:]]+//')
  20. ext4_block_size=$(dumpe2fs /dev/mapper/dmroot | grep '^Block size:' | sed -E 's/Block size:[[:space:]]+//')
  21. rootfs_size=$((ext4_block_count * ext4_block_size / 512))
  22. # 5 MB in 512-byte units for some random extra bits
  23. size_margin=$((5 * 1024 * 2))
  24. if [ "$(cat $sysfs_xvda/size)" -lt \
  25. $(( rootfs_size + boot_data_size + size_margin )) ]; then
  26. echo "root filesystem already at $rootfs_size blocks" >&2
  27. exit 0
  28. fi
  29. # resize needed, do it
  30. /usr/lib/qubes/resize-rootfs