diff --git a/dom0/qvm-core/qubes.py b/dom0/qvm-core/qubes.py index 3d6b88c2..333212e8 100755 --- a/dom0/qvm-core/qubes.py +++ b/dom0/qvm-core/qubes.py @@ -640,10 +640,35 @@ class QubesVm(object): return os.path.getsize(self.private_img) def resize_private_img(self, size): + assert size >= self.get_private_img_sz(), "Cannot shrink private.img" + f_private = open (self.private_img, "a+b") f_private.truncate (size) f_private.close () + retcode = 0 + if self.is_running(): + # find loop device + p = subprocess.Popen (["losetup", "--associated", vm.private_img], + stdout=subprocess.PIPE) + result = p.communicate() + m = re.match(r"^(/dev/loop\d+):\s", result[0]) + if m is None: + raise QubesException("ERROR: Cannot find loop device!") + + loop_dev = m.group(1) + + # resize loop device + subprocess.check_call(["sudo", "losetup", "--set-capacity", loop_dev]) + + retcode = self.run("root:while [ \"`blockdev --getsize64 /dev/xvdb`\" -lt {0} ]; do " + + "head /dev/xvdb > /dev/null; sleep 0.2; done; resize2fs /dev/xvdb".format(size_bytes), wait=True) + else: + retcode = subprocess.check_call(["sudo", "resize2fs", "-f", vm.private_img]) + if retcode != 0: + raise QubesException("resize2fs failed") + + # FIXME: should be outside of QubesVM? def get_timezone(self): clock_config = open('/etc/sysconfig/clock', "r") diff --git a/dom0/qvm-tools/qvm-grow-private b/dom0/qvm-tools/qvm-grow-private index 5e778c1c..b9a5a996 100755 --- a/dom0/qvm-tools/qvm-grow-private +++ b/dom0/qvm-tools/qvm-grow-private @@ -51,42 +51,14 @@ def main(): print >> sys.stderr, "A VM with the name '{0}' does not exist in the system.".format(vmname) exit(1) - if vm.is_running() and os.geteuid() != 0: - print >> sys.stderr, "You must be root to grow private.img on running VM." - exit(1) - size_bytes = parse_size(size) - if size_bytes < vm.get_private_img_sz(): - print >> sys.stderr, "Cannot shrink private.img ({0} < {1})".format(size_bytes, vm.get_private_img_sz()) - exit(1) - try: vm.resize_private_img(size_bytes) except (IOError, OSError, QubesException) as err: print >> sys.stderr, "ERROR: {0}".format(err) exit (1) - if vm.is_running(): - # find loop device - p = subprocess.Popen (["losetup", "--associated", vm.private_img], - stdout=subprocess.PIPE) - result = p.communicate() - m = re.match(r"^(/dev/loop\d+):\s", result[0]) - if m is None: - print >> sys.stderr, "ERROR: Cannot find loop device!" - exit(1) - - loop_dev = m.group(1) - - # resize loop device - retcode = subprocess.check_call(["losetup", "--set-capacity", loop_dev]) - - retcode = subprocess.check_call([qvm_run_path, "-uroot", "--pass-io", vmname, - "while [ \"`blockdev --getsize64 /dev/xvdb`\" -lt {0} ]; do sleep 0.2; done; resize2fs /dev/xvdb".format(size_bytes) ]) - else: - retcode = subprocess.check_call(["resize2fs", "-f", vm.private_img]) - exit (0)