storage_reflink.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org
  3. #
  4. # Copyright (C) 2018 Rusty Bird <rustybird@net-c.com>
  5. #
  6. # This library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. #
  19. ''' Tests for the file-reflink storage driver '''
  20. # pylint: disable=protected-access
  21. # pylint: disable=invalid-name
  22. import os
  23. import shutil
  24. import subprocess
  25. import sys
  26. import qubes.tests
  27. from qubes.storage import reflink
  28. class ReflinkMixin:
  29. def setUp(self, fs_type='btrfs'): # pylint: disable=arguments-differ
  30. super().setUp()
  31. self.test_dir = '/var/tmp/test-reflink-units-on-' + fs_type
  32. mkdir_fs(self.test_dir, fs_type, cleanup_via=self.addCleanup)
  33. def test_000_copy_file(self):
  34. source = os.path.join(self.test_dir, 'source-file')
  35. dest = os.path.join(self.test_dir, 'new-directory', 'dest-file')
  36. content = os.urandom(1024**2)
  37. with open(source, 'wb') as source_io:
  38. source_io.write(content)
  39. ficlone_succeeded = reflink._copy_file(source, dest)
  40. self.assertEqual(ficlone_succeeded, self.ficlone_supported)
  41. self.assertNotEqual(os.stat(source).st_ino, os.stat(dest).st_ino)
  42. with open(source, 'rb') as source_io:
  43. self.assertEqual(source_io.read(), content)
  44. with open(dest, 'rb') as dest_io:
  45. self.assertEqual(dest_io.read(), content)
  46. def test_001_create_and_resize_files_and_update_loopdevs(self):
  47. img_real = os.path.join(self.test_dir, 'img-real')
  48. img_sym = os.path.join(self.test_dir, 'img-sym')
  49. size_initial = 111 * 1024**2
  50. size_resized = 222 * 1024**2
  51. os.symlink(img_real, img_sym)
  52. reflink._create_sparse_file(img_real, size_initial)
  53. self.assertEqual(reflink._get_file_disk_usage(img_real), 0)
  54. self.assertEqual(os.stat(img_real).st_size, size_initial)
  55. dev_from_real = setup_loopdev(img_real, cleanup_via=self.addCleanup)
  56. dev_from_sym = setup_loopdev(img_sym, cleanup_via=self.addCleanup)
  57. reflink._resize_file(img_real, size_resized)
  58. self.assertEqual(reflink._get_file_disk_usage(img_real), 0)
  59. self.assertEqual(os.stat(img_real).st_size, size_resized)
  60. reflink_update_loopdev_sizes(os.path.join(self.test_dir, 'unrelated'))
  61. for dev in (dev_from_real, dev_from_sym):
  62. self.assertEqual(get_blockdev_size(dev), size_initial)
  63. reflink_update_loopdev_sizes(img_sym)
  64. for dev in (dev_from_real, dev_from_sym):
  65. self.assertEqual(get_blockdev_size(dev), size_resized)
  66. class TC_00_ReflinkOnBtrfs(ReflinkMixin, qubes.tests.QubesTestCase):
  67. def setUp(self): # pylint: disable=arguments-differ
  68. super().setUp('btrfs')
  69. self.ficlone_supported = True
  70. class TC_01_ReflinkOnExt4(ReflinkMixin, qubes.tests.QubesTestCase):
  71. def setUp(self): # pylint: disable=arguments-differ
  72. super().setUp('ext4')
  73. self.ficlone_supported = False
  74. def setup_loopdev(img, cleanup_via=None):
  75. dev = str.strip(cmd('sudo', 'losetup', '-f', '--show', img).decode())
  76. if cleanup_via is not None:
  77. cleanup_via(detach_loopdev, dev)
  78. return dev
  79. def detach_loopdev(dev):
  80. cmd('sudo', 'losetup', '-d', dev)
  81. def get_fs_type(directory):
  82. # 'stat -f -c %T' would identify ext4 as 'ext2/ext3'
  83. return cmd('df', '--output=fstype', directory).decode().splitlines()[1]
  84. def mkdir_fs(directory, fs_type,
  85. accessible=True, max_size=100*1024**3, cleanup_via=None):
  86. os.mkdir(directory)
  87. if get_fs_type(directory) != fs_type:
  88. img = os.path.join(directory, 'img')
  89. with open(img, 'xb') as img_io:
  90. img_io.truncate(max_size)
  91. cmd('mkfs.' + fs_type, img)
  92. dev = setup_loopdev(img)
  93. os.remove(img)
  94. cmd('sudo', 'mount', dev, directory)
  95. detach_loopdev(dev)
  96. if accessible:
  97. cmd('sudo', 'chmod', '777', directory)
  98. else:
  99. cmd('sudo', 'chmod', '000', directory)
  100. cmd('sudo', 'chattr', '+i', directory) # cause EPERM on write as root
  101. if cleanup_via is not None:
  102. cleanup_via(rmtree_fs, directory)
  103. def rmtree_fs(directory):
  104. cmd('sudo', 'chattr', '-i', directory)
  105. cmd('sudo', 'chmod', '777', directory)
  106. if os.path.ismount(directory):
  107. try:
  108. cmd('sudo', 'umount', directory)
  109. except:
  110. cmd('sudo', 'fuser', '-vm', directory)
  111. raise
  112. # loop device and backing file are garbage collected automatically
  113. shutil.rmtree(directory)
  114. def get_blockdev_size(dev):
  115. return int(cmd('sudo', 'blockdev', '--getsize64', dev))
  116. def reflink_update_loopdev_sizes(img):
  117. env = [k + '=' + v for k, v in os.environ.items() # 'sudo -E' alone would
  118. if k.startswith('PYTHON')] # drop some of these
  119. code = ('from qubes.storage import reflink\n'
  120. 'reflink._update_loopdev_sizes(%r)' % img)
  121. cmd('sudo', '-E', 'env', *env, sys.executable, '-c', code)
  122. def cmd(*argv):
  123. p = subprocess.run(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  124. if p.returncode != 0:
  125. raise Exception(str(p)) # this will show stdout and stderr
  126. return p.stdout