grub.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. for cmd in [cmd_install1, cmd_install2, cmd_update_grub]:
  55. try:
  56. self.loop.run_until_complete(vm.run_for_stdio(
  57. cmd, user="root"))
  58. except subprocess.CalledProcessError as err:
  59. self.fail("Failed command: {}\nSTDOUT: {}\nSTDERR: {}"
  60. .format(cmd, err.stdout, err.stderr))
  61. def get_kernel_version(self, vm):
  62. if self.template.startswith('fedora-'):
  63. cmd_get_kernel_version = 'rpm -q kernel-core|sort -V|tail -1|' \
  64. 'cut -d - -f 3-'
  65. elif self.template.startswith('debian-'):
  66. cmd_get_kernel_version = \
  67. 'dpkg-query --showformat=\'${Package}\\n\' --show ' \
  68. '\'linux-image-*-amd64\'|sort -V|tail -1|cut -d - -f 3-'
  69. else:
  70. raise RuntimeError("Unsupported template?!")
  71. kver, _ = self.loop.run_until_complete(vm.run_for_stdio(
  72. cmd_get_kernel_version, user="root"))
  73. return kver.strip()
  74. def assertXenScrubPagesEnabled(self, vm):
  75. enabled, _ = self.loop.run_until_complete(vm.run_for_stdio(
  76. 'cat /sys/devices/system/xen_memory/xen_memory0/scrub_pages || '
  77. 'echo 1'))
  78. enabled = enabled.decode().strip()
  79. self.assertEqual(enabled, '1',
  80. 'Xen scrub pages not enabled in {}'.format(vm.name))
  81. def test_000_standalone_vm(self):
  82. self.testvm1 = self.app.add_new_vm('StandaloneVM',
  83. name=self.make_vm_name('vm1'),
  84. label='red')
  85. self.testvm1.virt_mode = self.virt_mode
  86. self.testvm1.features.update(self.app.domains[self.template].features)
  87. self.loop.run_until_complete(
  88. self.testvm1.clone_disk_files(self.app.domains[self.template]))
  89. self.loop.run_until_complete(self.testvm1.start())
  90. self.install_packages(self.testvm1)
  91. kver = self.get_kernel_version(self.testvm1)
  92. self.loop.run_until_complete(self.testvm1.shutdown(wait=True))
  93. self.testvm1.kernel = self.kernel
  94. self.loop.run_until_complete(self.testvm1.start())
  95. (actual_kver, _) = self.loop.run_until_complete(
  96. self.testvm1.run_for_stdio('uname -r'))
  97. self.assertEquals(actual_kver.strip(), kver)
  98. self.assertXenScrubPagesEnabled(self.testvm1)
  99. def test_010_template_based_vm(self):
  100. self.test_template = self.app.add_new_vm('TemplateVM',
  101. name=self.make_vm_name('template'), label='red')
  102. self.test_template.virt_mode = self.virt_mode
  103. self.test_template.features.update(self.app.domains[self.template].features)
  104. self.loop.run_until_complete(
  105. self.test_template.clone_disk_files(self.app.domains[self.template]))
  106. self.testvm1 = self.app.add_new_vm("AppVM",
  107. template=self.test_template,
  108. name=self.make_vm_name('vm1'),
  109. label='red')
  110. self.testvm1.virt_mode = self.virt_mode
  111. self.loop.run_until_complete(self.testvm1.create_on_disk())
  112. self.loop.run_until_complete(self.test_template.start())
  113. self.install_packages(self.test_template)
  114. kver = self.get_kernel_version(self.test_template)
  115. self.loop.run_until_complete(self.test_template.shutdown(wait=True))
  116. self.test_template.kernel = self.kernel
  117. self.testvm1.kernel = self.kernel
  118. # Check if TemplateBasedVM boots and has the right kernel
  119. self.loop.run_until_complete(
  120. self.testvm1.start())
  121. (actual_kver, _) = self.loop.run_until_complete(
  122. self.testvm1.run_for_stdio('uname -r'))
  123. self.assertEquals(actual_kver.strip(), kver)
  124. self.assertXenScrubPagesEnabled(self.testvm1)
  125. # And the same for the TemplateVM itself
  126. self.loop.run_until_complete(self.test_template.start())
  127. (actual_kver, _) = self.loop.run_until_complete(
  128. self.test_template.run_for_stdio('uname -r'))
  129. self.assertEquals(actual_kver.strip(), kver)
  130. self.assertXenScrubPagesEnabled(self.test_template)
  131. @unittest.skipUnless(os.path.exists('/var/lib/qubes/vm-kernels/pvgrub2'),
  132. 'grub-xen package not installed')
  133. class TC_40_PVGrub(GrubBase):
  134. virt_mode = 'pv'
  135. kernel = 'pvgrub2'
  136. class TC_41_HVMGrub(GrubBase):
  137. virt_mode = 'hvm'
  138. kernel = None
  139. def create_testcases_for_templates():
  140. yield from qubes.tests.create_testcases_for_templates('TC_40_PVGrub',
  141. TC_40_PVGrub, qubes.tests.SystemTestCase,
  142. module=sys.modules[__name__])
  143. yield from qubes.tests.create_testcases_for_templates('TC_41_HVMGrub',
  144. TC_41_HVMGrub, qubes.tests.SystemTestCase,
  145. module=sys.modules[__name__])
  146. def load_tests(loader, tests, pattern):
  147. tests.addTests(loader.loadTestsFromNames(
  148. create_testcases_for_templates()))
  149. return tests
  150. qubes.tests.maybe_create_testcases_on_import(create_testcases_for_templates)