storage_reflink.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. stat = os.stat(img_real)
  54. self.assertEqual(stat.st_blocks, 0)
  55. self.assertEqual(stat.st_size, size_initial)
  56. dev_from_real = setup_loopdev(img_real, cleanup_via=self.addCleanup)
  57. dev_from_sym = setup_loopdev(img_sym, cleanup_via=self.addCleanup)
  58. reflink._resize_file(img_real, size_resized)
  59. stat = os.stat(img_real)
  60. self.assertEqual(stat.st_blocks, 0)
  61. self.assertEqual(stat.st_size, size_resized)
  62. reflink_update_loopdev_sizes(os.path.join(self.test_dir, 'unrelated'))
  63. for dev in (dev_from_real, dev_from_sym):
  64. self.assertEqual(get_blockdev_size(dev), size_initial)
  65. reflink_update_loopdev_sizes(img_sym)
  66. for dev in (dev_from_real, dev_from_sym):
  67. self.assertEqual(get_blockdev_size(dev), size_resized)
  68. class TC_00_ReflinkOnBtrfs(ReflinkMixin, qubes.tests.QubesTestCase):
  69. def setUp(self): # pylint: disable=arguments-differ
  70. super().setUp('btrfs')
  71. self.ficlone_supported = True
  72. class TC_01_ReflinkOnExt4(ReflinkMixin, qubes.tests.QubesTestCase):
  73. def setUp(self): # pylint: disable=arguments-differ
  74. super().setUp('ext4')
  75. self.ficlone_supported = False
  76. def setup_loopdev(img, cleanup_via=None):
  77. dev = str.strip(cmd('sudo', 'losetup', '-f', '--show', img).decode())
  78. if cleanup_via is not None:
  79. cleanup_via(detach_loopdev, dev)
  80. return dev
  81. def detach_loopdev(dev):
  82. cmd('sudo', 'losetup', '-d', dev)
  83. def get_fs_type(directory):
  84. # 'stat -f -c %T' would identify ext4 as 'ext2/ext3'
  85. return cmd('df', '--output=fstype', directory).decode().splitlines()[1]
  86. def mkdir_fs(directory, fs_type,
  87. accessible=True, max_size=100*1024**3, cleanup_via=None):
  88. os.mkdir(directory)
  89. if get_fs_type(directory) != fs_type:
  90. img = os.path.join(directory, 'img')
  91. with open(img, 'xb') as img_io:
  92. img_io.truncate(max_size)
  93. cmd('mkfs.' + fs_type, img)
  94. dev = setup_loopdev(img)
  95. os.remove(img)
  96. cmd('sudo', 'mount', dev, directory)
  97. detach_loopdev(dev)
  98. if accessible:
  99. cmd('sudo', 'chmod', '777', directory)
  100. else:
  101. cmd('sudo', 'chmod', '000', directory)
  102. cmd('sudo', 'chattr', '+i', directory) # cause EPERM on write as root
  103. if cleanup_via is not None:
  104. cleanup_via(rmtree_fs, directory)
  105. def rmtree_fs(directory):
  106. cmd('sudo', 'chattr', '-i', directory)
  107. cmd('sudo', 'chmod', '777', directory)
  108. if os.path.ismount(directory):
  109. cmd('sudo', 'umount', '-l', directory)
  110. # loop device and backing file are garbage collected automatically
  111. shutil.rmtree(directory)
  112. def get_blockdev_size(dev):
  113. return int(cmd('sudo', 'blockdev', '--getsize64', dev))
  114. def reflink_update_loopdev_sizes(img):
  115. env = [k + '=' + v for k, v in os.environ.items() # 'sudo -E' alone would
  116. if k.startswith('PYTHON')] # drop some of these
  117. code = ('from qubes.storage import reflink\n'
  118. 'reflink._update_loopdev_sizes(%r)' % img)
  119. cmd('sudo', '-E', 'env', *env, sys.executable, '-c', code)
  120. def cmd(*argv):
  121. p = subprocess.run(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  122. if p.returncode != 0:
  123. raise Exception(str(p)) # this will show stdout and stderr
  124. return p.stdout