pvgrub.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 unittest
  26. import qubes.tests
  27. @unittest.skipUnless(os.path.exists('/var/lib/qubes/vm-kernels/pvgrub2'),
  28. 'grub-xen package not installed')
  29. class TC_40_PVGrub(object):
  30. def setUp(self):
  31. super(TC_40_PVGrub, self).setUp()
  32. supported = False
  33. if self.template.startswith('fedora-'):
  34. supported = True
  35. elif self.template.startswith('debian-'):
  36. supported = True
  37. if not supported:
  38. self.skipTest("Template {} not supported by this test".format(
  39. self.template))
  40. def install_packages(self, vm):
  41. if self.template.startswith('fedora-'):
  42. cmd_install1 = 'dnf clean expire-cache && ' \
  43. 'dnf install -y qubes-kernel-vm-support grub2-tools'
  44. cmd_install2 = 'dnf install -y kernel-core && ' \
  45. 'KVER=$(rpm -q --qf ' \
  46. '\'%{VERSION}-%{RELEASE}.%{ARCH}\\n\' kernel-core|head -1) && ' \
  47. 'dnf install --allowerasing -y kernel-devel-$KVER && ' \
  48. 'dkms autoinstall -k $KVER'
  49. cmd_update_grub = 'grub2-mkconfig -o /boot/grub2/grub.cfg'
  50. elif self.template.startswith('debian-'):
  51. cmd_install1 = 'apt-get update && apt-get install -y ' \
  52. 'qubes-kernel-vm-support grub2-common'
  53. cmd_install2 = 'apt-get install -y linux-image-amd64'
  54. cmd_update_grub = 'mkdir -p /boot/grub && update-grub2'
  55. else:
  56. assert False, "Unsupported template?!"
  57. for cmd in [cmd_install1, cmd_install2, cmd_update_grub]:
  58. try:
  59. self.loop.run_until_complete(vm.run_for_stdio(
  60. cmd, user="root"))
  61. except subprocess.CalledProcessError as err:
  62. self.fail("Failed command: {}\nSTDOUT: {}\nSTDERR: {}"
  63. .format(cmd, err.stdout, err.stderr))
  64. def get_kernel_version(self, vm):
  65. if self.template.startswith('fedora-'):
  66. cmd_get_kernel_version = 'rpm -q kernel-core|sort -n|tail -1|' \
  67. 'cut -d - -f 3-'
  68. elif self.template.startswith('debian-'):
  69. cmd_get_kernel_version = \
  70. 'dpkg-query --showformat=\'${Package}\\n\' --show ' \
  71. '\'linux-image-*-amd64\'|sort -n|tail -1|cut -d - -f 3-'
  72. else:
  73. raise RuntimeError("Unsupported template?!")
  74. kver, _ = self.loop.run_until_complete(vm.run_for_stdio(
  75. cmd_get_kernel_version, user="root"))
  76. return kver.strip()
  77. def test_000_standalone_vm(self):
  78. self.testvm1 = self.app.add_new_vm('StandaloneVM',
  79. name=self.make_vm_name('vm1'),
  80. label='red')
  81. self.testvm1.virt_mode = 'pv'
  82. self.testvm1.features.update(self.app.domains[self.template].features)
  83. self.loop.run_until_complete(
  84. self.testvm1.clone_disk_files(self.app.domains[self.template]))
  85. self.loop.run_until_complete(self.testvm1.start())
  86. self.install_packages(self.testvm1)
  87. kver = self.get_kernel_version(self.testvm1)
  88. self.loop.run_until_complete(self.testvm1.shutdown(wait=True))
  89. self.testvm1.kernel = 'pvgrub2'
  90. self.loop.run_until_complete(self.testvm1.start())
  91. (actual_kver, _) = self.loop.run_until_complete(
  92. self.testvm1.run_for_stdio('uname -r'))
  93. self.assertEquals(actual_kver.strip(), kver)
  94. def test_010_template_based_vm(self):
  95. self.test_template = self.app.add_new_vm('TemplateVM',
  96. name=self.make_vm_name('template'), label='red')
  97. self.test_template.virt_mode = 'pv'
  98. self.test_template.features.update(self.app.domains[self.template].features)
  99. self.loop.run_until_complete(
  100. self.test_template.clone_disk_files(self.app.domains[self.template]))
  101. self.testvm1 = self.app.add_new_vm("AppVM",
  102. template=self.test_template,
  103. name=self.make_vm_name('vm1'),
  104. label='red')
  105. self.testvm1.virt_mode = 'pv'
  106. self.loop.run_until_complete(self.testvm1.create_on_disk())
  107. self.loop.run_until_complete(self.test_template.start())
  108. self.install_packages(self.test_template)
  109. kver = self.get_kernel_version(self.test_template)
  110. self.loop.run_until_complete(self.test_template.shutdown(wait=True))
  111. self.test_template.kernel = 'pvgrub2'
  112. self.testvm1.kernel = 'pvgrub2'
  113. # Check if TemplateBasedVM boots and has the right kernel
  114. self.loop.run_until_complete(
  115. self.testvm1.start())
  116. (actual_kver, _) = self.loop.run_until_complete(
  117. self.testvm1.run_for_stdio('uname -r'))
  118. self.assertEquals(actual_kver.strip(), kver)
  119. # And the same for the TemplateVM itself
  120. self.loop.run_until_complete(self.test_template.start())
  121. (actual_kver, _) = self.loop.run_until_complete(
  122. self.test_template.run_for_stdio('uname -r'))
  123. self.assertEquals(actual_kver.strip(), kver)
  124. def load_tests(loader, tests, pattern):
  125. for template in qubes.tests.list_templates():
  126. tests.addTests(loader.loadTestsFromTestCase(
  127. type(
  128. 'TC_40_PVGrub_' + template,
  129. (TC_40_PVGrub, qubes.tests.SystemTestCase),
  130. {'template': template})))
  131. return tests