grub.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2016 Marek Marczykowski-Górecki
  7. # <marmarek@invisiblethingslab.com>
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  21. #
  22. #
  23. import os
  24. import subprocess
  25. import sys
  26. import unittest
  27. import qubes.tests
  28. class GrubBase(object):
  29. virt_mode = None
  30. kernel = None
  31. def setUp(self):
  32. super(GrubBase, self).setUp()
  33. supported = False
  34. if self.template.startswith('fedora-'):
  35. supported = True
  36. elif self.template.startswith('debian-'):
  37. supported = True
  38. if not supported:
  39. self.skipTest("Template {} not supported by this test".format(
  40. self.template))
  41. def install_packages(self, vm):
  42. if self.template.startswith('fedora-'):
  43. cmd_install1 = 'dnf clean expire-cache && ' \
  44. 'dnf install -y qubes-kernel-vm-support grub2-tools'
  45. cmd_install2 = 'dnf install -y kernel-core'
  46. cmd_update_grub = 'grub2-mkconfig -o /boot/grub2/grub.cfg'
  47. elif self.template.startswith('debian-'):
  48. cmd_install1 = 'apt-get update && apt-get install -y ' \
  49. 'qubes-kernel-vm-support grub2-common'
  50. cmd_install2 = 'apt-get install -y linux-image-amd64'
  51. cmd_update_grub = 'mkdir -p /boot/grub && update-grub2'
  52. else:
  53. assert False, "Unsupported template?!"
  54. # wait for full VM startup first, to have functional network
  55. self.loop.run_until_complete(self.wait_for_session(vm))
  56. for cmd in [cmd_install1, cmd_install2, cmd_update_grub]:
  57. try:
  58. self.loop.run_until_complete(vm.run_for_stdio(
  59. cmd, user="root"))
  60. except subprocess.CalledProcessError as err:
  61. self.fail("Failed command: {}\nSTDOUT: {}\nSTDERR: {}"
  62. .format(cmd, err.stdout, err.stderr))
  63. def get_kernel_version(self, vm):
  64. if self.template.startswith('fedora-'):
  65. cmd_get_kernel_version = 'rpm -q kernel-core|sort -V|tail -1|' \
  66. 'cut -d - -f 3-'
  67. elif self.template.startswith('debian-'):
  68. cmd_get_kernel_version = \
  69. 'dpkg-query --showformat=\'${Package}\\n\' --show ' \
  70. '\'linux-image-*-amd64\'|sort -V|tail -1|cut -d - -f 3-'
  71. else:
  72. raise RuntimeError("Unsupported template?!")
  73. kver, _ = self.loop.run_until_complete(vm.run_for_stdio(
  74. cmd_get_kernel_version, user="root"))
  75. return kver.strip()
  76. def assertXenScrubPagesEnabled(self, vm):
  77. enabled, _ = self.loop.run_until_complete(vm.run_for_stdio(
  78. 'cat /sys/devices/system/xen_memory/xen_memory0/scrub_pages || '
  79. 'echo 1'))
  80. enabled = enabled.decode().strip()
  81. self.assertEqual(enabled, '1',
  82. 'Xen scrub pages not enabled in {}'.format(vm.name))
  83. def test_000_standalone_vm(self):
  84. self.testvm1 = self.app.add_new_vm('StandaloneVM',
  85. name=self.make_vm_name('vm1'),
  86. label='red')
  87. self.testvm1.virt_mode = self.virt_mode
  88. self.testvm1.features.update(self.app.domains[self.template].features)
  89. self.loop.run_until_complete(
  90. self.testvm1.clone_disk_files(self.app.domains[self.template]))
  91. self.loop.run_until_complete(self.testvm1.start())
  92. self.install_packages(self.testvm1)
  93. kver = self.get_kernel_version(self.testvm1)
  94. self.loop.run_until_complete(self.testvm1.shutdown(wait=True))
  95. self.testvm1.kernel = self.kernel
  96. self.loop.run_until_complete(self.testvm1.start())
  97. (actual_kver, _) = self.loop.run_until_complete(
  98. self.testvm1.run_for_stdio('uname -r'))
  99. self.assertEquals(actual_kver.strip(), kver)
  100. self.assertXenScrubPagesEnabled(self.testvm1)
  101. def test_010_template_based_vm(self):
  102. self.test_template = self.app.add_new_vm('TemplateVM',
  103. name=self.make_vm_name('template'), label='red')
  104. self.test_template.virt_mode = self.virt_mode
  105. self.test_template.features.update(self.app.domains[self.template].features)
  106. self.loop.run_until_complete(
  107. self.test_template.clone_disk_files(self.app.domains[self.template]))
  108. self.testvm1 = self.app.add_new_vm("AppVM",
  109. template=self.test_template,
  110. name=self.make_vm_name('vm1'),
  111. label='red')
  112. self.testvm1.virt_mode = self.virt_mode
  113. self.loop.run_until_complete(self.testvm1.create_on_disk())
  114. self.loop.run_until_complete(self.test_template.start())
  115. self.install_packages(self.test_template)
  116. kver = self.get_kernel_version(self.test_template)
  117. self.loop.run_until_complete(self.test_template.shutdown(wait=True))
  118. self.test_template.kernel = self.kernel
  119. self.testvm1.kernel = self.kernel
  120. # Check if TemplateBasedVM boots and has the right kernel
  121. self.loop.run_until_complete(
  122. self.testvm1.start())
  123. (actual_kver, _) = self.loop.run_until_complete(
  124. self.testvm1.run_for_stdio('uname -r'))
  125. self.assertEquals(actual_kver.strip(), kver)
  126. self.assertXenScrubPagesEnabled(self.testvm1)
  127. # And the same for the TemplateVM itself
  128. self.loop.run_until_complete(self.test_template.start())
  129. (actual_kver, _) = self.loop.run_until_complete(
  130. self.test_template.run_for_stdio('uname -r'))
  131. self.assertEquals(actual_kver.strip(), kver)
  132. self.assertXenScrubPagesEnabled(self.test_template)
  133. @unittest.skipUnless(os.path.exists('/var/lib/qubes/vm-kernels/pvgrub2'),
  134. 'grub-xen package not installed')
  135. class TC_40_PVGrub(GrubBase):
  136. virt_mode = 'pv'
  137. kernel = 'pvgrub2'
  138. def setUp(self):
  139. if 'fedora' in self.template:
  140. # requires a zstd decompression filter in grub
  141. # (see grub_file_filter_id enum in grub sources)
  142. self.skipTest('Fedora kernel is compressed with zstd '
  143. 'which is not supported by pvgrub2')
  144. super().setUp()
  145. class TC_41_HVMGrub(GrubBase):
  146. virt_mode = 'hvm'
  147. kernel = None
  148. @unittest.skipUnless(os.path.exists('/var/lib/qubes/vm-kernels/pvgrub2-pvh'),
  149. 'grub2-xen-pvh package not installed')
  150. class TC_42_PVHGrub(GrubBase):
  151. virt_mode = 'pvh'
  152. kernel = 'pvgrub2-pvh'
  153. def create_testcases_for_templates():
  154. yield from qubes.tests.create_testcases_for_templates('TC_40_PVGrub',
  155. TC_40_PVGrub, qubes.tests.SystemTestCase,
  156. module=sys.modules[__name__])
  157. yield from qubes.tests.create_testcases_for_templates('TC_41_HVMGrub',
  158. TC_41_HVMGrub, qubes.tests.SystemTestCase,
  159. module=sys.modules[__name__])
  160. yield from qubes.tests.create_testcases_for_templates('TC_42_PVHGrub',
  161. TC_42_PVHGrub, qubes.tests.SystemTestCase,
  162. module=sys.modules[__name__])
  163. def load_tests(loader, tests, pattern):
  164. tests.addTests(loader.loadTestsFromNames(
  165. create_testcases_for_templates()))
  166. return tests
  167. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)