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 sys
  26. import unittest
  27. import qubes.tests
  28. @unittest.skipUnless(os.path.exists('/var/lib/qubes/vm-kernels/pvgrub2'),
  29. 'grub-xen package not installed')
  30. class TC_40_PVGrub(object):
  31. def setUp(self):
  32. super(TC_40_PVGrub, 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 test_000_standalone_vm(self):
  79. self.testvm1 = self.app.add_new_vm('StandaloneVM',
  80. name=self.make_vm_name('vm1'),
  81. label='red')
  82. self.testvm1.virt_mode = 'pv'
  83. self.testvm1.features.update(self.app.domains[self.template].features)
  84. self.loop.run_until_complete(
  85. self.testvm1.clone_disk_files(self.app.domains[self.template]))
  86. self.loop.run_until_complete(self.testvm1.start())
  87. self.install_packages(self.testvm1)
  88. kver = self.get_kernel_version(self.testvm1)
  89. self.loop.run_until_complete(self.testvm1.shutdown(wait=True))
  90. self.testvm1.kernel = 'pvgrub2'
  91. self.loop.run_until_complete(self.testvm1.start())
  92. (actual_kver, _) = self.loop.run_until_complete(
  93. self.testvm1.run_for_stdio('uname -r'))
  94. self.assertEquals(actual_kver.strip(), kver)
  95. def test_010_template_based_vm(self):
  96. self.test_template = self.app.add_new_vm('TemplateVM',
  97. name=self.make_vm_name('template'), label='red')
  98. self.test_template.virt_mode = 'pv'
  99. self.test_template.features.update(self.app.domains[self.template].features)
  100. self.loop.run_until_complete(
  101. self.test_template.clone_disk_files(self.app.domains[self.template]))
  102. self.testvm1 = self.app.add_new_vm("AppVM",
  103. template=self.test_template,
  104. name=self.make_vm_name('vm1'),
  105. label='red')
  106. self.testvm1.virt_mode = 'pv'
  107. self.loop.run_until_complete(self.testvm1.create_on_disk())
  108. self.loop.run_until_complete(self.test_template.start())
  109. self.install_packages(self.test_template)
  110. kver = self.get_kernel_version(self.test_template)
  111. self.loop.run_until_complete(self.test_template.shutdown(wait=True))
  112. self.test_template.kernel = 'pvgrub2'
  113. self.testvm1.kernel = 'pvgrub2'
  114. # Check if TemplateBasedVM boots and has the right kernel
  115. self.loop.run_until_complete(
  116. self.testvm1.start())
  117. (actual_kver, _) = self.loop.run_until_complete(
  118. self.testvm1.run_for_stdio('uname -r'))
  119. self.assertEquals(actual_kver.strip(), kver)
  120. # And the same for the TemplateVM itself
  121. self.loop.run_until_complete(self.test_template.start())
  122. (actual_kver, _) = self.loop.run_until_complete(
  123. self.test_template.run_for_stdio('uname -r'))
  124. self.assertEquals(actual_kver.strip(), kver)
  125. def load_tests(loader, tests, pattern):
  126. tests.addTests(loader.loadTestsFromNames(
  127. qubes.tests.create_testcases_for_templates('TC_40_PVGrub',
  128. TC_40_PVGrub, qubes.tests.SystemTestCase,
  129. module=sys.modules[__name__])))
  130. return tests