test_btrfs_progs.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import os
  2. import subprocess
  3. import infra.basetest
  4. class TestBtrfsProgs(infra.basetest.BRTest):
  5. # This test needs a Kernel with btrfs support.
  6. kern_frag = \
  7. infra.filepath("tests/package/test_btrfs_progs/linux-btrfs.fragment")
  8. config = \
  9. f"""
  10. BR2_aarch64=y
  11. BR2_TOOLCHAIN_EXTERNAL=y
  12. BR2_TARGET_GENERIC_GETTY_PORT="ttyAMA0"
  13. BR2_LINUX_KERNEL=y
  14. BR2_LINUX_KERNEL_CUSTOM_VERSION=y
  15. BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="6.6.39"
  16. BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG=y
  17. BR2_LINUX_KERNEL_CUSTOM_CONFIG_FILE="board/qemu/aarch64-virt/linux.config"
  18. BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="{kern_frag}"
  19. BR2_LINUX_KERNEL_NEEDS_HOST_OPENSSL=y
  20. BR2_PACKAGE_BTRFS_PROGS=y
  21. BR2_TARGET_ROOTFS_CPIO=y
  22. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  23. # BR2_TARGET_ROOTFS_TAR is not set
  24. """
  25. def test_run(self):
  26. # Prepare the disk image.
  27. disk_file = os.path.join(self.builddir, "images", "disk.img")
  28. self.emulator.logfile.write(f"Creating disk image: {disk_file}")
  29. self.emulator.logfile.flush()
  30. subprocess.check_call(
  31. ["dd", "if=/dev/zero", f"of={disk_file}", "bs=1M", "count=256"],
  32. stdout=self.emulator.logfile,
  33. stderr=self.emulator.logfile)
  34. # Run the emulator with a blank drive.
  35. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  36. kern = os.path.join(self.builddir, "images", "Image")
  37. bootargs = ["console=ttyAMA0"]
  38. qemu_opts = ["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
  39. "-initrd", img,
  40. "-drive", f"file={disk_file},if=virtio,format=raw"]
  41. self.emulator.boot(arch="aarch64",
  42. kernel=kern,
  43. kernel_cmdline=bootargs,
  44. options=qemu_opts)
  45. self.emulator.login()
  46. # Check a program can run and show its version.
  47. self.assertRunOk("btrfs --version")
  48. # Variables for this test.
  49. dev = "/dev/vda"
  50. label = "BR_TEST"
  51. mnt_pt = "/tmp/btrfs"
  52. data_file = f"{mnt_pt}/data.bin"
  53. txt = "Hello Buildroot!"
  54. subvol = f"{mnt_pt}/my-subvolume"
  55. snapshot = f"{mnt_pt}/my-snapshot"
  56. # We create the btrfs filesystem on our device.
  57. self.assertRunOk(f"mkfs.btrfs {dev}", timeout=10)
  58. # We set a label on this filesystem.
  59. self.assertRunOk(f"btrfs filesystem label {dev} '{label}'")
  60. # We create a mount point and mount this filesystem.
  61. self.assertRunOk(f"mkdir -p {mnt_pt}")
  62. self.assertRunOk(f"mount {dev} {mnt_pt}")
  63. # We create a file with random data, to use this new
  64. # filesystem a bit.
  65. self.assertRunOk(f"dd if=/dev/urandom of={data_file} bs=1M count=10")
  66. # We compute the sha256 hash and save it for later.
  67. hash_cmd = f"sha256sum {data_file}"
  68. out, ret = self.emulator.run(hash_cmd)
  69. self.assertEqual(ret, 0)
  70. data_sha256 = out[0]
  71. # We unmount the filesystem.
  72. self.assertRunOk(f"umount {mnt_pt}")
  73. # We run a filesystem check (this tool is the equivalent of
  74. # "fsck"). Since we cleanly unmounted the filesystem,
  75. # we are not expecting any repair. This is just to test the
  76. # program works correctly.
  77. self.assertRunOk(f"btrfs check {dev}")
  78. # We query the label and check it is the one we set at the
  79. # beginning.
  80. out, ret = self.emulator.run(f"btrfs filesystem label {dev}")
  81. self.assertEqual(ret, 0)
  82. self.assertEqual(out[0], label)
  83. # We remount our filesystem.
  84. self.assertRunOk(f"mount {dev} {mnt_pt}")
  85. # We perform a scrub in foreground.
  86. self.assertRunOk(f"btrfs scrub start -B {mnt_pt}")
  87. # We show device usage and statistics.
  88. self.assertRunOk(f"btrfs device usage {mnt_pt}")
  89. self.assertRunOk(f"btrfs device stats {mnt_pt}")
  90. # We create a btrfs subvolume and create a file in it.
  91. self.assertRunOk(f"btrfs subvolume create {subvol}")
  92. self.assertRunOk(f"echo '{txt}' > {subvol}/file.txt")
  93. # We create a read-only snapshot of this subvolume and we
  94. # delete our file previously created.
  95. self.assertRunOk(f"btrfs subvolume snapshot -r {subvol} {snapshot}")
  96. self.assertRunOk(f"rm -f {subvol}/file.txt")
  97. # We should still see our file in our snapshot.
  98. out, ret = self.emulator.run(f"cat {snapshot}/file.txt")
  99. self.assertEqual(ret, 0)
  100. self.assertEqual(out[0], txt)
  101. # We should recompute the same sha256 hash as before, on the
  102. # first data file we created at the beginning.
  103. out, ret = self.emulator.run(hash_cmd)
  104. self.assertEqual(ret, 0)
  105. self.assertEqual(out[0], data_sha256)