grub.py 7.2 KB

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