devices_block.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. autoescape=True)
  105. self.domains = {}
  106. class TestVM(object):
  107. def __init__(self, qdb, domain_xml=None, running=True, name='test-vm'):
  108. self.name = name
  109. self.untrusted_qdb = TestQubesDB(qdb)
  110. self.libvirt_domain = mock.Mock()
  111. self.is_running = lambda: running
  112. self.log = mock.Mock()
  113. self.app = TestApp()
  114. if domain_xml:
  115. self.libvirt_domain.configure_mock(**{
  116. 'XMLDesc.return_value': domain_xml
  117. })
  118. def __eq__(self, other):
  119. if isinstance(other, TestVM):
  120. return self.name == other.name
  121. class TC_00_Block(qubes.tests.QubesTestCase):
  122. def setUp(self):
  123. super().setUp()
  124. self.ext = qubes.ext.block.BlockDeviceExtension()
  125. def test_000_device_get(self):
  126. vm = TestVM({
  127. '/qubes-block-devices/sda': b'',
  128. '/qubes-block-devices/sda/desc': b'Test device',
  129. '/qubes-block-devices/sda/size': b'1024000',
  130. '/qubes-block-devices/sda/mode': b'w',
  131. })
  132. device_info = self.ext.device_get(vm, 'sda')
  133. self.assertIsInstance(device_info, qubes.ext.block.BlockDevice)
  134. self.assertEqual(device_info.backend_domain, vm)
  135. self.assertEqual(device_info.ident, 'sda')
  136. self.assertEqual(device_info.description, 'Test device')
  137. self.assertEqual(device_info._description, 'Test device')
  138. self.assertEqual(device_info.size, 1024000)
  139. self.assertEqual(device_info.mode, 'w')
  140. self.assertEqual(device_info.frontend_domain, None)
  141. self.assertEqual(device_info.device_node, '/dev/sda')
  142. def test_001_device_get_other_node(self):
  143. vm = TestVM({
  144. '/qubes-block-devices/mapper_dmroot': b'',
  145. '/qubes-block-devices/mapper_dmroot/desc': b'Test device',
  146. '/qubes-block-devices/mapper_dmroot/size': b'1024000',
  147. '/qubes-block-devices/mapper_dmroot/mode': b'w',
  148. })
  149. device_info = self.ext.device_get(vm, 'mapper_dmroot')
  150. self.assertIsInstance(device_info, qubes.ext.block.BlockDevice)
  151. self.assertEqual(device_info.backend_domain, vm)
  152. self.assertEqual(device_info.ident, 'mapper_dmroot')
  153. self.assertEqual(device_info.description, 'Test device')
  154. self.assertEqual(device_info._description, 'Test device')
  155. self.assertEqual(device_info.size, 1024000)
  156. self.assertEqual(device_info.mode, 'w')
  157. self.assertEqual(device_info.frontend_domain, None)
  158. self.assertEqual(device_info.device_node, '/dev/mapper/dmroot')
  159. def test_002_device_get_invalid_desc(self):
  160. vm = TestVM({
  161. '/qubes-block-devices/sda': b'',
  162. '/qubes-block-devices/sda/desc': b'Test device<>za\xc4\x87abc',
  163. '/qubes-block-devices/sda/size': b'1024000',
  164. '/qubes-block-devices/sda/mode': b'w',
  165. })
  166. device_info = self.ext.device_get(vm, 'sda')
  167. self.assertEqual(device_info.description, 'Test device__za__abc')
  168. def test_003_device_get_invalid_size(self):
  169. vm = TestVM({
  170. '/qubes-block-devices/sda': b'',
  171. '/qubes-block-devices/sda/desc': b'Test device',
  172. '/qubes-block-devices/sda/size': b'1024000abc',
  173. '/qubes-block-devices/sda/mode': b'w',
  174. })
  175. device_info = self.ext.device_get(vm, 'sda')
  176. self.assertEqual(device_info.size, 0)
  177. vm.log.warning.assert_called_once_with('Device sda has invalid size')
  178. def test_004_device_get_invalid_mode(self):
  179. vm = TestVM({
  180. '/qubes-block-devices/sda': b'',
  181. '/qubes-block-devices/sda/desc': b'Test device',
  182. '/qubes-block-devices/sda/size': b'1024000',
  183. '/qubes-block-devices/sda/mode': b'abc',
  184. })
  185. device_info = self.ext.device_get(vm, 'sda')
  186. self.assertEqual(device_info.mode, 'w')
  187. vm.log.warning.assert_called_once_with('Device sda has invalid mode')
  188. def test_005_device_get_none(self):
  189. vm = TestVM({
  190. '/qubes-block-devices/sda': b'',
  191. '/qubes-block-devices/sda/desc': b'Test device',
  192. '/qubes-block-devices/sda/size': b'1024000',
  193. '/qubes-block-devices/sda/mode': b'w',
  194. })
  195. device_info = self.ext.device_get(vm, 'sdb')
  196. self.assertIsNone(device_info)
  197. def test_010_devices_list(self):
  198. vm = TestVM({
  199. '/qubes-block-devices/sda': b'',
  200. '/qubes-block-devices/sda/desc': b'Test device',
  201. '/qubes-block-devices/sda/size': b'1024000',
  202. '/qubes-block-devices/sda/mode': b'w',
  203. '/qubes-block-devices/sdb': b'',
  204. '/qubes-block-devices/sdb/desc': b'Test device2',
  205. '/qubes-block-devices/sdb/size': b'2048000',
  206. '/qubes-block-devices/sdb/mode': b'r',
  207. })
  208. devices = sorted(list(self.ext.on_device_list_block(vm, '')))
  209. self.assertEqual(len(devices), 2)
  210. self.assertEqual(devices[0].backend_domain, vm)
  211. self.assertEqual(devices[0].ident, 'sda')
  212. self.assertEqual(devices[0].description, 'Test device')
  213. self.assertEqual(devices[0].size, 1024000)
  214. self.assertEqual(devices[0].mode, 'w')
  215. self.assertEqual(devices[1].backend_domain, vm)
  216. self.assertEqual(devices[1].ident, 'sdb')
  217. self.assertEqual(devices[1].description, 'Test device2')
  218. self.assertEqual(devices[1].size, 2048000)
  219. self.assertEqual(devices[1].mode, 'r')
  220. def test_011_devices_list_empty(self):
  221. vm = TestVM({})
  222. devices = sorted(list(self.ext.on_device_list_block(vm, '')))
  223. self.assertEqual(len(devices), 0)
  224. def test_012_devices_list_invalid_ident(self):
  225. vm = TestVM({
  226. '/qubes-block-devices/invalid ident': b'',
  227. '/qubes-block-devices/invalid+ident': b'',
  228. '/qubes-block-devices/invalid#': b'',
  229. })
  230. devices = sorted(list(self.ext.on_device_list_block(vm, '')))
  231. self.assertEqual(len(devices), 0)
  232. msg = 'test-vm vm\'s device path name contains unsafe characters. '\
  233. 'Skipping it.'
  234. self.assertEqual(vm.log.warning.mock_calls, [
  235. mock.call(msg),
  236. mock.call(msg),
  237. mock.call(msg),
  238. ])
  239. def test_020_find_unused_frontend(self):
  240. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  241. frontend = self.ext.find_unused_frontend(vm)
  242. self.assertEqual(frontend, 'xvdi')
  243. def test_022_find_unused_frontend2(self):
  244. disk = '''
  245. <disk type="block" device="disk">
  246. <driver name="phy" />
  247. <source dev="/dev/sda" />
  248. <target dev="xvdi" />
  249. <readonly />
  250. <backenddomain name="sys-usb" />
  251. </disk>
  252. '''
  253. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  254. frontend = self.ext.find_unused_frontend(vm)
  255. self.assertEqual(frontend, 'xvdj')
  256. def test_030_list_attached_empty(self):
  257. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  258. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  259. self.assertEqual(len(devices), 0)
  260. def test_031_list_attached(self):
  261. disk = '''
  262. <disk type="block" device="disk">
  263. <driver name="phy" />
  264. <source dev="/dev/sda" />
  265. <target dev="xvdi" />
  266. <readonly />
  267. <backenddomain name="sys-usb" />
  268. </disk>
  269. '''
  270. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  271. vm.app.domains['test-vm'] = vm
  272. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  273. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  274. self.assertEqual(len(devices), 1)
  275. dev = devices[0][0]
  276. options = devices[0][1]
  277. self.assertEqual(dev.backend_domain, vm.app.domains['sys-usb'])
  278. self.assertEqual(dev.ident, 'sda')
  279. self.assertEqual(options['frontend-dev'], 'xvdi')
  280. self.assertEqual(options['read-only'], 'yes')
  281. def test_032_list_attached_dom0(self):
  282. disk = '''
  283. <disk type="block" device="disk">
  284. <driver name="phy" />
  285. <source dev="/dev/sda" />
  286. <target dev="xvdi" />
  287. </disk>
  288. '''
  289. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  290. vm.app.domains['test-vm'] = vm
  291. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  292. vm.app.domains['dom0'] = TestVM({}, name='dom0')
  293. vm.app.domains[0] = vm.app.domains['dom0']
  294. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  295. self.assertEqual(len(devices), 1)
  296. dev = devices[0][0]
  297. options = devices[0][1]
  298. self.assertEqual(dev.backend_domain, vm.app.domains['dom0'])
  299. self.assertEqual(dev.ident, 'sda')
  300. self.assertEqual(options['frontend-dev'], 'xvdi')
  301. self.assertEqual(options['read-only'], 'no')
  302. def test_033_list_attached_cdrom(self):
  303. disk = '''
  304. <disk type="block" device="cdrom">
  305. <driver name="phy" />
  306. <source dev="/dev/sr0" />
  307. <target dev="xvdi" />
  308. <readonly />
  309. <backenddomain name="sys-usb" />
  310. </disk>
  311. '''
  312. vm = TestVM({}, domain_xml=domain_xml_template.format(disk))
  313. vm.app.domains['test-vm'] = vm
  314. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  315. devices = sorted(list(self.ext.on_device_list_attached(vm, '')))
  316. self.assertEqual(len(devices), 1)
  317. dev = devices[0][0]
  318. options = devices[0][1]
  319. self.assertEqual(dev.backend_domain, vm.app.domains['sys-usb'])
  320. self.assertEqual(dev.ident, 'sr0')
  321. self.assertEqual(options['frontend-dev'], 'xvdi')
  322. self.assertEqual(options['read-only'], 'yes')
  323. self.assertEqual(options['devtype'], 'cdrom')
  324. def test_040_attach(self):
  325. back_vm = TestVM(name='sys-usb', qdb={
  326. '/qubes-block-devices/sda': b'',
  327. '/qubes-block-devices/sda/desc': b'Test device',
  328. '/qubes-block-devices/sda/size': b'1024000',
  329. '/qubes-block-devices/sda/mode': b'w',
  330. })
  331. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  332. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  333. self.ext.on_device_pre_attached_block(vm, '', dev, {})
  334. device_xml = (
  335. '<disk type="block" device="disk">\n'
  336. ' <driver name="phy" />\n'
  337. ' <source dev="/dev/sda" />\n'
  338. ' <target dev="xvdi" />\n'
  339. ' <backenddomain name="sys-usb" />\n'
  340. '</disk>')
  341. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  342. def test_041_attach_frontend(self):
  343. back_vm = TestVM(name='sys-usb', qdb={
  344. '/qubes-block-devices/sda': b'',
  345. '/qubes-block-devices/sda/desc': b'Test device',
  346. '/qubes-block-devices/sda/size': b'1024000',
  347. '/qubes-block-devices/sda/mode': b'w',
  348. })
  349. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  350. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  351. self.ext.on_device_pre_attached_block(vm, '', dev,
  352. {'frontend-dev': 'xvdj'})
  353. device_xml = (
  354. '<disk type="block" device="disk">\n'
  355. ' <driver name="phy" />\n'
  356. ' <source dev="/dev/sda" />\n'
  357. ' <target dev="xvdj" />\n'
  358. ' <backenddomain name="sys-usb" />\n'
  359. '</disk>')
  360. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  361. def test_042_attach_read_only(self):
  362. back_vm = TestVM(name='sys-usb', qdb={
  363. '/qubes-block-devices/sda': b'',
  364. '/qubes-block-devices/sda/desc': b'Test device',
  365. '/qubes-block-devices/sda/size': b'1024000',
  366. '/qubes-block-devices/sda/mode': b'w',
  367. })
  368. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  369. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  370. self.ext.on_device_pre_attached_block(vm, '', dev,
  371. {'read-only': 'yes'})
  372. device_xml = (
  373. '<disk type="block" device="disk">\n'
  374. ' <driver name="phy" />\n'
  375. ' <source dev="/dev/sda" />\n'
  376. ' <target dev="xvdi" />\n'
  377. ' <readonly />\n'
  378. ' <backenddomain name="sys-usb" />\n'
  379. '</disk>')
  380. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  381. def test_043_attach_invalid_option(self):
  382. back_vm = TestVM(name='sys-usb', qdb={
  383. '/qubes-block-devices/sda': b'',
  384. '/qubes-block-devices/sda/desc': b'Test device',
  385. '/qubes-block-devices/sda/size': b'1024000',
  386. '/qubes-block-devices/sda/mode': b'w',
  387. })
  388. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  389. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  390. with self.assertRaises(qubes.exc.QubesValueError):
  391. self.ext.on_device_pre_attached_block(vm, '', dev,
  392. {'no-such-option': '123'})
  393. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  394. def test_044_attach_invalid_option2(self):
  395. back_vm = TestVM(name='sys-usb', qdb={
  396. '/qubes-block-devices/sda': b'',
  397. '/qubes-block-devices/sda/desc': b'Test device',
  398. '/qubes-block-devices/sda/size': b'1024000',
  399. '/qubes-block-devices/sda/mode': b'w',
  400. })
  401. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  402. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  403. with self.assertRaises(qubes.exc.QubesValueError):
  404. self.ext.on_device_pre_attached_block(vm, '', dev,
  405. {'read-only': 'maybe'})
  406. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  407. def test_045_attach_backend_not_running(self):
  408. back_vm = TestVM(name='sys-usb', running=False, qdb={
  409. '/qubes-block-devices/sda': b'',
  410. '/qubes-block-devices/sda/desc': b'Test device',
  411. '/qubes-block-devices/sda/size': b'1024000',
  412. '/qubes-block-devices/sda/mode': b'w',
  413. })
  414. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  415. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  416. with self.assertRaises(qubes.exc.QubesVMNotRunningError):
  417. self.ext.on_device_pre_attached_block(vm, '', dev, {})
  418. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  419. def test_046_attach_ro_dev_rw(self):
  420. back_vm = TestVM(name='sys-usb', qdb={
  421. '/qubes-block-devices/sda': b'',
  422. '/qubes-block-devices/sda/desc': b'Test device',
  423. '/qubes-block-devices/sda/size': b'1024000',
  424. '/qubes-block-devices/sda/mode': b'r',
  425. })
  426. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  427. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  428. with self.assertRaises(qubes.exc.QubesValueError):
  429. self.ext.on_device_pre_attached_block(vm, '', dev,
  430. {'read-only': 'no'})
  431. self.assertFalse(vm.libvirt_domain.attachDevice.called)
  432. def test_047_attach_read_only_auto(self):
  433. back_vm = TestVM(name='sys-usb', qdb={
  434. '/qubes-block-devices/sda': b'',
  435. '/qubes-block-devices/sda/desc': b'Test device',
  436. '/qubes-block-devices/sda/size': b'1024000',
  437. '/qubes-block-devices/sda/mode': b'r',
  438. })
  439. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  440. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  441. self.ext.on_device_pre_attached_block(vm, '', dev, {})
  442. device_xml = (
  443. '<disk type="block" device="disk">\n'
  444. ' <driver name="phy" />\n'
  445. ' <source dev="/dev/sda" />\n'
  446. ' <target dev="xvdi" />\n'
  447. ' <readonly />\n'
  448. ' <backenddomain name="sys-usb" />\n'
  449. '</disk>')
  450. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  451. def test_048_attach_cdrom_xvdi(self):
  452. back_vm = TestVM(name='sys-usb', qdb={
  453. '/qubes-block-devices/sda': b'',
  454. '/qubes-block-devices/sda/desc': b'Test device',
  455. '/qubes-block-devices/sda/size': b'1024000',
  456. '/qubes-block-devices/sda/mode': b'r',
  457. })
  458. vm = TestVM({}, domain_xml=domain_xml_template.format(modules_disk))
  459. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  460. self.ext.on_device_pre_attached_block(vm, '', dev, {'devtype': 'cdrom'})
  461. device_xml = (
  462. '<disk type="block" device="cdrom">\n'
  463. ' <driver name="phy" />\n'
  464. ' <source dev="/dev/sda" />\n'
  465. ' <target dev="xvdi" />\n'
  466. ' <readonly />\n'
  467. ' <backenddomain name="sys-usb" />\n'
  468. '</disk>')
  469. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  470. def test_048_attach_cdrom_xvdd(self):
  471. back_vm = TestVM(name='sys-usb', qdb={
  472. '/qubes-block-devices/sda': b'',
  473. '/qubes-block-devices/sda/desc': b'Test device',
  474. '/qubes-block-devices/sda/size': b'1024000',
  475. '/qubes-block-devices/sda/mode': b'r',
  476. })
  477. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  478. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  479. self.ext.on_device_pre_attached_block(vm, '', dev, {'devtype': 'cdrom'})
  480. device_xml = (
  481. '<disk type="block" device="cdrom">\n'
  482. ' <driver name="phy" />\n'
  483. ' <source dev="/dev/sda" />\n'
  484. ' <target dev="xvdd" />\n'
  485. ' <readonly />\n'
  486. ' <backenddomain name="sys-usb" />\n'
  487. '</disk>')
  488. vm.libvirt_domain.attachDevice.assert_called_once_with(device_xml)
  489. def test_050_detach(self):
  490. back_vm = TestVM(name='sys-usb', qdb={
  491. '/qubes-block-devices/sda': b'',
  492. '/qubes-block-devices/sda/desc': b'Test device',
  493. '/qubes-block-devices/sda/size': b'1024000',
  494. '/qubes-block-devices/sda/mode': b'r',
  495. })
  496. device_xml = (
  497. '<disk type="block" device="disk">\n'
  498. ' <driver name="phy" />\n'
  499. ' <source dev="/dev/sda" />\n'
  500. ' <target dev="xvdi" />\n'
  501. ' <readonly />\n'
  502. ' <backenddomain name="sys-usb" />\n'
  503. '</disk>')
  504. vm = TestVM({}, domain_xml=domain_xml_template.format(device_xml))
  505. vm.app.domains['test-vm'] = vm
  506. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  507. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  508. self.ext.on_device_pre_detached_block(vm, '', dev)
  509. vm.libvirt_domain.detachDevice.assert_called_once_with(device_xml)
  510. def test_051_detach_not_attached(self):
  511. back_vm = TestVM(name='sys-usb', qdb={
  512. '/qubes-block-devices/sda': b'',
  513. '/qubes-block-devices/sda/desc': b'Test device',
  514. '/qubes-block-devices/sda/size': b'1024000',
  515. '/qubes-block-devices/sda/mode': b'r',
  516. })
  517. device_xml = (
  518. '<disk type="block" device="disk">\n'
  519. ' <driver name="phy" />\n'
  520. ' <source dev="/dev/sda" />\n'
  521. ' <target dev="xvdi" />\n'
  522. ' <readonly />\n\n'
  523. ' <backenddomain name="sys-usb" />\n'
  524. '</disk>')
  525. vm = TestVM({}, domain_xml=domain_xml_template.format(''))
  526. vm.app.domains['test-vm'] = vm
  527. vm.app.domains['sys-usb'] = TestVM({}, name='sys-usb')
  528. dev = qubes.ext.block.BlockDevice(back_vm, 'sda')
  529. self.ext.on_device_pre_detached_block(vm, '', dev)
  530. self.assertFalse(vm.libvirt_domain.detachDevice.called)