test_lvm2.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import os
  2. import subprocess
  3. import infra.basetest
  4. class TestLvm2(infra.basetest.BRTest):
  5. # The lvm2 package has _LINUX_CONFIG_FIXUPS, so we cannot use
  6. # the runtime test pre-built Kernel. We need to compile a Kernel
  7. # to make sure it will include the required configuration.
  8. config = \
  9. """
  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.1.77"
  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_NEEDS_HOST_OPENSSL=y
  19. BR2_PACKAGE_E2FSPROGS=y
  20. BR2_PACKAGE_E2FSPROGS_RESIZE2FS=y
  21. BR2_PACKAGE_LVM2=y
  22. BR2_TARGET_ROOTFS_CPIO=y
  23. BR2_TARGET_ROOTFS_CPIO_GZIP=y
  24. # BR2_TARGET_ROOTFS_TAR is not set
  25. """
  26. def get_free_disk_space(self, path):
  27. out, ret = self.emulator.run(f"df -k {path}")
  28. self.assertEqual(ret, 0)
  29. return int(out[1].split()[3])
  30. def test_run(self):
  31. # Test configuration:
  32. storage_devs = ["/dev/vda", "/dev/vdb", "/dev/vdc"]
  33. storage_size = 16 # Mega Bytes
  34. lvm_vg = "br_vg" # Volume Group name
  35. lvm_lv = "br_lv" # Logical Volume name
  36. lv_dev = f"/dev/{lvm_vg}/{lvm_lv}" # Logical Volume dev name
  37. mnt_pt = "/mnt/lvm2-storage"
  38. data_file = f"{mnt_pt}/data.bin"
  39. qemu_storage_opts = []
  40. for i in range(len(storage_devs)):
  41. disk_file = os.path.join(self.builddir, "images", f"disk{i}.img")
  42. self.emulator.logfile.write(f"Creating disk image: {disk_file}\n")
  43. self.emulator.logfile.flush()
  44. subprocess.check_call(
  45. ["dd", "if=/dev/zero", f"of={disk_file}",
  46. "bs=1M", f"count={storage_size}"],
  47. stdout=self.emulator.logfile,
  48. stderr=self.emulator.logfile)
  49. opts = ["-drive", f"file={disk_file},if=virtio,format=raw"]
  50. qemu_storage_opts += opts
  51. img = os.path.join(self.builddir, "images", "rootfs.cpio.gz")
  52. kern = os.path.join(self.builddir, "images", "Image")
  53. self.emulator.boot(arch="aarch64",
  54. kernel=kern,
  55. kernel_cmdline=["console=ttyAMA0"],
  56. options=["-M", "virt", "-cpu", "cortex-a57", "-m", "256M",
  57. "-initrd", img] + qemu_storage_opts)
  58. self.emulator.login()
  59. # Test the program can execute.
  60. self.assertRunOk("lvm version")
  61. # We did not created any Physical Volume yet. We should NOT
  62. # see any of our storage devices in a pvscan.
  63. out, ret = self.emulator.run("pvscan")
  64. self.assertEqual(ret, 0)
  65. for dev in storage_devs:
  66. self.assertNotIn(dev, "\n".join(out))
  67. # We initialize our Physical Volumes (PVs).
  68. pv_devs = " ".join(storage_devs)
  69. self.assertRunOk(f"pvcreate {pv_devs}")
  70. # We run few diagnostic commands related to PVs.
  71. self.assertRunOk(f"pvck {pv_devs}")
  72. self.assertRunOk(f"pvdisplay {pv_devs}")
  73. self.assertRunOk("pvs")
  74. # Now we initialized the PVs, we should see them in a pvscan.
  75. out, ret = self.emulator.run("pvscan")
  76. self.assertEqual(ret, 0)
  77. for dev in storage_devs:
  78. self.assertIn(dev, "\n".join(out))
  79. # We create a Volume Group (VG) including two of our three
  80. # PVs.
  81. cmd = f"vgcreate {lvm_vg} {storage_devs[0]} {storage_devs[1]}"
  82. self.assertRunOk(cmd)
  83. # We run few diagnostic commands related to VGs.
  84. self.assertRunOk(f"vgck {lvm_vg}")
  85. self.assertRunOk(f"vgdisplay {lvm_vg}")
  86. self.assertRunOk("vgscan")
  87. self.assertRunOk("vgs")
  88. # We create a Logical Volume (LV) in our VG.
  89. self.assertRunOk(f"lvcreate -l 100%FREE -n {lvm_lv} {lvm_vg}")
  90. # We check LVM created the LV device.
  91. self.assertRunOk(f"ls -al {lv_dev}")
  92. # We run few diagnostic commands related to LVs.
  93. self.assertRunOk("lvscan")
  94. self.assertRunOk("lvs")
  95. # We create a ext4 filesystem on our LV.
  96. self.assertRunOk(f"mkfs.ext4 {lv_dev}")
  97. # We create a mount point directory and mount the device.
  98. self.assertRunOk(f"mkdir -p {mnt_pt}")
  99. self.assertRunOk(f"mount {lv_dev} {mnt_pt}")
  100. # We create a data file in our new filesystem. Note: this file
  101. # is slightly larger than a single PV. This data file should
  102. # span over the two PVs in the VG.
  103. data_size = storage_size + 4
  104. cmd = f"dd if=/dev/urandom of={data_file} bs=1M count={data_size}"
  105. self.assertRunOk(cmd)
  106. # We compute the hash of our data, and save it for later.
  107. hash_cmd = f"sha256sum {data_file}"
  108. out, ret = self.emulator.run(hash_cmd)
  109. self.assertEqual(ret, 0)
  110. data_sha256 = out[0]
  111. # We compute the free space of the mount point.
  112. fs_free_space = self.get_free_disk_space(mnt_pt)
  113. # We extend of VG with our third PV.
  114. self.assertRunOk(f"vgextend {lvm_vg} {storage_devs[2]}")
  115. # We grow the LV to use all the space of the VG.
  116. self.assertRunOk(f"lvresize -l +100%FREE {lvm_vg}/{lvm_lv}")
  117. # We resize the filesystem to use all the LV space.
  118. self.assertRunOk(f"resize2fs {lv_dev}")
  119. # Now we grew the LV and resized the filesystem, we recompute
  120. # the free space and check we have more.
  121. fs2_free_space = self.get_free_disk_space(mnt_pt)
  122. self.assertGreater(fs2_free_space, fs_free_space)
  123. # With all those on-the-fly operations on the mounted
  124. # filesystem, the data file should be intact. We should
  125. # recompute the same data checksum as before.
  126. out, ret = self.emulator.run(hash_cmd)
  127. self.assertEqual(ret, 0)
  128. self.assertEqual(out[0], data_sha256)
  129. # Finally, we unmount the filesystem. It should not contain
  130. # any error.
  131. self.assertRunOk(f"umount {mnt_pt}")
  132. self.assertRunOk(f"e2fsck -f -n {lv_dev}")