devices_block.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. # -*- encoding: utf8 -*-
  2. #
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2017 Marek Marczykowski-Górecki
  6. # <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. from unittest import mock
  21. import jinja2
  22. import qubes.tests
  23. import qubes.ext.block
  24. modules_disk = '''
  25. <disk type='block' device='disk'>
  26. <driver name='phy'/>
  27. <source dev='/var/lib/qubes/vm-kernels/4.4.55-11/modules.img'/>
  28. <backingStore/>
  29. <target dev='xvdd' bus='xen'/>
  30. <readonly/>
  31. </disk>
  32. '''
  33. domain_xml_template = '''
  34. <domain type='xen' id='9'>
  35. <name>test-vm</name>
  36. <uuid>00000000-0000-0000-0000-0000000000ae</uuid>
  37. <memory unit='KiB'>4096000</memory>
  38. <currentMemory unit='KiB'>409600</currentMemory>
  39. <vcpu placement='static'>8</vcpu>
  40. <os>
  41. <type arch='x86_64' machine='xenpv'>linux</type>
  42. <kernel>/var/lib/qubes/vm-kernels/4.4.55-11/vmlinuz</kernel>
  43. <initrd>/var/lib/qubes/vm-kernels/4.4.55-11/initramfs</initrd>
  44. <cmdline>root=/dev/mapper/dmroot ro nomodeset console=hvc0 rd_NO_PLYMOUTH rd.plymouth.enable=0 plymouth.enable=0 dyndbg=&quot;file drivers/xen/gntdev.c +p&quot; printk=8</cmdline>
  45. </os>
  46. <clock offset='utc' adjustment='reset'>
  47. <timer name='tsc' mode='native'/>
  48. </clock>
  49. <on_poweroff>destroy</on_poweroff>
  50. <on_reboot>destroy</on_reboot>
  51. <on_crash>destroy</on_crash>
  52. <devices>
  53. <disk type='block' device='disk'>
  54. <driver name='phy'/>
  55. <source dev='/var/lib/qubes/vm-templates/fedora-25/root.img:/var/lib/qubes/vm-templates/fedora-25/root-cow.img'/>
  56. <backingStore/>
  57. <script path='block-snapshot'/>
  58. <target dev='xvda' bus='xen'/>
  59. <readonly/>
  60. </disk>
  61. <disk type='block' device='disk'>
  62. <driver name='phy'/>
  63. <source dev='/var/lib/qubes/appvms/test-vm/private.img'/>
  64. <backingStore/>
  65. <target dev='xvdb' bus='xen'/>
  66. </disk>
  67. <disk type='block' device='disk'>
  68. <driver name='phy'/>
  69. <source dev='/var/lib/qubes/appvms/test-vm/volatile.img'/>
  70. <backingStore/>
  71. <target dev='xvdc' bus='xen'/>
  72. </disk>
  73. {}
  74. <interface type='ethernet'>
  75. <mac address='00:16:3e:5e:6c:06'/>
  76. <ip address='10.137.1.8' family='ipv4'/>
  77. <script path='vif-route-qubes'/>
  78. <backenddomain name='sys-firewall'/>
  79. </interface>
  80. <console type='pty' tty='/dev/pts/0'>
  81. <source path='/dev/pts/0'/>
  82. <target type='xen' port='0'/>
  83. </console>
  84. </devices>
  85. </domain>
  86. '''
  87. class TestQubesDB(object):
  88. def __init__(self, data):
  89. self._data = data
  90. def read(self, key):
  91. return self._data.get(key, None)
  92. def list(self, prefix):
  93. return [key for key in self._data if key.startswith(prefix)]
  94. class TestApp(object):
  95. def __init__(self):
  96. #: jinja2 environment for libvirt XML templates
  97. self.env = jinja2.Environment(
  98. loader=jinja2.FileSystemLoader([
  99. 'templates',
  100. '/etc/qubes/templates',
  101. '/usr/share/qubes/templates',
  102. ]),
  103. undefined=jinja2.StrictUndefined)
  104. self.domains = {}
  105. class TestVM(object):
  106. def __init__(self, qdb, domain_xml=None, running=True, name='test-vm'):
  107. self.name = name
  108. self.untrusted_qdb = TestQubesDB(qdb)
  109. self.libvirt_domain = mock.Mock()
  110. self.is_running = lambda: running
  111. self.log = mock.Mock()
  112. self.app = TestApp()
  113. if domain_xml:
  114. self.libvirt_domain.configure_mock(**{
  115. 'XMLDesc.return_value': domain_xml
  116. })
  117. def __eq__(self, other):
  118. if isinstance(other, TestVM):
  119. return self.name == other.name
  120. class TC_00_Block(qubes.tests.QubesTestCase):
  121. def setUp(self):
  122. super().setUp()
  123. self.ext = qubes.ext.block.BlockDeviceExtension()
  124. def test_000_device_get(self):
  125. vm = TestVM({
  126. '/qubes-block-devices/sda': b'',
  127. '/qubes-block-devices/sda/desc': b'Test device',
  128. '/qubes-block-devices/sda/size': b'1024000',
  129. '/qubes-block-devices/sda/mode': b'w',
  130. })
  131. device_info = self.ext.device_get(vm, 'sda')
  132. self.assertIsInstance(device_info, qubes.ext.block.BlockDevice)
  133. self.assertEqual(device_info.backend_domain, vm)
  134. self.assertEqual(device_info.ident, 'sda')
  135. self.assertEqual(device_info.description, 'Test device')
  136. self.assertEqual(device_info._description, 'Test device')
  137. self.assertEqual(device_info.size, 1024000)
  138. self.assertEqual(device_info.mode, 'w')
  139. self.assertEqual(device_info.frontend_domain, None)
  140. self.assertEqual(device_info.device_node, '/dev/sda')
  141. def test_001_device_get_other_node(self):
  142. vm = TestVM({
  143. '/qubes-block-devices/mapper_dmroot': b'',
  144. '/qubes-block-devices/mapper_dmroot/desc': b'Test device',
  145. '/qubes-block-devices/mapper_dmroot/size': b'1024000',
  146. '/qubes-block-devices/mapper_dmroot/mode': b'w',
  147. })
  148. device_info = self.ext.device_get(vm, 'mapper_dmroot')
  149. self.assertIsInstance(device_info, qubes.ext.block.BlockDevice)
  150. self.assertEqual(device_info.backend_domain, vm)
  151. self.assertEqual(device_info.ident, 'mapper_dmroot')
  152. self.assertEqual(device_info.description, 'Test device')
  153. self.assertEqual(device_info._description, 'Test device')
  154. self.assertEqual(device_info.size, 1024000)
  155. self.assertEqual(device_info.mode, 'w')
  156. self.assertEqual(device_info.frontend_domain, None)
  157. self.assertEqual(device_info.device_node, '/dev/mapper/dmroot')
  158. def test_002_device_get_invalid_desc(self):
  159. vm = TestVM({
  160. '/qubes-block-devices/sda': b'',
  161. '/qubes-block-devices/sda/desc': b'Test device<>za\xc4\x87abc',
  162. '/qubes-block-devices/sda/size': b'1024000',
  163. '/qubes-block-devices/sda/mode': b'w',
  164. })
  165. device_info = self.ext.device_get(vm, 'sda')
  166. self.assertEqual(device_info.description, 'Test device__za__abc')
  167. def test_003_device_get_invalid_size(self):
  168. vm = TestVM({
  169. '/qubes-block-devices/sda': b'',
  170. '/qubes-block-devices/sda/desc': b'Test device',
  171. '/qubes-block-devices/sda/size': b'1024000abc',
  172. '/qubes-block-devices/sda/mode': b'w',
  173. })
  174. device_info = self.ext.device_get(vm, 'sda')
  175. self.assertEqual(device_info.size, 0)
  176. vm.log.warning.assert_called_once_with('Device sda has invalid size')
  177. def test_004_device_get_invalid_mode(self):
  178. vm = TestVM({
  179. '/qubes-block-devices/sda': b'',
  180. '/qubes-block-devices/sda/desc': b'Test device',
  181. '/qubes-block-devices/sda/size': b'1024000',
  182. '/qubes-block-devices/sda/mode': b'abc',
  183. })
  184. device_info = self.ext.device_get(vm, 'sda')
  185. self.assertEqual(device_info.mode, 'w')
  186. vm.log.warning.assert_called_once_with('Device sda has invalid mode')
  187. def test_005_device_get_none(self):
  188. vm = TestVM({
  189. '/qubes-block-devices/sda': b'',
  190. '/qubes-block-devices/sda/desc': b'Test device',
  191. '/qubes-block-devices/sda/size': b'1024000',
  192. '/qubes-block-devices/sda/mode': b'w',
  193. })
  194. device_info = self.ext.device_get(vm, 'sdb')
  195. self.assertIsNone(device_info)
  196. def test_010_devices_list(self):
  197. vm = TestVM({
  198. '/qubes-block-devices/sda': b'',
  199. '/qubes-block-devices/sda/desc': b'Test device',
  200. '/qubes-block-devices/sda/size': b'1024000',
  201. '/qubes-block-devices/sda/mode': b'w',
  202. '/qubes-block-devices/sdb': b'',
  203. '/qubes-block-devices/sdb/desc': b'Test device2',
  204. '/qubes-block-devices/sdb/size': b'2048000',
  205. '/qubes-block-devices/sdb/mode': b'r',
  206. })
  207. devices = sorted(list(self.ext.on_device_list_block(vm, '')))
  208. self.assertEqual(len(devices), 2)
  209. self.assertEqual(devices[0].backend_domain, vm)
  210. self.assertEqual(devices[0].ident, 'sda')
  211. self.assertEqual(devices[0].description, 'Test device')
  212. self.assertEqual(devices[0].size, 1024000)
  213. self.assertEqual(devices[0].mode, 'w')
  214. self.assertEqual(devices[1].backend_domain, vm)
  215. self.assertEqual(devices[1].ident, 'sdb')
  216. self.assertEqual(devices[1].description, 'Test device2')
  217. self.assertEqual(devices[1].size, 2048000)
  218. self.assertEqual(devices[1].mode, 'r')
  219. def test_011_devices_list_empty(self):
  220. vm = TestVM({})
  221. devices = sorted(list(self.ext.on_device_list_block(vm, '')))
  222. self.assertEqual(len(devices), 0)
  223. def test_012_devices_list_invalid_ident(self):
  224. vm = TestVM({
  225. '/qubes-block-devices/invalid ident': b'',
  226. '/qubes-block-devices/invalid+ident': b'',
  227. '/qubes-block-devices/invalid#': b'',
  228. })
  229. devices = sorted(list(self.ext.on_device_list_block(vm, '')))
  230. self.assertEqual(len(devices), 0)
  231. msg = 'test-vm vm\'s device path name contains unsafe characters. '\
  232. 'Skipping it.'
  233. self.assertEqual(vm.log.warning.mock_calls, [
  234. mock.call(msg),
  235. mock.call(msg),
  236. mock.call(msg),
  237. ])
  238. def test_020_find_unused_frontend(self):
  239. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  240. frontend = self.ext.find_unused_frontend(vm)
  241. self.assertEqual(frontend, 'xvdi')
  242. def test_022_find_unused_frontend2(self):
  243. disk = '''
  244. <disk type="block" device="disk">
  245. <driver name="phy" />
  246. <source dev="/dev/sda" />
  247. <target dev="xvdi" />
  248. <readonly />
  249. <backenddomain name="sys-usb" />
  250. </disk>
  251. '''
  252. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  253. frontend = self.ext.find_unused_frontend(vm)
  254. self.assertEqual(frontend, 'xvdj')
  255. def test_030_list_attached_empty(self):
  256. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  257. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  258. self.assertEqual(len(devices), 0)
  259. def test_031_list_attached(self):
  260. disk = '''
  261. <disk type="block" device="disk">
  262. <driver name="phy" />
  263. <source dev="/dev/sda" />
  264. <target dev="xvdi" />
  265. <readonly />
  266. <backenddomain name="sys-usb" />
  267. </disk>
  268. '''
  269. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  270. vm.app.domains['test-vm'] = vm
  271. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  272. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  273. self.assertEqual(len(devices), 1)
  274. dev = devices[0][0]
  275. options = devices[0][1]
  276. self.assertEqual(dev.backend_domain, vm.app.domains['sys-usb'])
  277. self.assertEqual(dev.ident, 'sda')
  278. self.assertEqual(options['frontend-dev'], 'xvdi')
  279. self.assertEqual(options['read-only'], 'yes')
  280. def test_032_list_attached_dom0(self):
  281. disk = '''
  282. <disk type="block" device="disk">
  283. <driver name="phy" />
  284. <source dev="/dev/sda" />
  285. <target dev="xvdi" />
  286. </disk>
  287. '''
  288. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  289. vm.app.domains['test-vm'] = vm
  290. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  291. vm.app.domains['dom0'] = TestVM({}, name='dom0')
  292. vm.app.domains[0] = vm.app.domains['dom0']
  293. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  294. self.assertEqual(len(devices), 1)
  295. dev = devices[0][0]
  296. options = devices[0][1]
  297. self.assertEqual(dev.backend_domain, vm.app.domains['dom0'])
  298. self.assertEqual(dev.ident, 'sda')
  299. self.assertEqual(options['frontend-dev'], 'xvdi')
  300. self.assertEqual(options['read-only'], 'no')
  301. def test_033_list_attached_cdrom(self):
  302. disk = '''
  303. <disk type="block" device="cdrom">
  304. <driver name="phy" />
  305. <source dev="/dev/sr0" />
  306. <target dev="xvdi" />
  307. <readonly />
  308. <backenddomain name="sys-usb" />
  309. </disk>
  310. '''
  311. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  312. vm.app.domains['test-vm'] = vm
  313. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  314. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  315. self.assertEqual(len(devices), 1)
  316. dev = devices[0][0]
  317. options = devices[0][1]
  318. self.assertEqual(dev.backend_domain, vm.app.domains['sys-usb'])
  319. self.assertEqual(dev.ident, 'sr0')
  320. self.assertEqual(options['frontend-dev'], 'xvdi')
  321. self.assertEqual(options['read-only'], 'yes')
  322. self.assertEqual(options['devtype'], 'cdrom')
  323. def test_040_attach(self):
  324. back_vm = TestVM(name='sys-usb', qdb={
  325. '/qubes-block-devices/sda': b'',
  326. '/qubes-block-devices/sda/desc': b'Test device',
  327. '/qubes-block-devices/sda/size': b'1024000',
  328. '/qubes-block-devices/sda/mode': b'w',
  329. })
  330. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  331. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  332. self.ext.on_device_pre_attached_block(vm, '', dev, {})
  333. device_xml = (
  334. '<disk type="block" device="disk">\n'
  335. ' <driver name="phy" />\n'
  336. ' <source dev="/dev/sda" />\n'
  337. ' <target dev="xvdi" />\n'
  338. ' <backenddomain name="sys-usb" />\n'
  339. '</disk>')
  340. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  341. def test_041_attach_frontend(self):
  342. back_vm = TestVM(name='sys-usb', qdb={
  343. '/qubes-block-devices/sda': b'',
  344. '/qubes-block-devices/sda/desc': b'Test device',
  345. '/qubes-block-devices/sda/size': b'1024000',
  346. '/qubes-block-devices/sda/mode': b'w',
  347. })
  348. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  349. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  350. self.ext.on_device_pre_attached_block(vm, '', dev,
  351. {'frontend-dev': 'xvdj'})
  352. device_xml = (
  353. '<disk type="block" device="disk">\n'
  354. ' <driver name="phy" />\n'
  355. ' <source dev="/dev/sda" />\n'
  356. ' <target dev="xvdj" />\n'
  357. ' <backenddomain name="sys-usb" />\n'
  358. '</disk>')
  359. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  360. def test_042_attach_read_only(self):
  361. back_vm = TestVM(name='sys-usb', qdb={
  362. '/qubes-block-devices/sda': b'',
  363. '/qubes-block-devices/sda/desc': b'Test device',
  364. '/qubes-block-devices/sda/size': b'1024000',
  365. '/qubes-block-devices/sda/mode': b'w',
  366. })
  367. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  368. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  369. self.ext.on_device_pre_attached_block(vm, '', dev,
  370. {'read-only': 'yes'})
  371. device_xml = (
  372. '<disk type="block" device="disk">\n'
  373. ' <driver name="phy" />\n'
  374. ' <source dev="/dev/sda" />\n'
  375. ' <target dev="xvdi" />\n'
  376. ' <readonly />\n'
  377. ' <backenddomain name="sys-usb" />\n'
  378. '</disk>')
  379. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  380. def test_043_attach_invalid_option(self):
  381. back_vm = TestVM(name='sys-usb', qdb={
  382. '/qubes-block-devices/sda': b'',
  383. '/qubes-block-devices/sda/desc': b'Test device',
  384. '/qubes-block-devices/sda/size': b'1024000',
  385. '/qubes-block-devices/sda/mode': b'w',
  386. })
  387. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  388. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  389. with self.assertRaises(qubes.exc.QubesValueError):
  390. self.ext.on_device_pre_attached_block(vm, '', dev,
  391. {'no-such-option': '123'})
  392. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  393. def test_044_attach_invalid_option2(self):
  394. back_vm = TestVM(name='sys-usb', qdb={
  395. '/qubes-block-devices/sda': b'',
  396. '/qubes-block-devices/sda/desc': b'Test device',
  397. '/qubes-block-devices/sda/size': b'1024000',
  398. '/qubes-block-devices/sda/mode': b'w',
  399. })
  400. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  401. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  402. with self.assertRaises(qubes.exc.QubesValueError):
  403. self.ext.on_device_pre_attached_block(vm, '', dev,
  404. {'read-only': 'maybe'})
  405. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  406. def test_045_attach_backend_not_running(self):
  407. back_vm = TestVM(name='sys-usb', running=False, qdb={
  408. '/qubes-block-devices/sda': b'',
  409. '/qubes-block-devices/sda/desc': b'Test device',
  410. '/qubes-block-devices/sda/size': b'1024000',
  411. '/qubes-block-devices/sda/mode': b'w',
  412. })
  413. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  414. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  415. with self.assertRaises(qubes.exc.QubesVMNotRunningError):
  416. self.ext.on_device_pre_attached_block(vm, '', dev, {})
  417. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  418. def test_046_attach_ro_dev_rw(self):
  419. back_vm = TestVM(name='sys-usb', qdb={
  420. '/qubes-block-devices/sda': b'',
  421. '/qubes-block-devices/sda/desc': b'Test device',
  422. '/qubes-block-devices/sda/size': b'1024000',
  423. '/qubes-block-devices/sda/mode': b'r',
  424. })
  425. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  426. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  427. with self.assertRaises(qubes.exc.QubesValueError):
  428. self.ext.on_device_pre_attached_block(vm, '', dev,
  429. {'read-only': 'no'})
  430. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  431. def test_047_attach_read_only_auto(self):
  432. back_vm = TestVM(name='sys-usb', qdb={
  433. '/qubes-block-devices/sda': b'',
  434. '/qubes-block-devices/sda/desc': b'Test device',
  435. '/qubes-block-devices/sda/size': b'1024000',
  436. '/qubes-block-devices/sda/mode': b'r',
  437. })
  438. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  439. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  440. self.ext.on_device_pre_attached_block(vm, '', dev, {})
  441. device_xml = (
  442. '<disk type="block" device="disk">\n'
  443. ' <driver name="phy" />\n'
  444. ' <source dev="/dev/sda" />\n'
  445. ' <target dev="xvdi" />\n'
  446. ' <readonly />\n'
  447. ' <backenddomain name="sys-usb" />\n'
  448. '</disk>')
  449. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  450. def test_048_attach_cdrom_xvdi(self):
  451. back_vm = TestVM(name='sys-usb', qdb={
  452. '/qubes-block-devices/sda': b'',
  453. '/qubes-block-devices/sda/desc': b'Test device',
  454. '/qubes-block-devices/sda/size': b'1024000',
  455. '/qubes-block-devices/sda/mode': b'r',
  456. })
  457. vm = TestVM({}, domain_xml=domain_xml_template.format(modules_disk))
  458. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  459. self.ext.on_device_pre_attached_block(vm, '', dev, {'devtype': 'cdrom'})
  460. device_xml = (
  461. '<disk type="block" device="cdrom">\n'
  462. ' <driver name="phy" />\n'
  463. ' <source dev="/dev/sda" />\n'
  464. ' <target dev="xvdi" />\n'
  465. ' <readonly />\n'
  466. ' <backenddomain name="sys-usb" />\n'
  467. '</disk>')
  468. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  469. def test_048_attach_cdrom_xvdd(self):
  470. back_vm = TestVM(name='sys-usb', qdb={
  471. '/qubes-block-devices/sda': b'',
  472. '/qubes-block-devices/sda/desc': b'Test device',
  473. '/qubes-block-devices/sda/size': b'1024000',
  474. '/qubes-block-devices/sda/mode': b'r',
  475. })
  476. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  477. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  478. self.ext.on_device_pre_attached_block(vm, '', dev, {'devtype': 'cdrom'})
  479. device_xml = (
  480. '<disk type="block" device="cdrom">\n'
  481. ' <driver name="phy" />\n'
  482. ' <source dev="/dev/sda" />\n'
  483. ' <target dev="xvdd" />\n'
  484. ' <readonly />\n'
  485. ' <backenddomain name="sys-usb" />\n'
  486. '</disk>')
  487. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  488. def test_050_detach(self):
  489. back_vm = TestVM(name='sys-usb', qdb={
  490. '/qubes-block-devices/sda': b'',
  491. '/qubes-block-devices/sda/desc': b'Test device',
  492. '/qubes-block-devices/sda/size': b'1024000',
  493. '/qubes-block-devices/sda/mode': b'r',
  494. })
  495. device_xml = (
  496. '<disk type="block" device="disk">\n'
  497. ' <driver name="phy" />\n'
  498. ' <source dev="/dev/sda" />\n'
  499. ' <target dev="xvdi" />\n'
  500. ' <readonly />\n'
  501. ' <backenddomain name="sys-usb" />\n'
  502. '</disk>')
  503. vm = TestVM({}, domain_xml=domain_xml_template.format(device_xml))
  504. vm.app.domains['test-vm'] = vm
  505. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  506. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  507. self.ext.on_device_pre_detached_block(vm, '', dev)
  508. vm.libvirt_domain.detachDevice.assert_called_once_with(device_xml)
  509. def test_051_detach_not_attached(self):
  510. back_vm = TestVM(name='sys-usb', qdb={
  511. '/qubes-block-devices/sda': b'',
  512. '/qubes-block-devices/sda/desc': b'Test device',
  513. '/qubes-block-devices/sda/size': b'1024000',
  514. '/qubes-block-devices/sda/mode': b'r',
  515. })
  516. device_xml = (
  517. '<disk type="block" device="disk">\n'
  518. ' <driver name="phy" />\n'
  519. ' <source dev="/dev/sda" />\n'
  520. ' <target dev="xvdi" />\n'
  521. ' <readonly />\n\n'
  522. ' <backenddomain name="sys-usb" />\n'
  523. '</disk>')
  524. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  525. vm.app.domains['test-vm'] = vm
  526. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  527. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  528. self.ext.on_device_pre_detached_block(vm, '', dev)
  529. self.assertFalse(vm.libvirt_domain.detachDevice.called)