devices_block.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # vim: fileencoding=utf-8
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2018
  6. # Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. #
  8. # This library is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU Lesser General Public
  10. # License as published by the Free Software Foundation; either
  11. # version 2.1 of the License, or (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. # Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public
  19. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  20. #
  21. import os
  22. import sys
  23. import qubes
  24. import qubes.tests
  25. import subprocess
  26. # the same class for both dom0 and VMs
  27. class TC_00_List(qubes.tests.SystemTestCase):
  28. template = None
  29. def setUp(self):
  30. super().setUp()
  31. self.img_path = '/tmp/test.img'
  32. self.mount_point = '/tmp/test-dir'
  33. if self.template is not None:
  34. self.vm = self.app.add_new_vm(
  35. "AppVM",
  36. label='red',
  37. name=self.make_vm_name("vm"))
  38. self.loop.run_until_complete(
  39. self.vm.create_on_disk())
  40. self.app.save()
  41. self.loop.run_until_complete(self.vm.start())
  42. else:
  43. self.vm = self.app.domains[0]
  44. def tearDown(self):
  45. super().tearDown()
  46. if self.template is None:
  47. if os.path.exists(self.mount_point):
  48. subprocess.call(['sudo', 'umount', self.mount_point])
  49. subprocess.call(['sudo', 'rmdir', self.mount_point])
  50. if os.path.exists('/dev/mapper/test-dm'):
  51. subprocess.call(['sudo', 'dmsetup', 'remove', 'test-dm'])
  52. if os.path.exists(self.img_path):
  53. loopdev = subprocess.check_output(['losetup', '-j',
  54. self.img_path])
  55. for dev in loopdev.decode().splitlines():
  56. subprocess.call(
  57. ['sudo', 'losetup', '-d', dev.split(':')[0]])
  58. subprocess.call(['sudo', 'rm', '-f', self.img_path])
  59. def run_script(self, script, user="user"):
  60. if self.template is None:
  61. if user == "user":
  62. subprocess.check_call(script, shell=True)
  63. elif user == "root":
  64. subprocess.check_call(['sudo', 'sh', '-c', script])
  65. else:
  66. self.loop.run_until_complete(
  67. self.vm.run_for_stdio(script, user=user))
  68. def test_000_list_loop(self):
  69. if self.template is None:
  70. self.skipTest('loop devices excluded in dom0')
  71. self.run_script(
  72. "set -e;"
  73. "truncate -s 128M {path}; "
  74. "losetup -f {path}; "
  75. "udevadm settle".format(path=self.img_path), user="root")
  76. dev_list = list(self.vm.devices['block'])
  77. found = False
  78. for dev in dev_list:
  79. if dev.description == self.img_path:
  80. self.assertTrue(dev.ident.startswith('loop'))
  81. self.assertEquals(dev.mode, 'w')
  82. self.assertEquals(dev.size, 1024 * 1024 * 128)
  83. found = True
  84. if not found:
  85. self.fail("Device {} not found in {!r}".format(
  86. self.img_path, dev_list))
  87. def test_001_list_loop_mounted(self):
  88. if self.template is None:
  89. self.skipTest('loop devices excluded in dom0')
  90. self.run_script(
  91. "set -e;"
  92. "truncate -s 128M {path}; "
  93. "mkfs.ext4 -q -F {path}; "
  94. "mkdir -p {mntdir}; "
  95. "mount {path} {mntdir} -o loop; "
  96. "udevadm settle".format(
  97. path=self.img_path,
  98. mntdir=self.mount_point),
  99. user="root")
  100. dev_list = list(self.vm.devices['block'])
  101. for dev in dev_list:
  102. if dev.description == self.img_path:
  103. self.fail(
  104. 'Device {} ({}) should not be listed because is mounted'
  105. .format(dev, self.img_path))
  106. def test_010_list_dm(self):
  107. self.run_script(
  108. "set -e;"
  109. "truncate -s 128M {path}; "
  110. "loopdev=`losetup -f`; "
  111. "losetup $loopdev {path}; "
  112. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  113. "/sys/block/$(basename $loopdev)/dev) 0\";"
  114. "udevadm settle".format(path=self.img_path), user="root")
  115. dev_list = list(self.vm.devices['block'])
  116. found = False
  117. for dev in dev_list:
  118. if dev.ident.startswith('loop'):
  119. self.assertNotEquals(dev.description, self.img_path,
  120. "Device {} ({}) should not be listed as it is used in "
  121. "device-mapper".format(dev, self.img_path)
  122. )
  123. elif dev.description == 'test-dm':
  124. self.assertEquals(dev.mode, 'w')
  125. self.assertEquals(dev.size, 1024 * 1024 * 128)
  126. found = True
  127. if not found:
  128. self.fail("Device {} not found in {!r}".format('test-dm', dev_list))
  129. def test_011_list_dm_mounted(self):
  130. self.run_script(
  131. "set -e;"
  132. "truncate -s 128M {path}; "
  133. "loopdev=`losetup -f`; "
  134. "losetup $loopdev {path}; "
  135. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  136. "/sys/block/$(basename $loopdev)/dev) 0\";"
  137. "mkfs.ext4 -q -F /dev/mapper/test-dm;"
  138. "mkdir -p {mntdir};"
  139. "mount /dev/mapper/test-dm {mntdir};"
  140. "udevadm settle".format(
  141. path=self.img_path,
  142. mntdir=self.mount_point),
  143. user="root")
  144. dev_list = list(self.vm.devices['block'])
  145. for dev in dev_list:
  146. if dev.ident.startswith('loop'):
  147. self.assertNotEquals(dev.description, self.img_path,
  148. "Device {} ({}) should not be listed as it is used in "
  149. "device-mapper".format(dev, self.img_path)
  150. )
  151. else:
  152. self.assertNotEquals(dev.description, 'test-dm',
  153. "Device {} ({}) should not be listed as it is "
  154. "mounted".format(dev, 'test-dm')
  155. )
  156. def test_012_list_dm_delayed(self):
  157. self.run_script(
  158. "set -e;"
  159. "truncate -s 128M {path}; "
  160. "loopdev=`losetup -f`; "
  161. "losetup $loopdev {path}; "
  162. "udevadm settle; "
  163. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  164. "/sys/block/$(basename $loopdev)/dev) 0\";"
  165. "udevadm settle".format(path=self.img_path), user="root")
  166. dev_list = list(self.vm.devices['block'])
  167. found = False
  168. for dev in dev_list:
  169. if dev.ident.startswith('loop'):
  170. self.assertNotEquals(dev.description, self.img_path,
  171. "Device {} ({}) should not be listed as it is used in "
  172. "device-mapper".format(dev, self.img_path)
  173. )
  174. elif dev.description == 'test-dm':
  175. self.assertEquals(dev.mode, 'w')
  176. self.assertEquals(dev.size, 1024 * 1024 * 128)
  177. found = True
  178. if not found:
  179. self.fail("Device {} not found in {!r}".format('test-dm', dev_list))
  180. def test_013_list_dm_removed(self):
  181. if self.template is None:
  182. self.skipTest('test not supported in dom0 - loop devices excluded '
  183. 'in dom0')
  184. self.run_script(
  185. "set -e;"
  186. "truncate -s 128M {path}; "
  187. "loopdev=`losetup -f`; "
  188. "losetup $loopdev {path}; "
  189. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  190. "/sys/block/$(basename $loopdev)/dev) 0\";"
  191. "udevadm settle;"
  192. "dmsetup remove test-dm;"
  193. "udevadm settle".format(path=self.img_path), user="root")
  194. dev_list = list(self.vm.devices['block'])
  195. found = False
  196. for dev in dev_list:
  197. if dev.description == self.img_path:
  198. self.assertTrue(dev.ident.startswith('loop'))
  199. self.assertEquals(dev.mode, 'w')
  200. self.assertEquals(dev.size, 1024 * 1024 * 128)
  201. found = True
  202. if not found:
  203. self.fail("Device {} not found in {!r}".format(self.img_path, dev_list))
  204. def test_020_list_loop_partition(self):
  205. if self.template is None:
  206. self.skipTest('loop devices excluded in dom0')
  207. self.run_script(
  208. "set -e;"
  209. "truncate -s 128M {path}; "
  210. "echo ,,L | sfdisk {path};"
  211. "loopdev=`losetup -f`; "
  212. "losetup -P $loopdev {path}; "
  213. "blockdev --rereadpt $loopdev; "
  214. "udevadm settle".format(path=self.img_path), user="root")
  215. dev_list = list(self.vm.devices['block'])
  216. found = False
  217. for dev in dev_list:
  218. if dev.description == self.img_path:
  219. self.assertTrue(dev.ident.startswith('loop'))
  220. self.assertEquals(dev.mode, 'w')
  221. self.assertEquals(dev.size, 1024 * 1024 * 128)
  222. self.assertIn(dev.ident + 'p1', [d.ident for d in dev_list])
  223. found = True
  224. if not found:
  225. self.fail("Device {} not found in {!r}".format(self.img_path, dev_list))
  226. def test_021_list_loop_partition_mounted(self):
  227. if self.template is None:
  228. self.skipTest('loop devices excluded in dom0')
  229. self.run_script(
  230. "set -e;"
  231. "truncate -s 128M {path}; "
  232. "echo ,,L | sfdisk {path};"
  233. "loopdev=`losetup -f`; "
  234. "losetup -P $loopdev {path}; "
  235. "blockdev --rereadpt $loopdev; "
  236. "mkfs.ext4 -q -F ${{loopdev}}p1; "
  237. "mkdir -p {mntdir}; "
  238. "mount ${{loopdev}}p1 {mntdir}; "
  239. "udevadm settle".format(
  240. path=self.img_path, mntdir=self.mount_point),
  241. user="root")
  242. dev_list = list(self.vm.devices['block'])
  243. for dev in dev_list:
  244. if dev.description == self.img_path:
  245. self.fail(
  246. 'Device {} ({}) should not be listed because its '
  247. 'partition is mounted'
  248. .format(dev, self.img_path))
  249. elif dev.ident.startswith('loop') and dev.ident.endswith('p1'):
  250. # FIXME: risky assumption that only tests create partitioned
  251. # loop devices
  252. self.fail(
  253. 'Device {} ({}) should not be listed because is mounted'
  254. .format(dev, self.img_path))
  255. def create_testcases_for_templates():
  256. return qubes.tests.create_testcases_for_templates('TC_00_List',
  257. TC_00_List,
  258. module=sys.modules[__name__])
  259. def load_tests(loader, tests, pattern):
  260. tests.addTests(loader.loadTestsFromNames(
  261. create_testcases_for_templates()))
  262. return tests
  263. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)