block.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # vim: fileencoding=utf-8
  2. #
  3. # The Qubes OS Project, https://www.qubes-os.org/
  4. #
  5. # Copyright (C) 2016
  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 qubes.tests
  23. import qubes.qubesutils
  24. import subprocess
  25. # the same class for both dom0 and VMs
  26. class TC_00_List(qubes.tests.SystemTestsMixin, qubes.tests.QubesTestCase):
  27. template = None
  28. def setUp(self):
  29. super(TC_00_List, self).setUp()
  30. self.img_path = '/tmp/test.img'
  31. self.mount_point = '/tmp/test-dir'
  32. if self.template is not None:
  33. self.vm = self.qc.add_new_vm(
  34. "QubesAppVm",
  35. name=self.make_vm_name("vm"),
  36. template=self.qc.get_vm_by_name(self.template))
  37. self.vm.create_on_disk(verbose=False)
  38. self.app.save()
  39. self.qc.unlock_db()
  40. self.vm.start()
  41. else:
  42. self.qc.unlock_db()
  43. self.vm = self.qc[0]
  44. def tearDown(self):
  45. super(TC_00_List, self).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. subprocess.call(['sudo', 'dmsetup', 'remove', 'test-dm'])
  51. if os.path.exists(self.img_path):
  52. loopdev = subprocess.check_output(['losetup', '-j',
  53. self.img_path])
  54. for dev in loopdev.splitlines():
  55. subprocess.call(
  56. ['sudo', 'losetup', '-d', dev.split(':')[0]])
  57. subprocess.call(['sudo', 'rm', '-f', self.img_path])
  58. def run_script(self, script, user="user"):
  59. if self.template is None:
  60. if user == "user":
  61. subprocess.check_call(script, shell=True)
  62. elif user == "root":
  63. subprocess.check_call(['sudo', 'sh', '-c', script])
  64. else:
  65. retcode = self.vm.run(script, user=user, wait=True)
  66. if retcode != 0:
  67. raise subprocess.CalledProcessError
  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 = qubes.qubesutils.block_list_vm(self.vm)
  77. found = False
  78. for dev in dev_list.keys():
  79. if dev_list[dev]['desc'] == self.img_path:
  80. self.assertTrue(dev.startswith(self.vm.name + ':loop'))
  81. self.assertEquals(dev_list[dev]['mode'], 'w')
  82. self.assertEquals(dev_list[dev]['size'], 1024 * 1024 * 128)
  83. self.assertEquals(
  84. dev_list[dev]['device'], '/dev/' + dev.split(':')[1])
  85. found = True
  86. if not found:
  87. self.fail("Device {} not found in {!r}".format(self.img_path, dev_list))
  88. def test_001_list_loop_mounted(self):
  89. if self.template is None:
  90. self.skipTest('loop devices excluded in dom0')
  91. self.run_script(
  92. "set -e;"
  93. "truncate -s 128M {path}; "
  94. "mkfs.ext4 -q -F {path}; "
  95. "mkdir -p {mntdir}; "
  96. "mount {path} {mntdir} -o loop; "
  97. "udevadm settle".format(
  98. path=self.img_path,
  99. mntdir=self.mount_point),
  100. user="root")
  101. dev_list = qubes.qubesutils.block_list_vm(self.vm)
  102. for dev in dev_list.keys():
  103. if dev_list[dev]['desc'] == self.img_path:
  104. self.fail(
  105. 'Device {} ({}) should not be listed because is mounted'
  106. .format(dev, self.img_path))
  107. def test_010_list_dm(self):
  108. self.run_script(
  109. "set -e;"
  110. "truncate -s 128M {path}; "
  111. "loopdev=`losetup -f`; "
  112. "losetup $loopdev {path}; "
  113. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  114. "/sys/block/$(basename $loopdev)/dev) 0\";"
  115. "udevadm settle".format(path=self.img_path), user="root")
  116. dev_list = qubes.qubesutils.block_list_vm(self.vm)
  117. found = False
  118. for dev in dev_list.keys():
  119. if dev.startswith(self.vm.name + ':loop'):
  120. self.assertNotEquals(dev_list[dev]['desc'], self.img_path,
  121. "Device {} ({}) should not be listed as it is used in "
  122. "device-mapper".format(dev, self.img_path)
  123. )
  124. elif dev_list[dev]['desc'] == 'test-dm':
  125. self.assertEquals(dev_list[dev]['mode'], 'w')
  126. self.assertEquals(dev_list[dev]['size'], 1024 * 1024 * 128)
  127. self.assertEquals(
  128. dev_list[dev]['device'], '/dev/' + dev.split(':')[1])
  129. found = True
  130. if not found:
  131. self.fail("Device {} not found in {!r}".format('test-dm', dev_list))
  132. def test_011_list_dm_mounted(self):
  133. self.run_script(
  134. "set -e;"
  135. "truncate -s 128M {path}; "
  136. "loopdev=`losetup -f`; "
  137. "losetup $loopdev {path}; "
  138. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  139. "/sys/block/$(basename $loopdev)/dev) 0\";"
  140. "mkfs.ext4 -q -F /dev/mapper/test-dm;"
  141. "mkdir -p {mntdir};"
  142. "mount /dev/mapper/test-dm {mntdir};"
  143. "udevadm settle".format(
  144. path=self.img_path,
  145. mntdir=self.mount_point),
  146. user="root")
  147. dev_list = qubes.qubesutils.block_list_vm(self.vm)
  148. for dev in dev_list.keys():
  149. if dev.startswith(self.vm.name + ':loop'):
  150. self.assertNotEquals(dev_list[dev]['desc'], self.img_path,
  151. "Device {} ({}) should not be listed as it is used in "
  152. "device-mapper".format(dev, self.img_path)
  153. )
  154. else:
  155. self.assertNotEquals(dev_list[dev]['desc'], 'test-dm',
  156. "Device {} ({}) should not be listed as it is "
  157. "mounted".format(dev, 'test-dm')
  158. )
  159. def test_012_list_dm_delayed(self):
  160. self.run_script(
  161. "set -e;"
  162. "truncate -s 128M {path}; "
  163. "loopdev=`losetup -f`; "
  164. "losetup $loopdev {path}; "
  165. "udevadm settle; "
  166. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  167. "/sys/block/$(basename $loopdev)/dev) 0\";"
  168. "udevadm settle".format(path=self.img_path), user="root")
  169. dev_list = qubes.qubesutils.block_list_vm(self.vm)
  170. found = False
  171. for dev in dev_list.keys():
  172. if dev.startswith(self.vm.name + ':loop'):
  173. self.assertNotEquals(dev_list[dev]['desc'], self.img_path,
  174. "Device {} ({}) should not be listed as it is used in "
  175. "device-mapper".format(dev, self.img_path)
  176. )
  177. elif dev_list[dev]['desc'] == 'test-dm':
  178. self.assertEquals(dev_list[dev]['mode'], 'w')
  179. self.assertEquals(dev_list[dev]['size'], 1024 * 1024 * 128)
  180. self.assertEquals(
  181. dev_list[dev]['device'], '/dev/' + dev.split(':')[1])
  182. found = True
  183. if not found:
  184. self.fail("Device {} not found in {!r}".format('test-dm', dev_list))
  185. def test_013_list_dm_removed(self):
  186. if self.template is None:
  187. self.skipTest('test not supported in dom0 - loop devices excluded '
  188. 'in dom0')
  189. self.run_script(
  190. "set -e;"
  191. "truncate -s 128M {path}; "
  192. "loopdev=`losetup -f`; "
  193. "losetup $loopdev {path}; "
  194. "dmsetup create test-dm --table \"0 262144 linear $(cat "
  195. "/sys/block/$(basename $loopdev)/dev) 0\";"
  196. "udevadm settle;"
  197. "dmsetup remove test-dm;"
  198. "udevadm settle".format(path=self.img_path), user="root")
  199. dev_list = qubes.qubesutils.block_list_vm(self.vm)
  200. found = False
  201. for dev in dev_list.keys():
  202. if dev_list[dev]['desc'] == self.img_path:
  203. self.assertTrue(dev.startswith(self.vm.name + ':loop'))
  204. self.assertEquals(dev_list[dev]['mode'], 'w')
  205. self.assertEquals(dev_list[dev]['size'], 1024 * 1024 * 128)
  206. self.assertEquals(
  207. dev_list[dev]['device'], '/dev/' + dev.split(':')[1])
  208. found = True
  209. if not found:
  210. self.fail("Device {} not found in {!r}".format(self.img_path, dev_list))
  211. def test_020_list_loop_partition(self):
  212. if self.template is None:
  213. self.skipTest('loop devices excluded in dom0')
  214. self.run_script(
  215. "set -e;"
  216. "truncate -s 128M {path}; "
  217. "echo ,,L | sfdisk {path};"
  218. "loopdev=`losetup -f`; "
  219. "losetup -P $loopdev {path}; "
  220. "blockdev --rereadpt $loopdev; "
  221. "udevadm settle".format(path=self.img_path), user="root")
  222. dev_list = qubes.qubesutils.block_list_vm(self.vm)
  223. found = False
  224. for dev in dev_list.keys():
  225. if dev_list[dev]['desc'] == self.img_path:
  226. self.assertTrue(dev.startswith(self.vm.name + ':loop'))
  227. self.assertEquals(dev_list[dev]['mode'], 'w')
  228. self.assertEquals(dev_list[dev]['size'], 1024 * 1024 * 128)
  229. self.assertEquals(
  230. dev_list[dev]['device'], '/dev/' + dev.split(':')[1])
  231. self.assertIn(dev + 'p1', dev_list)
  232. found = True
  233. if not found:
  234. self.fail("Device {} not found in {!r}".format(self.img_path, dev_list))
  235. def test_021_list_loop_partition_mounted(self):
  236. if self.template is None:
  237. self.skipTest('loop devices excluded in dom0')
  238. self.run_script(
  239. "set -e;"
  240. "truncate -s 128M {path}; "
  241. "echo ,,L | sfdisk {path};"
  242. "loopdev=`losetup -f`; "
  243. "losetup -P $loopdev {path}; "
  244. "blockdev --rereadpt $loopdev; "
  245. "mkfs.ext4 -q -F ${{loopdev}}p1; "
  246. "mkdir -p {mntdir}; "
  247. "mount ${{loopdev}}p1 {mntdir}; "
  248. "udevadm settle".format(
  249. path=self.img_path, mntdir=self.mount_point),
  250. user="root")
  251. dev_list = qubes.qubesutils.block_list_vm(self.vm)
  252. for dev in dev_list.keys():
  253. if dev_list[dev]['desc'] == self.img_path:
  254. self.fail(
  255. 'Device {} ({}) should not be listed because its '
  256. 'partition is mounted'
  257. .format(dev, self.img_path))
  258. elif dev.startswith(self.vm.name + ':loop') and dev.endswith('p1'):
  259. # FIXME: risky assumption that only tests create partitioned
  260. # loop devices
  261. self.fail(
  262. 'Device {} ({}) should not be listed because is mounted'
  263. .format(dev, self.img_path))
  264. def load_tests(loader, tests, pattern):
  265. try:
  266. qc = qubes.qubes.QubesVmCollection()
  267. qc.lock_db_for_reading()
  268. qc.load()
  269. qc.unlock_db()
  270. templates = [vm.name for vm in qc.values() if
  271. isinstance(vm, qubes.qubes.QubesTemplateVm)]
  272. except OSError:
  273. templates = []
  274. for template in templates:
  275. tests.addTests(loader.loadTestsFromTestCase(
  276. type(
  277. 'TC_00_List_' + template,
  278. (TC_00_List, qubes.tests.QubesTestCase),
  279. {'template': template})))
  280. return tests