001QubesResizableVm.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. class QubesResizableVm(QubesVm):
  33. def resize_root_img(self, size):
  34. if self.template:
  35. raise QubesException("Cannot resize root.img of template-based VM"
  36. ". Resize the root.img of the template "
  37. "instead.")
  38. if self.is_running():
  39. raise QubesException("Cannot resize root.img of running VM")
  40. if size < self.get_root_img_sz():
  41. raise QubesException(
  42. "For your own safety shringing of root.img is disabled. If "
  43. "you really know what you are doing, use 'truncate' manually.")
  44. f_root = open(self.root_img, "a+b")
  45. f_root.truncate(size)
  46. f_root.close()
  47. class QubesResizableVmWithResize2fs(QubesResizableVm):
  48. def resize_root_img(self, size):
  49. super(QubesResizableVmWithResize2fs, self).resize_root_img(size)
  50. self.start(start_guid=False)
  51. self.run("resize2fs /dev/mapper/dmroot", user="root", wait=True,
  52. gui=False)
  53. self.shutdown()
  54. register_qubes_vm_class(QubesResizableVm)
  55. register_qubes_vm_class(QubesResizableVmWithResize2fs)