qvm-grow-root 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/python2
  2. # -*- encoding: utf8 -*-
  3. #
  4. # The Qubes OS Project, http://www.qubes-os.org
  5. #
  6. # Copyright (C) 2014 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
  7. #
  8. # This program is free software; you can redistribute it and/or
  9. # modify it under the terms of the GNU General Public License
  10. # as published by the Free Software Foundation; either version 2
  11. # of the License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. #
  22. #
  23. from qubes.qubes import QubesVmCollection
  24. from qubes.qubes import QubesException
  25. from qubes.qubesutils import parse_size
  26. from optparse import OptionParser
  27. import subprocess
  28. import os
  29. import re
  30. import sys
  31. def main():
  32. usage = "usage: %prog <vm-name> <size>"
  33. parser = OptionParser (usage)
  34. parser.add_option("--allow-start", action="store_true",
  35. dest="allow_start", default=False,
  36. help="Allow VM to be started to complete the operation")
  37. (options, args) = parser.parse_args ()
  38. if (len (args) != 2):
  39. parser.error ("You must specify VM name and new size!")
  40. vmname = args[0]
  41. size = args[1]
  42. qvm_collection = QubesVmCollection()
  43. qvm_collection.lock_db_for_reading()
  44. qvm_collection.load()
  45. qvm_collection.unlock_db()
  46. vm = qvm_collection.get_vm_by_name(vmname)
  47. if vm is None:
  48. print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname)
  49. exit(1)
  50. size_bytes = parse_size(size)
  51. if not hasattr(vm, 'resize_root_img'):
  52. print >> sys.stderr, "Operation not supported for this VM type"
  53. exit(1)
  54. try:
  55. vm.resize_root_img(size_bytes, allow_start=options.allow_start)
  56. except (IOError, OSError, QubesException) as err:
  57. print >> sys.stderr, "ERROR: {0}".format(err)
  58. exit (1)
  59. exit (0)
  60. main()