test_mdadm.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import subprocess
  3. import time
  4. import infra.basetest
  5. class TestMdadm(infra.basetest.BRTest):
  6. # This test creates a dm-raid volume with mdadm. A specific Kernel
  7. # need to be built with a config fragment enabling this support.
  8. kernel_fragment = \
  9. infra.filepath("tests/package/test_mdadm/linux-mdadm.fragment")
  10. config = \
  11. f"""
  12. BR2_aarch64=y
  13. BR2_TOOLCHAIN_EXTERNAL=y
  14. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  15. BR2_LINUX_KERNEL=y
  16. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  17. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.1.75"
  18. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  19. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  20. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kernel_fragment}"
  21. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  22. BR2_PACKAGE_E2FSPROGS=y
  23. BR2_PACKAGE_MDADM=y
  24. BR2_TARGET_ROOTFS_CPIO=y
  25. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  26. # BR2_TARGET_ROOTFS_TAR is not set
  27. """
  28. def test_run(self):
  29. # Test configuration:
  30. md_dev = "/dev/md0"
  31. storage_devs = ["/dev/vda", "/dev/vdb", "/dev/vdc"]
  32. storage_size = 16 # Mega Bytes
  33. failing_dev = storage_devs[-1]
  34. mnt_pt = "/mnt/raid-storage"
  35. data_file = f"{mnt_pt}/data.bin"
  36. qemu_storage_opts = []
  37. for i in range(len(storage_devs)):
  38. disk_file = os.path.join(self.builddir, "images", f"disk{i}.img")
  39. self.emulator.logfile.write(f"Creating disk image: {disk_file}\n")
  40. self.emulator.logfile.flush()
  41. subprocess.check_call(
  42. ["dd", "if=/dev/zero", f"of={disk_file}",
  43. "bs=1M", f"count={storage_size}"],
  44. stdout=self.emulator.logfile,
  45. stderr=self.emulator.logfile)
  46. opts = ["-drive", f"file={disk_file},if=virtio,format=raw"]
  47. qemu_storage_opts += opts
  48. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  49. kern = os.path.join(self.builddir, "images", "Image")
  50. self.emulator.boot(arch="aarch64",
  51. kernel=kern,
  52. kernel_cmdline=["console=ttyAMA0"],
  53. options=["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
  54. "-initrd", img] + qemu_storage_opts)
  55. self.emulator.login()
  56. # Test the program can execute.
  57. self.assertRunOk("mdadm --version")
  58. # Show the mdstat, to confirm the Kernel has support and the
  59. # configuration is empty.
  60. cat_mdstat_cmd = "cat /proc/mdstat"
  61. self.assertRunOk(cat_mdstat_cmd)
  62. # We create a raid5 array with the drives.
  63. cmd = f"mdadm --create --verbose {md_dev} --level=5 "
  64. cmd += f"--raid-devices={len(storage_devs)} "
  65. cmd += " ".join(storage_devs)
  66. self.assertRunOk(cmd)
  67. # We show again mdstat, to confirm the array creation. This is
  68. # also for debugging.
  69. self.assertRunOk(cat_mdstat_cmd)
  70. # We format the device as ext4 and mount it.
  71. self.assertRunOk(f"mkfs.ext4 {md_dev}")
  72. self.assertRunOk(f"mkdir -p {mnt_pt}")
  73. self.assertRunOk(f"mount {md_dev} {mnt_pt}")
  74. # We store some random data on this new filesystem. Note: this
  75. # file is slightly larger than a single storage drive. This
  76. # data file should span over two drives and use the raid5.
  77. file_size = storage_size + 4
  78. cmd = f"dd if=/dev/urandom of={data_file} bs=1M count={file_size}"
  79. self.assertRunOk(cmd)
  80. # We compute the hash of our data, and save it for later.
  81. hash_cmd = f"sha256sum {data_file}"
  82. out, ret = self.emulator.run(hash_cmd)
  83. self.assertEqual(ret, 0)
  84. data_sha256 = out[0]
  85. # We run few common mdadm commands.
  86. self.assertRunOk("mdadm --detail --scan")
  87. self.assertRunOk(f"mdadm --query {md_dev}")
  88. self.assertRunOk(f"mdadm --detail --test {md_dev}")
  89. self.assertRunOk(f"mdadm --action=check {md_dev}")
  90. self.assertRunOk(f"mdadm --monitor --oneshot {md_dev}")
  91. # We mark a device as "failed".
  92. self.assertRunOk(f"mdadm {md_dev} --fail {failing_dev}")
  93. # The monitoring should now report the array as degraded.
  94. monitor_cmd = f"mdadm --monitor --oneshot {md_dev}"
  95. out, ret = self.emulator.run(monitor_cmd)
  96. self.assertEqual(ret, 0)
  97. self.assertIn("DegradedArray", "\n".join(out))
  98. # We remove the failing drive from the array.
  99. self.assertRunOk(f"mdadm {md_dev} --remove {failing_dev}")
  100. # We wipe the failing drive by writing zeroes.
  101. cmd = f"dd if=/dev/zero of={failing_dev} bs=1M count={storage_size}"
  102. self.assertRunOk(cmd)
  103. # We add back this blank drive to the array.
  104. self.assertRunOk(f"mdadm {md_dev} --add {failing_dev}")
  105. # We wait few seconds to let the device rebuild.
  106. time.sleep(3)
  107. # The array should no longer be marked as degraded.
  108. out, ret = self.emulator.run(monitor_cmd)
  109. self.assertEqual(ret, 0)
  110. self.assertNotIn("DegradedArray", "\n".join(out))
  111. # With all those array manipulations, the data file should not
  112. # be corrupted. We should be able to recompute the same hash
  113. # as before.
  114. out, ret = self.emulator.run(hash_cmd)
  115. self.assertEqual(ret, 0)
  116. self.assertEqual(out[0], data_sha256)