001QubesResizableVm.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. #
  20. #
  21. from __future__ import (
  22. absolute_import,
  23. division,
  24. print_function,
  25. unicode_literals,
  26. )
  27. from qubes.qubes import (
  28. register_qubes_vm_class,
  29. QubesException,
  30. QubesVm,
  31. )
  32. from time import sleep
  33. class QubesResizableVm(QubesVm):
  34. def resize_root_img(self, size, allow_start=False):
  35. if self.template:
  36. raise QubesException("Cannot resize root.img of template-based VM"
  37. ". Resize the root.img of the template "
  38. "instead.")
  39. if self.is_running():
  40. raise QubesException("Cannot resize root.img of running VM")
  41. if size < self.get_root_img_sz():
  42. raise QubesException(
  43. "For your own safety shringing of root.img is disabled. If "
  44. "you really know what you are doing, use 'truncate' manually.")
  45. f_root = open(self.root_img, "a+b")
  46. f_root.truncate(size)
  47. f_root.close()
  48. class QubesResizableVmWithResize2fs(QubesResizableVm):
  49. def resize_root_img(self, size, allow_start=False):
  50. super(QubesResizableVmWithResize2fs, self).\
  51. resize_root_img(size, allow_start=allow_start)
  52. if not allow_start:
  53. raise QubesException("VM start required to complete the "
  54. "operation, but not allowed. Either run the "
  55. "operation again allowing VM start this "
  56. "time, or run resize2fs in the VM manually.")
  57. self.start(start_guid=False)
  58. self.run("resize2fs /dev/mapper/dmroot", user="root", wait=True,
  59. gui=False)
  60. self.shutdown()
  61. while self.is_running():
  62. sleep(1)
  63. register_qubes_vm_class(QubesResizableVm)
  64. register_qubes_vm_class(QubesResizableVmWithResize2fs)