run_fat_tests.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Run filesystem operations tests on an 1 MiB disk image that is formatted with
  5. # a vfat filesystem and mounted in a temporary directory using a loop device.
  6. #
  7. # Copyright 2022 Red Hat Inc.
  8. # Author: Javier Martinez Canillas <javierm@redhat.com>
  9. set -e
  10. set -u
  11. set -o pipefail
  12. BASE_DIR="$(dirname $0)"
  13. TMP_DIR="$(mktemp -d /tmp/fat_tests_tmp.XXXXXX)"
  14. IMG_PATH="${TMP_DIR}/fat.img"
  15. MNT_PATH="${TMP_DIR}/mnt"
  16. cleanup()
  17. {
  18. mountpoint -q "${MNT_PATH}" && unmount_image
  19. rm -rf "${TMP_DIR}"
  20. }
  21. trap cleanup SIGINT SIGTERM EXIT
  22. create_loopback()
  23. {
  24. touch "${IMG_PATH}"
  25. chattr +C "${IMG_PATH}" >/dev/null 2>&1 || true
  26. truncate -s 1M "${IMG_PATH}"
  27. mkfs.vfat "${IMG_PATH}" >/dev/null 2>&1
  28. }
  29. mount_image()
  30. {
  31. mkdir -p "${MNT_PATH}"
  32. sudo mount -o loop "${IMG_PATH}" "${MNT_PATH}"
  33. }
  34. rename_exchange_test()
  35. {
  36. local rename_exchange="${BASE_DIR}/rename_exchange"
  37. local old_path="${MNT_PATH}/old_file"
  38. local new_path="${MNT_PATH}/new_file"
  39. echo old | sudo tee "${old_path}" >/dev/null 2>&1
  40. echo new | sudo tee "${new_path}" >/dev/null 2>&1
  41. sudo "${rename_exchange}" "${old_path}" "${new_path}" >/dev/null 2>&1
  42. sudo sync -f "${MNT_PATH}"
  43. grep new "${old_path}" >/dev/null 2>&1
  44. grep old "${new_path}" >/dev/null 2>&1
  45. }
  46. rename_exchange_subdir_test()
  47. {
  48. local rename_exchange="${BASE_DIR}/rename_exchange"
  49. local dir_path="${MNT_PATH}/subdir"
  50. local old_path="${MNT_PATH}/old_file"
  51. local new_path="${dir_path}/new_file"
  52. sudo mkdir -p "${dir_path}"
  53. echo old | sudo tee "${old_path}" >/dev/null 2>&1
  54. echo new | sudo tee "${new_path}" >/dev/null 2>&1
  55. sudo "${rename_exchange}" "${old_path}" "${new_path}" >/dev/null 2>&1
  56. sudo sync -f "${MNT_PATH}"
  57. grep new "${old_path}" >/dev/null 2>&1
  58. grep old "${new_path}" >/dev/null 2>&1
  59. }
  60. unmount_image()
  61. {
  62. sudo umount "${MNT_PATH}" &> /dev/null
  63. }
  64. create_loopback
  65. mount_image
  66. rename_exchange_test
  67. rename_exchange_subdir_test
  68. unmount_image
  69. exit 0