pvgrub.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 unittest
  25. import qubes.tests
  26. @unittest.skipUnless(os.path.exists('/var/lib/qubes/vm-kernels/pvgrub2'),
  27. 'grub-xen package not installed')
  28. class TC_40_PVGrub(qubes.tests.SystemTestsMixin):
  29. def setUp(self):
  30. super(TC_40_PVGrub, self).setUp()
  31. supported = False
  32. if self.template.startswith('fedora-'):
  33. supported = True
  34. elif self.template.startswith('debian-'):
  35. supported = True
  36. if not supported:
  37. self.skipTest("Template {} not supported by this test".format(
  38. self.template))
  39. def install_packages(self, vm):
  40. if self.template.startswith('fedora-'):
  41. cmd_install1 = 'dnf clean expire-cache && ' \
  42. 'dnf install -y qubes-kernel-vm-support grub2-tools'
  43. cmd_install2 = 'dnf install -y kernel && ' \
  44. 'KVER=$(rpm -q --qf %{VERSION}-%{RELEASE}.%{ARCH} kernel) && ' \
  45. 'dnf install --allowerasing -y kernel-devel-$KVER && ' \
  46. 'dkms autoinstall -k $KVER'
  47. cmd_update_grub = 'grub2-mkconfig -o /boot/grub2/grub.cfg'
  48. elif self.template.startswith('debian-'):
  49. cmd_install1 = 'apt-get update && apt-get install -y ' \
  50. 'qubes-kernel-vm-support grub2-common'
  51. cmd_install2 = 'apt-get install -y linux-image-amd64'
  52. cmd_update_grub = 'mkdir /boot/grub && update-grub2'
  53. else:
  54. assert False, "Unsupported template?!"
  55. for cmd in [cmd_install1, cmd_install2, cmd_update_grub]:
  56. p = vm.run(cmd, user="root", passio_popen=True, passio_stderr=True)
  57. (stdout, stderr) = p.communicate()
  58. self.assertEquals(p.returncode, 0,
  59. "Failed command: {}\nSTDOUT: {}\nSTDERR: {}"
  60. .format(cmd, stdout, stderr))
  61. def get_kernel_version(self, vm):
  62. if self.template.startswith('fedora-'):
  63. cmd_get_kernel_version = 'rpm -q kernel|sort -n|tail -1|' \
  64. 'cut -d - -f 2-'
  65. elif self.template.startswith('debian-'):
  66. cmd_get_kernel_version = \
  67. 'dpkg-query --showformat=\'${Package}\\n\' --show ' \
  68. '\'linux-image-*-amd64\'|sort -n|tail -1|cut -d - -f 3-'
  69. else:
  70. raise RuntimeError("Unsupported template?!")
  71. p = vm.run(cmd_get_kernel_version, user="root", passio_popen=True)
  72. (kver, _) = p.communicate()
  73. self.assertEquals(p.returncode, 0,
  74. "Failed command: {}".format(cmd_get_kernel_version))
  75. return kver.strip()
  76. def test_000_standalone_vm(self):
  77. testvm1 = self.qc.add_new_vm("QubesAppVm",
  78. template=None,
  79. name=self.make_vm_name('vm1'))
  80. testvm1.create_on_disk(verbose=False,
  81. source_template=self.qc.get_vm_by_name(
  82. self.template))
  83. self.app.save()
  84. self.qc.unlock_db()
  85. testvm1 = self.qc[testvm1.qid]
  86. testvm1.start()
  87. self.install_packages(testvm1)
  88. kver = self.get_kernel_version(testvm1)
  89. self.shutdown_and_wait(testvm1)
  90. self.qc.lock_db_for_writing()
  91. self.qc.load()
  92. testvm1 = self.qc[testvm1.qid]
  93. testvm1.kernel = 'pvgrub2'
  94. self.app.save()
  95. self.qc.unlock_db()
  96. testvm1 = self.qc[testvm1.qid]
  97. testvm1.start()
  98. p = testvm1.run('uname -r', passio_popen=True)
  99. (actual_kver, _) = p.communicate()
  100. self.assertEquals(actual_kver.strip(), kver)
  101. def test_010_template_based_vm(self):
  102. test_template = self.qc.add_new_vm("QubesTemplateVm",
  103. template=None,
  104. name=self.make_vm_name('template'))
  105. test_template.clone_attrs(self.qc.get_vm_by_name(self.template))
  106. test_template.clone_disk_files(
  107. src_vm=self.qc.get_vm_by_name(self.template),
  108. verbose=False)
  109. testvm1 = self.qc.add_new_vm("QubesAppVm",
  110. template=test_template,
  111. name=self.make_vm_name('vm1'))
  112. testvm1.create_on_disk(verbose=False,
  113. source_template=test_template)
  114. self.app.save()
  115. self.qc.unlock_db()
  116. test_template = self.qc[test_template.qid]
  117. testvm1 = self.qc[testvm1.qid]
  118. test_template.start()
  119. self.install_packages(test_template)
  120. kver = self.get_kernel_version(test_template)
  121. self.shutdown_and_wait(test_template)
  122. self.qc.lock_db_for_writing()
  123. self.qc.load()
  124. test_template = self.qc[test_template.qid]
  125. test_template.kernel = 'pvgrub2'
  126. testvm1 = self.qc[testvm1.qid]
  127. testvm1.kernel = 'pvgrub2'
  128. self.app.save()
  129. self.qc.unlock_db()
  130. # Check if TemplateBasedVM boots and has the right kernel
  131. testvm1 = self.qc[testvm1.qid]
  132. testvm1.start()
  133. p = testvm1.run('uname -r', passio_popen=True)
  134. (actual_kver, _) = p.communicate()
  135. self.assertEquals(actual_kver.strip(), kver)
  136. # And the same for the TemplateVM itself
  137. test_template = self.qc[test_template.qid]
  138. test_template.start()
  139. p = test_template.run('uname -r', passio_popen=True)
  140. (actual_kver, _) = p.communicate()
  141. self.assertEquals(actual_kver.strip(), kver)
  142. def load_tests(loader, tests, pattern):
  143. try:
  144. qc = qubes.qubes.QubesVmCollection()
  145. qc.lock_db_for_reading()
  146. qc.load()
  147. qc.unlock_db()
  148. templates = [vm.name for vm in qc.values() if
  149. isinstance(vm, qubes.qubes.QubesTemplateVm)]
  150. except OSError:
  151. templates = []
  152. for template in templates:
  153. tests.addTests(loader.loadTestsFromTestCase(
  154. type(
  155. 'TC_40_PVGrub_' + template,
  156. (TC_40_PVGrub, qubes.tests.QubesTestCase),
  157. {'template': template})))
  158. return tests